# Load necessary library and data
library(ISLR)
data("Auto")
# Check the structure of the Auto dataset
str(Auto)
## 'data.frame': 392 obs. of 9 variables:
## $ mpg : num 18 15 18 16 17 15 14 14 14 15 ...
## $ cylinders : num 8 8 8 8 8 8 8 8 8 8 ...
## $ displacement: num 307 350 318 304 302 429 454 440 455 390 ...
## $ horsepower : num 130 165 150 150 140 198 220 215 225 190 ...
## $ weight : num 3504 3693 3436 3433 3449 ...
## $ acceleration: num 12 11.5 11 12 10.5 10 9 8.5 10 8.5 ...
## $ year : num 70 70 70 70 70 70 70 70 70 70 ...
## $ origin : num 1 1 1 1 1 1 1 1 1 1 ...
## $ name : Factor w/ 304 levels "amc ambassador brougham",..: 49 36 231 14 161 141 54 223 241 2 ...
# View the first few rows of the Auto dataset
head(Auto)
## mpg cylinders displacement horsepower weight acceleration year origin
## 1 18 8 307 130 3504 12.0 70 1
## 2 15 8 350 165 3693 11.5 70 1
## 3 18 8 318 150 3436 11.0 70 1
## 4 16 8 304 150 3433 12.0 70 1
## 5 17 8 302 140 3449 10.5 70 1
## 6 15 8 429 198 4341 10.0 70 1
## name
## 1 chevrolet chevelle malibu
## 2 buick skylark 320
## 3 plymouth satellite
## 4 amc rebel sst
## 5 ford torino
## 6 ford galaxie 500
# a. Perform simple linear regression with mpg as the response and horsepower as the predictor
lm_model <- lm(mpg ~ horsepower, data = Auto)
# Print the summary of the linear regression model
summary(lm_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
# i. Is there a relationship between the predictor and the response?
# Answer: To determine if there's a relationship, check the p-value associated with the predictor (horsepower) coefficient.
# If the p-value is less than the significance level (e.g., 0.05), then there's evidence of a relationship.
# Look for the "Pr(>|t|)" value for horsepower in the summary output.
# ii. How strong is the relationship between the predictor and the response?
# Answer: Look for the R-squared value in the summary output.
# The R-squared value indicates the proportion of variance in the response variable (mpg) explained by the predictor variable (horsepower).
# Higher R-squared values indicate stronger relationships.
# iii. Is the relationship between the predictor and the response positive or negative?
# Answer: Look at the sign of the coefficient for horsepower in the summary output.
# A positive coefficient indicates a positive relationship, while a negative coefficient indicates a negative relationship.
# b. Plot the response and the predictor using a scatterplot
plot(Auto$horsepower, Auto$mpg, xlab = "Horsepower", ylab = "MPG", main = "Scatterplot of MPG vs Horsepower")
abline(lm_model, col = "red") # Add least squares regression line
