简体   繁体   中英

Different best fit, and obtain p-value from regression in R

I have this graph, from package car , figure 1: 在此处输入图片说明

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 在此处输入图片说明

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

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")

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM