Lab 4 .

docx

School

Durham College *

*We aren’t endorsed by this school

Course

DATA

Subject

Statistics

Date

Apr 3, 2024

Type

docx

Pages

6

Uploaded by JudgeBravery13371

Report
Lab 4: Linear Regression Extensions 1. Write coefficients from a linear model with response = mpg, and explanatory variables horsepower and origin, with an interaction between horsepower and origin CODE:- library(tidyverse) library(ISLR2) smallData<- ISLR2::Auto autoModel <- lm(mpg ~ horsepower + origin + horsepower:origin, data =smallData) coef(autoModel) OUTPUT:- 2. Predict the average mpg for four cars: (100 horsepower, American), (100 horsepower, Japanese), (170 horsepower, American), (170 horsepower, Japanese). CODE :- mpgPrediction <- data.frame(horsepower = c(100, 100, 170, 170), origin=c(1,3,1,3)) predict(autoModel,mpgPrediction) OUTPUT:- 3. What do you notice about how the predictions change as horsepower increases? ANS:- From the obtained output we can observe that predictions show decreasing trend as horsepower increases.
4. What Plot a scatter plot with x = horsepower, y = mpg CODE : - autoModel %>% ggplot(aes(x = horsepower, y = mpg)) + geom_point() + geom_smooth(method="lm") OUPUT : - 5.Based on the plot, do you think a simple linear regression will work well here? ANS:- No, the simple linear regression will not work well here as it more looks like curve. 6.Write coefficients from a linear model with response = mpg and explanatory = horsepower, with a suitable transformation for the horsepower variable (for example, square it)
CODE:- autoSqrModel <- lm(mpg ~ horsepower + I(horsepower^2), data = smallData) coef(autoSqrModel) 7.Predict the average mpg for 3 cars: 80 horsepower, 100 horsepower, 120 horsepower.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
mpg_Average_Prediction <- data.frame(horsepower = c(80, 80, 80, 100, 100, 100, 120, 120, 120),origin = c(1, 2, 3, 1, 2, 3, 1, 2,3)) predict(autoModel, mpg_Average_Prediction) 8. What do you notice about these predictions? ANS:- For 80 , horsepower increases from American to japnese For 100 , firstly it increases and then it decreases for japnese
For 120 , it shows similar values for Europe and japnese .
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help