NMSA407 Linear Regression: Tutorial

Regression line

Data Cars2004nh




Introduction

Load used data and calculate basic summaries

data(Cars2004nh, package = "mffSM")
head(Cars2004nh)
##                         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

In the following, we will use a subset of the dataset Cars2004nh where all needed variables (consumption, lweight, engine.size) are known (not missing).

isComplete <- complete.cases(Cars2004nh[, c("consumption", "lweight", "engine.size")])
sum(!isComplete)
## [1] 16
CarsNow <- subset(Cars2004nh, isComplete)
dim(CarsNow)
## [1] 409  20
summary(CarsNow)
##     vname                type           drive        price.retail     price.dealer   
##  Length:409         Min.   :1.000   Min.   :1.000   Min.   : 10280   Min.   :  9875  
##  Class :character   1st Qu.:1.000   1st Qu.:1.000   1st Qu.: 20585   1st Qu.: 19261  
##  Mode  :character   Median :1.000   Median :1.000   Median : 27905   Median : 25672  
##                     Mean   :2.232   Mean   :1.699   Mean   : 32819   Mean   : 30052  
##                     3rd Qu.:3.000   3rd Qu.:2.000   3rd Qu.: 39235   3rd Qu.: 35688  
##                     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.: 19912   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   : 31435   Mean   :12.36   Mean   : 9.142   Mean   :10.75   Mean   :3.178   Mean   : 5.763  
##  3rd Qu.: 37514   3rd Qu.:13.80   3rd Qu.: 9.800   3rd Qu.:11.65   3rd Qu.:3.800   3rd Qu.: 6.000  
##  Max.   :183012   Max.   :23.50   Max.   :19.600   Max.   :21.55   Max.   :6.000   Max.   :12.000  
##                                                                                                    
##    horsepower        weight        iweight             lweight        wheel.base        length   
##  Min.   :100.0   Min.   : 923   Min.   :0.0003445   Min.   :6.828   Min.   :226.0   Min.   :363  
##  1st Qu.:165.0   1st Qu.:1415   1st Qu.:0.0005543   1st Qu.:7.255   1st Qu.:262.0   1st Qu.:450  
##  Median :210.0   Median :1577   Median :0.0006341   Median :7.363   Median :272.0   Median :472  
##  Mean   :215.8   Mean   :1622   Mean   :0.0006416   Mean   :7.371   Mean   :274.6   Mean   :470  
##  3rd Qu.:250.0   3rd Qu.:1804   3rd Qu.:0.0007067   3rd Qu.:7.498   3rd Qu.:284.0   3rd Qu.:490  
##  Max.   :493.0   Max.   :2903   Max.   :0.0010834   Max.   :7.973   Max.   :366.0   Max.   :561  
##                                                                                     NA's   :23   
##      width            ftype       fdrive   
##  Min.   :163.0   personal:231   front:212  
##  1st Qu.:175.0   wagon   : 29   rear :108  
##  Median :180.0   SUV     : 59   4x4  : 89  
##  Mean   :181.1   pickup  : 23              
##  3rd Qu.:185.0   sport   : 47              
##  Max.   :206.0   minivan : 20              
##  NA's   :25




Simple regression

In the following, we will try to model dependence of the car consumption (consumption) on its weight (weight) or possibly its logarithmic transformation (lweight).

Basic scatterplot (with weight as a covariate)

par(mfrow = c(1, 1), bty = BTY, mar = c(4, 4, 1, 1) + 0.1)
plot(consumption ~ weight, data = CarsNow, pch = PCH, col = COL, bg = BGC, xlab = "Weight [kg]", ylab = "Consumption [l/100 km]")

plot of chunk fig-NormalLM-01-00

#lines(lowess(CarsNow[, "weight"], CarsNow[, "consumption"]), col = "blue", lwd = 2)

Basic scatterplot (with lweight as a covariate)

par(mfrow = c(1, 1), bty = BTY, mar = c(4, 4, 1, 1) + 0.1)
plot(consumption ~ lweight, data = CarsNow, pch = PCH, col = COL, bg = BGC, xlab = "Log(weight) [log(kg)]", ylab = "Consumption [l/100 km]")

plot of chunk fig-NormalLM-01-01

#lines(lowess(CarsNow[, "lweight"], CarsNow[, "consumption"]), col = "blue", lwd = 2)

Model with intercept only

(meanY <- with(CarsNow, mean(consumption)))
## [1] 10.75134
(sdY <- with(CarsNow, sd(consumption)))
## [1] 2.133556
m0 <- lm(consumption ~ 1, data = CarsNow)
summary(m0)
## 
## Call:
## lm(formula = consumption ~ 1, data = CarsNow)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.1013 -1.1013 -0.0513  0.8987 10.7987 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  10.7513     0.1055   101.9   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.134 on 408 degrees of freedom

Regression line (with lweight as a covariate)

m1 <- lm(consumption ~ lweight, data = CarsNow)
summary(m1)
## 
## Call:
## lm(formula = consumption ~ lweight, data = CarsNow)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.6544 -0.7442 -0.1526  0.5160  5.1616 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -58.2480     1.8941  -30.75   <2e-16 ***
## lweight       9.3606     0.2569   36.44   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.035 on 407 degrees of freedom
## Multiple R-squared:  0.7654, Adjusted R-squared:  0.7648 
## F-statistic:  1328 on 1 and 407 DF,  p-value: < 2.2e-16

Fitted line and the line from intercept only model

par(mfrow = c(1, 1), bty = BTY, mar = c(4, 4, 1, 1) + 0.1)
plot(consumption ~ lweight, data = CarsNow, pch = PCH, col = COL2, bg = BGC2, xlab = "Log(weight) [log(kg)]", ylab = "Consumption [l/100 km]")
abline(h = coef(m0), col = "blue", lty = 5, lwd = 2)
abline(m1, col = "red2", lwd = 2)
legend(6.9, 20, legend = c("m0", "m1"), col = c("blue", "red2"), lty = c(5, 1), lwd = 2)

plot of chunk fig-NormalLM-01-02




Useful components of the object m1

List of elements of m1

names(m1)
##  [1] "coefficients"  "residuals"     "effects"       "rank"          "fitted.values" "assign"       
##  [7] "qr"            "df.residual"   "xlevels"       "call"          "terms"         "model"

Estimated regression coefficients

m1[["coefficients"]]
## (Intercept)     lweight 
##   -58.24803     9.36056
coef(m1)                   ## hat{beta}
## (Intercept)     lweight 
##   -58.24803     9.36056

Fitted values

m1[["fitted.values"]]
##         1         2         3         4         5         6         7         8         9        10 
##  7.089395  7.001912  8.017106  8.227639  8.017106  7.890073  8.048596  8.001320  7.977593  7.977593 
##        11        12        13        14        15        16        17        18        19        20 
##  8.281458  7.330084  7.589535  7.638931  6.625287  6.770556  6.966689  8.079981  8.079981  8.304429 
##        21        22        23        24        25        26        28        31        32        33 
## 10.132628  7.219104  7.431371  8.173510  8.258431  8.296778  7.679896  7.638931  7.890073  8.553455 
##        34        35        36        37        38        39        40        41        42        43 
##  8.281458  8.281458  8.281458  8.486193  8.486193  6.966689  8.227639  8.227639  8.312073  8.501182 
##        44        45        46        47        48        49        50        51        52        53 
##  7.597786  7.679896  7.679896  5.662408  5.892803  5.753239 10.337952 10.646670  9.825697 10.176560 
##        54        55        56        57        58        59        60        61        62        63 
## 10.300954  9.608688  9.615338  9.819194  9.961219 10.658572  9.845178  9.825697  8.478689 10.207814 
##        64        65        66        67        68        71        72        73        75        76 
## 10.226516  9.276886  9.440871  7.961741  8.590614  9.948396  9.948396 10.126335  9.427315 10.207814 
##        77        80        81        82        83        84        85        86        87        88 
##  8.235347  9.413739  8.516147  9.124001  9.655142 10.676396  8.553455  9.890476  9.186847 10.145201 
##        89        90        91        92        93        95        96        97        98        99 
## 10.411513  9.562002 10.176560  9.825697 10.514745  8.968577  9.088903  9.304417  8.715866  8.575768 
##       100       101       102       103       104       105       106       107       108       109 
##  9.986812 10.050489  9.954810 10.916649 10.634753 10.835302 10.676396 11.020208 10.232742 10.562933 
##       110       111       112       113       114       115       116       117       118       119 
## 10.951296 10.682329 10.864436  9.948396 10.350253 10.706027 12.120180 12.120180 10.325636 10.170296 
##       120       121       122       123       124       126       127       128       129       130 
## 11.133947 11.133947 10.288589 10.508704 10.544891 10.044141 10.550909 12.110000 12.110000 10.232742 
##       131       132       133       134       135       136       137       138       139       140 
## 10.870252 11.128293  9.890476 10.664517 10.676396  9.555313 10.694186 10.454158 10.723761 11.025928 
##       141       142       143       144       145       146       147       148       149       150 
## 10.514745 10.362537 10.574941  9.838689  9.548620 10.018705  8.538549 10.939761 11.099970 10.634753 
##       151       152       153       154       155       156       157       158       159       160 
## 10.957058 11.071562 10.899277 11.704087  9.890476 10.899277 10.634753 10.145201 10.145201 10.694186 
##       161       162       163       164       165       166       167       168       169       170 
## 10.544891 10.980071 11.456182 11.246320 11.133947 10.598912 12.120180 11.201532 10.207814 10.782633 
##       171       172       173       174       175       176       177       178       179       181 
## 10.628789 10.056833 10.145201 11.212749 11.212749 10.356397 10.356397 10.550909 10.604895 12.110000 
##       182       184       185       186       187       188       189       190       191       192 
## 12.435313  9.825697  9.825697 10.658572 10.658572 11.082935 10.574941 11.313102 11.877972 11.757121 
##       193       194       195       196       197       198       199       200       201       202 
## 11.423357 10.928212 10.939761 11.235143 11.704087 11.735944 11.543156 12.017878 11.597108 12.043559 
##       203       204       205       206       207       208       209       210       211       212 
## 12.877248 11.570171 11.043067 10.664517 11.543156 12.830210 13.016961 11.772973 11.950777 12.089607 
##       213       214       215       216       217       218       219       220       221       222 
## 11.971475 11.634690 11.935224 11.450719 11.688118 12.094709 11.516064 11.516064 11.867525 11.128293 
##       223       224       225       226       227       228       229       230       231       232 
## 11.296451 11.966305 11.428836 11.428836 12.816052 12.816052 13.035432 10.846967 12.186082 13.035432 
##       233       234       235       236       237       238       239       240       243       244 
## 11.434311 10.962817 11.094295 11.543156 12.356279 12.858461 10.688259 11.257483 10.604895 10.604895 
##       245       246       247       248       249       250       251       252       253       254 
## 11.139598  9.760466 12.043559  9.694778  9.046611 10.331796 10.508704 11.461642  9.081868  9.290662 
##       255       256       257       258       260       261       262       263       264       265 
## 11.122635 10.031432 10.037789  9.481422 10.157757 10.319472 11.461642  8.766957  9.366068 11.456182 
##       266       267       268       269       270       271       272       273       274       275 
## 11.940411 11.666783 12.084502 11.607861  7.158797  7.158797  9.461169  9.386528 12.140507 12.523436 
##       276       277       278       279       280       281       282       283       285       286 
## 12.942707  9.467925  9.961219 10.018705 10.176560 10.082167  9.864618 10.544891  9.707952 10.018705 
##       287       288       289       290       291       292       293       294       295       296 
##  9.661760  9.694778  8.686545  9.011222  9.555313 10.082167  7.589535  6.374916 14.738988 12.668489 
##       297       298       299       301       302       303       304       305       306       307 
## 13.978199 14.172229 14.052984 14.077781 13.974027 14.155871 15.989878 16.388400 14.015666 15.734063 
##       308       309       310       311       312       313       314       315       316       317 
## 14.102511 14.568227 12.989184 12.043559 13.741638 13.299256 12.043559 12.933384 13.012337 12.853759 
##       318       319       320       321       322       323       324       325       326       327 
## 11.597108 11.483448 12.246506 14.762034 13.577641 15.123258 12.140507 13.763004 14.838447 13.839522 
##       328       329       330       331       332       333       334       335       336       337 
## 12.825493 12.296562 13.534002 11.682789 11.456182 13.982370 10.417617 11.212749 12.069169 11.836114 
##       338       339       340       341       342       343       344       345       346       347 
## 14.781196 14.237374 13.375195  8.868309 10.319472 10.069509 10.652623 10.870252 11.570171 10.939761 
##       348       349       350       351       352       353       354       355       356       357 
## 13.249788 10.939761  9.575364 10.018705 11.412390  9.359238  9.661760 12.069169 11.836114 10.985816 
##       358       359       360       361       362       363       364       365       366       367 
## 10.628789 13.450523  8.319711 10.729665 12.120180 12.687661  7.389301 10.496610 10.658572 11.909244 
##       368       369       371       372       373       374       375       376       377       378 
## 12.513686 10.706027 11.510636  8.312073 11.054475  9.628625  7.304590  9.575364 10.550909  9.081868 
##       379       380       381       382       383       384       385       386       387       388 
##  8.235347  9.400143 10.294774 12.145582  8.723182 11.564774 13.308222 11.257483 12.145582 12.735419 
##       389       390       391       392       393       394       395       396       397       398 
## 11.661442 12.965975 12.610737 12.687661 12.687661 12.806602 13.698759 11.537744 12.754454 12.017878 
##       399       400       401       402       403       404       405       406       407       408 
## 12.390938 11.867525 11.516064 12.947365 12.266560 12.366195 15.594714 15.266114 11.060174 12.316510 
##       409       410       411       412       413       414       415       416       417       418 
## 13.703056 13.616743 11.296451 11.580955 13.177366 13.672936 14.906690  9.379713 10.331796 13.190988 
##       420       421       422       423       424       425       426       427       428 
## 12.181029  9.172918 10.928212 11.830868 14.599507 10.700108  8.478689 11.809857 12.956674
fitted(m1)                 ## hat{Y}
##         1         2         3         4         5         6         7         8         9        10 
##  7.089395  7.001912  8.017106  8.227639  8.017106  7.890073  8.048596  8.001320  7.977593  7.977593 
##        11        12        13        14        15        16        17        18        19        20 
##  8.281458  7.330084  7.589535  7.638931  6.625287  6.770556  6.966689  8.079981  8.079981  8.304429 
##        21        22        23        24        25        26        28        31        32        33 
## 10.132628  7.219104  7.431371  8.173510  8.258431  8.296778  7.679896  7.638931  7.890073  8.553455 
##        34        35        36        37        38        39        40        41        42        43 
##  8.281458  8.281458  8.281458  8.486193  8.486193  6.966689  8.227639  8.227639  8.312073  8.501182 
##        44        45        46        47        48        49        50        51        52        53 
##  7.597786  7.679896  7.679896  5.662408  5.892803  5.753239 10.337952 10.646670  9.825697 10.176560 
##        54        55        56        57        58        59        60        61        62        63 
## 10.300954  9.608688  9.615338  9.819194  9.961219 10.658572  9.845178  9.825697  8.478689 10.207814 
##        64        65        66        67        68        71        72        73        75        76 
## 10.226516  9.276886  9.440871  7.961741  8.590614  9.948396  9.948396 10.126335  9.427315 10.207814 
##        77        80        81        82        83        84        85        86        87        88 
##  8.235347  9.413739  8.516147  9.124001  9.655142 10.676396  8.553455  9.890476  9.186847 10.145201 
##        89        90        91        92        93        95        96        97        98        99 
## 10.411513  9.562002 10.176560  9.825697 10.514745  8.968577  9.088903  9.304417  8.715866  8.575768 
##       100       101       102       103       104       105       106       107       108       109 
##  9.986812 10.050489  9.954810 10.916649 10.634753 10.835302 10.676396 11.020208 10.232742 10.562933 
##       110       111       112       113       114       115       116       117       118       119 
## 10.951296 10.682329 10.864436  9.948396 10.350253 10.706027 12.120180 12.120180 10.325636 10.170296 
##       120       121       122       123       124       126       127       128       129       130 
## 11.133947 11.133947 10.288589 10.508704 10.544891 10.044141 10.550909 12.110000 12.110000 10.232742 
##       131       132       133       134       135       136       137       138       139       140 
## 10.870252 11.128293  9.890476 10.664517 10.676396  9.555313 10.694186 10.454158 10.723761 11.025928 
##       141       142       143       144       145       146       147       148       149       150 
## 10.514745 10.362537 10.574941  9.838689  9.548620 10.018705  8.538549 10.939761 11.099970 10.634753 
##       151       152       153       154       155       156       157       158       159       160 
## 10.957058 11.071562 10.899277 11.704087  9.890476 10.899277 10.634753 10.145201 10.145201 10.694186 
##       161       162       163       164       165       166       167       168       169       170 
## 10.544891 10.980071 11.456182 11.246320 11.133947 10.598912 12.120180 11.201532 10.207814 10.782633 
##       171       172       173       174       175       176       177       178       179       181 
## 10.628789 10.056833 10.145201 11.212749 11.212749 10.356397 10.356397 10.550909 10.604895 12.110000 
##       182       184       185       186       187       188       189       190       191       192 
## 12.435313  9.825697  9.825697 10.658572 10.658572 11.082935 10.574941 11.313102 11.877972 11.757121 
##       193       194       195       196       197       198       199       200       201       202 
## 11.423357 10.928212 10.939761 11.235143 11.704087 11.735944 11.543156 12.017878 11.597108 12.043559 
##       203       204       205       206       207       208       209       210       211       212 
## 12.877248 11.570171 11.043067 10.664517 11.543156 12.830210 13.016961 11.772973 11.950777 12.089607 
##       213       214       215       216       217       218       219       220       221       222 
## 11.971475 11.634690 11.935224 11.450719 11.688118 12.094709 11.516064 11.516064 11.867525 11.128293 
##       223       224       225       226       227       228       229       230       231       232 
## 11.296451 11.966305 11.428836 11.428836 12.816052 12.816052 13.035432 10.846967 12.186082 13.035432 
##       233       234       235       236       237       238       239       240       243       244 
## 11.434311 10.962817 11.094295 11.543156 12.356279 12.858461 10.688259 11.257483 10.604895 10.604895 
##       245       246       247       248       249       250       251       252       253       254 
## 11.139598  9.760466 12.043559  9.694778  9.046611 10.331796 10.508704 11.461642  9.081868  9.290662 
##       255       256       257       258       260       261       262       263       264       265 
## 11.122635 10.031432 10.037789  9.481422 10.157757 10.319472 11.461642  8.766957  9.366068 11.456182 
##       266       267       268       269       270       271       272       273       274       275 
## 11.940411 11.666783 12.084502 11.607861  7.158797  7.158797  9.461169  9.386528 12.140507 12.523436 
##       276       277       278       279       280       281       282       283       285       286 
## 12.942707  9.467925  9.961219 10.018705 10.176560 10.082167  9.864618 10.544891  9.707952 10.018705 
##       287       288       289       290       291       292       293       294       295       296 
##  9.661760  9.694778  8.686545  9.011222  9.555313 10.082167  7.589535  6.374916 14.738988 12.668489 
##       297       298       299       301       302       303       304       305       306       307 
## 13.978199 14.172229 14.052984 14.077781 13.974027 14.155871 15.989878 16.388400 14.015666 15.734063 
##       308       309       310       311       312       313       314       315       316       317 
## 14.102511 14.568227 12.989184 12.043559 13.741638 13.299256 12.043559 12.933384 13.012337 12.853759 
##       318       319       320       321       322       323       324       325       326       327 
## 11.597108 11.483448 12.246506 14.762034 13.577641 15.123258 12.140507 13.763004 14.838447 13.839522 
##       328       329       330       331       332       333       334       335       336       337 
## 12.825493 12.296562 13.534002 11.682789 11.456182 13.982370 10.417617 11.212749 12.069169 11.836114 
##       338       339       340       341       342       343       344       345       346       347 
## 14.781196 14.237374 13.375195  8.868309 10.319472 10.069509 10.652623 10.870252 11.570171 10.939761 
##       348       349       350       351       352       353       354       355       356       357 
## 13.249788 10.939761  9.575364 10.018705 11.412390  9.359238  9.661760 12.069169 11.836114 10.985816 
##       358       359       360       361       362       363       364       365       366       367 
## 10.628789 13.450523  8.319711 10.729665 12.120180 12.687661  7.389301 10.496610 10.658572 11.909244 
##       368       369       371       372       373       374       375       376       377       378 
## 12.513686 10.706027 11.510636  8.312073 11.054475  9.628625  7.304590  9.575364 10.550909  9.081868 
##       379       380       381       382       383       384       385       386       387       388 
##  8.235347  9.400143 10.294774 12.145582  8.723182 11.564774 13.308222 11.257483 12.145582 12.735419 
##       389       390       391       392       393       394       395       396       397       398 
## 11.661442 12.965975 12.610737 12.687661 12.687661 12.806602 13.698759 11.537744 12.754454 12.017878 
##       399       400       401       402       403       404       405       406       407       408 
## 12.390938 11.867525 11.516064 12.947365 12.266560 12.366195 15.594714 15.266114 11.060174 12.316510 
##       409       410       411       412       413       414       415       416       417       418 
## 13.703056 13.616743 11.296451 11.580955 13.177366 13.672936 14.906690  9.379713 10.331796 13.190988 
##       420       421       422       423       424       425       426       427       428 
## 12.181029  9.172918 10.928212 11.830868 14.599507 10.700108  8.478689 11.809857 12.956674

Residuals

m1[["residuals"]]
##            1            2            3            4            5            6            7 
##  0.560605107  0.648087614 -0.317105535 -0.527639294 -0.317105535 -0.590073084 -0.748596095 
##            8            9           10           11           12           13           14 
##  0.048679561 -0.377592770  0.072407230 -0.231457858 -0.530083712 -1.689534710 -0.838930920 
##           15           16           17           18           19           20           21 
##  0.974712673  0.829444033  0.633310836 -0.129981071 -0.129981071 -0.354428569 -1.132627888 
##           22           23           24           25           26           28           31 
##  0.830895501  0.968628806  0.426490490  0.341569362  0.303222073 -0.279896237 -0.088930920 
##           32           33           34           35           36           37           38 
## -0.340073084 -0.103454529 -0.431457858 -0.431457858 -0.431457858 -0.636192729 -0.636192729 
##           39           40           41           42           43           44           45 
## -0.166689164  0.272360706  0.272360706  0.387927037  0.748818380 -0.947785534 -1.029896237 
##           46           47           48           49           50           51           52 
## -1.029896237  0.437591665  0.657197067  0.346760719 -0.537952493 -1.346670138 -1.475696780 
##           53           54           55           56           57           58           59 
## -0.926559528 -1.000954184 -0.208687774 -0.215338261 -0.569194133 -0.161219149 -1.008571679 
##           60           61           62           63           64           65           66 
## -0.045177661 -0.025696780  1.321310726  0.042186486  0.473484062 -1.326886349 -1.490871158 
##           67           68           71           72           73           75           76 
## -1.061740844 -0.190614403  0.601603538  0.601603538  0.123664926 -0.827314950  0.042186486 
##           77           80           81           82           83           84           85 
## -0.085346631  0.686260919  0.783853453 -0.524000751  0.294857738 -0.876395664 -0.103454529 
##           86           87           88           89           90           91           92 
##  0.209523999  0.363153029 -0.345200838 -0.161513266 -1.112001592 -0.526559528 -1.375696780 
##           93           95           96           97           98           99          100 
## -0.564744622 -0.268576560 -0.388902997 -3.654417428 -0.015865797  0.124231849 -0.586811947 
##          101          102          103          104          105          106          107 
## -0.900489472 -0.004810001 -0.966648580 -0.834753447 -0.085302418 -0.876395664 -0.270208384 
##          108          109          110          111          112          113          114 
## -1.432741784  0.187067060 -0.051295973 -1.032329458 -0.314435838  0.001603538 -1.100252837 
##          115          116          117          118          119          120          121 
##  0.193972909 -0.520180285 -0.520180285 -0.825635965 -0.670296189  0.266053156  0.266053156 
##          122          123          124          126          127          128          129 
##  0.761411155  0.541296398  0.505108621 -0.794141178  0.149090903 -0.510000230 -0.510000230 
##          130          131          132          133          134          135          136 
##  0.467258216  0.379748343 -0.078292625  0.209523999 -0.564516779 -0.576395664  0.394686911 
##          137          138          139          140          141          142          143 
## -0.894185774 -0.654158261 -0.473761028 -0.325928250 -0.864744622 -0.712537038 -0.624941357 
##          144          145          146          147          148          149          150 
## -0.338688538 -0.748619803 -0.868704857  0.861450825 -0.839761086 -2.099970182 -0.534753447 
##          151          152          153          154          155          156          157 
##  0.442941909  0.178438216 -0.649276673 -0.454086889  0.059523999 -0.349276673 -0.084753447 
##          158          159          160          161          162          163          164 
## -0.345200838 -0.345200838 -0.744185774 -0.144891379 -1.030071158 -1.506181919  0.003680136 
##          165          166          167          168          169          170          171 
## -0.233946844 -0.798912075 -0.520180285 -0.151532409  0.492186486  0.467367331 -0.678789407 
##          172          173          174          175          176          177          178 
##  1.193166536  1.304799162 -0.812749379 -0.812749379  0.243603047  0.543603047  0.149090903 
##          179          181          182          184          185          186          187 
## -0.204895175 -0.510000230 -0.435313059  0.274303220  0.274303220 -1.008571679 -1.008571679 
##          188          189          190          191          192          193          194 
## -0.382935491 -0.924941357 -0.613101789 -0.627972263 -1.507121366 -1.323357011  0.321788034 
##          195          196          197          198          199          200          201 
## -0.839761086 -0.985143074 -0.254086889 -0.285943619 -1.293156391 -0.767878064 -0.347107895 
##          202          203          204          205          206          207          208 
## -0.243558629 -1.077248148  2.729828987 -0.643066906 -0.864516779 -0.493156391 -1.780210059 
##          209          210          211          212          213          214          215 
## -1.966960594 -1.022973292 -0.900776987 -1.039606845 -0.921474751  0.365310224  0.064776426 
##          216          217          218          219          220          221          222 
## -0.400719089 -0.938117775 -0.294709358 -0.766063578 -0.766063578 -0.067525209  0.121707375 
##          223          224          225          226          227          228          229 
##  0.353549078 -0.716304599  0.371164171  0.371164171 -1.216052415 -1.216052415 -1.435432352 
##          230          231          232          233          234          235          236 
##  2.103033331  0.063917855  2.214567648 -1.034311442  1.287183336 -0.544295396  1.706843609 
##          237          238          239          240          243          244          245 
## -1.306278836 -0.608461268 -1.038259493 -1.757483324 -0.804895175 -0.204895175 -0.439597650 
##          246          247          248          249          250          251          252 
##  2.039533868  1.156441371  0.405222279  1.053388762 -0.681796255  1.741296398  0.988358436 
##          253          254          255          256          257          258          260 
##  1.018132374  0.359337989  0.477365012  1.218568342  1.212211425  2.118577615 -0.207756923 
##          261          262          263          264          265          266          267 
##  1.280528381  0.338358436  1.833043299  1.333932118 -0.406181919 -0.890410917  0.783216844 
##          268          269          270          271          272          273          274 
##  0.365498451  0.042139019  2.141203046  2.141203046  1.788831269  2.063471869  0.309492757 
##          275          276          277          278          279          280          281 
##  1.476563862  2.307293123  0.182075181  2.288780851 -0.218704857 -0.376559528  0.967833475 
##          282          283          285          286          287          288          289 
##  0.535381916 -0.144891379  1.342047667  1.781295143  1.388240162  2.105222279  1.263454697 
##          290          291          292          293          294          295          296 
##  2.038778446  0.694686911  1.367833475  0.860465290  1.825084333  0.211011850  0.281510958 
##          297          298          299          301          302          303          304 
##  0.971800799  0.777771217 -0.602984367 -0.027780567  0.075973102 -0.605871313 -0.039878167 
##          305          306          307          308          309          310          311 
##  5.161599809 -0.265666469 -0.134062644  1.147488745  0.731773072 -0.989184431  0.406441371 
##          312          313          314          315          316          317          318 
## -1.041638160  0.150744312 -1.343558629  0.016616391  0.737663046 -0.603758650  0.902892105 
##          319          320          321          322          323          324          325 
##  1.466551647  1.303494449  2.387965732  0.472359250  0.826741743 -0.690507243  1.836995567 
##          326          327          328          329          330          331          332 
##  2.611552572  1.460478300  0.124506777  0.203437858  0.515998304  1.267211323 -0.756181919 
##          333          334          335          336          337          338          339 
##  0.967630356 -0.317617316  0.687250621  0.080831068 -0.386113904  1.168803883 -0.487374310 
##          340          341          342          343          344          345          346 
##  0.374805207  2.681690537  1.330528381  0.230491435 -0.152622800 -0.470251657 -0.770171013 
##          347          348          349          350          351          352          353 
##  2.610238914  3.900211716  1.210238914  0.474635722 -0.068704857  1.387610255  2.190762151 
##          354          355          356          357          358          359          360 
##  0.038240162 -0.819168932  1.613886096 -0.285815595 -1.378789407 -1.450522784 -0.269711120 
##          361          362          363          364          365          366          367 
## -0.029664882  0.579819715  1.362339297  0.660698810  0.953390147  0.241428321 -1.359243651 
##          368          369          371          372          373          374          375 
## -0.263685553 -0.006027091 -0.910635593 -1.012072963 -0.804475268 -1.278625077 -0.154589660 
##          376          377          378          379          380          381          382 
##  0.224635722 -0.750909097 -0.131867626 -0.935346631 -0.600143494 -1.144773556 -0.895582092 
##          383          384          385          386          387          388          389 
##  0.676818407 -1.314774322  1.991778257 -0.557483324 -1.445582092 -1.485418766 -1.261441895 
##          390          391          392          393          394          395          396 
## -1.715974501 -0.610737331  0.562339297 -1.437660703 -1.556602076 -0.998758744 -0.287744098 
##          397          398          399          400          401          402          403 
## -0.304453974 -1.317878064 -1.140938448 -1.167525209 -0.816063578 -1.497365031 -1.716560321 
##          404          405          406          407          408          409          410 
## -1.816194685  0.355285819 -0.316114423  0.589825761  1.133489934  2.246944464 -0.066742706 
##          411          412          413          414          415          416          417 
##  1.403549078  1.119044931 -0.227365795  0.377063507  0.043310033 -0.429713016  0.768203745 
##          418          420          421          422          423          424          425 
## -0.390987762  1.868970789 -0.222917563  3.121788034  0.969131577  0.350492976 -0.900108305 
##          426          427          428 
##  1.221310726  1.440142944  2.343325609
residuals(m1)              ## U = Y - hat{Y}
##            1            2            3            4            5            6            7 
##  0.560605107  0.648087614 -0.317105535 -0.527639294 -0.317105535 -0.590073084 -0.748596095 
##            8            9           10           11           12           13           14 
##  0.048679561 -0.377592770  0.072407230 -0.231457858 -0.530083712 -1.689534710 -0.838930920 
##           15           16           17           18           19           20           21 
##  0.974712673  0.829444033  0.633310836 -0.129981071 -0.129981071 -0.354428569 -1.132627888 
##           22           23           24           25           26           28           31 
##  0.830895501  0.968628806  0.426490490  0.341569362  0.303222073 -0.279896237 -0.088930920 
##           32           33           34           35           36           37           38 
## -0.340073084 -0.103454529 -0.431457858 -0.431457858 -0.431457858 -0.636192729 -0.636192729 
##           39           40           41           42           43           44           45 
## -0.166689164  0.272360706  0.272360706  0.387927037  0.748818380 -0.947785534 -1.029896237 
##           46           47           48           49           50           51           52 
## -1.029896237  0.437591665  0.657197067  0.346760719 -0.537952493 -1.346670138 -1.475696780 
##           53           54           55           56           57           58           59 
## -0.926559528 -1.000954184 -0.208687774 -0.215338261 -0.569194133 -0.161219149 -1.008571679 
##           60           61           62           63           64           65           66 
## -0.045177661 -0.025696780  1.321310726  0.042186486  0.473484062 -1.326886349 -1.490871158 
##           67           68           71           72           73           75           76 
## -1.061740844 -0.190614403  0.601603538  0.601603538  0.123664926 -0.827314950  0.042186486 
##           77           80           81           82           83           84           85 
## -0.085346631  0.686260919  0.783853453 -0.524000751  0.294857738 -0.876395664 -0.103454529 
##           86           87           88           89           90           91           92 
##  0.209523999  0.363153029 -0.345200838 -0.161513266 -1.112001592 -0.526559528 -1.375696780 
##           93           95           96           97           98           99          100 
## -0.564744622 -0.268576560 -0.388902997 -3.654417428 -0.015865797  0.124231849 -0.586811947 
##          101          102          103          104          105          106          107 
## -0.900489472 -0.004810001 -0.966648580 -0.834753447 -0.085302418 -0.876395664 -0.270208384 
##          108          109          110          111          112          113          114 
## -1.432741784  0.187067060 -0.051295973 -1.032329458 -0.314435838  0.001603538 -1.100252837 
##          115          116          117          118          119          120          121 
##  0.193972909 -0.520180285 -0.520180285 -0.825635965 -0.670296189  0.266053156  0.266053156 
##          122          123          124          126          127          128          129 
##  0.761411155  0.541296398  0.505108621 -0.794141178  0.149090903 -0.510000230 -0.510000230 
##          130          131          132          133          134          135          136 
##  0.467258216  0.379748343 -0.078292625  0.209523999 -0.564516779 -0.576395664  0.394686911 
##          137          138          139          140          141          142          143 
## -0.894185774 -0.654158261 -0.473761028 -0.325928250 -0.864744622 -0.712537038 -0.624941357 
##          144          145          146          147          148          149          150 
## -0.338688538 -0.748619803 -0.868704857  0.861450825 -0.839761086 -2.099970182 -0.534753447 
##          151          152          153          154          155          156          157 
##  0.442941909  0.178438216 -0.649276673 -0.454086889  0.059523999 -0.349276673 -0.084753447 
##          158          159          160          161          162          163          164 
## -0.345200838 -0.345200838 -0.744185774 -0.144891379 -1.030071158 -1.506181919  0.003680136 
##          165          166          167          168          169          170          171 
## -0.233946844 -0.798912075 -0.520180285 -0.151532409  0.492186486  0.467367331 -0.678789407 
##          172          173          174          175          176          177          178 
##  1.193166536  1.304799162 -0.812749379 -0.812749379  0.243603047  0.543603047  0.149090903 
##          179          181          182          184          185          186          187 
## -0.204895175 -0.510000230 -0.435313059  0.274303220  0.274303220 -1.008571679 -1.008571679 
##          188          189          190          191          192          193          194 
## -0.382935491 -0.924941357 -0.613101789 -0.627972263 -1.507121366 -1.323357011  0.321788034 
##          195          196          197          198          199          200          201 
## -0.839761086 -0.985143074 -0.254086889 -0.285943619 -1.293156391 -0.767878064 -0.347107895 
##          202          203          204          205          206          207          208 
## -0.243558629 -1.077248148  2.729828987 -0.643066906 -0.864516779 -0.493156391 -1.780210059 
##          209          210          211          212          213          214          215 
## -1.966960594 -1.022973292 -0.900776987 -1.039606845 -0.921474751  0.365310224  0.064776426 
##          216          217          218          219          220          221          222 
## -0.400719089 -0.938117775 -0.294709358 -0.766063578 -0.766063578 -0.067525209  0.121707375 
##          223          224          225          226          227          228          229 
##  0.353549078 -0.716304599  0.371164171  0.371164171 -1.216052415 -1.216052415 -1.435432352 
##          230          231          232          233          234          235          236 
##  2.103033331  0.063917855  2.214567648 -1.034311442  1.287183336 -0.544295396  1.706843609 
##          237          238          239          240          243          244          245 
## -1.306278836 -0.608461268 -1.038259493 -1.757483324 -0.804895175 -0.204895175 -0.439597650 
##          246          247          248          249          250          251          252 
##  2.039533868  1.156441371  0.405222279  1.053388762 -0.681796255  1.741296398  0.988358436 
##          253          254          255          256          257          258          260 
##  1.018132374  0.359337989  0.477365012  1.218568342  1.212211425  2.118577615 -0.207756923 
##          261          262          263          264          265          266          267 
##  1.280528381  0.338358436  1.833043299  1.333932118 -0.406181919 -0.890410917  0.783216844 
##          268          269          270          271          272          273          274 
##  0.365498451  0.042139019  2.141203046  2.141203046  1.788831269  2.063471869  0.309492757 
##          275          276          277          278          279          280          281 
##  1.476563862  2.307293123  0.182075181  2.288780851 -0.218704857 -0.376559528  0.967833475 
##          282          283          285          286          287          288          289 
##  0.535381916 -0.144891379  1.342047667  1.781295143  1.388240162  2.105222279  1.263454697 
##          290          291          292          293          294          295          296 
##  2.038778446  0.694686911  1.367833475  0.860465290  1.825084333  0.211011850  0.281510958 
##          297          298          299          301          302          303          304 
##  0.971800799  0.777771217 -0.602984367 -0.027780567  0.075973102 -0.605871313 -0.039878167 
##          305          306          307          308          309          310          311 
##  5.161599809 -0.265666469 -0.134062644  1.147488745  0.731773072 -0.989184431  0.406441371 
##          312          313          314          315          316          317          318 
## -1.041638160  0.150744312 -1.343558629  0.016616391  0.737663046 -0.603758650  0.902892105 
##          319          320          321          322          323          324          325 
##  1.466551647  1.303494449  2.387965732  0.472359250  0.826741743 -0.690507243  1.836995567 
##          326          327          328          329          330          331          332 
##  2.611552572  1.460478300  0.124506777  0.203437858  0.515998304  1.267211323 -0.756181919 
##          333          334          335          336          337          338          339 
##  0.967630356 -0.317617316  0.687250621  0.080831068 -0.386113904  1.168803883 -0.487374310 
##          340          341          342          343          344          345          346 
##  0.374805207  2.681690537  1.330528381  0.230491435 -0.152622800 -0.470251657 -0.770171013 
##          347          348          349          350          351          352          353 
##  2.610238914  3.900211716  1.210238914  0.474635722 -0.068704857  1.387610255  2.190762151 
##          354          355          356          357          358          359          360 
##  0.038240162 -0.819168932  1.613886096 -0.285815595 -1.378789407 -1.450522784 -0.269711120 
##          361          362          363          364          365          366          367 
## -0.029664882  0.579819715  1.362339297  0.660698810  0.953390147  0.241428321 -1.359243651 
##          368          369          371          372          373          374          375 
## -0.263685553 -0.006027091 -0.910635593 -1.012072963 -0.804475268 -1.278625077 -0.154589660 
##          376          377          378          379          380          381          382 
##  0.224635722 -0.750909097 -0.131867626 -0.935346631 -0.600143494 -1.144773556 -0.895582092 
##          383          384          385          386          387          388          389 
##  0.676818407 -1.314774322  1.991778257 -0.557483324 -1.445582092 -1.485418766 -1.261441895 
##          390          391          392          393          394          395          396 
## -1.715974501 -0.610737331  0.562339297 -1.437660703 -1.556602076 -0.998758744 -0.287744098 
##          397          398          399          400          401          402          403 
## -0.304453974 -1.317878064 -1.140938448 -1.167525209 -0.816063578 -1.497365031 -1.716560321 
##          404          405          406          407          408          409          410 
## -1.816194685  0.355285819 -0.316114423  0.589825761  1.133489934  2.246944464 -0.066742706 
##          411          412          413          414          415          416          417 
##  1.403549078  1.119044931 -0.227365795  0.377063507  0.043310033 -0.429713016  0.768203745 
##          418          420          421          422          423          424          425 
## -0.390987762  1.868970789 -0.222917563  3.121788034  0.969131577  0.350492976 -0.900108305 
##          426          427          428 
##  1.221310726  1.440142944  2.343325609

Model rank

m1[["rank"]]               ## r
## [1] 2

Residual defrees of freedom

m1[["df.residual"]]        ## n - r
## [1] 407




Object obtained by applying the summary method

Many other useful quantities can be extracted from the object returned by the summary method.

sm1 <- summary(m1)
print(sm1)
## 
## Call:
## lm(formula = consumption ~ lweight, data = CarsNow)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.6544 -0.7442 -0.1526  0.5160  5.1616 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -58.2480     1.8941  -30.75   <2e-16 ***
## lweight       9.3606     0.2569   36.44   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.035 on 407 degrees of freedom
## Multiple R-squared:  0.7654, Adjusted R-squared:  0.7648 
## F-statistic:  1328 on 1 and 407 DF,  p-value: < 2.2e-16
names(sm1)
##  [1] "call"          "terms"         "residuals"     "coefficients"  "aliased"       "sigma"        
##  [7] "df"            "r.squared"     "adj.r.squared" "fstatistic"    "cov.unscaled"

Estimated residual standard deviation

sm1[["sigma"]]             ## sqrt(MS_e)
## [1] 1.034628

Coefficient of determination

sm1[["r.squared"]]         ## R^2
## [1] 0.7654182

Adjusted coefficient of determination

sm1[["adj.r.squared"]]     ## R^2_{adj}
## [1] 0.7648418

Overall F-statistic

sm1[["fstatistic"]]        ## Overall F-statistic
##    value    numdf    dendf 
## 1328.002    1.000  407.000




Additional methods for objects of class lm

Residual sum of squares

deviance(m1)        ## SS_e
## [1] 435.6751

Total sum of squares (\(=\) residual sum of squares in intercept only model)

deviance(m0)        ## SS_T
## [1] 1857.242

Overall F-test (once more)

anova(m0, m1)
## Analysis of Variance Table
## 
## Model 1: consumption ~ 1
## Model 2: consumption ~ lweight
##   Res.Df     RSS Df Sum of Sq    F    Pr(>F)    
## 1    408 1857.24                                
## 2    407  435.68  1    1421.6 1328 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Estimated covariance matrix of LSE of the regression coefficients

Matrix \[ \widehat{\mbox{var}}\bigl(\widehat{\beta}\bigr) = \mbox{MS}_e\,\bigl(\mathbf{X}^\top\mathbf{X}\bigr)^{-1}: \]

vcov(m1)
##             (Intercept)     lweight
## (Intercept)   3.5876356 -0.48634907
## lweight      -0.4863491  0.06597886

Correlation matrix derived from vcov(m1)

cov2cor(vcov(m1))
##             (Intercept)    lweight
## (Intercept)   1.0000000 -0.9996352
## lweight      -0.9996352  1.0000000

Confidence intervals for regression coefficients

confint(m1, level = 0.95)
##                  2.5 %     97.5 %
## (Intercept) -61.971477 -54.524575
## lweight       8.855615   9.865505

Estimated model based response means

The code below calculates a series of confidence intervals for \(\mathbf{l}^\top\beta\), where \(\mathbf{l} = (1,\,x)^\top\), \(x = 6.8, 6.9, \ldots, 9\). That is, it calculates a series of confidence intervals for \(\mbox{E}(Y\,|\,X=x)\).

print(lweight.grid <- seq(6.8, 9, by = 0.1))
##  [1] 6.8 6.9 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.0 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9.0
pdata <- data.frame(lweight = lweight.grid)

Only point estimates

predict(m1, newdata = pdata)
##         1         2         3         4         5         6         7         8         9        10 
##  5.403783  6.339839  7.275895  8.211951  9.148007 10.084063 11.020119 11.956175 12.892231 13.828287 
##        11        12        13        14        15        16        17        18        19        20 
## 14.764343 15.700399 16.636455 17.572511 18.508567 19.444623 20.380679 21.316735 22.252791 23.188847 
##        21        22        23 
## 24.124903 25.060959 25.997015

Confidence intervals including related standard errors

predict(m1, newdata = pdata, se.fit = TRUE, interval = "confidence", level = 0.95)
## $fit
##          fit       lwr       upr
## 1   5.403783  5.098287  5.709280
## 2   6.339839  6.081488  6.598191
## 3   7.275895  7.063145  7.488645
## 4   8.211951  8.042013  8.381889
## 5   9.148007  9.015362  9.280652
## 6  10.084063  9.977247 10.190880
## 7  11.020119 10.918511 11.121728
## 8  11.956175 11.836433 12.075918
## 9  12.892231 12.739092 13.045370
## 10 13.828287 13.634215 14.022360
## 11 14.764343 14.525646 15.003040
## 12 15.700399 15.415114 15.985685
## 13 16.636455 16.303441 16.969470
## 14 17.572511 17.191055 17.953967
## 15 18.508567 18.078197 18.938937
## 16 19.444623 18.965012 19.924235
## 17 20.380679 19.851590 20.909769
## 18 21.316735 20.737992 21.895478
## 19 22.252791 21.624261 22.881322
## 20 23.188847 22.510425 23.867270
## 21 24.124903 23.396506 24.853301
## 22 25.060959 24.282520 25.839399
## 23 25.997015 25.168479 26.825552
## 
## $se.fit
##          1          2          3          4          5          6          7          8          9 
## 0.15540481 0.13142251 0.10822512 0.08644682 0.06747597 0.05433726 0.05168797 0.06091249 0.07790118 
##         10         11         12         13         14         15         16         17         18 
## 0.09872403 0.12142426 0.14512355 0.16940312 0.19404527 0.21892760 0.24397663 0.26914583 0.29440437 
##         19         20         21         22         23 
## 0.31973109 0.34511097 0.37053309 0.39598931 0.42147346 
## 
## $df
## [1] 407
## 
## $residual.scale
## [1] 1.034628

Only confidence intervals

predict(m1, newdata = pdata, interval = "confidence", level = 0.95)
##          fit       lwr       upr
## 1   5.403783  5.098287  5.709280
## 2   6.339839  6.081488  6.598191
## 3   7.275895  7.063145  7.488645
## 4   8.211951  8.042013  8.381889
## 5   9.148007  9.015362  9.280652
## 6  10.084063  9.977247 10.190880
## 7  11.020119 10.918511 11.121728
## 8  11.956175 11.836433 12.075918
## 9  12.892231 12.739092 13.045370
## 10 13.828287 13.634215 14.022360
## 11 14.764343 14.525646 15.003040
## 12 15.700399 15.415114 15.985685
## 13 16.636455 16.303441 16.969470
## 14 17.572511 17.191055 17.953967
## 15 18.508567 18.078197 18.938937
## 16 19.444623 18.965012 19.924235
## 17 20.380679 19.851590 20.909769
## 18 21.316735 20.737992 21.895478
## 19 22.252791 21.624261 22.881322
## 20 23.188847 22.510425 23.867270
## 21 24.124903 23.396506 24.853301
## 22 25.060959 24.282520 25.839399
## 23 25.997015 25.168479 26.825552

Keep results in an object p1

p1 <- predict(m1, newdata = pdata, interval = "confidence", level = 0.95)

Visualization of calculated confidence intervals for model based response means

Logarithmic weight on the horizontal axis

par(mfrow = c(1, 1), bty = BTY, mar = c(4, 4, 1, 1) + 0.1)
plot(consumption ~ lweight, data = CarsNow, pch = PCH, col = COL2, bg = BGC2, xlab = "Log(weight) [log(kg)]", ylab = "Consumption [l/100 km]")
abline(m1, col = "red2", lwd = 2)
lines(pdata[, "lweight"], p1[, "lwr"], col = "blue", lwd = 2, lty = 5)
lines(pdata[, "lweight"], p1[, "upr"], col = "blue", lwd = 2, lty = 5)

plot of chunk fig-NormalLM-01-03

Original weight on the horizontal axis

par(mfrow = c(1, 1), bty = BTY, mar = c(4, 4, 1, 1) + 0.1)
plot(consumption ~ weight, data = CarsNow, pch = PCH, col = COL2, bg = BGC2, xlab = "Weight [kg]", ylab = "Consumption [l/100 km]")
lines(exp(pdata[, "lweight"]), p1[, "fit"], col = "red2", lwd = 2)
lines(exp(pdata[, "lweight"]), p1[, "lwr"], col = "blue", lwd = 2, lty = 5)
lines(exp(pdata[, "lweight"]), p1[, "upr"], col = "blue", lwd = 2, lty = 5)

plot of chunk fig-NormalLM-01-04