2025-06-03

What is Simple Linear Regression

Simple Linear Regression is used to model linear relationships between two variables, independent (always on x-axis) and dependent (always on y-axis).

It is used in almost any field, typically showing if two variables are correlated with each other (remember: correlation does not equal causation).

Formula

Simple linear regression is modeled as any other linear equation, or:

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

Where:

  • \(y\) is the dependent variable
  • \(x\) is the independent variable
  • \(\beta_0\) is the intercept
  • \(\beta_1\) is the slope
  • \(\varepsilon\) is the error term

Residuals

Residuals are the differences between observed values and the values predicted by the regression model.

They can be expressed with the following equation:

\[ e_i = y_i - \hat{y}_i = y_i - (\hat{\beta}_0 + \hat{\beta}_1 x_i) \]

These residuals can also be seen as the difference from the expected and actual y values. The closer each residual value is to the line typically means that the two variables are more likely to be correlated. With that, residuals are a great way to show validity of specific models such as linear regression.

ggplot: Scatterplot with Regression Line

We will be using the mtcars dataset for the following graphs.

ggplot2: Residual Plot

plotly: 3D Visualization

R Code: Residuals

This is the code used to create the plot for residuals

model <- lm(mpg ~ hp, data = mtcars) residuals_df <- data.frame(hp = mtcars$hp, residuals = resid(model))

rplot <- ggplot(residuals_df, aes(x = hp, y = residuals)) + geom_point(color = “darkseagreen”) + geom_hline(yintercept = 0, linetype = “dashed”) + labs(title = “Residual Plot”, x = “Horsepower”, y = “Residuals”)

print(rplot)

Summary

  • linear regression models can be helpful with looking at linear correlation
  • if two variables do not have linear correlation, that does not automatically mean they are not correlated (they could be exponentially correlated for example)
  • once again, correlation does not mean causation! Even if two things have a linear correlation that does not mean one has specifically caused the other