Book contents

STATHARBOR ANALYTICS · INTRODUCTION TO STATISTICS · CHAPTER 12

Simple Linear Regression

Regression analysis explores the relationship between a quantitative response variable and one or more explanatory variables. In simple linear regression, we model the relationship between a response YY and a single predictor XX by a straight line plus random error. This chapter introduces the model, estimation, inference, diagnostics and interpretation, with code examples in R, Python and Julia.

The Model

Assume we observe pairs (xi,yi)(x_i, y_i) for i=1,…,ni=1,\dots,n. The simple linear regression model is Yi=β0+β1xi+εi,εi∼Normal(0,σ2),Y_i = \beta_0 + \beta_1 x_i + \varepsilon_i, \quad \varepsilon_i \sim \mathrm{Normal}(0, \sigma^2), where β0\beta_0 is the intercept (expected value of YY when X=0X=0) and β1\beta_1 is the slope (expected change in YY per unit change in XX). The errors εi\varepsilon_i are independent and identically distributed.

Least Squares Estimation

We estimate β0\beta_0 and β1\beta_1 by minimising the sum of squared residuals S(β0,β1)=∑i=1n(yi−β0−β1xi)2.S(\beta_0, \beta_1) = \sum_{i=1}^n (y_i - \beta_0 - \beta_1 x_i)^2. The least–squares estimates are β̂1=∑(xi−x‾)(yi−y‾)∑(xi−x‾)2,β̂0=y‾−β̂1x‾.\hat{\beta}_1 = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sum (x_i - \bar{x})^2}, \quad \hat{\beta}_0 = \bar{y} - \hat{\beta}_1 \bar{x}. The residual standard error σ̂\hat{\sigma} and coefficient of determination R2R^2 measure the variability around the fitted line and the proportion of variance explained.

Inference

Under the normal error assumptions, the estimators β̂0\hat{\beta}_0 and β̂1\hat{\beta}_1 are unbiased and normally distributed. Standard errors are SE(β̂1)=σ̂1∑(xi−x‾)2,SE(β̂0)=σ̂1n+x‾2∑(xi−x‾)2.\text{SE}(\hat{\beta}_1) = \hat{\sigma}\sqrt{\frac{1}{\sum (x_i - \bar{x})^2}}, \quad \text{SE}(\hat{\beta}_0) = \hat{\sigma}\sqrt{\frac{1}{n} + \frac{\bar{x}^2}{\sum (x_i - \bar{x})^2}}. We can test H0:β1=0H_0: \beta_1 = 0 using the tt–statistic t=β̂1/SE(β̂1)t = \hat{\beta}_1/\text{SE}(\hat{\beta}_1) with n−2n-2 degrees of freedom. A 95% confidence interval for β1\beta_1 is β̂1±t0.975,n−2SE(β̂1)\hat{\beta}_1 \pm t_{0.975, n-2}\,\text{SE}(\hat{\beta}_1).

Example: Relationship Between Height and Weight

Consider a dataset of individuals’ heights (cm) and weights (kg). We fit a simple linear regression of weight on height.

Worked example

Fitting a linear model in R, Python and Julia.

height <- c(160, 165, 170, 175, 180)
weight <- c(55, 60, 65, 72, 78)
lm_fit <- lm(weight ~ height)
summary(lm_fit)
import numpy as np
import statsmodels.api as sm

x = np.array([160, 165, 170, 175, 180])
y = np.array([55, 60, 65, 72, 78])
x_const = sm.add_constant(x)
model = sm.OLS(y, x_const).fit()
print(model.summary())
using DataFrames, GLM

df = DataFrame(height = [160,165,170,175,180],
               weight = [55,60,65,72,78])
lm_fit = lm(@formula(weight ~ height), df)
coef(lm_fit)

These commands fit the regression model and print parameter estimates, standard errors and R2R^2. The slope estimate tells us the expected additional kilograms per centimetre of height.

Interpretation

Assumptions and Diagnostics

Simple linear regression relies on linearity, independence, homoscedasticity (constant variance) and normality of errors. To assess these, we plot residuals against fitted values, create a Q–Q plot of residuals, and compute influence diagnostics such as Cook’s distance. Violations suggest transformations or alternative models.

Exercises

Practice

  1. Collect or simulate data for study hours and exam scores for 30 students. Fit a linear regression model. Plot the data, estimate the coefficients, and interpret the results. Perform diagnostics to assess the model assumptions.

  2. Show that β̂1\hat{\beta}_1 is an unbiased estimator of β1\beta_1. Derive Var(β̂1)\mathrm{Var}(\hat{\beta}_1).

  3. Use R/Python/Julia to simulate 1,000 datasets with n=50n=50 from Y=2+0.5X+εY = 2 + 0.5 X + \varepsilon with X∼Unif(0,10)X \sim \mathrm{Unif}(0, 10) and ε∼Normal(0,1)\varepsilon \sim \mathrm{Normal}(0,1). Examine the distribution of the estimated slopes. Compare the empirical mean and variance to the theoretical values.

  4. Fit a regression of weight on height for a publicly available dataset (e.g., the NHANES dataset). Interpret the slope and R2R^2 value. Discuss potential confounders.

References