Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

lab2_Revathy.

R
revak

2024-01-28
#Library is already installed
#To load the ISLR2 library
library(ISLR2)

# To load the Auto dataset


data("Auto")

#Lab Steps
#1. Plot a "boxplot" with y = mpg and x = origin
#To plot the boxplot
boxplot(mpg ~ origin, data = Auto, main = "MPG by Origin", xlab = "Origin",
ylab = "MPG")

#2.Which origin seems to have the more fuel-efficient automobiles?


#To print the summary statistics for each origin
summary_by_origin <- tapply(Auto$mpg, Auto$origin, summary)
print(summary_by_origin)
## $`1`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 9.00 15.00 18.50 20.03 24.00 39.00
##
## $`2`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 16.20 23.75 26.00 27.60 30.12 44.30
##
## $`3`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 18.00 25.70 31.60 30.45 34.05 46.60

#Result - Japanese cars($'3') with highest mean mgp of 30.45 is more fuel-
efficient

#3.Plot a "boxplot" with y = weight and x = origin


#To plot boxplot
boxplot(weight ~ origin, data = Auto, main = "Weight by Origin", xlab =
"Origin", ylab = "Weight")

#4.What do you notice about the weight of the cars for different origins?
#To print the summary statistics for each origin
summary_weight_by_origin <- tapply(Auto$weight, Auto$origin, summary)
print(summary_weight_by_origin)

## $`1`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1800 2720 3381 3372 4055 5140
##
## $`2`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1825 2072 2240 2433 2804 3820
##
## $`3`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1613 1985 2155 2221 2412 2930

#Result - Japanese cars($'3') with mean weight 2221 pounds have the lowest
mean weight

#5.Create a linear model with response = mpg and the single explanatory
variable origin
#To create linear model
model_origin <- lm(mpg ~ origin, data = Auto)
summary(model_origin)

##
## Call:
## lm(formula = mpg ~ origin, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -13.2416 -5.2533 -0.7651 3.8967 18.7115
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 14.8120 0.7164 20.68 <2e-16 ***
## origin 5.4765 0.4048 13.53 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 6.447 on 390 degrees of freedom
## Multiple R-squared: 0.3195, Adjusted R-squared: 0.3177
## F-statistic: 183.1 on 1 and 390 DF, p-value: < 2.2e-16

#6.How fuel efficient do you predict a Japanese car will be on average, based
on this model?
#To predict average mpg for Japanese cars
predicted_mpg_japanese <- coef(model_origin)[1] + coef(model_origin)[2] * 3
print(predicted_mpg_japanese)

## (Intercept)
## 31.24162

#Result - the predicted average mpg for Japanese cars based on this model is
approximately 31.2415

#7.Create a linear model with response = mpg and explanatory variables origin
and weight
#To create linear model
model_origin_weight <- lm(mpg ~ origin + weight, data = Auto)
summary(model_origin_weight)

##
## Call:
## lm(formula = mpg ~ origin + weight, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -13.0698 -2.7888 -0.3122 2.4489 15.4816
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 42.4908175 1.3266161 32.03 < 2e-16 ***
## origin 1.1540278 0.3306915 3.49 0.000539 ***
## weight -0.0070071 0.0003136 -22.34 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.272 on 389 degrees of freedom
## Multiple R-squared: 0.702, Adjusted R-squared: 0.7004
## F-statistic: 458.1 on 2 and 389 DF, p-value: < 2.2e-16

#8.Predict the average mpg for two cars: a Japanese car that weighs 3000 and
an American car that weighs 3000
#To predict mpg for Japanese car weighing 3000
predict(model_origin_weight, newdata = data.frame(origin = 3, weight = 3000))

## 1
## 24.93157

#Result is 24.93157

#To predict mpg for American car weighing 3000


predict(model_origin_weight, newdata = data.frame(origin = 1, weight = 3000))

## 1
## 22.62352

#Result is 22.62352

#9.How much more fuel-efficient are Japanese cars? Is the result what you
expect?
#Inorder to asses how much more fuel-efficient Japanese cars are,we need to
calculate the difference
#To calculate the difference in predicted average mpg
difference_mpg <- predicted_mpg_japanese - predict(model_origin, newdata =
data.frame(origin = 1))
print(difference_mpg)
## (Intercept)
## 10.95309

#Result is Japanese cars are 10.95 mpg more predicted than American cars

#Yes,I expect the Japanese cars to be more fuel efficient than the American
cars

You might also like