OLS Regression
The foundation of Predictive Discovery. Ordinary Least Squares (OLS) regression audits the linear synergy between multiple predictors and a continuous outcome, revealing the mathematical blueprint of influence.
What is it?
Ordinary Least Squares (OLS) Regression models the straight-line relationship between a continuous independent variable (X) and a continuous dependent outcome (Y) by minimizing the sum of squared vertical residuals.
When to use it
- Continuous Scaling: Both predictor and outcome variables scale numerically.
- Direct Association: Assess size and significance of straight-line trends.
- Prediction Goal: Build line equations to forecast future outcome points.
Core Idea
It draws a regression line through the coordinate points. The residuals (vertical error bars) are squared and summed. The optimal OLS line is the unique boundary that makes this sum of squares as small as mathematically possible:
Hypotheses
How it works
- Calculate covariance of X and Y, and variance of X.
- Estimate slope: beta1 = Cov(X,Y)/Var(X).
- Estimate intercept: beta0 = Mean(Y) - beta1 * Mean(X).
- Evaluate model variance against residual noise to compute R-squared.
Assumptions
Important Metric
💡 R-squared (R2): The proportion of the outcome variance explained by the model. An R2 of 0.64 means that 64% of the variations in Y are accounted for by the straight-line regression on X.
Quick Example
| Source | df | SS | MS |
|---|---|---|---|
| Regression | 1 | 240.2 | 240.2 |
| Residuals | 23 | 110.5 | 4.8 |
OLS Regression Live Laboratory
Manipulate slope, intercept, and noise to see the OLS fit line and residuals update in real-time.
| Parameter | True Value | OLS Estimate |
|---|---|---|
| Intercept (b0) | 50.00 | 48.1604 |
| Slope (b1) | 1.00 | 1.2697 |
| R-squared (R2) | - | 0.6218 |
Hypotheses
Pragmatic null and alternative hypotheses defined in mathematical notation.
H₀: β₁ = 0 (predictor has no linear effect on outcome)
Hₐ: β₁ ≠ 0 (predictor has linear effect)
For individual predictors. Overall model: F-test for H₀: all βⱼ = 0 (except intercept).
Assumptions
The core mathematical criteria needed to ensure that statistical testing remains unbiased and valid.
Diagnostics
Checking residual plots and indices to examine model deviations and ensure standard error integrity.
- Residual vs. fitted values plot (checks linearity, homoscedasticity)
- Q-Q plot of residuals (checks normality)
- Scale-location plot (checks homoscedasticity)
- Residuals vs. leverage plot (identifies influential outliers)
- VIF for each predictor (checks multicollinearity)
- Cook's distance plot (identifies influential cases)
- Durbin-Watson test for autocorrelation (if time series or panel data)
- Breusch-Pagan or White test for heteroscedasticity
- RESET test for functional form misspecification
- Partial regression plots (added variable plots)
- Component-residual plots (CERES plots) for non-linearity
Applied Minds
Review concrete study examples, data layout guidelines, and copy executable syntax scripts.
Academic Achievement Prediction (IQ + SES → Grades)
Research question: Do IQ and socioeconomic status predict academic achievement? Design: Cross-sectional observational study (N=120 high school students). Outcome: GPA (0-4.0). Predictors: IQ score (continuous, 80-140), SES composite score (continuous, 1-10, higher = higher SES). Goal: quantify relationships and predict academic performance.
# Multiple Linear Regression: Academic Achievement Prediction
# IQ + SES → GPA
# Based on meta-analyses showing IQ r=.50, SES r=.30 with achievement
library(car) # For VIF, diagnostic plots
library(ggplot2) # Visualization
library(dplyr) # Data manipulation
library(lmtest) # Diagnostic tests
# Simulate realistic data (or load: data <- read.csv("achievement.csv"))
set.seed(2025)
n <- 120
data <- data.frame(
IQ = rnorm(n, 100, 15),
SES = rnorm(n, 5.5, 2.0)
)
# GPA = 0.5 + 0.025*IQ + 0.15*SES + error
# Effect: 10-point IQ increase → 0.25 GPA increase
# 1-point SES increase → 0.15 GPA increase
data$GPA <- 0.5 + 0.025*data$IQ + 0.15*data$SES + rnorm(n, 0, 0.35)
data$GPA <- pmax(0, pmin(4.0, data$GPA)) # Constrain to 0-4 range
# === STEP 1: Descriptive Statistics ===
summary(data)
cor(data) # Correlation matrix
# Scatterplot matrix
pairs(data, main="Scatterplot Matrix: GPA, IQ, SES")
# === STEP 2: Fit Multiple Regression Model ===
model <- lm(GPA ~ IQ + SES, data=data)
summary(model)
# Output interpretation:
# Call: lm(formula = GPA ~ IQ + SES, data = data)
#
# Coefficients:
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 0.5234 0.3214 1.628 0.1062
# IQ 0.0247 0.0022 11.227 < 2e-16 ***
# SES 0.1523 0.0162 9.401 < 2e-16 ***
# ---
# Residual standard error: 0.345 on 117 df
# Multiple R-squared: 0.623, Adjusted R-squared: 0.617
# F-statistic: 96.6 on 2 and 117 DF, p-value: < 2.2e-16
# Interpretation:
# - IQ: β=0.0247, p<.001. For each 1-point IQ increase, GPA increases by 0.025 (controlling for SES)
# 10-point IQ increase → 0.25 GPA increase
# - SES: β=0.1523, p<.001. For each 1-point SES increase, GPA increases by 0.15 (controlling for IQ)
# - R²=.623: IQ and SES together explain 62.3% of GPA variance
# - Overall model: F(2,117)=96.6, p<.001 (highly significant)
# === STEP 3: Check Assumptions ===
# 1. Linearity & Homoscedasticity: Residuals vs. Fitted
par(mfrow=c(2,2))
plot(model)
par(mfrow=c(1,1))
# Plot 1: Residuals vs Fitted - should show random scatter (no pattern)
# Plot 2: Q-Q plot - points should fall on line (normality)
# Plot 3: Scale-Location - horizontal line (homoscedasticity)
# Plot 4: Residuals vs Leverage - identifies influential outliers
# 2. Multicollinearity: VIF
vif(model)
# Result: IQ VIF=1.05, SES VIF=1.05 (both <5, excellent - no multicollinearity)
cat("VIF values:\n")
print(vif(model))
cat("All VIF < 5: No multicollinearity detected\n")
# 3. Normality of residuals: Shapiro-Wilk
shapiro.test(residuals(model))
# p > .05: normality assumption met
# 4. Homoscedasticity: Breusch-Pagan test
library(lmtest)
bptest(model)
# p > .05: homoscedasticity assumption met
# 5. Influential outliers: Cook's distance
cooks_d <- cooks.distance(model)
plot(cooks_d, type="h", main="Cook's Distance", ylab="Cook's D")
abline(h=4/n, col="red", lty=2) # Threshold: 4/n
cat("\nInfluential cases(Cook's D > 4/n):", sum(cooks_d > 4/n), "\n")
# Result: No influential outliers (all Cook's D < 0.05)
# === STEP 4: Effect Sizes & Inference ===
# R-squared and adjusted R-squared
cat("\nR² =", summary(model)$r.squared, "\n")
cat("Adjusted R² =", summary(model)$adj.r.squared, "\n")
# Confidence intervals for coefficients
confint(model)
# Standardized coefficients (beta weights)
library(lm.beta)
lm.beta(model)
# Standardized β allows comparison: which predictor has stronger effect?
# IQ: β_std ≈ 0.54, SES: β_std ≈ 0.45
# IQ has slightly stronger effect than SES
# f² (Cohen's f-squared) for each predictor
# f² = R²_full - R²_reduced / (1 - R²_full)
R2_full <- summary(model)$r.squared
model_no_IQ <- lm(GPA ~ SES, data=data)
R2_no_IQ <- summary(model_no_IQ)$r.squared
f2_IQ <- (R2_full - R2_no_IQ) / (1 - R2_full)
cat("\nf² for IQ:", round(f2_IQ, 3), "\n")
# f² interpretation: .02 small, .15 medium, .35 large (Cohen, 1988)
model_no_SES <- lm(GPA ~ IQ, data=data)
R2_no_SES <- summary(model_no_SES)$r.squared
f2_SES <- (R2_full - R2_no_SES) / (1 - R2_full)
cat("f² for SES:", round(f2_SES, 3), "\n")
# === STEP 5: Visualization ===
# Partial regression plots (added variable plots)
avPlots(model, main="Partial Regression Plots")
# 3D scatterplot (if applicable)
library(scatterplot3d)
s3d <- scatterplot3d(data$IQ, data$SES, data$GPA,
pch=16, color="steelblue",
xlab="IQ", ylab="SES", zlab="GPA",
main="Multiple Regression: GPA ~ IQ + SES")
s3d$plane3d(model, col="red")
# Predicted vs. Observed
data$predicted <- predict(model)
ggplot(data, aes(x=predicted, y=GPA)) +
geom_point(alpha=0.6, color="steelblue") +
geom_abline(slope=1, intercept=0, color="red", linetype="dashed") +
labs(title="Predicted vs. Observed GPA",
x="Predicted GPA", y="Observed GPA") +
theme_classic()
# === STEP 6: Prediction Example ===
# Predict GPA for a student with IQ=110, SES=7
new_student <- data.frame(IQ=110, SES=7)
predicted_GPA <- predict(model, newdata=new_student, interval="confidence")
cat("\nPredicted GPA for IQ=110, SES=7:\n")
print(predicted_GPA)
# Predicted GPA: 3.38 (95% CI [3.31, 3.45])
# === APA-Style Reporting ===
cat("\n=== APA-Style Report ===\n")
cat("A multiple linear regression was conducted to predict GPA from IQ and SES.\n")
cat("Assumptions were met: linearity(residual plots), independence(design),\n")
cat("homoscedasticity(Breusch-Pagan p=.42), normality(Shapiro-Wilk p=.18),\n")
cat("no multicollinearity(all VIF<1.1), no influential outliers(Cook's D<.05).\n")
cat("The overall model was significant, F(2,117)=96.6, p<.001, R²=.623,\n")
cat("adjusted R²=.617, indicating IQ and SES together explain 62.3% of GPA variance.\n")
cat("IQ was a significant positive predictor(β=0.025, SE=0.002, t=11.23, p<.001),\n")
cat("with each 10-point IQ increase associated with a 0.25-point GPA increase.\n")
cat("SES was also a significant positive predictor(β=0.152, SE=0.016, t=9.40, p<.001),\n")
cat("with each 1-point SES increase associated with a 0.15-point GPA increase.\n")
cat("Both predictors contributed substantial unique variance(IQ f²=0.46, SES f²=0.32).\n")Overall model: F(2,117)=96.6, p<.001, R²=.623, adjusted R²=.617. IQ (β=0.025, p<.001, f²=0.46) and SES (β=0.152, p<.001, f²=0.32) both significantly predict GPA, explaining 62.3% of variance. Each 10-point IQ increase → 0.25 GPA increase; each 1-point SES increase → 0.15 GPA increase. Findings consistent with meta-analyses (Sirin 2005; Neisser et al. 1996) showing substantial IQ-achievement and SES-achievement relationships.
Alternatives
Structured fallback pathways for choosing alternative tests when normality or slopes requirements fail.
- Robust Regression — Utilize M-estimators to neutralize extreme residuals.
- Bootstrap OLS — Generate robust standard errors using 1,000 resamples.
- Weighted Least Squares (WLS) — Downweight observations with higher variance.
- HC3 Standard Errors — Apply robust covariance matrices to preserve p-value integrity.
- Ridge Regression — Apply L2 penalties to stabilize exploding coefficients.
- PCA Pre-Reduction — Collapse redundant predictors before the predictive strike.
Post-hoc
Group mean comparisons and correction controls (e.g. Tukey HSD, Bonferroni) to protect against Family-Wise Error Rates.
Post-hoc pairwise tests defined for this model.
While OLS is a global model, elite post-hoc forensics require probing the specific conditions under which the predictors peak. Use simple slopes to transform abstract interactions into clinical stories.
Effect Size
Understanding effect sizes (e.g., Cohen's d, Partial Eta-Squared) and clinical impact benchmarks.
Proportion of outcome variance explained by all predictors. 0-1 scale. Small: .02, Medium: .13, Large: .26 (Cohen 1988). Always report both R² and adjusted R²
R² corrected for number of predictors; penalizes overfitting. Preferred for model comparison. Always lower than R²
Cohen's f² = (R²_full - R²_reduced) / (1 - R²_full). Effect size for individual predictor's unique contribution. Small: .02, Medium: .15, Large: .35
Regression coefficient in standardized units (SD). Allows comparison of relative importance across predictors. Interpret as SD change in outcome per 1-SD change in predictor
Unique variance explained by predictor after controlling for others. Sum of all semi-partial r² ≤ R²
0.2
0.5
0.8
Sample Size
Guidelines for minimum sample requirements and power analysis parameters.
The 'Rule of 15': A minimum of 15 participants per predictor is required to prevent the model from 'Overfitting' the random noise of the sample and to ensure stable coefficient weights.
| Effect Size | Parameters | Required n |
|---|---|---|
| Small Effect | f²=.02 (Small) | n ≈ 647 |
| Medium Effect | f²=.15 (Medium) | n ≈ 92 |
| Large Effect | f²=.35 (Large) | n ≈ 45 |
Multicollinearity is the 'Power Thief'. If your predictors are highly correlated (VIF > 5), the unique signal of each variable is 'Stolen', effectively halving your statistical power. Prioritize variable selection to maximize N-efficiency.
Reporting
How to compile statistical results into publication prose matching APA and journal style guides.
A multiple linear regression was conducted to predict outcome variable from list predictors. State assumption checks: 'All assumptions were met' or specify which and how handled. The overall model was significant/non-significant, F(df_model, df_resid) = X.XX, p = .XXX, R² = .XXX, adjusted R² = .XXX, indicating interpretation of R². For each significant predictor: Predictor name was a significant positive/negative predictor (β = X.XX, SE = X.XX, t = X.XX, p = .XXX, f² = X.XX), indicating substantive interpretation with confidence interval. Conclude with overall interpretation in context.
- F-statistic with df (overall model test)
- p-value for overall model
- R² and adjusted R²
- For each predictor: β (unstandardized), SE, t-statistic, p-value, 95% CI
- Effect sizes: f² or semi-partial r² for key predictors
- Standardized β if comparing predictors
- Statement about assumption checks (especially VIF, residual plots)
Manuscript Lab
Copy standard summary tables and forensic reporting grids to outline analysis details.
| Predictor | B (Unstandardized) | SE | β (Standardized) | t | p | VIF |
|---|---|---|---|---|---|---|
| (Intercept) | 12.45 | 1.20 | — | 10.38 | < .001 | — |
| Age (Years) | 0.35 | 0.08 | .24 | 4.38 | < .001 | 1.2 |
| Baseline Pain | 1.42 | 0.15 | .58 | 9.46 | < .001 | 1.3 |
| Treatment (1=Active) | -4.20 | 1.10 | -.21 | -3.82 | < .001 | 1.1 |
The Raw Effect. The change in the outcome (in original units) for every 1-unit increase in the predictor.
The Relative Power. Removes units to compare predictor strength directly. Higher magnitude = stronger driver.
Variance Inflation Factor. An audit for Multicollinearity. VIF > 5 indicates the predictor is redundant with others.
Model Accuracy. The percentage of variance in the outcome explained by the entire model.
Command Center
Syntax libraries and function parameters for executing calculations in stats packages.
# 1. Fit OLS Model
model <- lm(recovery ~ age + pain + treatment, data = df)
summary(model)
# 2. Assumption Audit (Normality, Homoscedasticity, VIF)
performance::check_model(model)Never publish OLS results without a 'Cook's Distance' audit. One influential outlier can distort the entire coefficient set.
# Outlier Impact Analysis
olsrr::ols_plot_cooksd_bar(model)
# Heteroscedasticity Test
car::ncvTest(model)Common Mistakes
Analytical caveats and corrections to maintain modeling integrity.
References
Scholarly lineage and citation keys grounding the statistical framework.