# Load necessary libraries
library(tidymodels)
## Warning: package 'tidymodels' was built under R version 4.3.3
## ── Attaching packages ────────────────────────────────────── tidymodels 1.1.1 ──
## ✔ broom 1.0.5 ✔ recipes 1.0.8
## ✔ dials 1.2.0 ✔ rsample 1.2.0
## ✔ dplyr 1.1.3 ✔ tibble 3.2.1
## ✔ ggplot2 3.4.3 ✔ tidyr 1.3.0
## ✔ infer 1.0.5 ✔ tune 1.1.2
## ✔ modeldata 1.2.0 ✔ workflows 1.1.3
## ✔ parsnip 1.1.1 ✔ workflowsets 1.0.1
## ✔ purrr 1.0.2 ✔ yardstick 1.2.0
## ── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
## ✖ purrr::discard() masks scales::discard()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ✖ recipes::step() masks stats::step()
## • Learn how to get started at https://www.tidymodels.org/start/
library(ggplot2)
library(ISLR2)
## Warning: package 'ISLR2' was built under R version 4.3.3
library(MASS)
## Warning: package 'MASS' was built under R version 4.3.3
##
## Attaching package: 'MASS'
## The following object is masked from 'package:ISLR2':
##
## Boston
## The following object is masked from 'package:dplyr':
##
## select
# Load the Auto dataset
data(auto)
## Warning in data(auto): data set 'auto' not found
# (a) Perform simple linear regression
model <- lm(mpg ~ horsepower, data = Auto)
# Summary of the model
summary(model)
##
## 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
# (b) Plot the response and predictor
ggplot(Auto, aes(x = horsepower, y = mpg)) +
geom_point() +
geom_abline(intercept = coef(model)[1], slope = coef(model)[2], color = "red") +
labs(x = "Horsepower", y = "MPG") +
theme_minimal()

# (c) Diagnostic plots
plot(model)




predict(model)
## 1 2 3 4 5 6 7 8
## 19.416046 13.891480 16.259151 16.259151 17.837598 8.682604 5.210020 5.999243
## 9 10 11 12 13 14 15 16
## 4.420796 9.945362 13.102256 14.680704 16.259151 4.420796 24.940611 24.940611
## 17 18 19 20 21 22 23 24
## 24.624922 26.519059 26.045524 32.675003 26.203369 25.729835 24.940611 22.099406
## 25 26 27 28 29 30 31 32
## 25.729835 5.999243 8.366914 6.788467 9.471827 26.045524 25.729835 24.940611
## 34 35 36 37 38 39 40 41
## 24.151388 23.362164 24.151388 26.045524 24.151388 13.891480 12.313033 15.785617
## 42 43 44 45 46 47 48 49
## 16.259151 11.523809 13.102256 12.313033 22.572940 28.571040 24.151388 26.045524
## 50 51 52 53 54 55 56 57
## 26.361214 25.729835 28.886730 27.939661 29.675953 29.044574 30.465177 28.886730
## 58 59 60 61 62 63 64 65
## 24.940611 27.308282 31.412245 25.729835 26.361214 13.891480 12.313033 16.259151
## 66 67 68 69 70 71 72 73
## 15.785617 16.259151 7.104156 15.469927 14.680704 9.945362 24.624922 16.259151
## 74 75 76 77 78 79 80 81
## 19.416046 17.837598 16.259151 22.257251 27.939661 26.203369 29.044574 26.361214
## 82 83 84 85 86 87 88 89
## 25.414146 24.624922 27.308282 26.045524 12.313033 16.259151 17.048375 18.311133
## 90 91 92 93 94 95 96 97
## 16.259151 8.682604 16.259151 14.996393 16.259151 5.999243 4.420796 12.313033
## 98 99 100 101 102 103 104 105
## 23.362164 24.151388 24.151388 26.045524 24.940611 32.675003 16.259151 13.575791
## 106 107 108 109 110 111 112 113
## 13.102256 11.523809 24.151388 26.045524 28.571040 25.098456 25.729835 26.519059
## 114 115 116 117 118 119 120 121
## 23.046475 25.729835 17.048375 3.631572 32.201469 28.097506 25.571990 22.257251
## 122 123 124 125 126 128 129 130
## 16.259151 22.572940 20.678804 11.523809 24.940611 24.151388 24.151388 29.360264
## 131 132 133 134 135 136 137 138
## 27.308282 29.675953 28.097506 24.151388 22.572940 23.362164 17.837598 16.259151
## 139 140 141 142 143 144 145 146
## 16.259151 17.837598 16.259151 26.834748 29.360264 27.623972 31.727935 30.307332
## 147 148 149 150 151 152 153 154
## 28.097506 28.097506 28.097506 24.624922 25.256301 29.360264 24.940611 23.362164
## 155 156 157 158 159 160 161 162
## 28.571040 28.571040 13.102256 17.048375 16.259151 16.574840 22.572940 23.362164
## 163 164 165 166 167 168 169 170
## 22.572940 24.940611 22.572940 22.572940 19.573890 28.097506 26.834748 24.151388
## 171 172 173 174 175 176 177 178
## 27.623972 24.782767 28.728885 24.624922 24.624922 28.886730 25.729835 24.940611
## 179 180 181 182 183 184 185 186
## 26.045524 24.467077 21.783717 31.570090 26.361214 27.150438 25.414146 27.466127
## 187 188 189 190 191 192 193 194
## 26.834748 17.837598 16.259151 20.994493 15.943462 24.151388 23.362164 27.150438
## 195 196 197 198 199 200 201 202
## 25.729835 31.727935 30.465177 28.886730 31.570090 24.151388 27.623972 22.572940
## 203 204 205 206 207 208 209 210
## 24.940611 28.728885 28.886730 28.097506 28.571040 23.835698 16.259151 26.045524
## 211 212 213 214 215 216 217 218
## 22.888630 20.994493 11.523809 17.048375 19.416046 16.259151 29.202419 27.308282
## 219 220 221 222 223 224 225 226
## 30.780866 24.782767 28.886730 17.048375 22.572940 17.048375 19.416046 22.572940
## 227 228 229 230 231 232 233 234
## 23.362164 24.151388 24.467077 11.523809 13.102256 9.945362 16.416996 27.623972
## 235 236 237 238 239 240 241 242
## 26.045524 28.097506 25.887680 29.991643 26.834748 29.360264 27.623972 24.624922
## 243 244 245 246 247 248 249 250
## 22.572940 22.572940 32.359314 29.518109 31.727935 28.886730 30.465177 22.572940
## 251 252 253 254 255 256 257 258
## 17.837598 17.995443 23.362164 24.940611 26.519059 26.045524 24.151388 25.729835
## 259 260 261 262 263 264 265 266
## 23.362164 26.519059 22.572940 20.994493 17.048375 13.891480 17.995443 17.837598
## 267 268 269 270 271 272 273 274
## 29.202419 24.940611 24.624922 28.097506 24.940611 23.362164 26.519059 24.624922
## 275 276 277 278 279 280 281 282
## 23.677853 20.205269 21.783717 18.942511 28.728885 29.202419 21.783717 26.519059
## 283 284 285 286 287 288 289 290
## 26.045524 25.729835 22.572940 19.416046 19.573890 18.153288 18.626822 15.469927
## 291 292 293 294 295 296 297 298
## 17.521909 20.205269 16.259151 28.728885 29.675953 27.308282 27.308282 27.781817
## 299 300 301 302 303 304 305 306
## 20.205269 28.728885 25.729835 28.886730 28.886730 29.675953 29.044574 25.729835
## 307 308 309 310 311 312 313 314
## 21.783717 21.783717 25.729835 27.939661 30.465177 28.886730 29.675953 25.729835
## 315 316 317 318 319 320 321 322
## 26.045524 25.729835 25.729835 27.623972 25.729835 28.097506 25.414146 28.097506
## 323 324 325 326 327 328 329 330
## 29.675953 23.362164 29.675953 32.359314 32.359314 29.360264 29.360264 29.360264
## 332 333 334 335 336 338 339 340
## 29.360264 30.149488 19.100356 24.151388 26.045524 28.571040 26.676903 26.676903
## 341 342 343 344 345 346 347 348
## 25.414146 22.572940 26.676903 30.780866 29.833798 30.465177 29.360264 29.675953
## 349 350 351 352 353 354 356 357
## 30.149488 29.202419 29.991643 29.675953 29.675953 28.255351 28.097506 28.097506
## 358 359 360 361 362 363 364 365
## 24.151388 28.255351 27.308282 27.939661 21.625872 20.994493 22.572940 23.362164
## 366 367 368 369 370 371 372 373
## 26.045524 26.519059 26.045524 26.045524 26.045524 26.519059 26.676903 25.729835
## 374 375 376 377 378 379 380 381
## 25.414146 28.255351 29.202419 29.202419 29.991643 28.886730 26.045524 28.097506
## 382 383 384 385 386 387 388 389
## 28.886730 29.360264 29.360264 29.360264 22.572940 26.519059 25.414146 22.257251
## 390 391 392 393 394 395 396 397
## 24.782767 26.676903 25.729835 26.361214 31.727935 26.676903 27.466127 26.992593
"1. Is there a relationship between the predictor and the response?
Yes, there appears to be a relationship between horsepower and mpg. This is evident from the significant coefficients in the model summary.
2. How strong is the relationship between the predictor and the response?
This can be assessed by looking at the R-squared value in the summary output. A higher R-squared value indicates a stronger relationship between the predictor and the response.
3. Is the relationship between the predictor and the response positive or negative?
You can determine this from the sign of the coefficient for horsepower. If its negative, it indicates a negative relationship; if positive, its s a positive relationship.
4. What is the predicted mpg associated with a horsepower of 98? What are the associated 95 % confidence and prediction intervals?
To get this information, you can use the predict() function with your model object."
## [1] "1. Is there a relationship between the predictor and the response?\nYes, there appears to be a relationship between horsepower and mpg. This is evident from the significant coefficients in the model summary.\n2. How strong is the relationship between the predictor and the response?\nThis can be assessed by looking at the R-squared value in the summary output. A higher R-squared value indicates a stronger relationship between the predictor and the response.\n3. Is the relationship between the predictor and the response positive or negative?\nYou can determine this from the sign of the coefficient for horsepower. If its negative, it indicates a negative relationship; if positive, its s a positive relationship.\n4. What is the predicted mpg associated with a horsepower of 98? What are the associated 95 % confidence and prediction intervals?\nTo get this information, you can use the predict() function with your model object."