2025-02-09

Simple Linear Regression: Basic Introduction

What is a Simple Linear Regression?

  • A Simple Linear Regression models the relationship between one independent variable (x) and one dependent variable (y).

    Define Simple Regression model as:

\[ y = \beta_0 + \beta_1 x + \epsilon \]

  • I will use the mtcars dataset to predict mpg (miles per gallon) based on wt (car weight).

Dataset (mtcars)

  • Table Preview:
library(ggplot2)
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
  • mpg (miles per gallon) is the dependent variable and wt (weight) as the independent.

Scatter Plot using (ggplot2)

  • Visualize mpg vs wt:
  • As weight increases, fuel efficiency (mpg) decreases.
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + 
  labs(title = "Miles Per Gallon vs. Weight", 
x = "Car Weight (1000 lbs)", y = "Miles Per Gallon")

Linear Regression Model on the Scatterplot

ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() +
  geom_smooth(method="lm", se=FALSE, color="Orange") +
  labs(title = "Miles Per Gallon vs. Weight", 
x = "Car Weight (1000 lbs)", y = "Miles Per Gallon")

Regression Model

  • This model shows how mpg decreases as wt increases.
  • The R-squared measures how well wt explains mpg; a higher R-squared (closer to 1) means a better fit.
  • Adjusted R-squared accounts for predictors and decreases if extra variables do not improve the model.
Key Regression Statistics
Metric Value
R-squared 0.7528
Adjusted R-squared 0.7446
Residual Std. Error 3.0459

Interactive Regression Plot

library(ggplot2)
library(plotly)
g = ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() +
  geom_smooth(method="lm", se=FALSE, color="Orange") +
  labs(title = "Interactive Regression Plot",
       x = "Car Weight (1000 lbs)", y = "Miles Per Gallon")
ggplotly(g)

Equation and Prediction

  • Final Regression Equation

    \[ \hat{mpg} = 37.29 - 5.34 \cdot wt \]

  • Example: If a car weighs 3.5 (1000lbs). The predicted mpg is 37.29 - 5.34(3.5) = 18.6 mpg