2025-10-20

Chosen Topic: Simple Linear Regression

Delving into the relationship between car weight and fuel efficiency using the built-in mtcars dataset in R.

The dataset itself has the info of 32 car models including their fuel consumption for example.

Key Vars: mpg: miles per gallon & wt: weight in 1000lbs

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

Defining Simple Linear Regression (SLR):

What even is simple linear regression?

  • It is a method to model the relationship between x and y that assumes a linear relationship
  • Basically as x changes -> y changes proportionally

What is simple linear regression used for?

  • Commonly used for prediction and trend analysis, like it will be used for with mtcars.

Mathematical Model for SLR

Simple Linear Regression Model:

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

Where:

  • \(Y_i\): Dependent Var
  • \(Xi\): Independent Var
  • \(\beta_0\): Intercept
  • \(\beta_1\): Slope
  • \(\varepsilon_i\): Error Term

Scatterplot of MPG VS Weight

Fitting the Regression Model

The linear model is fit by using mpg~wt, recall:

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

Example:

## 
## 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

Regression Line Visualization

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

3D Plot: MPG VS Weight VS Horsepower

R Code Summary (Skeleton)

Load the dataset mtcars:

  • data(mtcars)

Fit linear regression model:

  • mod = lm(mpg~wt, data=mtcars)

Scatterplot and regression line w/ ggplot:

  • library(ggplot2)
  • ggplot() + geom_point() + geom_smooth() + labs()

3D plot w/ plotly:

  • library(plotly)
  • p = plot_ly(data, x, y, z, type, marker, layout)