# Applied
# 8. This question involves the use of simple linear regression on the Auto
# data set.
# (a) Use the lm() function to perform a simple linear regression with
# mpg as the response and horsepower as the predictor. Use the
# summary() function to print the results. Comment on the output.
# For example:
library(ISLR)
objects(grep("ISLR",search()))
## [1] "Auto" "Caravan" "Carseats" "College" "Default"
## [6] "Hitters" "Khan" "NCI60" "OJ" "Portfolio"
## [11] "Smarket" "Wage" "Weekly"
data("Auto")
names(Auto)
## [1] "mpg" "cylinders" "displacement" "horsepower"
## [5] "weight" "acceleration" "year" "origin"
## [9] "name"
lm.fit_1a <- lm(Auto$mpg~Auto$horsepower,data = Auto)
summary(lm.fit_1a)
##
## Call:
## lm(formula = Auto$mpg ~ Auto$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 ***
## Auto$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(lm.fit_1a)




cor(Auto$mpg,Auto$horsepower)
## [1] -0.7784268
#i. Is there a relationship between the predictor and the response?
# YES
# ii. How strong is the relationship between the predictor and
# the response?
# It is quite strong
# iii. Is the relationship between the predictor and the response
# positive or negative?
# using cor() we get a negative relationship
# iv. What is the predicted mpg associated with a horsepower of
# 98? What are the associated 95% confidence and prediction
# intervals?
predict(lm.fit_1a,data.frame(horsepower=(c(98))),
interval="confidence")
## Warning: 'newdata' had 1 row but variables found have 392 rows
## fit lwr upr
## 1 19.416046 18.831250 20.000841
## 2 13.891480 12.982802 14.800158
## 3 16.259151 15.504025 17.014277
## 4 16.259151 15.504025 17.014277
## 5 17.837598 17.174242 18.500955
## 6 8.682604 7.401151 9.964056
## 7 5.210020 3.667064 6.752976
## 8 5.999243 4.516273 7.482214
## 9 4.420796 2.817595 6.023998
## 10 9.945362 8.757051 11.133672
## 11 13.102256 12.139493 14.065020
## 12 14.680704 13.824838 15.536569
## 13 16.259151 15.504025 17.014277
## 14 4.420796 2.817595 6.023998
## 15 24.940611 24.438901 25.442321
## 16 24.940611 24.438901 25.442321
## 17 24.624922 24.128663 25.121180
## 18 26.519059 25.972996 27.065122
## 19 26.045524 25.515552 26.575497
## 20 32.675003 31.788265 33.561742
## 21 26.203369 25.668280 26.738459
## 22 25.729835 25.209322 26.250348
## 23 24.940611 24.438901 25.442321
## 24 22.099406 21.600408 22.598404
## 25 25.729835 25.209322 26.250348
## 26 5.999243 4.516273 7.482214
## 27 8.366914 7.061985 9.671844
## 28 6.788467 5.365189 8.211745
## 29 9.471827 8.248742 10.694913
## 30 26.045524 25.515552 26.575497
## 31 25.729835 25.209322 26.250348
## 32 24.940611 24.438901 25.442321
## 33 24.151388 23.660958 24.641817
## 34 23.362164 22.874970 23.849358
## 35 24.151388 23.660958 24.641817
## 36 26.045524 25.515552 26.575497
## 37 24.151388 23.660958 24.641817
## 38 13.891480 12.982802 14.800158
## 39 12.313033 11.295112 13.330953
## 40 15.785617 15.001060 16.570174
## 41 16.259151 15.504025 17.014277
## 42 11.523809 10.449826 12.597792
## 43 13.102256 12.139493 14.065020
## 44 12.313033 11.295112 13.330953
## 45 22.572940 22.080777 23.065104
## 46 28.571040 27.933378 29.208703
## 47 24.151388 23.660958 24.641817
## 48 26.045524 25.515552 26.575497
## 49 26.361214 25.820758 26.901669
## 50 25.729835 25.209322 26.250348
## 51 28.886730 28.232427 29.541033
## 52 27.939661 27.333469 28.545854
## 53 29.675953 28.977756 30.374151
## 54 29.044574 28.381744 29.707404
## 55 30.465177 29.720276 31.210078
## 56 28.886730 28.232427 29.541033
## 57 24.940611 24.438901 25.442321
## 58 27.308282 26.730818 27.885746
## 59 31.412245 30.608285 32.216206
## 60 25.729835 25.209322 26.250348
## 61 26.361214 25.820758 26.901669
## 62 13.891480 12.982802 14.800158
## 63 12.313033 11.295112 13.330953
## 64 16.259151 15.504025 17.014277
## 65 15.785617 15.001060 16.570174
## 66 16.259151 15.504025 17.014277
## 67 7.104156 5.704666 8.503647
## 68 15.469927 14.665349 16.274505
## 69 14.680704 13.824838 15.536569
## 70 9.945362 8.757051 11.133672
## 71 24.624922 24.128663 25.121180
## 72 16.259151 15.504025 17.014277
## 73 19.416046 18.831250 20.000841
## 74 17.837598 17.174242 18.500955
## 75 16.259151 15.504025 17.014277
## 76 22.257251 21.760844 22.753658
## 77 27.939661 27.333469 28.545854
## 78 26.203369 25.668280 26.738459
## 79 29.044574 28.381744 29.707404
## 80 26.361214 25.820758 26.901669
## 81 25.414146 24.902011 25.926280
## 82 24.624922 24.128663 25.121180
## 83 27.308282 26.730818 27.885746
## 84 26.045524 25.515552 26.575497
## 85 12.313033 11.295112 13.330953
## 86 16.259151 15.504025 17.014277
## 87 17.048375 16.340481 17.756268
## 88 18.311133 17.672969 18.949296
## 89 16.259151 15.504025 17.014277
## 90 8.682604 7.401151 9.964056
## 91 16.259151 15.504025 17.014277
## 92 14.996393 14.161242 15.831545
## 93 16.259151 15.504025 17.014277
## 94 5.999243 4.516273 7.482214
## 95 4.420796 2.817595 6.023998
## 96 12.313033 11.295112 13.330953
## 97 23.362164 22.874970 23.849358
## 98 24.151388 23.660958 24.641817
## 99 24.151388 23.660958 24.641817
## 100 26.045524 25.515552 26.575497
## 101 24.940611 24.438901 25.442321
## 102 32.675003 31.788265 33.561742
## 103 16.259151 15.504025 17.014277
## 104 13.575791 12.645619 14.505962
## 105 13.102256 12.139493 14.065020
## 106 11.523809 10.449826 12.597792
## 107 24.151388 23.660958 24.641817
## 108 26.045524 25.515552 26.575497
## 109 28.571040 27.933378 29.208703
## 110 25.098456 24.593565 25.603347
## 111 25.729835 25.209322 26.250348
## 112 26.519059 25.972996 27.065122
## 113 23.046475 22.558272 23.534677
## 114 25.729835 25.209322 26.250348
## 115 17.048375 16.340481 17.756268
## 116 3.631572 1.967894 5.295251
## 117 32.201469 31.346241 33.056697
## 118 28.097506 27.483687 28.711325
## 119 25.571990 25.055805 26.088176
## 120 22.257251 21.760844 22.753658
## 121 16.259151 15.504025 17.014277
## 122 22.572940 22.080777 23.065104
## 123 20.678804 20.143392 21.214215
## 124 11.523809 10.449826 12.597792
## 125 24.940611 24.438901 25.442321
## 126 24.151388 23.660958 24.641817
## 127 24.151388 23.660958 24.641817
## 128 29.360264 28.679992 30.040536
## 129 27.308282 26.730818 27.885746
## 130 29.675953 28.977756 30.374151
## 131 28.097506 27.483687 28.711325
## 132 24.151388 23.660958 24.641817
## 133 22.572940 22.080777 23.065104
## 134 23.362164 22.874970 23.849358
## 135 17.837598 17.174242 18.500955
## 136 16.259151 15.504025 17.014277
## 137 16.259151 15.504025 17.014277
## 138 17.837598 17.174242 18.500955
## 139 16.259151 15.504025 17.014277
## 140 26.834748 26.276776 27.392720
## 141 29.360264 28.679992 30.040536
## 142 27.623972 27.032512 28.215432
## 143 31.727935 30.903669 32.552201
## 144 30.307332 29.571971 31.042693
## 145 28.097506 27.483687 28.711325
## 146 28.097506 27.483687 28.711325
## 147 28.097506 27.483687 28.711325
## 148 24.624922 24.128663 25.121180
## 149 25.256301 24.747933 25.764669
## 150 29.360264 28.679992 30.040536
## 151 24.940611 24.438901 25.442321
## 152 23.362164 22.874970 23.849358
## 153 28.571040 27.933378 29.208703
## 154 28.571040 27.933378 29.208703
## 155 13.102256 12.139493 14.065020
## 156 17.048375 16.340481 17.756268
## 157 16.259151 15.504025 17.014277
## 158 16.574840 15.838898 17.310783
## 159 22.572940 22.080777 23.065104
## 160 23.362164 22.874970 23.849358
## 161 22.572940 22.080777 23.065104
## 162 24.940611 24.438901 25.442321
## 163 22.572940 22.080777 23.065104
## 164 22.572940 22.080777 23.065104
## 165 19.573890 18.996010 20.151771
## 166 28.097506 27.483687 28.711325
## 167 26.834748 26.276776 27.392720
## 168 24.151388 23.660958 24.641817
## 169 27.623972 27.032512 28.215432
## 170 24.782767 24.283936 25.281597
## 171 28.728885 28.082973 29.374797
## 172 24.624922 24.128663 25.121180
## 173 24.624922 24.128663 25.121180
## 174 28.886730 28.232427 29.541033
## 175 25.729835 25.209322 26.250348
## 176 24.940611 24.438901 25.442321
## 177 26.045524 25.515552 26.575497
## 178 24.467077 23.973079 24.961075
## 179 21.783717 21.278621 22.288812
## 180 31.570090 30.756012 32.384168
## 181 26.361214 25.820758 26.901669
## 182 27.150438 26.579678 27.721197
## 183 25.414146 24.902011 25.926280
## 184 27.466127 26.881761 28.050493
## 185 26.834748 26.276776 27.392720
## 186 17.837598 17.174242 18.500955
## 187 16.259151 15.504025 17.014277
## 188 20.994493 20.469092 21.519894
## 189 15.943462 15.168798 16.718125
## 190 24.151388 23.660958 24.641817
## 191 23.362164 22.874970 23.849358
## 192 27.150438 26.579678 27.721197
## 193 25.729835 25.209322 26.250348
## 194 31.727935 30.903669 32.552201
## 195 30.465177 29.720276 31.210078
## 196 28.886730 28.232427 29.541033
## 197 31.570090 30.756012 32.384168
## 198 24.151388 23.660958 24.641817
## 199 27.623972 27.032512 28.215432
## 200 22.572940 22.080777 23.065104
## 201 24.940611 24.438901 25.442321
## 202 28.728885 28.082973 29.374797
## 203 28.886730 28.232427 29.541033
## 204 28.097506 27.483687 28.711325
## 205 28.571040 27.933378 29.208703
## 206 23.835698 23.347546 24.323850
## 207 16.259151 15.504025 17.014277
## 208 26.045524 25.515552 26.575497
## 209 22.888630 22.399432 23.377828
## 210 20.994493 20.469092 21.519894
## 211 11.523809 10.449826 12.597792
## 212 17.048375 16.340481 17.756268
## 213 19.416046 18.831250 20.000841
## 214 16.259151 15.504025 17.014277
## 215 29.202419 28.530931 29.873907
## 216 27.308282 26.730818 27.885746
## 217 30.780866 30.016612 31.545121
## 218 24.782767 24.283936 25.281597
## 219 28.886730 28.232427 29.541033
## 220 17.048375 16.340481 17.756268
## 221 22.572940 22.080777 23.065104
## 222 17.048375 16.340481 17.756268
## 223 19.416046 18.831250 20.000841
## 224 22.572940 22.080777 23.065104
## 225 23.362164 22.874970 23.849358
## 226 24.151388 23.660958 24.641817
## 227 24.467077 23.973079 24.961075
## 228 11.523809 10.449826 12.597792
## 229 13.102256 12.139493 14.065020
## 230 9.945362 8.757051 11.133672
## 231 16.416996 15.671508 17.162484
## 232 27.623972 27.032512 28.215432
## 233 26.045524 25.515552 26.575497
## 234 28.097506 27.483687 28.711325
## 235 25.887680 25.362568 26.412791
## 236 29.991643 29.275071 30.708215
## 237 26.834748 26.276776 27.392720
## 238 29.360264 28.679992 30.040536
## 239 27.623972 27.032512 28.215432
## 240 24.624922 24.128663 25.121180
## 241 22.572940 22.080777 23.065104
## 242 22.572940 22.080777 23.065104
## 243 32.359314 31.493641 33.224987
## 244 29.518109 28.828932 30.207285
## 245 31.727935 30.903669 32.552201
## 246 28.886730 28.232427 29.541033
## 247 30.465177 29.720276 31.210078
## 248 22.572940 22.080777 23.065104
## 249 17.837598 17.174242 18.500955
## 250 17.995443 17.340622 18.650264
## 251 23.362164 22.874970 23.849358
## 252 24.940611 24.438901 25.442321
## 253 26.519059 25.972996 27.065122
## 254 26.045524 25.515552 26.575497
## 255 24.151388 23.660958 24.641817
## 256 25.729835 25.209322 26.250348
## 257 23.362164 22.874970 23.849358
## 258 26.519059 25.972996 27.065122
## 259 22.572940 22.080777 23.065104
## 260 20.994493 20.469092 21.519894
## 261 17.048375 16.340481 17.756268
## 262 13.891480 12.982802 14.800158
## 263 17.995443 17.340622 18.650264
## 264 17.837598 17.174242 18.500955
## 265 29.202419 28.530931 29.873907
## 266 24.940611 24.438901 25.442321
## 267 24.624922 24.128663 25.121180
## 268 28.097506 27.483687 28.711325
## 269 24.940611 24.438901 25.442321
## 270 23.362164 22.874970 23.849358
## 271 26.519059 25.972996 27.065122
## 272 24.624922 24.128663 25.121180
## 273 23.677853 23.190350 24.165357
## 274 20.205269 19.653000 20.757539
## 275 21.783717 21.278621 22.288812
## 276 18.942511 18.335857 19.549166
## 277 28.728885 28.082973 29.374797
## 278 29.202419 28.530931 29.873907
## 279 21.783717 21.278621 22.288812
## 280 26.519059 25.972996 27.065122
## 281 26.045524 25.515552 26.575497
## 282 25.729835 25.209322 26.250348
## 283 22.572940 22.080777 23.065104
## 284 19.416046 18.831250 20.000841
## 285 19.573890 18.996010 20.151771
## 286 18.153288 17.506866 18.799709
## 287 18.626822 18.004730 19.248914
## 288 15.469927 14.665349 16.274505
## 289 17.521909 16.841095 18.202722
## 290 20.205269 19.653000 20.757539
## 291 16.259151 15.504025 17.014277
## 292 28.728885 28.082973 29.374797
## 293 29.675953 28.977756 30.374151
## 294 27.308282 26.730818 27.885746
## 295 27.308282 26.730818 27.885746
## 296 27.781817 27.183079 28.380554
## 297 20.205269 19.653000 20.757539
## 298 28.728885 28.082973 29.374797
## 299 25.729835 25.209322 26.250348
## 300 28.886730 28.232427 29.541033
## 301 28.886730 28.232427 29.541033
## 302 29.675953 28.977756 30.374151
## 303 29.044574 28.381744 29.707404
## 304 25.729835 25.209322 26.250348
## 305 21.783717 21.278621 22.288812
## 306 21.783717 21.278621 22.288812
## 307 25.729835 25.209322 26.250348
## 308 27.939661 27.333469 28.545854
## 309 30.465177 29.720276 31.210078
## 310 28.886730 28.232427 29.541033
## 311 29.675953 28.977756 30.374151
## 312 25.729835 25.209322 26.250348
## 313 26.045524 25.515552 26.575497
## 314 25.729835 25.209322 26.250348
## 315 25.729835 25.209322 26.250348
## 316 27.623972 27.032512 28.215432
## 317 25.729835 25.209322 26.250348
## 318 28.097506 27.483687 28.711325
## 319 25.414146 24.902011 25.926280
## 320 28.097506 27.483687 28.711325
## 321 29.675953 28.977756 30.374151
## 322 23.362164 22.874970 23.849358
## 323 29.675953 28.977756 30.374151
## 324 32.359314 31.493641 33.224987
## 325 32.359314 31.493641 33.224987
## 326 29.360264 28.679992 30.040536
## 327 29.360264 28.679992 30.040536
## 328 29.360264 28.679992 30.040536
## 329 29.360264 28.679992 30.040536
## 330 30.149488 29.423571 30.875404
## 331 19.100356 18.501167 19.699545
## 332 24.151388 23.660958 24.641817
## 333 26.045524 25.515552 26.575497
## 334 28.571040 27.933378 29.208703
## 335 26.676903 26.124999 27.228808
## 336 26.676903 26.124999 27.228808
## 337 25.414146 24.902011 25.926280
## 338 22.572940 22.080777 23.065104
## 339 26.676903 26.124999 27.228808
## 340 30.780866 30.016612 31.545121
## 341 29.833798 29.126467 30.541129
## 342 30.465177 29.720276 31.210078
## 343 29.360264 28.679992 30.040536
## 344 29.675953 28.977756 30.374151
## 345 30.149488 29.423571 30.875404
## 346 29.202419 28.530931 29.873907
## 347 29.991643 29.275071 30.708215
## 348 29.675953 28.977756 30.374151
## 349 29.675953 28.977756 30.374151
## 350 28.255351 27.633741 28.876961
## 351 28.097506 27.483687 28.711325
## 352 28.097506 27.483687 28.711325
## 353 24.151388 23.660958 24.641817
## 354 28.255351 27.633741 28.876961
## 355 27.308282 26.730818 27.885746
## 356 27.939661 27.333469 28.545854
## 357 21.625872 21.117282 22.134462
## 358 20.994493 20.469092 21.519894
## 359 22.572940 22.080777 23.065104
## 360 23.362164 22.874970 23.849358
## 361 26.045524 25.515552 26.575497
## 362 26.519059 25.972996 27.065122
## 363 26.045524 25.515552 26.575497
## 364 26.045524 25.515552 26.575497
## 365 26.045524 25.515552 26.575497
## 366 26.519059 25.972996 27.065122
## 367 26.676903 26.124999 27.228808
## 368 25.729835 25.209322 26.250348
## 369 25.414146 24.902011 25.926280
## 370 28.255351 27.633741 28.876961
## 371 29.202419 28.530931 29.873907
## 372 29.202419 28.530931 29.873907
## 373 29.991643 29.275071 30.708215
## 374 28.886730 28.232427 29.541033
## 375 26.045524 25.515552 26.575497
## 376 28.097506 27.483687 28.711325
## 377 28.886730 28.232427 29.541033
## 378 29.360264 28.679992 30.040536
## 379 29.360264 28.679992 30.040536
## 380 29.360264 28.679992 30.040536
## 381 22.572940 22.080777 23.065104
## 382 26.519059 25.972996 27.065122
## 383 25.414146 24.902011 25.926280
## 384 22.257251 21.760844 22.753658
## 385 24.782767 24.283936 25.281597
## 386 26.676903 26.124999 27.228808
## 387 25.729835 25.209322 26.250348
## 388 26.361214 25.820758 26.901669
## 389 31.727935 30.903669 32.552201
## 390 26.676903 26.124999 27.228808
## 391 27.466127 26.881761 28.050493
## 392 26.992593 26.428333 27.556853
predict(lm.fit_1a,data.frame(horsepower=(c(98))),
interval="prediction")
## Warning: 'newdata' had 1 row but variables found have 392 rows
## fit lwr upr
## 1 19.416046 9.7532948 29.07880
## 2 13.891480 4.2037318 23.57923
## 3 16.259151 6.5845976 25.93370
## 4 16.259151 6.5845976 25.93370
## 5 17.837598 8.1697749 27.50542
## 6 8.682604 -1.0471901 18.41240
## 7 5.210020 -4.5576558 14.97770
## 8 5.999243 -3.7591362 15.75762
## 9 4.420796 -5.3565771 14.19817
## 10 9.945362 0.2273963 19.66333
## 11 13.102256 3.4092855 22.79523
## 12 14.680704 4.9977664 24.36364
## 13 16.259151 6.5845976 25.93370
## 14 4.420796 -5.3565771 14.19817
## 15 24.940611 15.2825327 34.59869
## 16 24.940611 15.2825327 34.59869
## 17 24.624922 14.9671249 34.28272
## 18 26.519059 16.8585745 36.17954
## 19 26.045524 16.3859365 35.70511
## 20 32.675003 22.9892883 42.36072
## 21 26.203369 16.5434991 35.86324
## 22 25.729835 16.0707614 35.38891
## 23 24.940611 15.2825327 34.59869
## 24 22.099406 12.4414680 31.75734
## 25 25.729835 16.0707614 35.38891
## 26 5.999243 -3.7591362 15.75762
## 27 8.366914 -1.3659995 18.09983
## 28 6.788467 -2.9610196 16.53795
## 29 9.471827 -0.2504514 19.19411
## 30 26.045524 16.3859365 35.70511
## 31 25.729835 16.0707614 35.38891
## 32 24.940611 15.2825327 34.59869
## 33 24.151388 14.4938885 33.80889
## 34 23.362164 13.7048286 33.01950
## 35 24.151388 14.4938885 33.80889
## 36 26.045524 16.3859365 35.70511
## 37 24.151388 14.4938885 33.80889
## 38 13.891480 4.2037318 23.57923
## 39 12.313033 2.6144281 22.01164
## 40 15.785617 6.1087217 25.46251
## 41 16.259151 6.5845976 25.93370
## 42 11.523809 1.8191602 21.22846
## 43 13.102256 3.4092855 22.79523
## 44 12.313033 2.6144281 22.01164
## 45 22.572940 12.9153529 32.23053
## 46 28.571040 18.9049457 38.23713
## 47 24.151388 14.4938885 33.80889
## 48 26.045524 16.3859365 35.70511
## 49 26.361214 16.7010451 36.02138
## 50 25.729835 16.0707614 35.38891
## 51 28.886730 19.2195232 38.55394
## 52 27.939661 18.2755918 37.60373
## 53 29.675953 20.0056767 39.34623
## 54 29.044574 19.3767870 38.71236
## 55 30.465177 20.7914163 40.13894
## 56 28.886730 19.2195232 38.55394
## 57 24.940611 15.2825327 34.59869
## 58 27.308282 17.6459724 36.97059
## 59 31.412245 21.7337578 41.09073
## 60 25.729835 16.0707614 35.38891
## 61 26.361214 16.7010451 36.02138
## 62 13.891480 4.2037318 23.57923
## 63 12.313033 2.6144281 22.01164
## 64 16.259151 6.5845976 25.93370
## 65 15.785617 6.1087217 25.46251
## 66 16.259151 6.5845976 25.93370
## 67 7.104156 -2.6418860 16.85020
## 68 15.469927 5.7913884 25.14847
## 69 14.680704 4.9977664 24.36364
## 70 9.945362 0.2273963 19.66333
## 71 24.624922 14.9671249 34.28272
## 72 16.259151 6.5845976 25.93370
## 73 19.416046 9.7532948 29.07880
## 74 17.837598 8.1697749 27.50542
## 75 16.259151 6.5845976 25.93370
## 76 22.257251 12.5994463 31.91506
## 77 27.939661 18.2755918 37.60373
## 78 26.203369 16.5434991 35.86324
## 79 29.044574 19.3767870 38.71236
## 80 26.361214 16.7010451 36.02138
## 81 25.414146 15.7555198 35.07277
## 82 24.624922 14.9671249 34.28272
## 83 27.308282 17.6459724 36.97059
## 84 26.045524 16.3859365 35.70511
## 85 12.313033 2.6144281 22.01164
## 86 16.259151 6.5845976 25.93370
## 87 17.048375 7.3773932 26.71936
## 88 18.311133 8.6450050 27.97726
## 89 16.259151 6.5845976 25.93370
## 90 8.682604 -1.0471901 18.41240
## 91 16.259151 6.5845976 25.93370
## 92 14.996393 5.3152647 24.67752
## 93 16.259151 6.5845976 25.93370
## 94 5.999243 -3.7591362 15.75762
## 95 4.420796 -5.3565771 14.19817
## 96 12.313033 2.6144281 22.01164
## 97 23.362164 13.7048286 33.01950
## 98 24.151388 14.4938885 33.80889
## 99 24.151388 14.4938885 33.80889
## 100 26.045524 16.3859365 35.70511
## 101 24.940611 15.2825327 34.59869
## 102 32.675003 22.9892883 42.36072
## 103 16.259151 6.5845976 25.93370
## 104 13.575791 3.8860027 23.26558
## 105 13.102256 3.4092855 22.79523
## 106 11.523809 1.8191602 21.22846
## 107 24.151388 14.4938885 33.80889
## 108 26.045524 16.3859365 35.70511
## 109 28.571040 18.9049457 38.23713
## 110 25.098456 15.4402117 34.75670
## 111 25.729835 16.0707614 35.38891
## 112 26.519059 16.8585745 36.17954
## 113 23.046475 13.3890882 32.70386
## 114 25.729835 16.0707614 35.38891
## 115 17.048375 7.3773932 26.71936
## 116 3.631572 -6.1558990 13.41904
## 117 32.201469 22.5185881 41.88435
## 118 28.097506 18.4329552 37.76206
## 119 25.571990 15.9131489 35.23083
## 120 22.257251 12.5994463 31.91506
## 121 16.259151 6.5845976 25.93370
## 122 22.572940 12.9153529 32.23053
## 123 20.678804 11.0189156 30.33869
## 124 11.523809 1.8191602 21.22846
## 125 24.940611 15.2825327 34.59869
## 126 24.151388 14.4938885 33.80889
## 127 24.151388 14.4938885 33.80889
## 128 29.360264 19.6912650 39.02926
## 129 27.308282 17.6459724 36.97059
## 130 29.675953 20.0056767 39.34623
## 131 28.097506 18.4329552 37.76206
## 132 24.151388 14.4938885 33.80889
## 133 22.572940 12.9153529 32.23053
## 134 23.362164 13.7048286 33.01950
## 135 17.837598 8.1697749 27.50542
## 136 16.259151 6.5845976 25.93370
## 137 16.259151 6.5845976 25.93370
## 138 17.837598 8.1697749 27.50542
## 139 16.259151 6.5845976 25.93370
## 140 26.834748 17.1735835 36.49591
## 141 29.360264 19.6912650 39.02926
## 142 27.623972 17.9608153 37.28713
## 143 31.727935 22.0477395 41.40813
## 144 30.307332 20.6343015 39.98036
## 145 28.097506 18.4329552 37.76206
## 146 28.097506 18.4329552 37.76206
## 147 28.097506 18.4329552 37.76206
## 148 24.624922 14.9671249 34.28272
## 149 25.256301 15.5978741 34.91473
## 150 29.360264 19.6912650 39.02926
## 151 24.940611 15.2825327 34.59869
## 152 23.362164 13.7048286 33.01950
## 153 28.571040 18.9049457 38.23713
## 154 28.571040 18.9049457 38.23713
## 155 13.102256 3.4092855 22.79523
## 156 17.048375 7.3773932 26.71936
## 157 16.259151 6.5845976 25.93370
## 158 16.574840 6.9017655 26.24792
## 159 22.572940 12.9153529 32.23053
## 160 23.362164 13.7048286 33.01950
## 161 22.572940 12.9153529 32.23053
## 162 24.940611 15.2825327 34.59869
## 163 22.572940 12.9153529 32.23053
## 164 22.572940 12.9153529 32.23053
## 165 19.573890 9.9115555 29.23623
## 166 28.097506 18.4329552 37.76206
## 167 26.834748 17.1735835 36.49591
## 168 24.151388 14.4938885 33.80889
## 169 27.623972 17.9608153 37.28713
## 170 24.782767 15.1248371 34.44070
## 171 28.728885 19.0622427 38.39553
## 172 24.624922 14.9671249 34.28272
## 173 24.624922 14.9671249 34.28272
## 174 28.886730 19.2195232 38.55394
## 175 25.729835 16.0707614 35.38891
## 176 24.940611 15.2825327 34.59869
## 177 26.045524 16.3859365 35.70511
## 178 24.467077 14.8093961 34.12476
## 179 21.783717 12.1254616 31.44197
## 180 31.570090 21.8907569 41.24942
## 181 26.361214 16.7010451 36.02138
## 182 27.150438 17.4885260 36.81235
## 183 25.414146 15.7555198 35.07277
## 184 27.466127 17.8034021 37.12885
## 185 26.834748 17.1735835 36.49591
## 186 17.837598 8.1697749 27.50542
## 187 16.259151 6.5845976 25.93370
## 188 20.994493 11.3351547 30.65383
## 189 15.943462 6.2673635 25.61956
## 190 24.151388 14.4938885 33.80889
## 191 23.362164 13.7048286 33.01950
## 192 27.150438 17.4885260 36.81235
## 193 25.729835 16.0707614 35.38891
## 194 31.727935 22.0477395 41.40813
## 195 30.465177 20.7914163 40.13894
## 196 28.886730 19.2195232 38.55394
## 197 31.570090 21.8907569 41.24942
## 198 24.151388 14.4938885 33.80889
## 199 27.623972 17.9608153 37.28713
## 200 22.572940 12.9153529 32.23053
## 201 24.940611 15.2825327 34.59869
## 202 28.728885 19.0622427 38.39553
## 203 28.886730 19.2195232 38.55394
## 204 28.097506 18.4329552 37.76206
## 205 28.571040 18.9049457 38.23713
## 206 23.835698 14.1783144 33.49308
## 207 16.259151 6.5845976 25.93370
## 208 26.045524 16.3859365 35.70511
## 209 22.888630 13.2311931 32.54607
## 210 20.994493 11.3351547 30.65383
## 211 11.523809 1.8191602 21.22846
## 212 17.048375 7.3773932 26.71936
## 213 19.416046 9.7532948 29.07880
## 214 16.259151 6.5845976 25.93370
## 215 29.202419 19.5340343 38.87080
## 216 27.308282 17.6459724 36.97059
## 217 30.780866 21.1055963 40.45614
## 218 24.782767 15.1248371 34.44070
## 219 28.886730 19.2195232 38.55394
## 220 17.048375 7.3773932 26.71936
## 221 22.572940 12.9153529 32.23053
## 222 17.048375 7.3773932 26.71936
## 223 19.416046 9.7532948 29.07880
## 224 22.572940 12.9153529 32.23053
## 225 23.362164 13.7048286 33.01950
## 226 24.151388 14.4938885 33.80889
## 227 24.467077 14.8093961 34.12476
## 228 11.523809 1.8191602 21.22846
## 229 13.102256 3.4092855 22.79523
## 230 9.945362 0.2273963 19.66333
## 231 16.416996 6.7431898 26.09080
## 232 27.623972 17.9608153 37.28713
## 233 26.045524 16.3859365 35.70511
## 234 28.097506 18.4329552 37.76206
## 235 25.887680 16.2283572 35.54700
## 236 29.991643 20.3200222 39.66326
## 237 26.834748 17.1735835 36.49591
## 238 29.360264 19.6912650 39.02926
## 239 27.623972 17.9608153 37.28713
## 240 24.624922 14.9671249 34.28272
## 241 22.572940 12.9153529 32.23053
## 242 22.572940 12.9153529 32.23053
## 243 32.359314 22.6755047 42.04312
## 244 29.518109 19.8484792 39.18774
## 245 31.727935 22.0477395 41.40813
## 246 28.886730 19.2195232 38.55394
## 247 30.465177 20.7914163 40.13894
## 248 22.572940 12.9153529 32.23053
## 249 17.837598 8.1697749 27.50542
## 250 17.995443 8.3282015 27.66268
## 251 23.362164 13.7048286 33.01950
## 252 24.940611 15.2825327 34.59869
## 253 26.519059 16.8585745 36.17954
## 254 26.045524 16.3859365 35.70511
## 255 24.151388 14.4938885 33.80889
## 256 25.729835 16.0707614 35.38891
## 257 23.362164 13.7048286 33.01950
## 258 26.519059 16.8585745 36.17954
## 259 22.572940 12.9153529 32.23053
## 260 20.994493 11.3351547 30.65383
## 261 17.048375 7.3773932 26.71936
## 262 13.891480 4.2037318 23.57923
## 263 17.995443 8.3282015 27.66268
## 264 17.837598 8.1697749 27.50542
## 265 29.202419 19.5340343 38.87080
## 266 24.940611 15.2825327 34.59869
## 267 24.624922 14.9671249 34.28272
## 268 28.097506 18.4329552 37.76206
## 269 24.940611 15.2825327 34.59869
## 270 23.362164 13.7048286 33.01950
## 271 26.519059 16.8585745 36.17954
## 272 24.624922 14.9671249 34.28272
## 273 23.677853 14.0205024 33.33520
## 274 20.205269 10.5444324 29.86611
## 275 21.783717 12.1254616 31.44197
## 276 18.942511 9.2784130 28.60661
## 277 28.728885 19.0622427 38.39553
## 278 29.202419 19.5340343 38.87080
## 279 21.783717 12.1254616 31.44197
## 280 26.519059 16.8585745 36.17954
## 281 26.045524 16.3859365 35.70511
## 282 25.729835 16.0707614 35.38891
## 283 22.572940 12.9153529 32.23053
## 284 19.416046 9.7532948 29.07880
## 285 19.573890 9.9115555 29.23623
## 286 18.153288 8.4866115 27.81996
## 287 18.626822 8.9617422 28.29190
## 288 15.469927 5.7913884 25.14847
## 289 17.521909 7.8528719 27.19095
## 290 20.205269 10.5444324 29.86611
## 291 16.259151 6.5845976 25.93370
## 292 28.728885 19.0622427 38.39553
## 293 29.675953 20.0056767 39.34623
## 294 27.308282 17.6459724 36.97059
## 295 27.308282 17.6459724 36.97059
## 296 27.781817 18.1182118 37.44542
## 297 20.205269 10.5444324 29.86611
## 298 28.728885 19.0622427 38.39553
## 299 25.729835 16.0707614 35.38891
## 300 28.886730 19.2195232 38.55394
## 301 28.886730 19.2195232 38.55394
## 302 29.675953 20.0056767 39.34623
## 303 29.044574 19.3767870 38.71236
## 304 25.729835 16.0707614 35.38891
## 305 21.783717 12.1254616 31.44197
## 306 21.783717 12.1254616 31.44197
## 307 25.729835 16.0707614 35.38891
## 308 27.939661 18.2755918 37.60373
## 309 30.465177 20.7914163 40.13894
## 310 28.886730 19.2195232 38.55394
## 311 29.675953 20.0056767 39.34623
## 312 25.729835 16.0707614 35.38891
## 313 26.045524 16.3859365 35.70511
## 314 25.729835 16.0707614 35.38891
## 315 25.729835 16.0707614 35.38891
## 316 27.623972 17.9608153 37.28713
## 317 25.729835 16.0707614 35.38891
## 318 28.097506 18.4329552 37.76206
## 319 25.414146 15.7555198 35.07277
## 320 28.097506 18.4329552 37.76206
## 321 29.675953 20.0056767 39.34623
## 322 23.362164 13.7048286 33.01950
## 323 29.675953 20.0056767 39.34623
## 324 32.359314 22.6755047 42.04312
## 325 32.359314 22.6755047 42.04312
## 326 29.360264 19.6912650 39.02926
## 327 29.360264 19.6912650 39.02926
## 328 29.360264 19.6912650 39.02926
## 329 29.360264 19.6912650 39.02926
## 330 30.149488 20.4771701 39.82180
## 331 19.100356 9.4367235 28.76399
## 332 24.151388 14.4938885 33.80889
## 333 26.045524 16.3859365 35.70511
## 334 28.571040 18.9049457 38.23713
## 335 26.676903 17.0160873 36.33772
## 336 26.676903 17.0160873 36.33772
## 337 25.414146 15.7555198 35.07277
## 338 22.572940 12.9153529 32.23053
## 339 26.676903 17.0160873 36.33772
## 340 30.780866 21.1055963 40.45614
## 341 29.833798 20.1628578 39.50474
## 342 30.465177 20.7914163 40.13894
## 343 29.360264 19.6912650 39.02926
## 344 29.675953 20.0056767 39.34623
## 345 30.149488 20.4771701 39.82180
## 346 29.202419 19.5340343 38.87080
## 347 29.991643 20.3200222 39.66326
## 348 29.675953 20.0056767 39.34623
## 349 29.675953 20.0056767 39.34623
## 350 28.255351 18.5903019 37.92040
## 351 28.097506 18.4329552 37.76206
## 352 28.097506 18.4329552 37.76206
## 353 24.151388 14.4938885 33.80889
## 354 28.255351 18.5903019 37.92040
## 355 27.308282 17.6459724 36.97059
## 356 27.939661 18.2755918 37.60373
## 357 21.625872 11.9674335 31.28431
## 358 20.994493 11.3351547 30.65383
## 359 22.572940 12.9153529 32.23053
## 360 23.362164 13.7048286 33.01950
## 361 26.045524 16.3859365 35.70511
## 362 26.519059 16.8585745 36.17954
## 363 26.045524 16.3859365 35.70511
## 364 26.045524 16.3859365 35.70511
## 365 26.045524 16.3859365 35.70511
## 366 26.519059 16.8585745 36.17954
## 367 26.676903 17.0160873 36.33772
## 368 25.729835 16.0707614 35.38891
## 369 25.414146 15.7555198 35.07277
## 370 28.255351 18.5903019 37.92040
## 371 29.202419 19.5340343 38.87080
## 372 29.202419 19.5340343 38.87080
## 373 29.991643 20.3200222 39.66326
## 374 28.886730 19.2195232 38.55394
## 375 26.045524 16.3859365 35.70511
## 376 28.097506 18.4329552 37.76206
## 377 28.886730 19.2195232 38.55394
## 378 29.360264 19.6912650 39.02926
## 379 29.360264 19.6912650 39.02926
## 380 29.360264 19.6912650 39.02926
## 381 22.572940 12.9153529 32.23053
## 382 26.519059 16.8585745 36.17954
## 383 25.414146 15.7555198 35.07277
## 384 22.257251 12.5994463 31.91506
## 385 24.782767 15.1248371 34.44070
## 386 26.676903 17.0160873 36.33772
## 387 25.729835 16.0707614 35.38891
## 388 26.361214 16.7010451 36.02138
## 389 31.727935 22.0477395 41.40813
## 390 26.676903 17.0160873 36.33772
## 391 27.466127 17.8034021 37.12885
## 392 26.992593 17.3310631 36.65412
# (b) Plot the response and the predictor. Use the abline() function
# to display the least squares regression line.
plot(Auto$mpg~Auto$horsepower)
abline(lm.fit_1a,col="red",lwd=3)

# (c) Use the plot() function to produce diagnostic plots of the least
# squares regression fit. Comment on any problems you see with
# the fit.
par(mfrow=c(2,2))
plot(lm.fit_1a)

# The residual plot shows a U shape and also
# there`s an indication that residuals are not
# normally distributed
# 9. This question involves the use of multiple linear regression on the
# Auto data set.
# (a) Produce a scatterplot matrix which includes all of the variables
# in the data set.
pairs(Auto)

# (b) Compute the matrix of correlations between the variables using
# the function cor(). You will need to exclude the name variable,
# cor()
# which is qualitative.
names(Auto)
## [1] "mpg" "cylinders" "displacement" "horsepower"
## [5] "weight" "acceleration" "year" "origin"
## [9] "name"
Auto <- Auto[,-9]
names(Auto)
## [1] "mpg" "cylinders" "displacement" "horsepower"
## [5] "weight" "acceleration" "year" "origin"
cor(Auto)
## 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
# (c) Use the lm() function to perform a multiple linear regression
# with mpg as the response and all other variables except name as
# the predictors. Use the summary() function to print the results.
# Comment on the output. For instance:
lm.fit_2a <- lm(Auto$mpg~.,data = Auto)
summary(lm.fit_2a)
##
## Call:
## lm(formula = Auto$mpg ~ ., data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.5903 -2.1565 -0.1169 1.8690 13.0604
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -17.218435 4.644294 -3.707 0.00024 ***
## cylinders -0.493376 0.323282 -1.526 0.12780
## displacement 0.019896 0.007515 2.647 0.00844 **
## horsepower -0.016951 0.013787 -1.230 0.21963
## weight -0.006474 0.000652 -9.929 < 2e-16 ***
## acceleration 0.080576 0.098845 0.815 0.41548
## year 0.750773 0.050973 14.729 < 2e-16 ***
## origin 1.426141 0.278136 5.127 4.67e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.328 on 384 degrees of freedom
## Multiple R-squared: 0.8215, Adjusted R-squared: 0.8182
## F-statistic: 252.4 on 7 and 384 DF, p-value: < 2.2e-16
# some variables are not significant such as cylinders,
# horsepower, acceleration
lm.fit3_a <- update(lm.fit_2a,~.-cylinders)
summary(lm.fit3_a)
##
## Call:
## lm(formula = Auto$mpg ~ displacement + horsepower + weight +
## acceleration + year + origin, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.3098 -2.1603 -0.1418 1.8466 12.9901
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.847e+01 4.579e+00 -4.034 6.60e-05 ***
## displacement 1.228e-02 5.631e-03 2.181 0.0298 *
## horsepower -1.437e-02 1.371e-02 -1.049 0.2950
## weight -6.601e-03 6.479e-04 -10.188 < 2e-16 ***
## acceleration 8.819e-02 9.889e-02 0.892 0.3730
## year 7.520e-01 5.106e-02 14.728 < 2e-16 ***
## origin 1.384e+00 2.773e-01 4.993 9.04e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.333 on 385 degrees of freedom
## Multiple R-squared: 0.8204, Adjusted R-squared: 0.8176
## F-statistic: 293.1 on 6 and 385 DF, p-value: < 2.2e-16
lm.fit3_b <- update(lm.fit3_a,~.-horsepower)
summary(lm.fit3_b)
##
## Call:
## lm(formula = Auto$mpg ~ displacement + weight + acceleration +
## year + origin, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.3110 -2.1671 -0.0526 1.8293 13.0061
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -2.054e+01 4.133e+00 -4.970 1.01e-06 ***
## displacement 1.060e-02 5.398e-03 1.963 0.0503 .
## weight -6.904e-03 5.799e-04 -11.904 < 2e-16 ***
## acceleration 1.522e-01 7.782e-02 1.956 0.0512 .
## year 7.639e-01 4.978e-02 15.344 < 2e-16 ***
## origin 1.319e+00 2.702e-01 4.881 1.55e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.334 on 386 degrees of freedom
## Multiple R-squared: 0.8199, Adjusted R-squared: 0.8175
## F-statistic: 351.4 on 5 and 386 DF, p-value: < 2.2e-16
# i. Is there a relationship between the predictors
# and the response?
# Yes
# ii. Which predictors appear to have a statistically significant
# relationship to the response?
# some predictors are statistically significant, such as,
# weight, year and origin
# iii. What does the coefficient for the year variable suggest?
# year is statistically signicant