2026-09-16

What is Simple Linear Regression?

  • a statistical method used to predict the relationship between 2 continuous variables
  • Goal is to find the best fitted line for a set of data points

Independent Variable (predictor) : x

Dependent Variable (outcome) : y

What is the best fitted line?

\(\hat{y_{i}}=b_{0}+b_{1}x_{i}\)

  • Simple Linear Regression calculates this equation with b0 being the y-intercept and b1 being the slope of the line.
  • This line is calculated so that it minimizes the error for all the data points

Calculating Simple Linear Regression in R

  • use the built in function lm(x ~ y, data = dataset)
  • save the result into an object
  • example : using mtcars built in data set, we can use linear regression to predict the cars mpg based on the horsepower

MPG vs Horsepower scatter plot :

Calculating the Best Fitting Line :

cars_example = lm(mpg ~ hp, data = mtcars)

cars_example now contains the following components :

  • coefficients (intercept, slopes)
  • residuals (differences between observed values and predicted values)
  • fitted.values (predicted values calculated)
  • residual (residual degrees of freedom)
  • call (exact formula used to run the model)

Plotting the Best Fitting Line

Use the coefficients of the lm() component to plot the line.

g = g + geom_abline(
  intercept = cars_example$coefficients[1], 
  slope = cars_example$coefficients[2])

Adding it to the plot

Interactive Version