Lab 4
Lab 4
R
Himanshu Kakkar
2023-02-12
#Importing Libraries
library(tidyverse)
library(ISLR2)
library(ggplot2)
library(dplyr)
CarData<- ISLR2::Auto
head(CarData)
summary(CarData)
#step 1
linModel <- lm(mpg ~ horsepower + origin, data = CarData)
coef(linModel)
#step 2
Prediction <- data.frame(origin = c(1,3,1,3 ), horsepower = c(100, 100
,170 , 170))
predict(linModel, Prediction)
## 1 2 3 4
## 22.55571 27.71051 13.22516 18.37996
#step 3
#It can be seen that, by increasing the horsepower of cars, the
average prediction for mpg decreases.
#step 4
CarData %>% ggplot(aes(x = horsepower, y = mpg)) + geom_point() +
geom_smooth(method="lm")
#step 5
#It can be seen from scatter plot that simple linear regression will
not work effectively as the best fit line that is not passes close to
most of the data points.
#step 6
linModel2 <- lm(mpg ~ horsepower + I(horsepower^2), data = CarData)
coef(linModel2)
#step 7
Prediction2 <- data.frame(horsepower = c(80, 100, 120))
predict(linModel2, Prediction2)
## 1 2 3
## 27.48036 22.58650 18.67706
#step 8
#It can be seen that, even by increasing the square of horsepower of
cars, the average prediction decreases.