November 30, 2025

1. Idea

  • Goal: predict a car’s fuel efficiency (MPG)
  • Data: built-in mtcars dataset
  • Inputs:
    • Horsepower (hp)
    • Weight (1000 lbs)
    • Number of cylinders (4, 6, 8)
  • Output:
    • Predicted MPG
    • Plot of MPG vs horsepower with the user car highlighted

2. Data and Model

data(mtcars)

# Convert cylinders to factor for the model
mtcars$cyl <- factor(mtcars$cyl)

lm_fit <- lm(mpg ~ hp + wt + cyl, data = mtcars)

summary(lm_fit)$coefficients
##                Estimate Std. Error   t value     Pr(>|t|)
## (Intercept) 35.84599532  2.0410191 17.562793 2.670320e-16
## hp          -0.02311981  0.0119522 -1.934357 6.361269e-02
## wt          -3.18140405  0.7196010 -4.421067 1.441756e-04
## cyl6        -3.35902490  1.4016697 -2.396445 2.374718e-02
## cyl8        -3.18588444  2.1704753 -1.467828 1.537047e-01