为了找到均方根误差,我们首先需要找到残差(也称为误差,我们需要对这些值均方根),然后需要计算这些残差的均方根。因此,如果我们有一个线性回归模型对象说M,则均方根误差可以找到为sqrt(mean(M $residuals ^ 2))。
x1<-rnorm(500,50,5) y1<-rnorm(500,50,2) M1<-lm(y1~x1) summary(M1)
输出结果
Call: lm(formula = y1 ~ x1) Residuals: Min 1Q Median 3Q Max -5.6621 -1.2257 -0.0272 1.4151 6.6421 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 50.178943 0.915473 54.812 <2e-16 *** x1 -0.002153 0.018241 -0.118 0.906 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 1.966 on 498 degrees of freedom Multiple R-squared: 2.798e-05, Adjusted R-squared: -0.00198 F-statistic: 0.01393 on 1 and 498 DF, p-value: 0.9061
从模型M1中找到均方根误差-
sqrt(mean(M1$residuals^2))
输出结果
[1] 1.961622
x2<-rnorm(5000,125,21) y2<-rnorm(5000,137,10) M2<-lm(y2~x2) summary(M2)
输出结果
Call: lm(formula = y2 ~ x2) Residuals: Min 1Q Median 3Q Max -37.425 -7.005 -0.231 6.836 36.627 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 138.683501 0.851247 162.918 <2e-16 *** x2 -0.014386 0.006735 -2.136 0.0327 * --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 10.06 on 4998 degrees of freedom Multiple R-squared: 0.0009121, Adjusted R-squared: 0.0007122 F-statistic: 4.563 on 1 and 4998 DF, p-value: 0.03272
从模型M2中找到均方根误差:
sqrt(mean(M2$residuals^2))
输出结果
[1] 10.05584
x37<-rpois(500,5) y3<-rpois(500,10) M3<-lm(y3~x3) summary(M3)
输出结果
Call: lm(formula = y3 ~ x3) Residuals: Min 1Q Median 3Q Max -7.9004 -1.9928 -0.2155 2.1921 9.3770 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 10.17770 0.32330 31.481 <2e-16 *** x3 -0.09244 0.06145 -1.504 0.133 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 3.027 on 498 degrees of freedom Multiple R-squared: 0.004524, Adjusted R-squared: 0.002525 F-statistic: 2.263 on 1 and 498 DF, p-value: 0.1331
从模型M3查找均方根误差-
sqrt(mean(M3$residuals^2))
输出结果
[1] 3.020734
x4<-runif(50000,5,10) y4<-runif(50000,2,10) M4<-lm(y4~x4) summary(M4)
输出结果
Call: lm(formula = y4 ~ x4) Residuals: Min 1Q Median 3Q Max -4.0007 -1.9934 -0.0063 1.9956 3.9995 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 5.9994268 0.0546751 109.729 <2e-16 *** x4 0.0001572 0.0071579 0.022 0.982 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 2.309 on 49998 degrees of freedom Multiple R-squared: 9.646e-09, Adjusted R-squared: -1.999e-05 F-statistic: 0.0004823 on 1 and 49998 DF, p-value: 0.9825
从模型M4找到均方根误差-
sqrt(mean(M4$residuals^2))
输出结果
[1] 2.308586
x5<-sample(5001:9999,100000,replace=TRUE) y5<-sample(1000:9999,100000,replace=TRUE) M5<-lm(y5~x5) summary(M5)
输出结果
Call: lm(formula = y5 ~ x5) Residuals: Min 1Q Median 3Q Max -4495 -2242 -4 2230 4512 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 5.504e+03 4.342e+01 126.765 <2e-16 *** x5 -1.891e-03 5.688e-03 -0.333 0.74 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 2594 on 99998 degrees of freedom Multiple R-squared: 1.106e-06, Adjusted R-squared: -8.895e-06 F-statistic: 0.1106 on 1 and 99998 DF, p-value: 0.7395
从模型M5中找到均方根误差<
sqrt(mean(M5$residuals^2))
输出结果
[1] 2593.709