2025-06-07

What is a Simple Linear Regression?

  • A simple linear regression is a model that demonstrates the relationship between the two relationships, \(X\) and \(Y\)
  • \(X\) is the independent variable
  • \(Y\) is the dependent variable
  • There can be more than one independent variable
  • The goal is to find the best fitted line between the two relationships

Formula

  • The simple linear regression formula can be written as:

\[ Y = \beta_0 + \beta_1X + \epsilon \]

  • \(Y\) is the predicted value of the dependent variable
  • \(X\) is the value of the independent variable
  • \(\beta_0\) is the y-intercept
  • \(\beta_1\) b is the slope
  • \(\epsilon\) is the error term

Example

  • We will be using the mtcars dataset to explore the relationship between displacement (independent variable) and mpg (dependent/predictor variable)
##                    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

ggplot Example 1: Scatter Plot

ggplot Example 2: With Regression Line

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

plotly Example: 3D Plot

R Code Example: Model Estimation Part 1

## 
## Call:
## lm(formula = mpg ~ disp, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.8922 -2.2022 -0.9631  1.6272  7.2305 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 29.599855   1.229720  24.070  < 2e-16 ***
## disp        -0.041215   0.004712  -8.747 9.38e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.251 on 30 degrees of freedom
## Multiple R-squared:  0.7183, Adjusted R-squared:  0.709 
## F-statistic: 76.51 on 1 and 30 DF,  p-value: 9.38e-10

R Code Example: Model Estimation Part 2

## (Intercept)        disp 
## 29.59985476 -0.04121512
##        1        2        3 
## 23.41759 19.29607 15.17456

Interpretation of Results

  • Estimated Regression Equation:

\[ \widehat{MPG} = 29.6 - 0.041 * Displacement \]

  • The slope is -0.041, meaning that for each cubic inch increase in displacement, MPG decreases by this slope
  • The intercept is 29.6
  • \(R^2\) is 72% of MPG variability
  • p-value has a highly notable relationship