0% found this document useful (0 votes)
18 views4 pages

Lab 4

Uploaded by

kakkarh28can
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

Lab 4

Uploaded by

kakkarh28can
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab4.

R
Himanshu Kakkar

2023-02-12
#Importing Libraries
library(tidyverse)

## ── Attaching packages ───────────────────────────────────────


tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0 ✔ purrr 1.0.1
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.5.0
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## ── Conflicts ──────────────────────────────────────────
tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()

library(ISLR2)
library(ggplot2)
library(dplyr)

CarData<- ISLR2::Auto

head(CarData)

## 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

summary(CarData)

## mpg cylinders displacement horsepower


weight
## Min. : 9.00 Min. :3.000 Min. : 68.0 Min. : 46.0
Min. :1613
## 1st Qu.:17.00 1st Qu.:4.000 1st Qu.:105.0 1st Qu.: 75.0
1st Qu.:2225
## Median :22.75 Median :4.000 Median :151.0 Median : 93.5
Median :2804
## Mean :23.45 Mean :5.472 Mean :194.4 Mean :104.5
Mean :2978
## 3rd Qu.:29.00 3rd Qu.:8.000 3rd Qu.:275.8 3rd Qu.:126.0
3rd Qu.:3615
## Max. :46.60 Max. :8.000 Max. :455.0 Max. :230.0
Max. :5140
##

## acceleration year origin


name
## Min. : 8.00 Min. :70.00 Min. :1.000 amc
matador : 5
## 1st Qu.:13.78 1st Qu.:73.00 1st Qu.:1.000 ford
pinto : 5
## Median :15.50 Median :76.00 Median :1.000 toyota
corolla : 5
## Mean :15.54 Mean :75.98 Mean :1.577 amc
gremlin : 4
## 3rd Qu.:17.02 3rd Qu.:79.00 3rd Qu.:2.000 amc
hornet : 4
## Max. :24.80 Max. :82.00 Max. :3.000 chevrolet
chevette: 4
##
(Other) :365

#step 1
linModel <- lm(mpg ~ horsepower + origin, data = CarData)
coef(linModel)

## (Intercept) horsepower origin


## 33.3076652 -0.1332936 2.5774023

#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")

## `geom_smooth()` using formula = 'y ~ x'

#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)

## (Intercept) horsepower I(horsepower^2)


## 56.900099702 -0.466189630 0.001230536

#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.

You might also like