2026-06-07

Simple Linear Regression

  • Simple linear regression is used to study the relationship between two variables.
  • It uses one variable to predict another variable.
  • In this example, we will look at car weight and miles per gallon.

What is Linear Regression?

Linear regression finds the best fitting straight line through a set of data points.

  • Predictor variable: car weight
  • Response variable: miles per gallon
  • Goal: see how weight affects fuel efficiency

Regression Equation

\[ y = \beta_0 + \beta_1x + \epsilon \]

where:

  • \(y\) is the response variable
  • \(x\) is the predictor variable
  • \(\beta_0\) is the intercept
  • \(\beta_1\) is the slope
  • \(\epsilon\) is the error term

Least Squares Method

\[ \sum_{i=1}^{n}(y_i - \hat{y}_i)^2 \]

The regression line is chosen by minimizing the sum of squared differences between the actual values and predicted values.

MPG vs Weight

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

Distribution of MPG

Interactive Plotly Plot

R Code Example

model <- lm(mpg ~ wt, data = mtcars)
summary(model)

This code creates a linear regression model using car weight to predict miles per gallon.

Conclusion

  • Simple linear regression is useful for studying relationships between two variables.
  • In this example, car weight is used to predict miles per gallon.
  • The plots show that heavier cars generally have worse fuel efficiency.
  • Regression can be used in many fields for prediction and analysis.