如何在R中散点图上显示带有回归模型线的R平方值?
R平方值是决定系数,它告诉我们自变量解释因变量变化的百分比或比例。为了在散点图上显示带有回归模型线的此值,而不借助任何包,我们可以使用plot函数以及abline和legend函数。
考虑以下数据框:
示例
set.seed(1234) x<-rnorm(20,1,0.096) y<-rnorm(20,2,0.06) df<-data.frame(x,y) df
输出
x y 1 0.8841217 2.008045 2 1.0266332 1.970559 3 1.1041064 1.973567 4 0.7748130 2.027575 5 1.0411960 1.958377 6 1.0485814 1.913108 7 0.9448250 2.034485 8 0.9475233 1.938581 9 0.9458126 1.999092 10 0.9145564 1.943843 11 0.9541895 2.066138 12 0.9041549 1.971464 13 0.9254796 1.957434 14 1.0061880 1.969925 15 1.0921114 1.902254 16 0.9894126 1.929943 17 0.9509431 1.869198 18 0.9125252 1.919540 19 0.9196315 1.982342 20 1.2319202 1.972046
创建回归模型以根据x预测y:
Model<-lm(y~x,data=df) summary(Model)
调用:
lm(formula = y ~ x, data = df) Residuals: Min 1Q Median 3Q Max -0.09955 -0.03138 0.00522 0.02981 0.09783
系数:
Estimate Std. Error t value Pr(>|t|) (Intercept) 2.0971 0.1084 19.349 1.7e-13 *** x -0.1350 0.1105 -1.221 0.238 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.04689 on 18 degrees of freedom Multiple R-squared: 0.07649, Adjusted R-squared: 0.02519 F-statistic: 1.491 on 1 and 18 DF, p-value: 0.2378
在图上创建带有R平方值的散点图:
plot(x,y) abline(Model) legend("topleft",legend=paste("R2 is", format(summary(Model)$r.squared,digits=3)))
输出
广告