Research Question and Learning Goals

Can vehicle weight help predict miles per gallon (MPG)?

  • Visualize the relationship between weight and fuel economy.
  • Explain the least-squares regression model and its coefficients.
  • Calculate a prediction and evaluate limitations of the model.
  • Use ggplot2 and interactive Plotly to explore the data.
Dataset: R’s built-in mtcars data (32 older automobile models). Results are descriptive, not estimates for modern vehicles.

Meet the Data

Vehicle MPG Weight (1,000 lb) Horsepower
Mazda RX4 21.0 2.62 110
Mazda RX4 Wag 21.0 2.88 110
Datsun 710 22.8 2.32 93
Hornet 4 Drive 21.4 3.21 110
Hornet Sportabout 18.7 3.44 175
Valiant 18.1 3.46 105
  • Response (Y): mpg = miles traveled per gallon of fuel.
  • Predictor (X): wt = car weight in thousands of pounds.
  • Additional variable: hp = horsepower, explored in the 3D plot.

First Look: MPG versus Weight

ggplot 1

The Statistical Model

LaTeX 1

Simple linear regression models the average MPG at a given weight:

\[Y_i = \beta_0 + \beta_1 X_i + \varepsilon_i\]

\[\widehat{Y}_i = \widehat{\beta}_0 + \widehat{\beta}_1 X_i\]

  • \(Y_i\): observed MPG; \(X_i\): weight in units of 1,000 lb.
  • \(\beta_0\): intercept; \(\beta_1\): change in mean MPG for an additional 1,000 lb.
  • \(\varepsilon_i\): unexplained difference between observation and population mean.

How Ordinary Least Squares Works

LaTeX 2

Choose the intercept and slope that minimize squared vertical errors:

\[\operatorname{SSE}(b_0,b_1)=\sum_{i=1}^{n}[y_i-(b_0+b_1x_i)]^2\]

\[\widehat{\beta}_1=\frac{\sum_i(x_i-\bar{x})(y_i-\bar{y})}{\sum_i(x_i-\bar{x})^2}\]

\[\widehat{\beta}_0=\bar{y}-\widehat{\beta}_1\bar{x}\]

Intuition: Find the line with the smallest overall sum of squared misses.

R Code: Fit and Predict

Code Slide

This R code generates the regression results and a prediction:

# Built-in data; no CSV files required.
fit <- lm(mpg ~ wt, data = mtcars)
summary(fit)       # Coefficients, R-squared, tests
predict(fit, newdata = data.frame(wt = 3))
This code is executed in the setup chunk for the actual plots and calculations. All results are computed dynamically, not typed in by hand.

Interpret the Regression Line

LaTeX 3

\[\widehat{\mathrm{MPG}} = 37.29 - 5.34\,(\mathrm{Weight\ in\ 1{,}000\ lb})\]

  • Slope: -5.34 MPG per additional 1,000 lb, on average.
  • Intercept: 37.29 MPG at weight 0; outside the data’s meaningful range.
  • \(R^2\): 0.753, meaning about 75.3% of MPG variation within this sample is accounted for by the fitted linear relationship.
  • Interpretation: Association does not by itself prove causation.

Worked Example: A 3,000-lb Car

LaTeX 4

Since weight is measured in thousands of pounds, use \(x=3\):

\[\widehat{\mathrm{MPG}}=\widehat{\beta}_0+\widehat{\beta}_1(3)\]

\[\widehat{\mathrm{MPG}}=37.29 - 5.34(3)\]

\[\boxed{\widehat{\mathrm{MPG}}\approx 21.25}\]

A predicted value is not a guaranteed MPG for an individual vehicle.

Model Fit: What Does R-Squared Mean?

LaTeX 5

\[R^2=1-\frac{\sum_i(y_i-\widehat{y}_i)^2}{\sum_i(y_i-\bar{y})^2}\]

  • The fitted model’s \(R^2\) is 0.753.
  • This measures how much of the sample’s observed variability is explained by the fitted relationship.
  • A larger \(R^2\) alone does not validate assumptions or show that predictions generalize.

Check Model Errors

ggplot 2

Another View: Distribution of MPG

ggplot 3

The sample has a broad range of fuel efficiencies.

Explore Interactively

Plotly

This interactive chart shows MPG versus weight. Hover over any point to see the vehicle’s name, MPG, weight, and horsepower; color also represents horsepower.

Interactive Plotly is included as required. The SVG scatter plot avoids WebGL-dependent blank 3D output; three variables remain visible through position, color, and hover labels.

Important Assumptions and Limitations

  • Linearity: The conditional mean relationship should be approximately linear.
  • Independence: Observations/errors should be independent for standard inference.
  • Constant error spread: Residual variation should be reasonably consistent.
  • Normal errors: Relevant primarily to small-sample inference, not fitting the line.
  • Scope: Data from older cars; modern vehicles may follow different patterns.
A residual plot is a diagnostic, not proof that every assumption holds. Use predictions cautiously, especially outside the observed weight range (1.51–5.42 thousand lb).

Conclusions and Source

  • Among these 32 cars, heavier weight is associated with lower MPG.
  • Linear regression summarizes that relationship with an interpretable equation.
  • For a 3,000-lb car, the estimated MPG is 21.25.
  • The residual plot and interactive Plotly chart reveal why data exploration matters.
  • Bottom line: Useful predictions require thoughtful interpretation and awareness of limitations.

Data source: R’s built-in mtcars dataset (?mtcars).
Tools: R, ggplot2, Plotly.js, jsonlite, and knitr. All plots and calculations are generated in this file.