2025-10-19

What Is Simple Linear Regression?

Simple linear regression helps us predict one variable based on another.
Example: predicting height based on weight.
It fits a straight line through data points that best describes their relationship.

The Math Behind It

We try to find a line that fits the data:

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

where
- \(y\) = dependent variable (what we predict)
- \(x\) = independent variable (what we use to predict)
- \(\beta_0\) = intercept
- \(\beta_1\) = slope
- \(\varepsilon\) = random error

Example Dataset (with Code)

set.seed(123)
x <- 1:20
y <- 3 + 2*x + rnorm(20, 0, 3)
df <- data.frame(x, y)
head(df)

Scatter Plot (ggplot2)

Each point represents one observation of x and y. The black line shows the best-fit regression line. You can see the overall trend of y increasing with x.

Residuals Plot (ggplot2)

Shows how far each observed y is from the predicted y. Points above zero are over-predictions; below zero are under-predictions. Random scatter around zero indicates a good fit.

Interactive 2D Plot (Plotly)

Description 2D Plot (Plotly)

(Not possible to render 3d) Displays the relationship between x and y as interactive points. The upward trend shows that as x increases, y generally increases too — matching the positive slope seen in simple linear regression.

The Math of Estimation

The slope (\(\beta_1\)) and intercept (\(\beta_0\)) are estimated using:

\[ \hat{\beta}_1 = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})} {\sum (x_i - \bar{x})^2} \]

\[ \hat{\beta}_0 = \bar{y} - \hat{\beta}_1 \bar{x} \]

Code Output Summary

Displays the regression model results, including slope, intercept, residuals, and R² value — to summarize how well the line fits the data.

## 
## Call:
## lm(formula = y ~ x, data = df)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -5.964 -1.808 -0.113  1.558  5.201 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   3.9303     1.3860   2.836    0.011 *  
## x             1.9519     0.1157  16.870 1.78e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.984 on 18 degrees of freedom
## Multiple R-squared:  0.9405, Adjusted R-squared:  0.9372 
## F-statistic: 284.6 on 1 and 18 DF,  p-value: 1.778e-12

Conclusion

  • Simple Linear Regression finds the best-fit line for predicting one variable from another.
  • The slope shows how much \(y\) changes when \(x\) changes.
  • Residuals help us check how good the line fits.
  • It’s the foundation for most predictive modeling in statistics.