How to calculate root mean square error for linear model in R?


To find the root mean square error, we first need to find the residuals (which are also called error and we need to root mean square for these values) then root mean of these residuals needs to be calculated. Therefore, if we have a linear regression model object say M then the root mean square error can be found as sqrt(mean(M$residuals^2)).

Example

 Live Demo

x1<-rnorm(500,50,5)
y1<-rnorm(500,50,2)
M1<-lm(y1~x1)
summary(M1)

Output

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

Finding the root mean square error from model M1 −

Example

sqrt(mean(M1$residuals^2))

Output

[1] 1.961622

Example

 Live Demo

x2<-rnorm(5000,125,21)
y2<-rnorm(5000,137,10)
M2<-lm(y2~x2)
summary(M2)

Output

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

Finding the root mean square error from model M2:

Example

sqrt(mean(M2$residuals^2))

Output

[1] 10.05584

Example

 Live Demo

x37<-rpois(500,5)
y3<-rpois(500,10)
M3<-lm(y3~x3)
summary(M3)

Output

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

Finding the root mean square error from model M3 −

Example

sqrt(mean(M3$residuals^2))

Output

[1] 3.020734

Example

 Live Demo

x4<-runif(50000,5,10)
y4<-runif(50000,2,10)
M4<-lm(y4~x4)
summary(M4)

Output

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

Finding the root mean square error from model M4 −

Example

sqrt(mean(M4$residuals^2))

Output

[1] 2.308586

Example

 Live Demo

x5<-sample(5001:9999,100000,replace=TRUE)
y5<-sample(1000:9999,100000,replace=TRUE)
M5<-lm(y5~x5)
summary(M5)

Output

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

Finding the root mean square error from model M5 <

Example

sqrt(mean(M5$residuals^2))

Output

[1] 2593.709

Updated on: 17-Oct-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements