- Simple linear regression is a statistical method used to model the relationship and correlation between a single independent variable \(x\) and a continuous dependent variable \(y\).
- It can be used in predicting outcomes and estimating values.
2024-03-17
\[ \hat{y} = \beta_0 + \beta_1 x + \varepsilon \]
Residuals are calculated as: \[ y - \hat{y} \]
The coefficient of determination (\(R^2\)) determines how well the linear regression model predicts the outcome and is calculated as:
\[ R^2 = \frac{\text{Explained variance}}{\text{Total variance}} = 1 - \frac{\text{Residual variance}}{\text{Total variance}} \]
# Uses a built-in dataset of RStudio
library(ggplot2)
# Scatter plot with linear regression line
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() + # Data points
geom_smooth(method = "lm", se = FALSE, color = "darkgreen") + # Linear regression line
labs(x = "Horsepower", y = "Miles per Gallon",
title = "Linear Regression: Miles per Gallon vs. Horsepower")
theme_minimal()