2025-06-08

Meet the mtcars Dataset

  • Summary of the data:
    • 32 observations of car models form 1974
    • Key variables will be: mpg(miles per gallon), wt(weight in 1000lbs), hp(horsepower)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

The Linear Regression Model

We aim to model the relationship between a response variable (MPG) and two predictor variables (Weight and Horsepower) using simple linear regression.

\[ \text{MPG} = \beta_0 + \beta_1 \cdot \text{Weight} + \beta_2 \cdot \text{Horsepower} + \epsilon \]

  • \(\beta_0\): Intercept — the estimated MPG when both weight and horsepower are 0
  • \(\beta_1\): Slope for weight — expected change in MPG per 1000 lb increase in weight
  • \(\beta_2\): Slope for horsepower — expected change in MPG per additional horsepower
  • \(\epsilon\): Error term — accounts for variability not explained by the model

Relationship between Weight & MPG

ggplot(mtcars, aes(wt, mpg)) + geom_point() + 
  geom_point(color = "darkred") +
  geom_smooth(method = "lm", se = FALSE, color = "black") +
  labs(title = "MPG vs Weight", x = "Weight", y = "Miles Per Gallon")
## `geom_smooth()` using formula = 'y ~ x'

Relationship Between Horsepower & MPG

ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point(color = "darkred") +
  geom_smooth(method = "lm", se = FALSE, color = "black") + 
  labs(title = "MPG vs Horsepower", 
  x = "Horsepower", y = "Miles Per Gallon")
## `geom_smooth()` using formula = 'y ~ x'

3D View: MPG, Weight, Horsepower

plot_ly(data = mtcars,x = ~wt,y = ~hp,z = ~mpg,type = "scatter3d",mode = "markers")
  • Heavier and more powerful cars tend to have lower MPG.
  • Each point is a car; plotted by weight (X), horsepower (Y), and MPG (Z).

Fitting the Regression Model MPG vs Weight

We now fit a linear regression model to predict MPG from both weight and horsepower using the lm() function.

Final Regression Model

After creating the model, it can be expressed with the following equation:

\[ \hat{\text{MPG}} = 37.23 - 3.88 \cdot \text{Weight} - 0.03 \cdot \text{Horsepower} \]

This equation helps us predict a car’s fuel efficiency (MPG) based on its weight and horsepower:

  • For every 1000 lbs of weight added, MPG drops by about 3.88
  • For every 1 unit of horsepower added, MPG drops by about 0.03
  • So, heavier and more powerful cars tend to be less fuel-efficient

Key Takeaways

  • We built a model to predict MPG using a car’s weight and horsepower
  • The model showed that:
    • Heavier cars use more fuel
    • More powerful cars (higher horsepower) also use more fuel
  • Weight had a bigger effect on MPG than horsepower
  • The model worked well — it explained about 83% of the variation in MPG
  • In short: lighter, less powerful cars tend to be more fuel-efficient