Chapter 3 Lab: Linear Regression

library(MASS)
library(ISLR)

Simple Linear Regression

data(Boston)
lm.fit <- lm(medv ~ lstat, data = Boston)
summary(lm.fit)
## 
## Call:
## lm(formula = medv ~ lstat, data = Boston)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -15.168  -3.990  -1.318   2.034  24.500 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 34.55384    0.56263   61.41   <2e-16 ***
## lstat       -0.95005    0.03873  -24.53   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.216 on 504 degrees of freedom
## Multiple R-squared:  0.5441, Adjusted R-squared:  0.5432 
## F-statistic: 601.6 on 1 and 504 DF,  p-value: < 2.2e-16
plot(Boston$lstat, Boston$medv)
abline(lm.fit, col = "red", lwd = 2)

Multiple Linear Regression

lm.fit2 <- lm(medv ~ ., data = Boston)
summary(lm.fit2)
## 
## Call:
## lm(formula = medv ~ ., data = Boston)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -15.595  -2.730  -0.518   1.777  26.199 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.646e+01  5.103e+00   7.144 3.28e-12 ***
## crim        -1.080e-01  3.286e-02  -3.287 0.001087 ** 
## zn           4.642e-02  1.373e-02   3.382 0.000778 ***
## indus        2.056e-02  6.150e-02   0.334 0.738288    
## chas         2.687e+00  8.616e-01   3.118 0.001925 ** 
## nox         -1.777e+01  3.820e+00  -4.651 4.25e-06 ***
## rm           3.810e+00  4.179e-01   9.116  < 2e-16 ***
## age          6.922e-04  1.321e-02   0.052 0.958229    
## dis         -1.476e+00  1.995e-01  -7.398 6.01e-13 ***
## rad          3.060e-01  6.635e-02   4.613 5.07e-06 ***
## tax         -1.233e-02  3.760e-03  -3.280 0.001112 ** 
## ptratio     -9.527e-01  1.308e-01  -7.283 1.31e-12 ***
## black        9.312e-03  2.686e-03   3.467 0.000573 ***
## lstat       -5.248e-01  5.072e-02 -10.347  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.745 on 492 degrees of freedom
## Multiple R-squared:  0.7406, Adjusted R-squared:  0.7338 
## F-statistic: 108.1 on 13 and 492 DF,  p-value: < 2.2e-16

Interaction Terms

lm.fit3 <- lm(medv ~ lstat * age, data = Boston)
summary(lm.fit3)
## 
## Call:
## lm(formula = medv ~ lstat * age, data = Boston)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -15.806  -4.045  -1.333   2.085  27.552 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 36.0885359  1.4698355  24.553  < 2e-16 ***
## lstat       -1.3921168  0.1674555  -8.313 8.78e-16 ***
## age         -0.0007209  0.0198792  -0.036   0.9711    
## lstat:age    0.0041560  0.0018518   2.244   0.0252 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.149 on 502 degrees of freedom
## Multiple R-squared:  0.5557, Adjusted R-squared:  0.5531 
## F-statistic: 209.3 on 3 and 502 DF,  p-value: < 2.2e-16

Non-linear Transformations of Predictors

lm.fit4 <- lm(medv ~ lstat + I(lstat^2), data = Boston)
summary(lm.fit4)
## 
## Call:
## lm(formula = medv ~ lstat + I(lstat^2), data = Boston)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -15.2834  -3.8313  -0.5295   2.3095  25.4148 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 42.862007   0.872084   49.15   <2e-16 ***
## lstat       -2.332821   0.123803  -18.84   <2e-16 ***
## I(lstat^2)   0.043547   0.003745   11.63   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.524 on 503 degrees of freedom
## Multiple R-squared:  0.6407, Adjusted R-squared:  0.6393 
## F-statistic: 448.5 on 2 and 503 DF,  p-value: < 2.2e-16

Qualitative Predictors

data(Carseats)
lm.fit5 <- lm(Sales ~ . + Income:Advertising + Price:Age, data = Carseats)
summary(lm.fit5)
## 
## Call:
## lm(formula = Sales ~ . + Income:Advertising + Price:Age, data = Carseats)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.9208 -0.7503  0.0177  0.6754  3.3413 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         6.5755654  1.0087470   6.519 2.22e-10 ***
## CompPrice           0.0929371  0.0041183  22.567  < 2e-16 ***
## Income              0.0108940  0.0026044   4.183 3.57e-05 ***
## Advertising         0.0702462  0.0226091   3.107 0.002030 ** 
## Population          0.0001592  0.0003679   0.433 0.665330    
## Price              -0.1008064  0.0074399 -13.549  < 2e-16 ***
## ShelveLocGood       4.8486762  0.1528378  31.724  < 2e-16 ***
## ShelveLocMedium     1.9532620  0.1257682  15.531  < 2e-16 ***
## Age                -0.0579466  0.0159506  -3.633 0.000318 ***
## Education          -0.0208525  0.0196131  -1.063 0.288361    
## UrbanYes            0.1401597  0.1124019   1.247 0.213171    
## USYes              -0.1575571  0.1489234  -1.058 0.290729    
## Income:Advertising  0.0007510  0.0002784   2.698 0.007290 ** 
## Price:Age           0.0001068  0.0001333   0.801 0.423812    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.011 on 386 degrees of freedom
## Multiple R-squared:  0.8761, Adjusted R-squared:  0.8719 
## F-statistic:   210 on 13 and 386 DF,  p-value: < 2.2e-16

Writing Functions

LoadLibraries <- function(){
  library(ISLR)
  library(MASS)
  print("The libraries have been loaded.")
}
LoadLibraries()
## [1] "The libraries have been loaded."

Applied Exercises (8, 10, 14)

Exercise 8

data(Auto)
lm.fit8 <- lm(mpg ~ horsepower, data = Auto)
summary(lm.fit8)
## 
## Call:
## lm(formula = mpg ~ horsepower, data = Auto)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -13.5710  -3.2592  -0.3435   2.7630  16.9240 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 39.935861   0.717499   55.66   <2e-16 ***
## horsepower  -0.157845   0.006446  -24.49   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.906 on 390 degrees of freedom
## Multiple R-squared:  0.6059, Adjusted R-squared:  0.6049 
## F-statistic: 599.7 on 1 and 390 DF,  p-value: < 2.2e-16
plot(Auto$horsepower, Auto$mpg)
abline(lm.fit8, col = "blue", lwd = 2)

Exercise 10

pairs(Auto)

cor(Auto[, -9])
##                     mpg  cylinders displacement horsepower     weight
## mpg           1.0000000 -0.7776175   -0.8051269 -0.7784268 -0.8322442
## cylinders    -0.7776175  1.0000000    0.9508233  0.8429834  0.8975273
## displacement -0.8051269  0.9508233    1.0000000  0.8972570  0.9329944
## horsepower   -0.7784268  0.8429834    0.8972570  1.0000000  0.8645377
## weight       -0.8322442  0.8975273    0.9329944  0.8645377  1.0000000
## acceleration  0.4233285 -0.5046834   -0.5438005 -0.6891955 -0.4168392
## year          0.5805410 -0.3456474   -0.3698552 -0.4163615 -0.3091199
## origin        0.5652088 -0.5689316   -0.6145351 -0.4551715 -0.5850054
##              acceleration       year     origin
## mpg             0.4233285  0.5805410  0.5652088
## cylinders      -0.5046834 -0.3456474 -0.5689316
## displacement   -0.5438005 -0.3698552 -0.6145351
## horsepower     -0.6891955 -0.4163615 -0.4551715
## weight         -0.4168392 -0.3091199 -0.5850054
## acceleration    1.0000000  0.2903161  0.2127458
## year            0.2903161  1.0000000  0.1815277
## origin          0.2127458  0.1815277  1.0000000
lm.fit10 <- lm(mpg ~ ., data = Auto)
summary(lm.fit10)
## 
## Call:
## lm(formula = mpg ~ ., data = Auto)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -5.646  0.000  0.000  0.000  5.646 
## 
## Coefficients: (1 not defined because of singularities)
##                                            Estimate Std. Error t value Pr(>|t|)
## (Intercept)                                0.187305  12.773943   0.015 0.988335
## cylinders                                 -0.918096   0.616653  -1.489 0.140231
## displacement                               0.003041   0.015621   0.195 0.846109
## horsepower                                -0.042342   0.029070  -1.457 0.148919
## weight                                    -0.004193   0.001209  -3.467 0.000829
## acceleration                              -0.481449   0.171537  -2.807 0.006206
## year                                       0.636498   0.112195   5.673 1.89e-07
## origin                                     1.324264   4.243221   0.312 0.755737
## nameamc ambassador dpl                     3.371358   3.245610   1.039 0.301870
## nameamc ambassador sst                     3.364264   3.270924   1.029 0.306616
## nameamc concord                           -0.122500   3.275058  -0.037 0.970251
## nameamc concord d/l                       -1.686548   3.422955  -0.493 0.623483
## nameamc concord dl 6                      -0.529687   3.514452  -0.151 0.880556
## nameamc gremlin                           -0.426606   2.958293  -0.144 0.885679
## nameamc hornet                             0.269026   2.902972   0.093 0.926382
## nameamc hornet sportabout (sw)            -0.403111   3.527112  -0.114 0.909278
## nameamc matador                            0.462233   2.667735   0.173 0.862853
## nameamc matador (sw)                       3.118929   2.894160   1.078 0.284233
## nameamc pacer                              0.012163   3.519402   0.003 0.997251
## nameamc pacer d/l                         -1.682001   3.517154  -0.478 0.633716
## nameamc rebel sst                          2.875941   3.284138   0.876 0.383658
## nameamc spirit dl                          0.713000   4.063462   0.175 0.861131
## nameaudi 100 ls                            0.936254   3.449074   0.271 0.786702
## nameaudi 100ls                            -3.133448   2.984726  -1.050 0.296773
## nameaudi 4000                              4.004843   3.244193   1.234 0.220431
## nameaudi 5000                             -4.109082   3.450859  -1.191 0.237070
## nameaudi 5000s (diesel)                   11.652927   3.494256   3.335 0.001266
## nameaudi fox                               3.199486   3.328970   0.961 0.339226
## namebmw 2002                               2.082893   3.429803   0.607 0.545276
## namebmw 320i                              -5.320674   3.378167  -1.575 0.118967
## namebuick century                          2.213275   3.073805   0.720 0.473473
## namebuick century 350                      2.163058   3.250910   0.665 0.507615
## namebuick century limited                  1.154506   3.653147   0.316 0.752754
## namebuick century luxus (sw)               3.701574   3.397404   1.090 0.278999
## namebuick century special                  0.471663   3.468124   0.136 0.892143
## namebuick electra 225 custom               5.565899   3.481596   1.599 0.113606
## namebuick estate wagon (sw)                2.288361   2.883255   0.794 0.429596
## namebuick lesabre custom                   3.878880   3.350669   1.158 0.250252
## namebuick opel isuzu deluxe                2.360903   4.090583   0.577 0.565360
## namebuick regal sport coupe (turbo)       -0.770758   3.673221  -0.210 0.834301
## namebuick skyhawk                          1.178015   3.453774   0.341 0.733885
## namebuick skylark                          0.485925   3.292621   0.148 0.883024
## namebuick skylark 320                      3.220548   3.239745   0.994 0.323008
## namebuick skylark limited                  2.526638   3.945353   0.640 0.523630
## namecadillac eldorado                      7.506805   3.434596   2.186 0.031592
## namecadillac seville                       4.705913   3.307504   1.423 0.158454
## namecapri ii                               0.213795   3.974233   0.054 0.957225
## namechevroelt chevelle malibu              1.190886   3.501889   0.340 0.734643
## namechevrolet bel air                      3.526745   3.332996   1.058 0.292994
## namechevrolet camaro                       1.016970   3.965053   0.256 0.798197
## namechevrolet caprice classic              1.486191   2.705678   0.549 0.584249
## namechevrolet cavalier                     1.711757   4.062490   0.421 0.674558
## namechevrolet cavalier 2-door              6.060983   4.085007   1.484 0.141584
## namechevrolet cavalier wagon               0.377051   4.065117   0.093 0.926318
## namechevrolet chevelle concours (sw)       1.497987   3.363217   0.445 0.657161
## namechevrolet chevelle malibu              2.508523   3.012061   0.833 0.407277
## namechevrolet chevelle malibu classic      2.148872   2.973671   0.723 0.471889
## namechevrolet chevette                     2.796588   3.583400   0.780 0.437307
## namechevrolet citation                     0.835024   3.277093   0.255 0.799488
## namechevrolet concours                    -0.962069   3.436400  -0.280 0.780185
## namechevrolet impala                       3.901975   2.655000   1.470 0.145343
## namechevrolet malibu                       0.338930   2.908792   0.117 0.907516
## namechevrolet malibu classic (sw)          1.566909   3.369001   0.465 0.643052
## namechevrolet monte carlo                  1.755561   3.510683   0.500 0.618323
## namechevrolet monte carlo landau           1.360482   2.830125   0.481 0.631955
## namechevrolet monte carlo s                2.817323   3.294556   0.855 0.394875
## namechevrolet monza 2+2                    1.960822   3.418832   0.574 0.567797
## namechevrolet nova                         0.013367   2.932203   0.005 0.996373
## namechevrolet nova custom                 -0.583803   3.483183  -0.168 0.867291
## namechevrolet vega                        -0.123809   3.451390  -0.036 0.971468
## namechevrolet vega (sw)                    0.835785   3.957409   0.211 0.833241
## namechevrolet vega 2300                    5.309133   3.978383   1.334 0.185606
## namechevrolet woody                        0.242401   4.035002   0.060 0.952237
## namechevy c10                             -1.686820   3.292205  -0.512 0.609722
## namechevy c20                              4.381943   3.755983   1.167 0.246612
## namechevy s-10                             4.822281   4.016031   1.201 0.233181
## namechrysler cordoba                       3.158331   3.288955   0.960 0.339636
## namechrysler lebaron medallion            -2.791925   4.039754  -0.691 0.491378
## namechrysler lebaron salon                -4.524894   3.582800  -1.263 0.210061
## namechrysler lebaron town @ country (sw)   2.084281   3.302393   0.631 0.529641
## namechrysler new yorker brougham           5.282481   3.390324   1.558 0.122924
## namechrysler newport royal                 4.391942   3.291715   1.334 0.185688
## namedatsun 1200                            7.452422   6.449055   1.156 0.251090
## namedatsun 200-sx                         -5.232326   6.424023  -0.814 0.417638
## namedatsun 200sx                           2.817519   6.441163   0.437 0.662913
## namedatsun 210                             5.606103   6.195917   0.905 0.368126
## namedatsun 210 mpg                         5.070308   6.390048   0.793 0.429714
## namedatsun 280-zx                          5.896050   6.830585   0.863 0.390466
## namedatsun 310                             4.643895   6.277737   0.740 0.461496
## namedatsun 310 gx                          4.043465   6.266014   0.645 0.520470
## namedatsun 510                            -2.468844   6.400190  -0.386 0.700649
## namedatsun 510 (sw)                        3.062356   6.557580   0.467 0.641698
## namedatsun 510 hatchback                   6.552699   6.395446   1.025 0.308465
## namedatsun 610                            -3.382103   6.545652  -0.517 0.606711
## namedatsun 710                             1.331250   6.306882   0.211 0.833331
## namedatsun 810                            -3.215340   6.816828  -0.472 0.638366
## namedatsun 810 maxima                     -2.442319   6.860787  -0.356 0.722736
## namedatsun b-210                           2.371920   6.357360   0.373 0.710004
## namedatsun b210                            3.331328   6.462449   0.515 0.607549
## namedatsun b210 gx                         9.604655   6.404554   1.500 0.137408
## namedatsun f-10 hatchback                  2.950464   6.329051   0.466 0.642281
## namedatsun pl510                           0.981674   6.254240   0.157 0.875648
## namedodge aries se                         0.403815   4.041675   0.100 0.920649
## namedodge aries wagon (sw)                -2.256829   4.019898  -0.561 0.575992
## namedodge aspen                           -0.206883   3.088343  -0.067 0.946748
## namedodge aspen 6                          0.366429   3.465245   0.106 0.916035
## namedodge aspen se                         3.002155   3.493627   0.859 0.392579
## namedodge challenger se                    2.064684   3.290611   0.627 0.532048
## namedodge charger 2.2                      5.309608   4.166685   1.274 0.206030
## namedodge colt                             1.482396   3.584355   0.414 0.680228
## namedodge colt (sw)                        3.716950   4.113570   0.904 0.368771
## namedodge colt hardtop                     1.522048   4.067987   0.374 0.709223
## namedodge colt hatchback custom            5.628627   4.197231   1.341 0.183482
## namedodge colt m/m                         6.221646   4.105872   1.515 0.133405
## namedodge coronet brougham                 2.669663   3.290052   0.811 0.419382
## namedodge coronet custom                   1.606868   3.247032   0.495 0.621967
## namedodge coronet custom (sw)              3.302820   3.364562   0.982 0.329059
## namedodge d100                            -1.672691   3.254823  -0.514 0.608647
## namedodge d200                             5.074896   3.746461   1.355 0.179140
## namedodge dart custom                     -0.700127   3.265096  -0.214 0.830727
## namedodge diplomat                         2.561880   3.282848   0.780 0.437333
## namedodge magnum xe                        2.349068   3.293521   0.713 0.477652
## namedodge monaco (sw)                      5.409949   3.475789   1.556 0.123313
## namedodge monaco brougham                  1.448836   3.286662   0.441 0.660460
## namedodge omni                             2.600955   4.128988   0.630 0.530433
## namedodge rampage                          0.321131   4.244538   0.076 0.939869
## namedodge st. regis                        1.874870   3.310142   0.566 0.572613
## namefiat 124 sport coupe                   0.843793   3.356728   0.251 0.802132
## namefiat 124 tc                           -1.284412   3.299112  -0.389 0.698013
## namefiat 124b                              4.234430   3.350724   1.264 0.209780
## namefiat 128                              -0.302820   2.911282  -0.104 0.917402
## namefiat 131                               1.571897   3.313206   0.474 0.636407
## namefiat strada custom                     6.505740   3.221602   2.019 0.046595
## namefiat x1.9                              3.420877   3.286517   1.041 0.300882
## nameford country                           3.810815   3.447980   1.105 0.272178
## nameford country squire (sw)               2.588023   2.891203   0.895 0.373242
## nameford escort 2h                         2.903207   4.052921   0.716 0.475754
## nameford escort 4w                         3.832150   4.172436   0.918 0.360985
## nameford f108                             -1.507274   3.300893  -0.457 0.649105
## nameford f250                              5.376492   3.649336   1.473 0.144369
## nameford fairmont                          1.688480   3.942005   0.428 0.669494
## nameford fairmont (auto)                  -2.420856   3.577091  -0.677 0.500392
## nameford fairmont (man)                   -0.267333   3.979340  -0.067 0.946596
## nameford fairmont 4                       -2.076328   3.945762  -0.526 0.600108
## nameford fairmont futura                  -2.654573   4.013149  -0.661 0.510101
## nameford fiesta                            5.590179   4.227911   1.322 0.189646
## nameford futura                           -1.916806   3.362257  -0.570 0.570117
## nameford galaxie 500                       4.442480   2.696732   1.647 0.103177
## nameford gran torino                       2.395149   2.717190   0.881 0.380544
## nameford gran torino (sw)                  4.305909   3.032697   1.420 0.159315
## nameford granada                           0.802555   3.476415   0.231 0.817981
## nameford granada ghia                      1.260545   3.617154   0.348 0.728333
## nameford granada gl                       -3.179138   3.572976  -0.890 0.376099
## nameford granada l                        -3.195561   3.585987  -0.891 0.375378
## nameford ltd                               2.931338   2.921112   1.004 0.318467
## nameford ltd landau                       -0.237362   3.326697  -0.071 0.943286
## nameford maverick                          0.203527   3.006394   0.068 0.946185
## nameford mustang                          -0.086763   3.673287  -0.024 0.981211
## nameford mustang gl                       -0.608234   4.034452  -0.151 0.880522
## nameford mustang ii                       -5.296511   3.364527  -1.574 0.119151
## nameford mustang ii 2+2                    1.150829   3.960070   0.291 0.772059
## nameford pinto                            -0.775120   3.301082  -0.235 0.814923
## nameford pinto (sw)                       -0.652033   4.001814  -0.163 0.870957
## nameford pinto runabout                   -2.119866   3.996017  -0.530 0.597152
## nameford ranger                            0.908753   4.018900   0.226 0.821651
## nameford thunderbird                       3.220573   3.297455   0.977 0.331497
## nameford torino                            2.803510   3.333644   0.841 0.402722
## nameford torino 500                        2.078087   3.646390   0.570 0.570247
## namehi 1200d                               6.272325   3.984511   1.574 0.119162
## namehonda accord                           1.948521   6.116848   0.319 0.750849
## namehonda accord cvcc                      2.063973   6.429582   0.321 0.748989
## namehonda accord lx                       -1.109939   6.347929  -0.175 0.861613
## namehonda civic                            1.053256   6.055926   0.174 0.862341
## namehonda civic (auto)                    -2.323039   6.246699  -0.372 0.710906
## namehonda civic 1300                       0.480561   6.224808   0.077 0.938645
## namehonda civic 1500 gl                   10.153049   6.196347   1.639 0.105003
## namehonda civic cvcc                       3.182648   6.123490   0.520 0.604593
## namehonda prelude                          0.704854   6.280545   0.112 0.910907
## namemaxda glc deluxe                       1.418177   6.244770   0.227 0.820893
## namemaxda rx3                            -10.867477   6.267079  -1.734 0.086533
## namemazda 626                              1.984386   6.282271   0.316 0.752875
## namemazda glc                             15.147599   6.344260   2.388 0.019175
## namemazda glc 4                            0.684088   6.266174   0.109 0.913324
## namemazda glc custom                      -2.344981   6.320355  -0.371 0.711545
## namemazda glc custom l                     4.174484   6.349602   0.657 0.512674
## namemazda glc deluxe                       2.292567   6.386457   0.359 0.720505
## namemazda rx-4                            -6.598216   6.446482  -1.024 0.308958
## namemazda rx-7 gs                         -8.439964   6.251072  -1.350 0.180548
## namemazda rx2 coupe                       -8.070898   6.367885  -1.267 0.208460
## namemercedes-benz 240d                     6.431350   3.628762   1.772 0.079923
## namemercedes-benz 280s                    -0.574813   3.970931  -0.145 0.885247
## namemercedes benz 300d                     4.052324   3.714017   1.091 0.278317
## namemercury capri 2000                    -0.712146   4.074233  -0.175 0.861658
## namemercury capri v6                      -0.303572   3.691241  -0.082 0.934648
## namemercury cougar brougham                1.589959   3.361893   0.473 0.637471
## namemercury grand marquis                 -0.237277   3.337834  -0.071 0.943496
## namemercury lynx l                         5.872369   4.147537   1.416 0.160468
## namemercury marquis                        3.228385   3.350958   0.963 0.338069
## namemercury marquis brougham               4.746644   3.423970   1.386 0.169282
## namemercury monarch                       -1.952366   3.676929  -0.531 0.596819
## namemercury monarch ghia                   2.483829   3.298276   0.753 0.453489
## namemercury zephyr                        -0.947324   3.554276  -0.267 0.790476
## namemercury zephyr 6                      -2.197060   3.553076  -0.618 0.537995
## namenissan stanza xe                       2.717783   6.321214   0.430 0.668323
## nameoldsmobile cutlass ciera (diesel)     13.431975   3.705632   3.625 0.000492
## nameoldsmobile cutlass ls                  9.023570   3.661744   2.464 0.015743
## nameoldsmobile cutlass salon brougham      4.510530   3.094646   1.458 0.148656
## nameoldsmobile cutlass supreme             3.859509   3.468811   1.113 0.269004
## nameoldsmobile delta 88 royale             2.897729   3.327483   0.871 0.386290
## nameoldsmobile omega                      -2.416124   3.226729  -0.749 0.456054
## nameoldsmobile omega brougham              2.387769   3.687973   0.647 0.519087
## nameoldsmobile starfire sx                -0.102617   3.905251  -0.026 0.979098
## nameoldsmobile vista cruiser               2.806911   3.342458   0.840 0.403391
## nameopel 1900                              0.364179   2.888955   0.126 0.899983
## nameopel manta                            -1.463586   2.886584  -0.507 0.613446
## namepeugeot 304                            6.452724   3.479899   1.854 0.067166
## namepeugeot 504                            2.256143   3.020582   0.747 0.457171
## namepeugeot 504 (sw)                       1.205706   3.655813   0.330 0.742359
## namepeugeot 505s turbo diesel              3.702626   3.568596   1.038 0.302418
## namepeugeot 604sl                         -3.734443   3.870878  -0.965 0.337405
## nameplymouth 'cuda 340                     0.001994   3.312175   0.001 0.999521
## nameplymouth arrow gs                     -0.550125   4.028316  -0.137 0.891698
## nameplymouth champ                         7.809841   4.225532   1.848 0.068044
## nameplymouth cricket                       3.723019   4.089471   0.910 0.365191
## nameplymouth custom suburb                 4.243663   3.356462   1.264 0.209569
## nameplymouth duster                        3.100163   3.013252   1.029 0.306472
## nameplymouth fury                          2.614640   3.537123   0.739 0.461822
## nameplymouth fury gran sedan               3.498384   3.316570   1.055 0.294497
## nameplymouth fury iii                      4.299562   2.679985   1.604 0.112352
## nameplymouth grand fury                    5.319669   3.369828   1.579 0.118138
## nameplymouth horizon                       4.301083   4.214230   1.021 0.310336
## nameplymouth horizon 4                     4.113045   4.189513   0.982 0.329010
## nameplymouth horizon miser                 6.302919   4.228303   1.491 0.139755
## nameplymouth horizon tc3                   5.209914   4.143729   1.257 0.212088
## nameplymouth reliant                      -0.545007   3.754920  -0.145 0.884940
## nameplymouth sapporo                      -0.765473   3.911418  -0.196 0.845310
## nameplymouth satellite                     4.364495   3.282401   1.330 0.187186
## nameplymouth satellite custom              0.448326   3.526696   0.127 0.899143
## nameplymouth satellite custom (sw)         3.223333   3.292706   0.979 0.330392
## nameplymouth satellite sebring             1.749802   3.497976   0.500 0.618204
## nameplymouth valiant                       1.232906   3.073459   0.401 0.689319
## nameplymouth valiant custom               -0.014077   3.503255  -0.004 0.996803
## nameplymouth volare                        1.061859   3.474219   0.306 0.760626
## nameplymouth volare custom                 1.277611   3.488038   0.366 0.715064
## nameplymouth volare premier v8            -1.282210   3.255780  -0.394 0.694696
## namepontiac astro                          0.074570   3.909875   0.019 0.984828
## namepontiac catalina                       5.445037   2.738420   1.988 0.049987
## namepontiac catalina brougham              5.087948   3.326698   1.529 0.129871
## namepontiac firebird                       2.261616   3.549778   0.637 0.525762
## namepontiac grand prix                     6.401045   3.428059   1.867 0.065314
## namepontiac grand prix lj                  2.265087   3.274738   0.692 0.491020
## namepontiac j2000 se hatchback             2.822025   4.099435   0.688 0.493079
## namepontiac lemans v6                      0.400000   3.461834   0.116 0.908285
## namepontiac phoenix                        3.126592   3.641875   0.859 0.393024
## namepontiac phoenix lj                     1.358450   3.465997   0.392 0.696086
## namepontiac safari (sw)                    7.162903   3.555491   2.015 0.047108
## namepontiac sunbird coupe                  0.108434   3.930485   0.028 0.978055
## namepontiac ventura sj                     1.102220   3.450690   0.319 0.750192
## namerenault 12 (sw)                        1.482167   3.395489   0.437 0.663573
## namerenault 12tl                          -0.731646   3.266427  -0.224 0.823303
## namerenault 5 gtl                          6.648358   3.298178   2.016 0.046983
## namesaab 99e                               3.370839   3.531246   0.955 0.342500
## namesaab 99gle                            -3.431693   3.516847  -0.976 0.331939
## namesaab 99le                              0.426670   3.063299   0.139 0.889555
## namesubaru                                -0.233280   6.193441  -0.038 0.970043
## namesubaru dl                              0.927894   6.146221   0.151 0.880357
## nametoyota carina                         -4.818346   6.613451  -0.729 0.468269
## nametoyota celica gt                       0.811953   6.436095   0.126 0.899906
## nametoyota celica gt liftback             -7.749581   6.454951  -1.201 0.233256
## nametoyota corolla                         0.502682   6.023789   0.083 0.933690
## nametoyota corolla 1200                    4.597137   6.314043   0.728 0.468565
## nametoyota corolla 1600 (sw)               0.864045   6.491090   0.133 0.894419
## nametoyota corolla liftback               -2.358642   6.455302  -0.365 0.715735
## nametoyota corolla tercel                  6.264712   6.367373   0.984 0.327968
## nametoyota corona                         -1.217090   6.101896  -0.199 0.842379
## nametoyota corona hardtop                 -1.623374   6.517263  -0.249 0.803893
## nametoyota corona liftback                 0.624486   6.480690   0.096 0.923461
## nametoyota corona mark ii                 -0.196994   6.567408  -0.030 0.976141
## nametoyota cressida                       -2.182111   6.794927  -0.321 0.748894
## nametoyota mark ii                        -3.168040   6.744488  -0.470 0.639756
## nametoyota starlet                         4.766154   6.246630   0.763 0.447578
## nametoyota tercel                          4.934523   6.305873   0.783 0.436079
## nametoyouta corona mark ii (sw)           -2.085503   6.540427  -0.319 0.750612
## nametriumph tr7 coupe                      6.023330   3.279325   1.837 0.069741
## namevokswagen rabbit                      -2.827106   3.226492  -0.876 0.383382
## namevolkswagen 1131 deluxe sedan           1.497675   3.578202   0.419 0.676597
## namevolkswagen 411 (sw)                   -0.947433   3.453190  -0.274 0.784470
## namevolkswagen dasher                     -0.858245   2.683260  -0.320 0.749866
## namevolkswagen jetta                       1.112715   3.228297   0.345 0.731190
## namevolkswagen model 111                   1.727602   3.449948   0.501 0.617833
## namevolkswagen rabbit                     -1.074894   2.814449  -0.382 0.703474
## namevolkswagen rabbit custom              -1.051324   3.224945  -0.326 0.745228
## namevolkswagen rabbit custom diesel       14.722011   3.409156   4.318 4.24e-05
## namevolkswagen rabbit l                    3.125355   3.244626   0.963 0.338159
## namevolkswagen scirocco                    0.942324   3.219495   0.293 0.770470
## namevolkswagen super beetle                0.311060   3.498963   0.089 0.929370
## namevolkswagen type 3                      0.764483   3.649570   0.209 0.834581
## namevolvo 144ea                           -2.766457   3.580135  -0.773 0.441829
## namevolvo 145e (sw)                       -3.338886   3.609192  -0.925 0.357530
## namevolvo 244dl                           -1.790859   3.478340  -0.515 0.607987
## namevolvo 245                             -2.848126   3.553844  -0.801 0.425122
## namevolvo 264gl                           -5.464384   3.628736  -1.506 0.135809
## namevolvo diesel                           7.278640   3.656834   1.990 0.049758
## namevw dasher (diesel)                    16.275630   3.503824   4.645 1.23e-05
## namevw pickup                             15.324526   3.586036   4.273 5.00e-05
## namevw rabbit                              4.756880   2.789326   1.705 0.091774
## namevw rabbit c (diesel)                  15.164570   3.399743   4.461 2.49e-05
## namevw rabbit custom                             NA         NA      NA       NA
##                                             
## (Intercept)                                 
## cylinders                                   
## displacement                                
## horsepower                                  
## weight                                   ***
## acceleration                             ** 
## year                                     ***
## origin                                      
## nameamc ambassador dpl                      
## nameamc ambassador sst                      
## nameamc concord                             
## nameamc concord d/l                         
## nameamc concord dl 6                        
## nameamc gremlin                             
## nameamc hornet                              
## nameamc hornet sportabout (sw)              
## nameamc matador                             
## nameamc matador (sw)                        
## nameamc pacer                               
## nameamc pacer d/l                           
## nameamc rebel sst                           
## nameamc spirit dl                           
## nameaudi 100 ls                             
## nameaudi 100ls                              
## nameaudi 4000                               
## nameaudi 5000                               
## nameaudi 5000s (diesel)                  ** 
## nameaudi fox                                
## namebmw 2002                                
## namebmw 320i                                
## namebuick century                           
## namebuick century 350                       
## namebuick century limited                   
## namebuick century luxus (sw)                
## namebuick century special                   
## namebuick electra 225 custom                
## namebuick estate wagon (sw)                 
## namebuick lesabre custom                    
## namebuick opel isuzu deluxe                 
## namebuick regal sport coupe (turbo)         
## namebuick skyhawk                           
## namebuick skylark                           
## namebuick skylark 320                       
## namebuick skylark limited                   
## namecadillac eldorado                    *  
## namecadillac seville                        
## namecapri ii                                
## namechevroelt chevelle malibu               
## namechevrolet bel air                       
## namechevrolet camaro                        
## namechevrolet caprice classic               
## namechevrolet cavalier                      
## namechevrolet cavalier 2-door               
## namechevrolet cavalier wagon                
## namechevrolet chevelle concours (sw)        
## namechevrolet chevelle malibu               
## namechevrolet chevelle malibu classic       
## namechevrolet chevette                      
## namechevrolet citation                      
## namechevrolet concours                      
## namechevrolet impala                        
## namechevrolet malibu                        
## namechevrolet malibu classic (sw)           
## namechevrolet monte carlo                   
## namechevrolet monte carlo landau            
## namechevrolet monte carlo s                 
## namechevrolet monza 2+2                     
## namechevrolet nova                          
## namechevrolet nova custom                   
## namechevrolet vega                          
## namechevrolet vega (sw)                     
## namechevrolet vega 2300                     
## namechevrolet woody                         
## namechevy c10                               
## namechevy c20                               
## namechevy s-10                              
## namechrysler cordoba                        
## namechrysler lebaron medallion              
## namechrysler lebaron salon                  
## namechrysler lebaron town @ country (sw)    
## namechrysler new yorker brougham            
## namechrysler newport royal                  
## namedatsun 1200                             
## namedatsun 200-sx                           
## namedatsun 200sx                            
## namedatsun 210                              
## namedatsun 210 mpg                          
## namedatsun 280-zx                           
## namedatsun 310                              
## namedatsun 310 gx                           
## namedatsun 510                              
## namedatsun 510 (sw)                         
## namedatsun 510 hatchback                    
## namedatsun 610                              
## namedatsun 710                              
## namedatsun 810                              
## namedatsun 810 maxima                       
## namedatsun b-210                            
## namedatsun b210                             
## namedatsun b210 gx                          
## namedatsun f-10 hatchback                   
## namedatsun pl510                            
## namedodge aries se                          
## namedodge aries wagon (sw)                  
## namedodge aspen                             
## namedodge aspen 6                           
## namedodge aspen se                          
## namedodge challenger se                     
## namedodge charger 2.2                       
## namedodge colt                              
## namedodge colt (sw)                         
## namedodge colt hardtop                      
## namedodge colt hatchback custom             
## namedodge colt m/m                          
## namedodge coronet brougham                  
## namedodge coronet custom                    
## namedodge coronet custom (sw)               
## namedodge d100                              
## namedodge d200                              
## namedodge dart custom                       
## namedodge diplomat                          
## namedodge magnum xe                         
## namedodge monaco (sw)                       
## namedodge monaco brougham                   
## namedodge omni                              
## namedodge rampage                           
## namedodge st. regis                         
## namefiat 124 sport coupe                    
## namefiat 124 tc                             
## namefiat 124b                               
## namefiat 128                                
## namefiat 131                                
## namefiat strada custom                   *  
## namefiat x1.9                               
## nameford country                            
## nameford country squire (sw)                
## nameford escort 2h                          
## nameford escort 4w                          
## nameford f108                               
## nameford f250                               
## nameford fairmont                           
## nameford fairmont (auto)                    
## nameford fairmont (man)                     
## nameford fairmont 4                         
## nameford fairmont futura                    
## nameford fiesta                             
## nameford futura                             
## nameford galaxie 500                        
## nameford gran torino                        
## nameford gran torino (sw)                   
## nameford granada                            
## nameford granada ghia                       
## nameford granada gl                         
## nameford granada l                          
## nameford ltd                                
## nameford ltd landau                         
## nameford maverick                           
## nameford mustang                            
## nameford mustang gl                         
## nameford mustang ii                         
## nameford mustang ii 2+2                     
## nameford pinto                              
## nameford pinto (sw)                         
## nameford pinto runabout                     
## nameford ranger                             
## nameford thunderbird                        
## nameford torino                             
## nameford torino 500                         
## namehi 1200d                                
## namehonda accord                            
## namehonda accord cvcc                       
## namehonda accord lx                         
## namehonda civic                             
## namehonda civic (auto)                      
## namehonda civic 1300                        
## namehonda civic 1500 gl                     
## namehonda civic cvcc                        
## namehonda prelude                           
## namemaxda glc deluxe                        
## namemaxda rx3                            .  
## namemazda 626                               
## namemazda glc                            *  
## namemazda glc 4                             
## namemazda glc custom                        
## namemazda glc custom l                      
## namemazda glc deluxe                        
## namemazda rx-4                              
## namemazda rx-7 gs                           
## namemazda rx2 coupe                         
## namemercedes-benz 240d                   .  
## namemercedes-benz 280s                      
## namemercedes benz 300d                      
## namemercury capri 2000                      
## namemercury capri v6                        
## namemercury cougar brougham                 
## namemercury grand marquis                   
## namemercury lynx l                          
## namemercury marquis                         
## namemercury marquis brougham                
## namemercury monarch                         
## namemercury monarch ghia                    
## namemercury zephyr                          
## namemercury zephyr 6                        
## namenissan stanza xe                        
## nameoldsmobile cutlass ciera (diesel)    ***
## nameoldsmobile cutlass ls                *  
## nameoldsmobile cutlass salon brougham       
## nameoldsmobile cutlass supreme              
## nameoldsmobile delta 88 royale              
## nameoldsmobile omega                        
## nameoldsmobile omega brougham               
## nameoldsmobile starfire sx                  
## nameoldsmobile vista cruiser                
## nameopel 1900                               
## nameopel manta                              
## namepeugeot 304                          .  
## namepeugeot 504                             
## namepeugeot 504 (sw)                        
## namepeugeot 505s turbo diesel               
## namepeugeot 604sl                           
## nameplymouth 'cuda 340                      
## nameplymouth arrow gs                       
## nameplymouth champ                       .  
## nameplymouth cricket                        
## nameplymouth custom suburb                  
## nameplymouth duster                         
## nameplymouth fury                           
## nameplymouth fury gran sedan                
## nameplymouth fury iii                       
## nameplymouth grand fury                     
## nameplymouth horizon                        
## nameplymouth horizon 4                      
## nameplymouth horizon miser                  
## nameplymouth horizon tc3                    
## nameplymouth reliant                        
## nameplymouth sapporo                        
## nameplymouth satellite                      
## nameplymouth satellite custom               
## nameplymouth satellite custom (sw)          
## nameplymouth satellite sebring              
## nameplymouth valiant                        
## nameplymouth valiant custom                 
## nameplymouth volare                         
## nameplymouth volare custom                  
## nameplymouth volare premier v8              
## namepontiac astro                           
## namepontiac catalina                     *  
## namepontiac catalina brougham               
## namepontiac firebird                        
## namepontiac grand prix                   .  
## namepontiac grand prix lj                   
## namepontiac j2000 se hatchback              
## namepontiac lemans v6                       
## namepontiac phoenix                         
## namepontiac phoenix lj                      
## namepontiac safari (sw)                  *  
## namepontiac sunbird coupe                   
## namepontiac ventura sj                      
## namerenault 12 (sw)                         
## namerenault 12tl                            
## namerenault 5 gtl                        *  
## namesaab 99e                                
## namesaab 99gle                              
## namesaab 99le                               
## namesubaru                                  
## namesubaru dl                               
## nametoyota carina                           
## nametoyota celica gt                        
## nametoyota celica gt liftback               
## nametoyota corolla                          
## nametoyota corolla 1200                     
## nametoyota corolla 1600 (sw)                
## nametoyota corolla liftback                 
## nametoyota corolla tercel                   
## nametoyota corona                           
## nametoyota corona hardtop                   
## nametoyota corona liftback                  
## nametoyota corona mark ii                   
## nametoyota cressida                         
## nametoyota mark ii                          
## nametoyota starlet                          
## nametoyota tercel                           
## nametoyouta corona mark ii (sw)             
## nametriumph tr7 coupe                    .  
## namevokswagen rabbit                        
## namevolkswagen 1131 deluxe sedan            
## namevolkswagen 411 (sw)                     
## namevolkswagen dasher                       
## namevolkswagen jetta                        
## namevolkswagen model 111                    
## namevolkswagen rabbit                       
## namevolkswagen rabbit custom                
## namevolkswagen rabbit custom diesel      ***
## namevolkswagen rabbit l                     
## namevolkswagen scirocco                     
## namevolkswagen super beetle                 
## namevolkswagen type 3                       
## namevolvo 144ea                             
## namevolvo 145e (sw)                         
## namevolvo 244dl                             
## namevolvo 245                               
## namevolvo 264gl                             
## namevolvo diesel                         *  
## namevw dasher (diesel)                   ***
## namevw pickup                            ***
## namevw rabbit                            .  
## namevw rabbit c (diesel)                 ***
## namevw rabbit custom                        
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.272 on 85 degrees of freedom
## Multiple R-squared:  0.9816, Adjusted R-squared:  0.9153 
## F-statistic:  14.8 on 306 and 85 DF,  p-value: < 2.2e-16

Exercise 14

set.seed(1)
library(boot)
lm.fit14 <- glm(mpg ~ horsepower, data = Auto)
cv.err <- cv.glm(Auto, lm.fit14)
cv.err$delta
## [1] 24.23151 24.23114