2026-06-06

What is Linear Regression?

Linear regression is a statistical method that helps us understand how one variable is related to another variable.

In this presentation, I use car data to study how car weight affects miles per gallon.

  • x = car weight
  • y = miles per gallon

Research Question

Can the weight of a car help predict its miles per gallon?

I use the built-in R dataset called mtcars.

head(mtcars)
##                    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

Regression Model

The general simple linear regression model is:

\[ y = \beta_0 + \beta_1x + \epsilon \]

This model uses one predictor variable to estimate one response variable.

Model for This Example

For this example, the formula becomes:

\[ mpg = \beta_0 + \beta_1(wt) + \epsilon \]

  • mpg = miles per gallon
  • wt = car weight
  • ε = random error

Creating the Regression Model in R

model <- lm(mpg ~ wt, data = mtcars)
summary(model)
## 
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.5432 -2.3647 -0.1252  1.4096  6.8727 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
## wt           -5.3445     0.5591  -9.559 1.29e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.046 on 30 degrees of freedom
## Multiple R-squared:  0.7528, Adjusted R-squared:  0.7446 
## F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10

The function lm() creates a linear model. Here, I am predicting mpg using wt.

Scatter Plot

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  labs(title = "Car Weight and MPG",
       x = "Car Weight",
       y = "Miles Per Gallon")

This plot shows the relationship between car weight and miles per gallon.

Regression Line Plot

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  geom_smooth(method = "lm") +
  labs(title = "Regression Line for MPG",
       x = "Car Weight",
       y = "Miles Per Gallon")
## `geom_smooth()` using formula = 'y ~ x'

The line helps show the overall trend in the data.

Interactive Plot

p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  geom_smooth(method = "lm") +
  labs(title = "Interactive MPG Prediction Plot",
       x = "Car Weight",
       y = "Miles Per Gallon")

ggplotly(p)
## `geom_smooth()` using formula = 'y ~ x'

This plot becomes interactive in the HTML version.

Interpretation

The graph shows a downward trend.

This means that as car weight increases, miles per gallon usually decreases.

Conclusion

In this example, car weight and MPG move in opposite directions.

Simple linear regression is useful because it helps us study and predict relationships between variables.