What Is Linear Regression?

Linear regression models the relationship between two variables.

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

Where:
- \(\beta_0\) is the intercept
- \(\beta_1\) is the slope
- \(\epsilon\) is the error

MPG vs Weight (ggplot)

Regression Line (ggplot)

MPG vs Weight (plotly)

R Code to Create the Plot

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  geom_smooth(method = "lm")

Model Summary

\[ \hat{y} = 37.29 - 5.34x \]

This means for every extra 1,000 lbs in weight, MPG goes down by about 5.34.

The R-squared value is around 0.75, which means the model explains 75% of the variance in MPG.

Assumptions of Linear Regression

\[ \begin{align*} 1. & \text{ Linearity} \\ 2. & \text{ Independence of errors} \\ 3. & \text{ Constant variance (Homoscedasticity)} \\ 4. & \text{ Normally distributed residuals} \end{align*} \]

Conclusion

  • Weight and MPG have a negative linear relationship
  • linear regression gives a quick way to measure
  • Plotly and ggplot help visualize this

Thank you!