2025-10-17

Simple Linear Regression

Simple Linear Regression is used to estimate the relationship between two quantitative variables.
Regression models describe the relationship between the two variables by fitting a line to the data. Linear models use a straight line.

What simple linear regression can provide

  • How strong the relationship is between two variables (e.g., the relationship between rainfall and soil erosion).
  • The value of the dependent variable at a certain value of the independent variable (e.g., the amount of soil erosion at a certain level of rainfall).

A few assumptions…

Simple linear regression requires making a few assumptions about your data, such as:

  • the size of the error in the prediction doesn’t change significantly.
  • there are no hidden relationships among data observations
  • the data follows a normal distribution
  • the relationship between the variables is linear, IE: no odd grouping or exponential data

Linear Regression (In a plane)

Here we have 3 data points. Miles per gallon, Engine Displacement and Vehicle weight. The linear regression plane is created by taking the relationship of (Engine displacement + Vehicle weight) and comparing the duo to overall MPG.

Linear regression as a mathmatical model

This relationship from the previous slide can be mathmatically shown using the equation: \(Y=b_0 +b_1X_1 +b_2X_2\) where \(Y\) = MPG, \(X_1\)=engine Displacement and \(X_2\)= Vehicle Weight. \(b_0\) represents the y intercept, and \(b_n\) is change in Y for one-unit change in \(X_n\).

\(b_n\) is also know as a regression coefficient for some \(X_n\)

Code for plot generation

The code below is used to generate the chart in the following slide. Its pulling data from the mtcars dataset. Note that the standard error has been removed (se=F) and we’re using a simple formula to generate the linear regression line (formula = y~x). Mathmatically this formula can be represented as \(Y=a+bx\) where \(Y\) is Horsepower(Predicted value), \(a\) is the y-intercept, \(b\) is the slope, \(x\) is Engine Displacement(Independent Variable).

ggplot(mtcars, aes(x=disp, y = hp))+geom_point()+
  labs(
    title = "Displacement vs Horsepower using simple linear regression",
    x = 'Engine Displacement (Cubic inches)',
    y = 'Horsepower'
  ) +stat_smooth(method = 'lm', se=F, formula = y~x, color='blue')

Displacement Vs Horsepower

As opposed to slide 4 showing a linear regression plane, here we have a linear regression line. This line is showing the relationship between engine displacement and horsepower produced by said engine. The confidence interval has been turned off for this chart.

Old Faithful Wait Times Vs Eruptions

The code used to generate this plot is similar to the previous slide. I use data from the Faithful dataset. The main difference is the inclusion of the standard error (shaded region). Standard error is calculated by the formula:

\(\sqrt{\frac{\sum (y_i - \hat{y}_i)^2}{n - 2}}\)