How to find the difference between regression line and the points in R?

R ProgrammingServer Side ProgrammingProgramming

The difference between regression line and the points on the scatterplot are actually the residuals, thus we need to calculate the residual from the model object. This can be simply done by using residuals function. For example, if we create a linear model defined as Model between x and y then the residuals will be found as residuals(Model).

Consider the below data frame −

Example

 Live Demo

set.seed(999)
x1<-rnorm(100,5,1)
y1<-rnorm(100,5,2)
set.seed(999)
x1<-rnorm(50,5,1)
x1

Output

[1] 4.718260 3.687440 5.795184 5.270070 4.722694 4.433976 3.121342 3.733209
[9] 4.032250 3.878991 6.325464 5.133977 5.938749 5.172538 5.957650 3.637314
[17] 5.068335 5.100658 5.901345 2.925643 3.771437 5.643044 4.640237 5.294036
[25] 3.874732 5.642266 3.893262 4.115160 3.445905 4.873321 7.382664 5.601276
[33] 5.179361 6.080531 4.753188 2.886263 4.629473 5.522868 5.517806 3.597489
[41] 4.514363 5.008498 3.717887 3.888421 5.300665 5.276479 2.949122 5.014190
[49] 5.582266 4.965274

Example

 Live Demo

y1<-rnorm(50,5,2)
y1

Output

[1] 6.222789 3.709758 4.657909 2.502828 6.815813 3.366121 5.061378 2.805245
[9] 6.263176 4.886151 2.502289 4.752686 4.920821 4.117212 4.965663 5.664139
[17] 5.903942 4.077208 6.065462 7.370909 4.147725 2.713465 7.280148 5.385931
[25] 3.962250 2.559837 3.806861 1.682934 4.501635 6.555197 3.401010 2.300028
[33] 5.379501 7.656098 3.800852 7.796749 4.400438 9.056425 6.675732 5.027199
[41] 3.768654 6.341022 5.703656 5.280295 3.070839 6.451703 3.750322 7.117050
[49] 4.822037 4.550620

Model1<-lm(y1~x1)

We can find the differences by using residuals function which is the actual description of this difference −

Example

Difference1<-residuals(Model1) Difference1

Output

       1          2           3          4           5             6
-0.318821302 -1.329658317 3.355482915 0.622179231 -0.219309849 0.492362522
7 8 9 10 11 12
1.120847727 -2.600159409 0.815723717 -1.179206602 -2.003523096 2.225944847
13 14 15 16 17 18
1.944418528 2.101980331 -0.177708526 -2.331382376 -2.917405743 -0.667140236
19 20 21 22 23 24
-0.973596995 1.987479224 -0.255997620 -0.265385092 1.817375711 -0.944140091
25 26 27 28 29 30
1.899985405 -0.001963328 1.028005379 -4.188344031 0.843900482 -0.412835686
31 32 33 34 35 36
-1.489716807 1.845872066 -2.563126203 0.024431186 -2.495232512 -0.757793692
37 38 39 40 41 42
2.645748932 -0.627006899 1.891963477 0.838600642 3.240642878 -0.043017748
       43       44           45          46           47           48
-1.998486011 2.518834654 -2.370852257 1.983021019 -1.162755388 -0.686229669
        49       50
-0.607433313 0.343427925

Let’s have a look at another example −

Example

 Live Demo

x2<-rpois(100,5)
x2

Output

[1]  8 2 6 5 4 4 4 4 2 3 5 5 4 10 3 2 3 5 5 8 9 8 3 6 4
[26] 5 3 2 5 6 5 2 6 8 3 9 11 2 4 4 8 2 8 1 3 2 5 8 2 6
[51] 7 4 3 4 6 2 3 2 8 4 9 4 8 3 6 4 8 9 10 4 9 9 2 7 10
[76] 8 6 7 5 6 1 2 8 8 9 10 5 1 7 3 3 1 7 2 6 2 3 4 2 7

Example

 Live Demo

y2<-rpois(100,10)
y2

Output

[1] 12 4 13 12 9 7 12 7 14 12 8 6 6 15 10 12 8 14 9 11 8 6 9 7 15
[26] 7 12 15 9 7 13 11 5 2 9 11 8 17 8 15 7 12 7 11 6 12 12 8 12 8
[51] 10 6 7 7 5 9 14 9 9 17 12 12 9 10 5 11 18 9 13 7 13 10 9 11 11
[76] 12 9 8 5 9 5 7 14 10 6 10 9 10 13 11 11 9 10 6 8 16 8 8 6 6

Model2<-lm(y2~x2)

Finding the residuals using model object Model2 −

Example

Difference2<-residuals(Model2)
Difference2

Output

       1        2       3            4           5          6
3.34416495 4.13956424 -0.06503647 1.93496353 2.24186460 -1.16733682
        7       8           9         10        11          12
-0.75813540 4.24186460 3.62806246 -2.16733682 0.54876566 -0.16733682
      13       14         15           16        17       18
-3.96273611 0.24186460 3.73036282 3.03726389 -2.65583505 0.24186460
      19       20          21           22        23        24
-3.75813540 0.24186460 0.13956424 0.44646531 -0.65583505 -5.16733682
    25          26         27           28        29        30
-0.75813540 3.03726389 -0.75813540 -4.75813540 2.13956424 1.52576211
    31          32         33          34           35         36
-1.75813540 1.03726389 -6.16733682 -2.16733682 -6.26963718 -1.55353469
      37        38         39         40            41         42
2.34416495 -1.16733682 -1.06503647 -2.96273611 -2.86043576 -3.75813540
     43         44           45         46         47             48
-0.96273611 -0.47423789 -1.55353469 -3.86043576 1.44646531 2.03726389
     49         50           51      52          53           54
1.73036282 0.24186460 -2.96273611 2.34416495 -0.96273611 -0.16733682
     55        56           57        58           59          60
1.03726389 -0.06503647 -2.16733682 1.24186460 -4.96273611 0.13956424
     61          62        63            64          65         66
-2.86043576 -0.65583505 -0.86043576 -1.06503647 -0.06503647 0.24186460
       67     68         69          70          71           72
1.13956424 0.13956424 1.24186460 -2.06503647 1.93496353 0.93496353
      73       74         75         76         77            78
0.34416495 -4.96273611 1.24186460 -5.06503647 -0.75813540 3.93496353
      79        80         81        82             83         84
4.83266318 -1.65583505 7.03726389 4.93496353 6.73036282 2.03726389
       85        86         87         88        89        90
-3.96273611 1.13956424 3.83266318 1.34416495 -2.06503647 -2.86043576
      91        92       93           94         95         96
9.13956424 2.93496353 -0.16733682 -0.26963718 -0.96273611 0.03726389
    97        98             99 100
3.24186460 2.13956424 -3.16733682 -0.86043576

An example with uniformly distributed data −

Example

 Live Demo

x3<-round(runif(50,2,5))
x3

Output

[1] 3 5 2 2 4 4 4 2 3 4 4 4 3 4 3 4 4 3 2 5 5 2 3 5 4 4 3 5 3 5 3 3 3 5 4 3 4 5
[39] 5 4 3 4 5 3 5 3 3 3 5 2

Example

 Live Demo

y3<-round(runif(50,2,10))
y3

Output

[1] 5 5 2 2 8 9 8 4 7 6 6 7 3 4 5 3 5 5 7 9 10 10 6 3 4
[26] 2 6 4 3 10 7 6 8 8 9 8 7 5 3 2 9 3 8 3 9 5 3 3 2 4

Model3<-lm(y3~x3)

Finding the residuals using model object Model3 −

Example

Difference3<-residuals(Model3)
Difference3

Output

       1 2 3 4 5 6
-1.09603340 -0.13883090 -4.05323591 2.86116910 0.90396660 -3.18162839
 7 8 9 10 11 12
-0.05323591 -3.13883090 -1.13883090 -1.13883090 2.90396660 2.86116910
13 14 15 16 17 18
1.86116910 -1.13883090 -2.09603340 0.86116910 -0.09603340 -1.13883090
19 20 21 22 23 24
-0.05323591 1.86116910 -0.13883090 0.90396660 2.81837161 -2.13883090
25 26 27 28 29 30
 -1.13883090 0.90396660 2.81837161 -0.09603340 2.81837161 -0.09603340
31 32 33 34 35 36
-0.13883090 -1.05323591 -2.09603340 3.86116910 1.86116910 2.86116910
37 38 39 40 41 42
2.90396660 1.86116910 1.90396660 -3.09603340 0.90396660 -4.18162839
43 44 45 46 47 48
-2.18162839 2.94676409 -4.09603340 -4.18162839 -2.05323591 2.90396660
49 50
 1.90396660 -3.13883090
raja
Published on 10-Oct-2020 12:27:43
Advertisements