Introduction

  • Goal: To understand how car weight affects fuel efficiency (MPG)
  • Method: Simple Linear Regression (SLR) using the mtcars dataset
  • Tools used: R, ggplot2, and plotly

Model Overview

We represent the relationship as: \[ Y_i = \beta_0 + \beta_1 X_i + \varepsilon_i,\quad i=1,2,\dots,n. \] Here, \(Y\) = Miles per Gallon (MPG), \(X\) = Weight of car in 1000 lbs.

Hypothesis Testing

To test if a linear relationship exists: \[ H_0: \beta_1 = 0 \quad \text{vs.} \quad H_a: \beta_1 \neq 0 \] If we reject \(H_0\), we conclude that car weight significantly influences MPG.

Scatterplot with Regression Line

ggplot(df, aes(Weight_1000lb, MilesPerGallon)) +
  geom_point() +
  geom_smooth(method = "lm", se = TRUE) +
  labs(x = "Weight (1000 lb)", y = "MPG", title = "Fuel Efficiency vs. Weight")

Residuals vs Fitted

ggplot(aug, aes(.fitted, .resid)) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  geom_point() +
  labs(x = "Fitted MPG", y = "Residuals", title = "Residuals vs Fitted Values")

Normal Q-Q Plot

ggplot(aug, aes(sample = .resid)) +
  stat_qq() + stat_qq_line() +
  labs(x = "Theoretical Quantiles", y = "Sample Quantiles", title = "Q-Q Plot of Residuals")

3D Interactive Plot

plot_ly(df,
        x = ~Weight_1000lb,
        y = ~Horsepower,
        z = ~MilesPerGallon,
        type = "scatter3d",
        mode = "markers")

Limitations and Observations

  • Relationship appears strong but is based on a small dataset (n=32)
  • Other variables like horsepower also affect MPG
  • The model assumes linearity and constant variance
  • Real-world data may show non-linear behavior

Key Takeaways

  • Heavier cars have lower fuel efficiency
  • The regression line shows a clear negative trend
  • Diagnostics confirm a decent linear fit for basic interpretation