# NMSA407 Linear Regression: Tutorial

Partial residuals

Data Cars2004nh

## Introduction

### Load used data and calculate basic summaries

``````data(Cars2004nh, package = "mffSM")
``````
``````##                         vname type drive price.retail price.dealer   price cons.city cons.highway
## 1          Chevrolet.Aveo.4dr    1     1        11690        10965 11327.5       8.4          6.9
## 2 Chevrolet.Aveo.LS.4dr.hatch    1     1        12585        11802 12193.5       8.4          6.9
## 3      Chevrolet.Cavalier.2dr    1     1        14610        13697 14153.5       9.0          6.4
## 4      Chevrolet.Cavalier.4dr    1     1        14810        13884 14347.0       9.0          6.4
## 5   Chevrolet.Cavalier.LS.2dr    1     1        16385        15357 15871.0       9.0          6.4
## 6           Dodge.Neon.SE.4dr    1     1        13670        12849 13259.5       8.1          6.5
##   consumption engine.size ncylinder horsepower weight      iweight  lweight wheel.base length width
## 1        7.65         1.6         4        103   1075 0.0009302326 6.980076        249    424   168
## 2        7.65         1.6         4        103   1065 0.0009389671 6.970730        249    389   168
## 3        7.70         2.2         4        140   1187 0.0008424600 7.079184        264    465   175
## 4        7.70         2.2         4        140   1214 0.0008237232 7.101676        264    465   173
## 5        7.70         2.2         4        140   1187 0.0008424600 7.079184        264    465   175
## 6        7.30         2.0         4        132   1171 0.0008539710 7.065613        267    442   170
##      ftype fdrive
## 1 personal  front
## 2 personal  front
## 3 personal  front
## 4 personal  front
## 5 personal  front
## 6 personal  front
``````
``````dim(Cars2004nh)
``````
``````## [1] 425  20
``````
``````summary(Cars2004nh)
``````
``````##     vname                type           drive        price.retail     price.dealer
##  Length:425         Min.   :1.000   Min.   :1.000   Min.   : 10280   Min.   :  9875
##  Class :character   1st Qu.:1.000   1st Qu.:1.000   1st Qu.: 20370   1st Qu.: 18973
##  Mode  :character   Median :1.000   Median :1.000   Median : 27905   Median : 25672
##                     Mean   :2.219   Mean   :1.692   Mean   : 32866   Mean   : 30096
##                     3rd Qu.:3.000   3rd Qu.:2.000   3rd Qu.: 39235   3rd Qu.: 35777
##                     Max.   :6.000   Max.   :3.000   Max.   :192465   Max.   :173560
##
##      price          cons.city      cons.highway     consumption     engine.size      ncylinder
##  Min.   : 10078   Min.   : 6.20   Min.   : 5.100   Min.   : 5.65   Min.   :1.300   Min.   :-1.000
##  1st Qu.: 19600   1st Qu.:11.20   1st Qu.: 8.100   1st Qu.: 9.65   1st Qu.:2.400   1st Qu.: 4.000
##  Median : 26656   Median :12.40   Median : 9.000   Median :10.70   Median :3.000   Median : 6.000
##  Mean   : 31481   Mean   :12.36   Mean   : 9.142   Mean   :10.75   Mean   :3.208   Mean   : 5.791
##  3rd Qu.: 37514   3rd Qu.:13.80   3rd Qu.: 9.800   3rd Qu.:11.65   3rd Qu.:3.900   3rd Qu.: 6.000
##  Max.   :183012   Max.   :23.50   Max.   :19.600   Max.   :21.55   Max.   :8.300   Max.   :12.000
##                   NA's   :14      NA's   :14       NA's   :14
##    horsepower        weight        iweight             lweight        wheel.base        length
##  Min.   :100.0   Min.   : 923   Min.   :0.0003067   Min.   :6.828   Min.   :226.0   Min.   :363.0
##  1st Qu.:165.0   1st Qu.:1412   1st Qu.:0.0005542   1st Qu.:7.253   1st Qu.:262.0   1st Qu.:450.0
##  Median :210.0   Median :1577   Median :0.0006341   Median :7.363   Median :272.0   Median :472.0
##  Mean   :216.8   Mean   :1626   Mean   :0.0006412   Mean   :7.373   Mean   :274.9   Mean   :470.6
##  3rd Qu.:255.0   3rd Qu.:1804   3rd Qu.:0.0007082   3rd Qu.:7.498   3rd Qu.:284.0   3rd Qu.:490.0
##  Max.   :500.0   Max.   :3261   Max.   :0.0010834   Max.   :8.090   Max.   :366.0   Max.   :577.0
##                  NA's   :2      NA's   :2           NA's   :2       NA's   :2       NA's   :26
##      width            ftype       fdrive
##  Min.   :163.0   personal:242   front:223
##  1st Qu.:175.0   wagon   : 30   rear :110
##  Median :180.0   SUV     : 60   4x4  : 92
##  Mean   :181.1   pickup  : 24
##  3rd Qu.:185.0   sport   : 49
##  Max.   :206.0   minivan : 20
##  NA's   :28
``````

### Complete cases subset used here

To be able to compare a model fitted here with other models where also other covariates will be included, we restrict ourselves to a subset of the dataset where all variables `consumption`, `lweight` and `engine.size` are known.

``````isComplete <- complete.cases(Cars2004nh[, c("consumption", "lweight", "engine.size")])
sum(!isComplete)
``````
``````## [1] 16
``````
``````CarsNow <- subset(Cars2004nh, isComplete, select = c("consumption", "drive", "fdrive", "weight", "lweight", "engine.size", "horsepower"))
dim(CarsNow)
``````
``````## [1] 409   7
``````
``````summary(CarsNow)
``````
``````##   consumption        drive         fdrive        weight        lweight       engine.size
##  Min.   : 5.65   Min.   :1.000   front:212   Min.   : 923   Min.   :6.828   Min.   :1.300
##  1st Qu.: 9.65   1st Qu.:1.000   rear :108   1st Qu.:1415   1st Qu.:7.255   1st Qu.:2.400
##  Median :10.70   Median :1.000   4x4  : 89   Median :1577   Median :7.363   Median :3.000
##  Mean   :10.75   Mean   :1.699               Mean   :1622   Mean   :7.371   Mean   :3.178
##  3rd Qu.:11.65   3rd Qu.:2.000               3rd Qu.:1804   3rd Qu.:7.498   3rd Qu.:3.800
##  Max.   :21.55   Max.   :3.000               Max.   :2903   Max.   :7.973   Max.   :6.000
##    horsepower
##  Min.   :100.0
##  1st Qu.:165.0
##  Median :210.0
##  Mean   :215.8
##  3rd Qu.:250.0
##  Max.   :493.0
``````

## Dependence of `consumption` on `lweight`, `engine.size` and `horsepower`

### Basic scatterplots to start

``````library("car")
palette(c("darkblue", "red3", "olivedrab", rainbow_hcl(5)))
scatterplotMatrix(~consumption + lweight + engine.size + horsepower,
reg.line = lm, smooth = FALSE, spread = TRUE, diagonal = "histogram", data = CarsNow, pch = 16)
``````

### Model with all covariates additively

``````m <- lm(consumption ~ lweight + engine.size + horsepower, data = CarsNow)
summary(m)
``````
``````##
## Call:
## lm(formula = consumption ~ lweight + engine.size + horsepower,
##     data = CarsNow)
##
## Residuals:
##     Min      1Q  Median      3Q     Max
## -3.1174 -0.6923 -0.1127  0.5473  5.2275
##
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)
## (Intercept) -42.353265   2.948614 -14.364  < 2e-16 ***
## lweight       6.935604   0.428971  16.168  < 2e-16 ***
## engine.size   0.352687   0.096730   3.646 0.000301 ***
## horsepower    0.003983   0.001085   3.672 0.000273 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9706 on 405 degrees of freedom
## Multiple R-squared:  0.7946, Adjusted R-squared:  0.793
## F-statistic: 522.1 on 3 and 405 DF,  p-value: < 2.2e-16
``````

### Response and covariate sample means

``````(ybar <- with(CarsNow, mean(consumption)))
``````
``````## [1] 10.75134
``````
``````(xbar <- sapply(subset(CarsNow, select = c("lweight", "engine.size", "horsepower")), mean))
``````
``````##     lweight engine.size  horsepower
##    7.371286    3.178240  215.757946
``````

### Zero-mean partial residuals

• Those are returned by the `residuals` function if the `type` argument is set to `partial`.
``````residuals(m, type = "partial")
``````
``````##          lweight   engine.size   horsepower
## 1   -2.095605965  0.0610504786  0.168561582
## 2   -2.095605965  0.1258696781  0.233380782
## 3   -2.404588949 -0.7236971495 -0.680427727
## 4   -2.404588949 -0.8796898178 -0.836420395
## 5   -2.404588949 -0.7236971495 -0.680427727
## 6   -2.702187580 -0.9977099297 -0.915766988
## 7   -2.702187580 -1.1151658082 -1.033222866
## 8   -1.944221599 -0.3221714502 -0.248194489
## 9   -2.314561787 -0.6749308844 -0.680613735
## 10  -1.944221599 -0.3045906959 -0.230613735
## 11  -1.944221599 -0.5297361837 -0.455759223
## 12  -3.028670574 -1.0150811362 -0.895042868
## 13  -3.936636555 -2.1152844749 -1.987280225
## 14  -3.028670574 -1.2439180713 -1.123879803
## 15  -2.145605965  0.3549258648  0.462436968
## 16  -2.145605965  0.2472906669  0.354801770
## 17  -2.145605965  0.1019679361  0.209479040
## 18  -2.076085523 -0.5123181032 -0.406477218
## 19  -2.076085523 -0.5123181032 -0.406477218
## 20  -2.076085523 -0.6786200037 -0.572779118
## 21  -1.167160412 -0.9832041407 -1.018438143
## 22  -1.699588955  0.3609605850  0.472454679
## 23  -1.349588955  0.5536839167  0.665178011
## 24  -1.299786211  0.1241448983  0.244761360
## 25  -1.299786211  0.0612235245  0.181839986
## 26  -1.299786211  0.0328105234  0.153426985
## 28  -2.393401852 -0.6742708719 -0.518963881
## 31  -2.357752192 -0.5377309677 -0.409148525
## 32  -2.357752192 -0.7238119863 -0.595229544
## 33  -1.654588949 -0.3710990250 -0.327829602
## 34  -2.254588949 -0.7695660895 -0.726296667
## 35  -2.254588949 -0.7695660895 -0.726296667
## 36  -2.254588949 -0.7695660895 -0.726296667
## 37  -2.254588949 -0.9212621361 -0.877992714
## 38  -2.254588949 -0.9212621361 -0.877992714
## 39  -2.930252196 -0.7179470168 -0.555252238
## 40  -1.699602529 -0.1394346765 -0.071689117
## 41  -1.699602529 -0.1394346765 -0.071689117
## 42  -1.250408702  0.1413927877  0.171556852
## 43  -0.700408702  0.5512748128  0.581438877
## 44  -3.273684155 -1.4231767101 -1.278662305
## 45  -3.273684155 -1.4840157306 -1.339501326
## 46  -3.273684155 -1.4840157306 -1.339501326
## 47  -3.630252196 -0.4515545109 -0.288859732
## 48  -3.180252196 -0.1722628490 -0.009568071
## 49  -3.630252196 -0.5188546991 -0.356159921
## 50  -0.761412117 -0.4827077959 -0.617452202
## 51  -1.387133236 -1.2313636855 -1.451999304
## 52  -1.774503902 -1.4336678529 -1.370483477
## 53  -1.552061770 -1.0127002211 -1.188944750
## 54  -1.387133236 -0.9752092591 -1.195844878
## 55  -0.814956299 -0.2427923749 -0.230230491
## 56  -0.814956299 -0.2477199799 -0.235158096
## 57  -0.964956299 -0.5487647411 -0.536202857
## 58  -0.719911994 -0.3031461405 -0.197240893
## 59  -0.869911994 -0.9698418121 -0.863936565
## 60  -0.414956299 -0.0180169501 -0.005455066
## 61  -0.414956299 -0.0035828058  0.008979079
## 62  -0.353541222  0.9148077491  1.148104333
## 63  -0.196483584  0.1433770585 -0.035758437
## 64   0.070298850  0.3963021368  0.400384208
## 65  -2.304786204 -1.4867776714 -1.434385881
## 66  -2.304786204 -1.6082804085 -1.555888618
## 67  -2.976466461 -1.4308964143 -1.263062259
## 68  -1.713711316 -0.5282920362 -0.334825358
## 71   0.149577724  0.5758444056  0.562259935
## 72   0.149577724  0.5758444056  0.562259935
## 73  -0.150422276  0.1440027518  0.130418282
## 75  -1.619517482 -0.9482360854 -0.860575573
## 76  -0.196483584  0.1433770585 -0.035758437
## 77  -1.834585400 -0.5270092675 -0.180518729
## 80  -0.249799785  0.5020779465  0.578945873
## 81  -1.009969879  0.4069691481  0.444007169
## 82  -1.504588949 -0.6438389608 -0.600569538
## 83  -0.717218284  0.1732129197 -0.067337652
## 84  -1.107867936 -0.8330482976 -1.115098993
## 85  -1.654588949 -0.3710990250 -0.327829602
## 86  -0.454024329  0.1209640456  0.049369296
## 87  -0.759969879  0.1600210608  0.197059082
## 88  -0.509969879 -0.3000606753 -0.263022654
## 89  -0.020139973 -0.0075520280 -0.010343913
## 90  -1.792837233 -1.1860816967 -1.145638878
## 91  -1.015548066 -0.6525301269 -0.612601140
## 92  -1.792837233 -1.3814637398 -1.341020921
## 93  -0.881099090 -0.6628495039 -0.668981825
## 95  -1.234476740 -0.3291043544 -0.314872252
## 96  -1.422833684 -0.6771532897 -0.333489356
## 97  -4.189463159 -3.5681970863 -3.578441121
## 98  -1.303343966 -0.2812657706 -0.057091554
## 99  -1.573030939 -0.3766114923 -0.023825191
## 100 -1.014105827 -0.7221088423 -0.510397429
## 101 -0.933003778 -0.8998002855 -0.595966257
## 102 -0.435646700 -0.0846695037  0.028045338
## 103 -0.977782889 -0.8809760009 -1.143111744
## 104 -1.107867936 -0.8021939556 -1.084244651
## 105 -0.317187559 -0.1601081292 -0.282839202
## 106 -1.107867936 -0.8330482976 -1.115098993
## 107 -0.317187559 -0.2971121665 -0.419843239
## 108 -2.002061770 -1.5043278450 -1.680572374
## 109 -0.157867936  0.2010206573 -0.081030038
## 110 -0.101211299 -0.1358821813 -0.112977182
## 111 -0.869911994 -0.9874448770 -0.881539630
## 112 -0.379517469 -0.3498302906 -0.398619121
## 113 -0.543765639 -0.2233051233  0.068066101
## 114 -0.964956299 -0.9422468355 -0.929684951
## 115 -0.029517469  0.1175409243  0.068752093
## 116  0.314390514 -0.1983963753 -0.667005073
## 117  0.314390514 -0.1983963753 -0.667005073
## 118 -1.285037783 -1.0324763877 -0.873057684
## 119 -1.285037783 -0.9173790921 -0.757960388
## 120  0.621836173  0.4518321414  0.251689669
## 121  0.621836173  0.4518321414  0.251689669
## 122  0.008958795  0.4653134561  0.528048361
## 123 -0.070701016  0.2225620415  0.364956758
## 124  0.632489375  0.5462522775  0.690831044
## 126 -0.908680599 -0.8707734075 -0.491262558
## 127 -0.056000463  0.1001847323  0.089491228
## 128  0.314390514 -0.1908535754 -0.659462273
## 129  0.314390514 -0.1908535754 -0.659462273
## 130  0.070298850  0.3916891644  0.395771236
## 131  0.428023277  0.4534012881  0.297071712
## 132  0.022642346 -0.0373660878 -0.199927066
## 133 -0.881296346 -0.1299643609 -0.126974314
## 134 -0.960956158 -0.7831411563 -0.700491298
## 135 -0.960956158 -0.7919426852 -0.709292827
## 136 -0.697303331  0.2670951986  0.006629674
## 137 -1.107867936 -0.8462296836 -1.128280379
## 138 -0.509969879 -0.5289792725 -0.491941252
## 139 -0.059969879 -0.2787384719 -0.241700451
## 140  0.026485953 -0.2398265018 -0.191931534
## 141 -1.015548066 -0.9031046452 -0.863175658
## 142 -1.015548066 -0.7903281138 -0.750399127
## 143 -0.881099090 -0.7074516117 -0.713583932
## 144 -1.055180716 -0.5123584346 -0.441721910
## 145 -1.134476740 -0.6588810030 -0.644648901
## 146 -0.933003778 -0.8762498253 -0.572415797
## 147 -0.718272500  0.4704552151  0.739020521
## 148 -0.875064944 -1.0069953450 -0.798624368
## 149 -1.083003778 -1.8274014514 -1.523567423
## 150 -0.605377971 -0.5818537672 -0.502094875
## 151  0.694622029  0.4793381583  0.559097051
## 152  0.544622029  0.2444979094  0.324256802
## 153 -0.455377971 -0.6278493362 -0.548090444
## 154  0.544622029 -0.2241645968 -0.144405704
## 155 -0.435646700 -0.0370019356  0.075712906
## 156  0.164353300 -0.1844616754 -0.071746833
## 157  0.164353300  0.0115338937  0.124248736
## 158 -0.925292924 -0.5390401100 -0.439366265
## 159 -0.925292924 -0.5390401100 -0.439366265
## 160 -0.775292924 -0.7958044481 -0.696130603
## 161  0.014353300 -0.0718837979  0.040831044
## 162 -0.977782889 -0.9279682611 -1.190104004
## 163 -0.977782889 -1.2807372826 -1.542873025
## 164  0.193605026 -0.0243918344 -0.016840604
## 165 -0.121126252 -0.2911302839 -0.248310331
## 166 -0.719911994 -0.7756376863 -0.669732439
## 167  0.254645655 -0.2581412340 -0.667005073
## 168  0.008958795 -0.2111220165 -0.148387111
## 169 -0.321126252  0.1950780006  0.237897953
## 170  0.516741095  0.4306958183  0.538335645
## 171 -0.881099090 -0.7473497305 -0.753482051
## 172  0.564536982  1.0162646273  1.076108567
## 173  0.764536982  1.1507897958  1.210633735
## 174 -0.353173858 -0.7579093704 -0.630354591
## 175 -0.353173858 -0.7579093704 -0.630354591
## 176  0.242812427  0.3315069192  0.345225190
## 177  0.542812427  0.6315069192  0.645225190
## 178 -0.056000463  0.1001847323  0.089491228
## 179 -0.356000463 -0.2398156572 -0.250509162
## 181  0.314390514 -0.1908535754 -0.659462273
## 182  0.403717248 -0.3425637959 -0.500499229
## 184 -0.212860845  0.0574377595  0.450053967
## 185 -0.212860845  0.0574377595  0.450053967
## 186 -0.808496917 -1.0495016237 -0.722861677
## 187 -0.927986634 -1.1689913410 -0.722861677
## 188  0.026485953 -0.2820653892 -0.234170421
## 189 -1.015548066 -0.9477067530 -0.907777766
## 190  0.184649190 -0.3649787782 -0.334172159
## 191 -0.007214721 -0.5521525773 -0.625931376
## 192 -0.231238474 -1.2156640210 -1.007357405
## 193 -0.466037663 -1.2737016328 -0.839520940
## 194  0.402326393  0.0320721304  0.606813879
## 195 -0.522313362 -0.7600499292 -0.692818202
## 196 -0.175476606 -0.7731480901 -0.620603342
## 197  0.548363466 -0.0440795497 -0.120749315
## 198  0.548363466 -0.0676834427 -0.144353208
## 199 -0.455377971 -1.1049249172 -1.025166025
## 200  0.544622029 -0.4566647092 -0.376905817
## 201  0.530938477 -0.2643894073  0.040665369
## 202  0.352758118 -0.2443316872 -0.269158213
## 203  0.233268400 -0.9815344661 -0.886871275
## 204  2.693438495  2.4471000267  2.581593124
## 205 -0.325292924 -0.6043040778 -0.504630232
## 206 -0.925292924 -0.9238215331 -0.824147688
## 207 -0.567354091 -0.7231389280 -0.718928133
## 208 -0.567354091 -1.6767671591 -1.672556365
## 209 -0.567354091 -1.8151379105 -1.810927116
## 210 -0.317187559 -0.8548650295 -0.977596102
## 211 -0.438742006 -0.8260113877 -1.091487566
## 212 -0.538316770 -1.0284506053 -1.194352019
## 213 -0.438742006 -0.8413471681 -1.106823347
## 214  0.287632328  0.0992955604  0.127982492
## 215  0.287632328 -0.1233816455 -0.094694714
## 216  0.284877170 -0.2961793758 -0.156675625
## 217 -0.673343939 -1.0070737605 -1.055798230
## 218 -0.005711034 -0.6407004083 -0.307057782
## 219 -0.673343939 -0.8795920993 -0.928316569
## 220 -0.673343939 -0.8795920993 -0.928316569
## 221 -0.005711034 -0.4723708166 -0.138728190
## 222  0.544622029  0.2024638180  0.282222710
## 223  0.167489396  0.1592295348  0.099134287
## 224 -0.192680699 -0.6972612596 -0.797186413
## 225  0.538224095  0.2908002084  0.292120037
## 226  0.538224095  0.2908002084  0.292120037
## 227  0.254645655 -0.7737400195 -1.182603859
## 228  0.254645655 -0.7737400195 -1.182603859
## 229  0.254645655 -0.9362871659 -1.345151005
## 230  1.660278800  1.5971033996  2.120130633
## 231  0.512642360  0.0921011028 -0.206909218
## 232  2.575547549  1.7020332271  1.987430496
## 233 -0.356000463 -0.8543624991 -0.865056004
## 234  0.512642360  0.9984661797  0.699455859
## 235 -0.229898406 -0.4763293004 -0.463124861
## 236  1.512642360  1.5684698553  1.269459534
## 237 -0.332935840 -1.1264635141 -1.286133526
## 238  0.512642360 -0.4060907977 -0.705101119
## 239 -0.662860845 -1.0316685770 -0.639052370
## 240 -0.812860845 -1.6034287011 -1.210812494
## 243 -0.602156856 -0.7681218268 -0.568359385
## 244 -0.146122710 -0.3473564028  0.066909337
## 245 -0.161292797 -0.5470960640 -0.240884902
## 246  0.745275244  1.4871304430  1.775161232
## 247  1.155309531  0.5582197260  1.130841787
## 248 -0.022833684  0.2739305107  0.617594445
## 249 -0.202068260  0.5749477472  1.097846257
## 250 -1.245405133 -0.9268706923 -0.798159527
## 251  1.024006649  1.2114635408  1.670762925
## 252  1.224006649  0.7053946599  1.164694044
## 253 -0.285646700  0.7121275283  0.824842370
## 254 -1.075292924 -0.0558789834  0.043794862
## 255 -0.067976582  0.1583567239  0.072115121
## 256 -0.925422242  0.4973820881  0.142674260
## 257 -0.925422242  0.4926720005  0.137964173
## 258  0.843999537  1.7926091254  1.781915621
## 260 -0.929987002 -0.2708877044 -0.580819334
## 261  0.171002853  0.9924312055  0.667210168
## 262  0.538224095  0.2664931606  0.267812989
## 263  0.097111993  1.2224098356  1.663978316
## 264  0.291611743  1.1493483675  1.143729878
## 265 -0.373343939 -0.5352234441 -0.583947913
## 266 -0.373343939 -0.8940075835 -0.942732053
## 267  0.644288966  0.3263667949  0.660009421
## 268  0.644288966  0.0168629555  0.350505582
## 269  0.167489396 -0.0715063166 -0.131601564
## 270 -0.671480042  1.5042911537  1.696601446
## 271 -0.671480042  1.5042911537  1.696601446
## 272  1.235799087  1.5293096776  2.117028062
## 273  1.272496473  1.6213111934  2.372332191
## 274  0.712642360  0.3258693246  0.026859004
## 275  1.325547549  0.8313911796  1.116788449
## 276  2.575547549  1.7707371456  2.056134415
## 277 -0.696973181 -0.0557812188  0.159334992
## 278  0.960278800  1.5533882633  2.076415496
## 279 -0.865548066 -0.3855694485 -0.345640462
## 280 -0.865548066 -0.5025301269 -0.462601140
## 281  0.494176730  0.5744469030  1.210025536
## 282 -0.748581950  0.0219091737  0.192184825
## 283 -0.748581950 -0.4821318275 -0.311856176
## 285 -0.245374409  0.6764651992  0.922995864
## 286  0.504625591  1.1962165408  1.442747206
## 287 -0.245374409  0.7106910191  0.957221684
## 288 -0.140618882  0.7909823088  1.682757448
## 289 -0.681435730  0.6797856614  0.897214645
## 290  0.122730943  1.4197306540  1.580305744
## 291 -0.130571685  0.3400647356  0.800391783
## 292  0.602326393  0.8589401762  1.433681925
## 293 -1.672833684  0.1837871187  0.527451053
## 294 -1.755548079  1.0010313169  1.177409647
## 295  3.134717128  0.9284344676  0.495737046
## 296  1.282023418  0.3629731987  0.276731596
## 297  3.134717128  1.4921326550  1.059435234
## 298  3.134717128  1.3483685743  0.915671153
## 299  2.105223848  0.1956157983 -0.284363678
## 301  2.732526589  0.7692773458  0.332532572
## 302  2.702332882  0.6748838880  0.550482598
## 303  1.950890644  0.0003183676 -0.295865349
## 304  3.768346356  0.8821115775  0.322022819
## 305  9.404193272  6.2226779912  5.626742318
## 306  2.402332882  0.3440315068  0.219630217
## 307  3.729533453  0.8212300347  0.373178844
## 308  3.289081056  1.6601951025  1.161521421
## 309  3.915393942  1.6240225950  1.183873024
## 310  0.939043842 -0.6055780338 -0.522928175
## 311  1.724707076  0.7043926062  0.804066452
## 312  1.082645909 -0.7020797665 -0.697868972
## 313  2.102332882  0.5748481798  0.450446889
## 314 -0.007048189 -0.8862877707 -1.087008437
## 315  1.602332882  0.3459370643  0.221535774
## 316  2.731764714  1.3463318416  1.033573608
## 317  1.288618607 -0.1556610292 -0.172585935
## 318  1.831625330  1.2126410557  1.114321758
## 319  1.991509573  1.7388898595  1.366386767
## 320  2.779802155  1.7854600098  1.577351556
## 321  5.703914504  3.1631375781  2.996079778
## 322  2.685308895  1.1279014595  0.667836936
## 323  4.585308895  1.8826932012  1.422628678
## 324  0.598985957 -0.3873553537 -0.373572722
## 325  4.003717248  2.2736979124  2.115762479
## 326  5.752472266  3.3666901835  3.027849957
## 327  3.618404228  1.9727651553  1.617992966
## 328  1.931764714  0.6847716448  0.372013411
## 329  1.532387205  0.6067623474  0.384456511
## 330  3.082387205  1.2398951472  1.017589310
## 331  1.988618607  1.4119562670  1.395031361
## 332 -0.007048189 -0.4510774710 -0.651798137
## 333  3.237632328  1.3098080349  1.338494966
## 334 -0.016537920 -0.1142788695 -0.059060475
## 335  1.439832865  0.9292911868  0.975451575
## 336  0.992360044  0.3057571224  0.132403559
## 337  0.598985957 -0.1618184637 -0.148035832
## 338  4.226839743  1.7776712222  1.676075851
## 339  2.974084585  0.3988240074  0.408045456
## 340  2.888707203  0.8464628708  1.152674033
## 341  1.240030121  2.3960379165  2.433075937
## 342  1.020298850  1.2774276497  1.281509721
## 343  0.045213796  0.2759376333  0.328329423
## 344  0.245213796  0.0438855671  0.096277357
## 345 -0.012371248 -0.2691430134 -0.270778512
## 346  0.585043701 -0.2961317634 -0.283569879
## 347  2.611424526  2.7616439013  2.369225856
## 348  5.892271448  4.5425139610  4.046024329
## 349  1.804183206  1.4253717505  1.498256687
## 350  0.055778401  0.5115581138  0.585535075
## 351 -0.304786204 -0.0364199195  0.015971871
## 352  2.148135486  1.7012848106  1.515917914
## 353  1.240030121  2.0322899394  2.069327960
## 354 -0.558769195 -0.0259284318  0.030446349
## 355  0.544622029 -0.4946681131 -0.414909221
## 356  1.843438495  1.4000525726  1.534545670
## 357  0.314353300 -0.0985817363  0.014133106
## 358 -1.552061770 -1.3477749660 -1.524019495
## 359  0.998788701 -0.8876567435 -0.864751744
## 360 -1.944221599 -0.5580795165 -0.484102556
## 361  0.253516416  0.2067170384  0.027581543
## 362  1.579298984  0.6785561523  0.820950869
## 363  2.437207093  1.4686831208  1.397795287
## 364 -1.699588955  0.2348552225  0.346349317
## 365  0.764536982  0.8904171588  0.950261098
## 366  0.542812427  0.4076138864  0.421332157
## 367 -0.229898406 -1.0801562293 -1.066951790
## 368  0.512642360 -0.1506330319 -0.449643353
## 369  0.253516416  0.2242311988  0.045095704
## 371 -0.381296346 -0.8304037552 -0.827413709
## 372 -2.623684155 -1.3024201087 -1.157905704
## 373 -0.327986634 -0.8623317426 -0.416202078
## 374 -1.754588949 -1.2677347938 -1.224465371
## 375 -2.580252196 -0.6183106656 -0.455615887
## 376 -0.509969879  0.1221534435  0.159191464
## 377 -0.509969879 -0.6006657388 -0.563627718
## 378 -1.249602529 -0.3223657449 -0.254620186
## 379 -2.623684155 -1.2455705784 -1.101056173
## 380 -1.134476740 -0.5488691183 -0.534637016
## 381 -0.933003778 -1.0807998854 -0.776965857
## 382 -0.007214721 -0.7504351309 -0.824213930
## 383 -0.718272500  0.3336538597  0.602219166
## 384 -0.231238474 -1.0731465983 -0.864839982
## 385  4.255618359  2.7567590797  2.258534868
## 386 -0.007048189 -0.3038539366 -0.504574603
## 387  0.048135486 -0.9419659788 -1.127332876
## 388  0.282387205 -0.9684036321 -1.190709469
## 389  0.185043701 -0.7637579176 -0.751196033
## 390  0.282387205 -1.1392313627 -1.361537200
## 391  1.084744276 -0.0383965041 -0.383596856
## 392  2.205618359  1.1665569431  0.668332732
## 393  0.288618607 -1.0325925858 -1.049517492
## 394  0.288618607 -1.1207208854 -1.137645792
## 395  1.917853183 -0.1525199606 -0.348679443
## 396  0.624281840 -0.0212549270 -0.021155846
## 397  1.397074185  0.2732543087 -0.145888284
## 398 -0.261381393 -1.0863245208 -1.103249427
## 399  0.288618607 -0.8127395011 -0.829664407
## 400 -0.007048189 -0.7558577480 -0.956578414
## 401 -0.007048189 -0.4954461263 -0.696166792
## 402  0.742951811 -0.8059531639 -1.006673830
## 403 -0.301014043 -1.3807529927 -1.366970361
## 404 -0.301014043 -1.4545759767 -1.440793345
## 405  3.688686545  1.0952441665  0.614815220
## 406  3.134717128  0.5378660652  0.105168644
## 407  1.194394049  0.8321699608  0.803231721
## 408  2.365788454  1.6017270111  1.143332705
## 409  3.867921121  2.6760823684  2.016418846
## 410  1.714802175  0.3400349087 -0.072747560
## 411  1.787570880  1.5676986869  1.360746619
## 412  1.787570880  1.3568984740  1.149946406
## 413  2.017655927  0.4041405879  0.217103473
## 414  2.736509579  1.0732250910  0.632497327
## 415  3.079533453  0.7842627018  0.336211511
## 416 -1.201806642 -0.4952551172 -0.475305445
## 417  0.644394049  0.8218536011  0.792915362
## 418  1.200890644 -0.0347617991 -0.330945516
## 420  3.005618359  2.3419399634  1.843715752
## 421 -1.201806642 -0.3420323055 -0.322082633
## 422  3.043713686  3.2024902542  2.877783049
## 423  2.148135486  1.3912176464  1.205850750
## 424  2.989081056  0.9919515647  0.493277883
## 425 -0.509969879 -0.7112132475 -0.674175227
## 426 -0.483092374  0.9263314853  0.907029445
## 427  2.523036858  1.8169558815  1.636150168
## 428  4.573036858  3.0172342159  2.836428503
## attr(,"constant")
## [1] 10.75134
``````
``````pr0 <- residuals(m, type = "partial")
``````
• Their sample means are indeed (numerically) equal to zero.
``````apply(pr0, 2, mean)
``````
``````##       lweight   engine.size    horsepower
## -3.241961e-16 -9.898315e-19 -1.519474e-17
``````

### Response-mean partial residuals

``````#residuals(m, type = "partial") + ybar
prY <- residuals(m, type = "partial") + ybar
``````
• Their sample means are indeed equal to the response mean.
``````apply(prY, 2, mean)
``````
``````##     lweight engine.size  horsepower
##    10.75134    10.75134    10.75134
``````

### Partial residuals according to the definition from the lecture

``````(betahat <- coef(m)[-1])         ## LSE of beta (excluding intercept)
``````
``````##     lweight engine.size  horsepower
## 6.935603862 0.352687221 0.003982991
``````
``````#residuals(m, type = "partial") + matrix(betahat * xbar, nrow = nrow(CarsNow), ncol = length(xbar), byrow = TRUE)
prLecture <- residuals(m, type = "partial") + matrix(betahat * xbar, nrow = nrow(CarsNow), ncol = length(xbar), byrow = TRUE)
``````
• Their sample means are indeed equal to the values of `betahat * xbar`.
``````apply(prLecture, 2, mean)
``````
``````##     lweight engine.size  horsepower
##  51.1243230   1.1209245   0.8593619
``````
``````betahat * xbar
``````
``````##     lweight engine.size  horsepower
##  51.1243230   1.1209245   0.8593619
``````

## Use of partial residuals as graphical diagnostic tools and as visualization tool

• We base all plots on the response-mean partial residuals:
``````(ybar <- with(CarsNow, mean(consumption)))
``````
``````## [1] 10.75134
``````
``````prY <- residuals(m, type = "partial") + ybar
print(prY[1:10,])
``````
``````##     lweight engine.size horsepower
## 1  8.655739   10.812395  10.919906
## 2  8.655739   10.877214  10.984726
## 3  8.346756   10.027648  10.070917
## 4  8.346756    9.871655   9.914924
## 5  8.346756   10.027648  10.070917
## 6  8.049157    9.753635   9.835578
## 7  8.049157    9.636179   9.718122
## 8  8.807123   10.429173  10.503150
## 9  8.436783   10.076414  10.070731
## 10 8.807123   10.446754  10.520731
``````

On all plots, we always show a marginal dependence of the response on a particular covariate (estimated slope comes from a simple regression line) and additionally, we show visualization of a partial dependence of the response on a particular covariate while adjusting for the effect of remaining (two) covariates

### Plot for `lweight`

``````par(mfrow = c(1, 2), mar = c(4, 4, 2, 1) + 0.1, bty = BTY)
#
YLIM <- range(c(CarsNow[, "consumption"], prY[, "lweight"]))
mMarg <- lm(consumption ~ lweight, data = CarsNow)
bex <- round(coef(m)["lweight"], 2)
bexCI <- round(confint(m)["lweight", ], 2)
beMarg <- round(coef(mMarg)[2], 2)
beMargCI <- round(confint(mMarg)[2, ], 2)
#
plot(consumption ~ lweight, data = CarsNow, ylim = YLIM, main = "Marginal",
xlab = "Log(weight) [log(kg)]", ylab = "Consumption [l/100 km]", pch = PCH, col = COL, bg = BGC)
abline(mMarg, col = "blue3", lwd = 2)
text(6.9, 19, labels = eval(substitute(expression(paste(hat(beta), " = ", be, " (", beCI1, ", ", beCI2, ")", sep="")),
list(be = beMarg, beCI1 = beMargCI[1], beCI2 = beMargCI[2]))),
pos = 4, cex = 1.7)
#
plot(prY[, "lweight"] ~ lweight, data = CarsNow, ylim = YLIM, main = "Partial",
xlab = "Log(weight) [log(kg)]", ylab = "Response-mean partial residuals [l/100 km]", pch = PCH3, col = COL3, bg = BGC3)
abline(lm(prY[, "lweight"] ~ lweight, data = CarsNow), col = "red2", lwd = 2)
text(6.9, 19, labels = eval(substitute(expression(paste(hat(beta), " = ", be, " (", beCI1, ", ", beCI2, ")", sep="")),
list(be = bex, beCI1 = bexCI[1], beCI2 = bexCI[2]))),
pos = 4, cex = 1.7)
``````

``````#
#par(mfrow = c(1, 1), mar = c(5, 4, 4, 1) + 0.1 )
``````

### Plot for `engine.size`

``````par(mfrow = c(1, 2), mar = c(4, 4, 2, 1) + 0.1, bty = BTY)
#
YLIM <- range(c(CarsNow[, "consumption"], prY[, "engine.size"]))
mMarg <- lm(consumption ~ engine.size, data = CarsNow)
bex <- round(coef(m)["engine.size"], 2)
bexCI <- round(confint(m)["engine.size", ], 2)
beMarg <- round(coef(mMarg)[2], 2)
beMargCI <- round(confint(mMarg)[2, ], 2)
#
plot(consumption ~ engine.size, data = CarsNow, ylim = YLIM, main = "Marginal",
xlab = "Engine size [l]", ylab = "Consumption [l/100 km]", pch = PCH, col = COL, bg = BGC)
abline(mMarg, col = "blue3", lwd = 2)
text(1.4, 19, labels = eval(substitute(expression(paste(hat(beta), " = ", be, " (", beCI1, ", ", beCI2, ")", sep="")),
list(be = beMarg, beCI1 = beMargCI[1], beCI2 = beMargCI[2]))),
pos = 4, cex = 1.7)
#
plot(prY[, "engine.size"] ~ engine.size, data = CarsNow, ylim = YLIM, main = "Partial",
xlab = "Engine size [l]", ylab = "Response-mean partial residuals [l/100 km]", pch = PCH3, col = COL3, bg = BGC3)
abline(lm(prY[, "engine.size"] ~ engine.size, data = CarsNow), col = "red2", lwd = 2)
text(1.4, 19, labels = eval(substitute(expression(paste(hat(beta), " = ", be, " (", beCI1, ", ", beCI2, ")", sep="")),
list(be = bex, beCI1 = bexCI[1], beCI2 = bexCI[2]))),
pos = 4, cex = 1.7)
``````

``````#
#par(mfrow = c(1, 1), mar = c(5, 4, 4, 1) + 0.1 )
``````

### Plot for `horsepower`

``````par(mfrow = c(1, 2), mar = c(4, 4, 2, 1) + 0.1, bty = BTY)
#
YLIM <- range(c(CarsNow[, "consumption"], prY[, "horsepower"]))
mMarg <- lm(consumption ~ horsepower, data = CarsNow)
bex <- round(coef(m)["horsepower"], 3)
bexCI <- round(confint(m)["horsepower", ], 3)
beMarg <- round(coef(mMarg)[2], 3)
beMargCI <- round(confint(mMarg)[2, ], 3)
#
plot(consumption ~ horsepower, data = CarsNow, ylim = YLIM, main = "Marginal",
xlab = "Horsepower", ylab = "Consumption [l/100 km]", pch = PCH, col = COL, bg = BGC)
abline(mMarg, col = "blue3", lwd = 2)
text(90, 19, labels = eval(substitute(expression(paste(hat(beta), " = ", be, " (", beCI1, ", ", beCI2, ")", sep="")),
list(be = beMarg, beCI1 = beMargCI[1], beCI2 = beMargCI[2]))),
pos = 4, cex = 1.7)
#
plot(prY[, "horsepower"] ~ horsepower, data = CarsNow, ylim = YLIM, main = "Partial",
xlab = "Horsepower", ylab = "Response-mean partial residuals [l/100 km]", pch = PCH3, col = COL3, bg = BGC3)
abline(lm(prY[, "horsepower"] ~ horsepower, data = CarsNow), col = "red2", lwd = 2)
text(90, 19, labels = eval(substitute(expression(paste(hat(beta), " = ", be, " (", beCI1, ", ", beCI2, ")", sep="")),
list(be = bex, beCI1 = bexCI[1], beCI2 = bexCI[2]))),
pos = 4, cex = 1.7)
``````

``````#
par(mfrow = c(1, 1), mar = c(5, 4, 4, 1) + 0.1 )
``````