2025-11-12

Slide 1: Introduction

Welcome to this presentation on Simple Linear Regression — a fundamental statistical technique for understanding the relationship between two quantitative variables.

We will use the mtcars dataset to explore the link between horsepower (hp) and miles per gallon (mpg).

Slide 2: The Regression Model

The simple linear regression model assumes a straight-line relationship between \(Y\) and \(X\):

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

where
\(\beta_0\) = intercept, \(\beta_1\) = slope, \(\varepsilon_i\) = random error.

Slide 3: The Data

We’ll inspect the variables of interest from the built-in mtcars dataset.

summary(mtcars[, c("mpg", "hp", "wt")])
##       mpg              hp              wt       
##  Min.   :10.40   Min.   : 52.0   Min.   :1.513  
##  1st Qu.:15.43   1st Qu.: 96.5   1st Qu.:2.581  
##  Median :19.20   Median :123.0   Median :3.325  
##  Mean   :20.09   Mean   :146.7   Mean   :3.217  
##  3rd Qu.:22.80   3rd Qu.:180.0   3rd Qu.:3.610  
##  Max.   :33.90   Max.   :335.0   Max.   :5.424

library(ggplot2) library(plotly)

Slide 4:Scatterplot (ggplot #1)

ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point(color = "darkred", size = 2) +
labs(title = "MPG vs Horsepower",
x = "Horsepower", y = "Miles per Gallon") +
theme_minimal()

Slide 5 :Fit model

model <- lm(mpg ~ hp, data = mtcars)
summary(model)
## 
## Call:
## lm(formula = mpg ~ hp, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.7121 -2.1122 -0.8854  1.5819  8.2360 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 30.09886    1.63392  18.421  < 2e-16 ***
## hp          -0.06823    0.01012  -6.742 1.79e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.863 on 30 degrees of freedom
## Multiple R-squared:  0.6024, Adjusted R-squared:  0.5892 
## F-statistic: 45.46 on 1 and 30 DF,  p-value: 1.788e-07

Slide 6 : Regression line (ggplot #2)

ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point(color = "steelblue") +
geom_smooth(method = "lm", se = FALSE, color = "red") +
labs(title = "Regression Line for MPG vs Horsepower",
x = "Horsepower", y = "Miles per Gallon") +
theme_classic()
## `geom_smooth()` using formula = 'y ~ x'

Slide 7: 3D plot

plot_ly(mtcars, x = ~hp, y = ~mpg, z = ~wt,
type = "scatter3d", mode = "markers",
marker = list(size = 5, color = ~mpg, colorscale = "Viridis")) %>%
layout(scene = list(
xaxis = list(title = "Horsepower"),
yaxis = list(title = "MPG"),
zaxis = list(title = "Weight")))

##Slide 8: Diagnostics

par(mfrow = c(2,2))
plot(model)