Professional Documents
Culture Documents
Lab 1
Lab 1
Setup:
library(tidyverse)
library(ISLR2)
DT = ISLR2::Auto
DT
DT = DT [, c("mpg", "horsepower", "weight", "acceleration")]
DT
head(DT)
summary(DT)
1. Using the Auto dataset: find the correlation between (mpg, horsepower), (mpg, weight),
(mpg, acceleration)
Answer:
cor(DT$mpg, DT$horsepower)
cor(DT$mpg, DT$weight)
cor(DT$mpg, DT$acceleration)
2. Graph a scatterplot for (mpg, horsepower), (mpg, weight) and (mpg, acceleration) and
include a line of best fit.
Answer:
y=DT$mpg
x1=DT$horsepower
x2=DT$weight
x3=DT$acceleration
4. Fit a simple linear regression model for: response (mpg), explanatory (horsepower).
Answer:
m1= lm(DT$mpg ~ DT$horsepower)
m1
5. Display the coefficients from this model.
Answer:
m1
6. Repeat steps 4 and 5 for: response (mpg), explanatory (weight).
Answer:
m2=lm(DT$mpg ~ DT$weight)
m2
7. If we increase the horsepower of a car by 1, what happens to mpg in our model?
Answer:
If we increase the horsepower of a car, then mpg of our model will decrease on average of
0.1578.
8. Predict the MPG for a car with 190 horsepower.
Answer:
a1=-0.1578
b1= 39.9359
a1*c(190)+b1
9. Predict the MPG for a car that weighs 4000lbs.
Answer:
a2=-0.007647
b2=46.216525
a2*c(4000)+b2