How to find the mean squared error for linear model in R?


To find the mean squared error for linear model, we can use predicted values of the model and find the error from dependent variable then take its square and the mean of the whole output. For example, if we have a linear model called M for a data frame df then we can find the mean squared error using the command mean((df$y-predict(M))^2).

Example1

Consider the below data frame −

 Live Demo

x1<-rnorm(20)
y1<-rnorm(20)
df1<-data.frame(x1,y1)
df1

Output

      x1          y1
1  -0.64419775  -0.655535847
2  -2.02925533  -0.074246704
3   1.42639556   0.226413529
4   0.21841252  -0.799586646
5  -0.08272931   0.021258680
6   1.36349138  -0.358914344
7   0.58243090   0.334064031
8  -0.18839329   0.596566815
9   1.97993745   1.808762160
10 -0.31676008   0.812349831
11 -0.06732802  -0.189255661
12  1.76175840  -0.317888508
13 -0.29681017   0.947048363
14 -1.02210007   0.428273333
15 -0.33408154   2.273976129
16  0.49158882  -0.483902966
17 -0.71446066   0.001058688
18 -0.98031110   0.011280707
19  0.78912612   0.620691096
20  0.63751954  -0.668467539

Creating linear model for y1 and x1 then finding predicted values and the mean squared error −

Model1<-lm(y1~x1,data=df1)
predict(Model1)
     1         2        3         4         5         6        7         8
0.1936091 0.1343150 0.2822509 0.2305373 0.2176455 0.2795580 0.2461209 0.2131220
     9        10         11       12       13        14        15        16
0.3059479 0.2076267 0.2183048 0.2966077 0.2084807 0.1774312 0.2068852 0.2422320
    17        18        19        20
0.1906012 0.1792202 0.2549695 0.2484792

mean((df1$y1-predict(Model1))^2)

[1] 0.6022432

Example2

 Live Demo

iv1<-rpois(20,2)
iv2<-rpois(20,3)
iv3<-rpois(20,1)
Y<-rpois(20,6)
df2<-data.frame(iv1,iv2,iv3,Y)
df2

Output

  iv1 iv2 iv3 Y
1  3   5   1  5
2  6   2   0 12
3  1   1   0 10
4  5   2   1  6
5  5   1   0  6
6  4   4   0  5
7  1   2   0  2
8  1   1   0  6
9  2   5   0  5
10 2   4   0  6
11 4   6   2  8
12 3   4   1  4
13 2   5   2  6
14 4   3   1  4
15 3   3   2 10
16 2   2   1  7
17 2   4   0 14
18 2   1   0  7
19 1   3   1  7
20 2   4   1  4

Creating linear model for Y, iv1, iv2, iv3 then finding predicted values and the mean squared error −

Model2<-lm(Y~iv1+iv2+iv3,data=df2)
predict(Model2)
     1        2       3         4       5        6         7       8
6.368896 7.886330 6.659550 7.545170 7.802283 6.911692 6.457914 6.659550
    9         10     11        12       13       14        15      16
6.138690 6.340326 6.397466 6.570532 6.027735 7.057851 6.716690 6.688120
    17       18      19        20
6.340326 6.945233 6.200801 6.284848

mean((df2$Y-predict(Model2))^2)

[1] 7.745138

Updated on: 06-Mar-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements