You are on page 1of 2

Lab 1: Simple Linear Regression

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

plot(x1, y, main = "Plot 1", xlab = "horsepower", ylab = "mpg")


abline(lm(y ~ x1, data = DT), col = "blue")

plot(x2, y, main = "Plot 2", xlab = "weight", ylab = "mpg")


abline(lm(y ~ x2, data = DT), col = "blue")

plot(x3, y, main = "Plot 3", xlab = "acceleration", ylab = "mpg")


abline(lm(y ~ x3, data = DT), col = "blue")
3. Describe the correlation in your own words. What types of relationships do you notice for
each pair? Do you notice any similarities between the scatterplots?
Answer:
Correlation is a statistical term describing the degree to which two variables move in
coordination with one another. If the two variables move in the same direction, then
those variables are said to have a positive correlation. If they move in opposite
directions, then they have a negative correlation. It takes values between -1 and +1.
A value of 0 indicate no linear association.
 The correlation between mpg and horsepower is -0.7784268 which indicates
that if we have increase in values of horsepower then there is a decrease in
mpg.
 The correlation between mpg and weight is -0.8322442which indicates that if
we have increase in values of weight then there is a decrease in mpg.
 The correlation between mpg and weight is 0.4233285 which indicates that if
we have increase in values of acceleration then there is also increase in mpg.
The scatter plot of (mpg, horsepower) is almost similar (not exactly but some values have
similar pattern) to (mpg, weight).

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

You might also like