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 and a single predictor 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 for . The simple linear regression model is where is the intercept (expected value of when ) and is the slope (expected change in per unit change in ). The errors are independent and identically distributed.
Least Squares Estimation
We estimate and by minimising the sum of squared residuals The least–squares estimates are The residual standard error and coefficient of determination measure the variability around the fitted line and the proportion of variance explained.
Inference
Under the normal error assumptions, the estimators and are unbiased and normally distributed. Standard errors are We can test using the –statistic with degrees of freedom. A 95% confidence interval for is .
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 . The slope estimate tells us the expected additional kilograms per centimetre of height.
Interpretation
The intercept represents the predicted weight when height is zero. In practice, extrapolating outside the range of data is unwise; thus the intercept often has little practical meaning.
The slope is the key parameter: a positive slope indicates that taller individuals tend to weigh more. Be careful not to infer causality from correlation.
Residual plots help assess whether the linear model is appropriate: look for random scatter around zero. Patterns may indicate nonlinearity or heteroscedasticity.
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
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.
Show that is an unbiased estimator of . Derive .
Use R/Python/Julia to simulate 1,000 datasets with from with and . Examine the distribution of the estimated slopes. Compare the empirical mean and variance to the theoretical values.
Fit a regression of weight on height for a publicly available dataset (e.g., the NHANES dataset). Interpret the slope and value. Discuss potential confounders.
References
Weisberg S. Applied Linear Regression. 4th ed. Wiley; 2014.
Draper NR, Smith H. Applied Regression Analysis. 3rd ed. Wiley; 1998.
Kutner MH, Nachtsheim CJ, Neter J. Applied Linear Regression Models. 5th ed. McGraw–Hill; 2013.