0

I have this graph, from package car, figure 1: enter image description here

The code is:

scatterplot(istat_22 ~ vpa_cnr_2,grid=F, smooth=F)

But if i want the p-value of the regression line, and r and r^2,how i can obtained it and plot them on the graph? Substancially i would the equation of the straight line and the value of the statistic. Another question is, why if I plot with lm, like:

plot(vpa_cnr_2,istat_22)
abline(lm(vpa_cnr_2 ~ istat_22),col="red")

the straight line is not the same of figure 1 (see the figure 2)?

figure 2 enter image description here

Yes i know, it depend from intercept, but R don't find the best fit with function lm?

Community
  • 1
  • 1
Diego
  • 633
  • 1
  • 9
  • 16
  • `car::scatterplot` returns either the list of points (via `id.method`, et al) or `NULL`; it does not offer returning any of the summary statistics. As you suspected, you'll need to perform the regression yourself. (If you look at the source for `car::scatterplot.default` and ultimately `car:::reg`, you'll see that the summary statistics are not retained.) – r2evans Mar 18 '16 at 15:29

2 Answers2

2

I would use Adjusted R-squared and overall p-value of the model if you would like show them in the graph, which can be directly obtained or calculated from the summary of the model.

summary <- summary(lm(istat_22 ~ via_cnr_2)) adjRsq <- summary$adj.r.squared

You can not get the overall p-value directly from the summary but you can calculate from the F-statistics, and here is how you can calculate it:

fStat <- summary$statistic pValue <- pf(fStat[1], fStat[2], fStat[3], lower.tail = F)

You can refer to this link: Adding Regression Line Equation and R2 on graph for how to label them in ggplot. As for why the regression line does not seem correct, it's because your variables are reverted in lm formula. abline(lm(istat_22 ~ vpa_cnr_2),col="red")

Community
  • 1
  • 1
Psidom
  • 209,562
  • 33
  • 339
  • 356
1

Specifically if you want to extract values from your linear regression one can use summary.

#use str(summary(x)) to explore the other useful pieces of information
x <- lm(istat_22 ~ vpa_cnr_2)
summary(x)$r.squared
summary(x)$adj.r.squared
summary(x)$coefficients[2,4] # p-value
TJGorrie
  • 386
  • 3
  • 13