2025-09-17

Dataset Overview (mtcars)

We use the built-in mtcars data set.

data(mtcars)
head(mtcars[, c("mpg", "wt")], 6)
##                    mpg    wt
## Mazda RX4         21.0 2.620
## Mazda RX4 Wag     21.0 2.875
## Datsun 710        22.8 2.320
## Hornet 4 Drive    21.4 3.215
## Hornet Sportabout 18.7 3.440
## Valiant           18.1 3.460

The Simple Linear Regression Model

We model fuel efficiency (miles per gallon) as a linear function of vehicle weight:

\[ mpg_i = \beta_0 + \beta_1 \, wt_i + \varepsilon_i \]

  • \(mpg_i\): miles per gallon of car \(i\)
  • \(wt_i\): weight of car \(i\) (in 1000 lbs)
  • \(\beta_0\): intercept (baseline mpg when \(wt = 0\))
  • \(\beta_1\): slope (change in mpg per extra 1000 lbs)
  • \(\varepsilon_i\): random error term

Hypothesis Test: Does Weight Affect MPG?

We want to know if car weight has an effect on fuel efficiency.

Hypotheses \[ H_0:\ \beta_1 = 0 \quad \text{(weight has no effect)} \] \[ H_a:\ \beta_1 \neq 0 \quad \text{(weight does affect MPG)} \]

Test idea - Compute a test statistic from the data
- Get a p-value
- If p-value < 0.05 → reject \(H_0\) and conclude that weight matters

MPG vs Weight

Scatterplot with regression line:

Residuals vs Fitted

3D View: MPG, Weight, Horsepower

R Code for 3D Plot

library(plotly)

plot_ly(
  mtcars,
  x = ~wt,
  y = ~hp,
  z = ~mpg,
  type = "scatter3d",
  mode = "markers"
)

Conclusion

  • Vehicle weight has a clear negative effect on fuel efficiency (MPG).
  • The regression line shows that heavier cars get fewer miles per gallon.
  • The effect is statistically significant (small p-value).
  • Diagnostics suggest the model is reasonable, but other variables (like horsepower) also play a role.

Takeaway: Simple linear regression is a powerful way to see and measure relationships between variables.