2025-06-08

Introduction

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.

What is Linear Regression?

It helps predict the value of one variable based on another. It’s commonly used in statistics and machine learning.

Regression Equation

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.

Example Data

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)

Scatter Plot with Line of Best Fit

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'

Code Used to Create the Plot

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")

Interactive 3D Plot

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")

Residual Plot

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")

Least Squares

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} \]

Conclusion

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.