Ordinary Least Squares (OLS) regression is a method for estimating the parameters in a linear regression model. This document explains OLS regression first using algebra and then using matrix notation, with a practical implementation in R.
Algebraic Approach to OLS
In simple linear regression, we model the relationship between a predictor variable \(X\) and a response variable \(y\) using the equation:
\[ y_i = \beta_0 + \beta_1 X_i + \epsilon_i \]
where: - \(y_i\) is the response variable for observation \(i\), - \(X_i\) is the predictor variable for observation \(i\), - \(\beta_0\) is the intercept, - \(\beta_1\) is the slope, - \(\epsilon_i\) is the error term.
Goal: Minimize the Sum of Squared Errors
The OLS method aims to find the values of \(\beta_0\) and \(\beta_1\) that minimize the sum of squared errors (SSE):
\[SSE = \sum_{i=1}^{n} (y_i - \hat{y}_i)^2\]
where \(\hat{y}_i = \beta_0 + \beta_1 X_i\) is the predicted value of \(y_i\).
Deriving the Coefficients
To minimize the SSE, we take the partial derivatives of the SSE with respect to \(\beta_0\) and \(\beta_1\) and set them to zero:
where \(\bar{X}\) and \(\bar{y}\) are the means of \(X\) and \(y\), respectively.
Matrix Approach to OLS
The algebraic method works well for simple linear regression, but for multiple regression, the matrix approach is more convenient. In matrix notation, the linear model is:
We’ll use a small dataset with one predictor (X) and one response variable (y).
Code
# Create a simple datasetset.seed(123) # For reproducibilityX <-matrix(rnorm(10), ncol=1) # 10 random predictorsy <-3+2* X +rnorm(10) # Response variable with some noise# Display the datadata.frame(X = X, y = y)
To verify our matrix-based implementation, we can compare the results with the lm function in R.
Code
# Verify with lm functionmodel <-lm(y ~ X)summary(model)
Call:
lm(formula = y ~ X)
Residuals:
Min 1Q Median 3Q Max
-1.33303 -0.64421 -0.02448 0.49596 1.41472
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 3.1617 0.2852 11.086 3.91e-06 ***
X 2.6287 0.3141 8.368 3.15e-05 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.8988 on 8 degrees of freedom
Multiple R-squared: 0.8975, Adjusted R-squared: 0.8847
F-statistic: 70.03 on 1 and 8 DF, p-value: 3.153e-05
Conclusion
In this document, we explained OLS regression using both algebra and matrix notation. We demonstrated how to derive the OLS estimates and implemented the matrix-based method in R. This approach helps build a strong understanding of the underlying mechanics of linear regression.