Simple linear regression is a method used to model the relationship between two variables (x and y) by fitting a straight line to the data.
2025-06-08
Simple linear regression is a method used to model the relationship between two variables (x and y) by fitting a straight line to the data.
It helps predict the value of one variable based on another. It’s commonly used in statistics and machine learning.
This is the basic formula used in linear regression.
The equation:
\[ y = \beta_0 + \beta_1 x + \epsilon \]
Where \(\beta_0\) is the intercept, \(\beta_1\) is the slope, and \(\epsilon\) is the error term.
Here is some mock height and weight data.
set.seed(1) height <- rnorm(100, mean = 65, sd = 3) weight <- 110 + 5 * height + rnorm(100, 0, 10) df <- data.frame(height, weight)
This plot shows the linear relationship between height and weight.
ggplot(df, aes(x = height, y = weight)) + geom_point() + geom_smooth(method = "lm", se = FALSE, color = "blue") + labs(title = "Height vs Weight", x = "Height (inches)", y = "Weight (lbs)")
## `geom_smooth()` using formula = 'y ~ x'
Here is the same plotting code shown as R code:
ggplot(df, aes(x = height, y = weight)) + geom_point() + geom_smooth(method = "lm", se = FALSE, color = "blue")
Here is a 3D plot using Plotly.
z <- height + weight + rnorm(100, 0, 5) plot_ly(x = ~height, y = ~weight, z = ~z, type = "scatter3d", mode = "markers")
Residuals show how far off the model’s predictions are from actual data.
model <- lm(weight ~ height, data = df) df$residuals <- residuals(model) ggplot(df, aes(x = height, y = residuals)) + geom_point() + geom_hline(yintercept = 0, linetype = "dashed") + labs(title = "Residual Plot", y = "Residual", x = "Height")
These are the formulas used to find the best fit line.
Slope:
\[ \beta_1 = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sum (x_i - \bar{x})^2} \]
Intercept:
\[ \beta_0 = \bar{y} - \beta_1 \bar{x} \]
Linear regression is a simple but powerful tool. It allows us to understand and predict relationships between variables in fields such as health, economics, and science.