2025-04-13

Introduction

Simple linear regression is a statistical method used to model the relationship between a dependent variable \(y\) and one independent variable \(x\). We use this method to predict the outcome of \(y\) based on the value of \(x\).

Regression Equation


The linear regression model assumes a relationship of the form:

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

Where: - \(\beta_0\): Intercept
- \(\beta_1\): Slope
- \(\epsilon\): Error term

This equation forms the basis of linear regression.

Example Dataset

We will use the built-in mtcars dataset to explore the relationship between car weight (wt) and miles per gallon (mpg).

head(mtcars[, c("wt", "mpg")])
##                      wt  mpg
## Mazda RX4         2.620 21.0
## Mazda RX4 Wag     2.875 21.0
## Datsun 710        2.320 22.8
## Hornet 4 Drive    3.215 21.4
## Hornet Sportabout 3.440 18.7
## Valiant           3.460 18.1

This provides a quick look at the data we’ll model.

Scatterplot and Regression Line

This plot shows a negative linear relationship between weight and mpg.

Fitting the Model

The lm() function in R is used to fit a linear regression model. Here is a summary of the results:

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

Residual Plot

This plot helps assess the fit and check for violations of regression assumptions:

3D Residual Plot (Plotly)

This 3D plot gives another perspective on how residuals vary across predictor and response variables:

Interpreting the Coefficients

From the fitted model:

\[ \hat{mpg} = \hat{\beta}_0 + \hat{\beta}_1 wt \]

  • The intercept \(\hat{\beta}_0\) represents the expected mpg when weight is 0.
  • The slope \(\hat{\beta}_1\) shows the change in mpg for each unit increase in weight.
  • A negative slope confirms an inverse relationship.

Summary of Findings

  • A strong linear trend exists between weight and mpg.
  • The model explains a large proportion of the variance in mpg.
  • Diagnostic plots support that linear regression is appropriate.
  • We can now predict mpg from weight with reasonable confidence.

Thank You!

Simple linear regression is a powerful tool for prediction and understanding relationships between variables.