2026-10-19

The Question

Do cars with bigger engines get fewer miles per gallon?

  • Dataset: mtcars, which has 32 cars
  • disp: engine displacement (cubic inches)
  • mpg: miles per gallon

The Statistical Idea

We use simple linear regression to model mpg using disp.

  • Response variable: mpg
  • Predictor variable: disp
  • We fit a line through the data

The Mathematics: The Model

\[ \text{mpg} = \beta_0 + \beta_1 \cdot \text{disp} + \varepsilon, \quad \varepsilon \sim N(0, \sigma^2) \]

  • \(\beta_0\): intercept
  • \(\beta_1\): slope
  • \(\varepsilon\): random error

The Mathematics: The Fitted Line

The fitted model using our data is:

\[ \text{mpg} = \hat{\beta}_0 + \hat{\beta}_1 \cdot \text{disp} \]

The hats mean these are estimates from the data.

R Code: Fitting the Model

mod <- lm(mpg ~ disp, data = mtcars)
summary(mod)
## 
## Call:
## lm(formula = mpg ~ disp, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.8922 -2.2022 -0.9631  1.6272  7.2305 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 29.599855   1.229720  24.070  < 2e-16 ***
## disp        -0.041215   0.004712  -8.747 9.38e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.251 on 30 degrees of freedom
## Multiple R-squared:  0.7183, Adjusted R-squared:  0.709 
## F-statistic: 76.51 on 1 and 30 DF,  p-value: 9.38e-10

Visualization 1: Scatter Plot with Fitted Line

The line slopes downward, so cars with larger engines tend to have lower mpg.

Visualization 2: Colored by Cylinders

The 4-cylinder cars have small engines and high mpg. The 8-cylinder cars have large engines and low mpg.

Interactive 3D Plot

What Did We Learn?

  • Cars with bigger engines tend to get fewer miles per gallon
  • The fitted line slopes downward, so mpg goes down as displacement goes up
  • The 4-cylinder cars have small engines and high mpg, and the 8-cylinder cars have large engines and low mpg
  • The R-squared is 0.718, so the line fits the data well