Sub topic 3
Estimating the Line with Ordinary Least Squares
This chapter assumes you know the mean and variance, the t-distribution, the algebra of straight lines, and basic Python with NumPy and SciPy.
Estimating the line
Ordinary least squares (OLS) chooses the intercept $b0$ and slope $b1$ that minimize the sum of squared residuals. The slope is $b1 = \text{cov}(x,y)/\text{var}(x)$, and the intercept is $b0 = \bar{y} - b_1\bar{x}$.
import numpy as np
from scipy import stats
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([2.1, 3.9, 6.2, 7.8, 10.1])
n = len(x)
b1 = np.cov(x, y, ddof=1)[0, 1] / np.var(x, ddof=1)
b0 = y.mean() - b1 * x.mean()
y_hat = b0 + b1 * x
sst = ((y - y.mean()) ** 2).sum()
sse = ((y - y_hat) ** 2).sum()
ssr = sst - sse
r2 = ssr / sst
see = np.sqrt(sse / (n - 2))
f_stat = ssr / (sse / (n - 2))
se_b1 = see / np.sqrt(((x - x.mean()) ** 2).sum())
t_crit = stats.t.ppf(0.975, df=n - 2)
ci = (b1 - t_crit * se_b1, b1 + t_crit * se_b1)
x_new = 6.0
y_new = b0 + b1 * x_new
se_f = see * np.sqrt(1 + 1 / n + (x_new - x.mean()) ** 2 / ((x - x.mean()) ** 2).sum())
pi = (y_new - t_crit * se_f, y_new + t_crit * se_f)
print(f"b0={b0:.2f} b1={b1:.2f} R2={r2:.3f} SEE={see:.3f} F={f_stat:.1f}")
print(f"slope CI={ci[0]:.2f} to {ci[1]:.2f} prediction at x=6: {pi[0]:.2f} to {pi[1]:.2f}")Explained and unexplained variation
Total sum of squares (SST) measures the spread of $y$ around its mean. The regression sum of squares (SSR) is the part the line explains, and the sum of squared errors (SSE) is the rest. R-squared equals SSR/SST. The standard error of estimate (SEE) equals $\sqrt{\text{SSE}/(n-2)}$. In the ANOVA table, the F-statistic is the mean square regression divided by the mean square error. For simple regression, F equals the squared slope t-statistic.
Testing the slope and building confidence intervals
The standard error of the slope is $\text{SEE}/\sqrt{\sum(x-\bar{x})^2}$. Test $H0: b1 = 0$ with $t = b1 / s{b1}$ on $n-2$ degrees of freedom. A 95% confidence interval is $b1 \pm tc s{b_1}$. For the example, the interval is about 1.80 to 2.18.
Forecasting and prediction intervals
A forecast at a new $x$ uses $\hat{y} = b0 + b1 x$. A prediction interval adds the error of the new observation, so its standard error includes a 1 under the square root. It is wider than a confidence interval for the mean.
Assumptions and limitations
OLS inference assumes a linear relationship, independent errors, constant error variance, and normally distributed errors. Violations distort the standard errors, so the t-tests and intervals can mislead. Outliers and extrapolation beyond the data also reduce reliability.
Functional forms and log transformations
When growth is proportional, fit $\ln(y) = b0 + b1 x$. The slope then approximates the percentage change in $y$ per unit of $x$.
Exercise
Exercise
In the example, what is the F-statistic for testing the slope, and what does it equal in terms of the slope t-statistic?
Show answer
F is about 1110, which equals the squared slope t-statistic (about 33.3 squared).