2025-11-09

Introduction

In this presentation, we examine how horsepower (hp) influences fuel efficiency (mpg) in cars. We use the built-in mtcars dataset.

What is Simple Linear Regression?

Simple linear regression relates a predictor \(X\) to a response \(Y\) using the model:

\[ Y = \beta_0 + \beta_1 X + \varepsilon \]

Where:

  • \(Y\): miles per gallon (mpg)
  • \(X\): horsepower (hp)
  • \(\beta_1\): change in mpg for each 1-unit increase in horsepower
  • \(\varepsilon\): random error

Ordinary Least Squares

The goal of regression is to choose \(\beta_0\) and \(\beta_1\) to minimize:

\[ \sum_{i=1}^n (Y_i - \beta_0 - \beta_1 X_i)^2 \]

  • If \(\beta_1 < 0\): mpg decreases as horsepower increases.
  • If \(\beta_1 > 0\): mpg increases as horsepower increases.

Load Data and Packages

library(tidyverse)
library(plotly)
library(broom)

df <- mtcars %>% 
  as_tibble(rownames = "car") %>% 
  select(car, mpg, hp, wt)

Scatter Plot of mpg vs horsepower (ggplot #1)

ggplot(df, aes(x = hp, y = mpg)) +
geom_point(alpha = 0.8, color = "black") +
geom_smooth(method = "lm", se = TRUE, color = "#8C1D40") +
labs(
title = "Fuel Efficiency vs Horsepower",
x = "Horsepower (hp)",
y = "Miles per Gallon (mpg)"
) +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

# Observation: #Cars with higher horsepower tend to have lower mpg. #This suggests a negative relationship.

Fitting the Regression Model (R Code Slide)

fit <- lm(mpg ~ hp, data = df)
tidy(fit)
## # A tibble: 2 × 5
##   term        estimate std.error statistic  p.value
##   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
## 1 (Intercept)  30.1       1.63       18.4  6.64e-18
## 2 hp           -0.0682    0.0101     -6.74 1.79e- 7

##is gives estimates for: ## \(\beta_0\): Intercept
## The predicted mpg when horsepower = 0. ## \(\beta_1\): Slope
##he change in mpg for each 1-unit increase in horsepower. ##*Interpretation:
##f the slope \(\beta_1\) is
negative, then mpg decreases** as horsepower increases.

Plot of Regression Line (ggplot #2)

ggplot(df, aes(x = hp, y = mpg)) +
geom_point(alpha = 0.8) +
geom_smooth(method = "lm", se = FALSE, color = "#8C1D40") +
labs(
title = "Fitted Regression Line",
x = "Horsepower (hp)",
y = "Miles per Gallon (mpg)"
) +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

##his line represents the predicted mpg for any horsepower value.

Model Diagnostics — Residuals vs Fitted

aug <- augment(fit)

ggplot(aug, aes(.fitted, .resid)) +
geom_point(alpha = 0.8) +
geom_hline(yintercept = 0, linetype = 2) +
labs(
title = "Residuals vs Fitted Values",
x = "Predicted mpg",
y = "Residuals"
) +
theme_minimal()

## If points are scattered randomly around 0 → The linear model is reasonable.

3D Visualization (Plotly)

plot_ly(
df, x = ~hp, y = ~wt, z = ~mpg,
type = "scatter3d", mode = "markers", text = ~car
) %>%
layout(
title = "3D Relationship: mpg, horsepower, and weight",
scene = list(
xaxis = list(title = "Horsepower"),
yaxis = list(title = "Weight (1000 lbs)"),
zaxis = list(title = "Miles per Gallon")
)
)

##Use your mouse (click + drag) to rotate.