The Question¶
“If I know how long a pregnancy is, how well can I predict birth weight?”
Correlation (Chapter 7) told us whether two variables are related. Least squares tells us how — by fitting the best straight line through the data.
Least Squares Fit¶
We want to find a line that best fits the data. “Best” means minimizing the sum of squared errors:
Why squared? Squaring penalizes large errors more than small ones, and it gives a smooth function we can minimize analytically.
Taking derivatives and setting to zero gives the normal equations:
The line always passes through .
Interpretation¶
For predicting birth weight from pregnancy length:
is the slope: each additional week of pregnancy adds pounds
is the intercept: predicted weight at 0 weeks (not meaningful here — extrapolation)
The intercept is only meaningful within the range of observed data. Extrapolating a linear model far outside the data range is almost always wrong.
Residuals¶
A residual is the difference between the observed value and the predicted value:
A good fit has residuals that:
Are centered at zero (no systematic bias)
Are roughly normal (for inference to work)
Have constant variance across the range of (homoscedasticity)
Show no pattern when plotted against (if they do, the relationship is nonlinear)
Residual plot is the key diagnostic tool. Always plot residuals vs fitted values.
Goodness of Fit — ¶
The coefficient of determination measures what fraction of the variance in is explained by the model:
where is the total variance (a baseline with no predictor).
: perfect fit, model explains all variation
: model is no better than predicting for everyone
for simple linear regression (note: lowercase from Chapter 7)
For pregnancy length → birth weight: — not great. Most birth weight variance is not explained by pregnancy length alone.
Bootstrap Confidence Intervals for the Slope¶
Is statistically meaningful, or could it be near zero by chance?
Instead of a formula, we use the bootstrap (Chapter 8):
Resample the data with replacement
Fit the line to the resample
Repeat 1000 times
The 95% confidence interval is the 2.5th to 97.5th percentile of bootstrap slopes
This makes no assumptions about the distribution of residuals.
Weighted Resampling¶
NSFG uses survey weights (finalwgt). For nationally representative estimates,
we should sample each observation in proportion to its weight.
In bootstrap resampling: instead of uniform resampling, use the weights as sampling probabilities. This is called weighted bootstrap.
Exercises¶
Fit a line predicting birth weight from pregnancy length. Report , , .
Plot the scatter with the fitted line and residuals.
Make a residual plot. Does the residual variance change across pregnancy lengths?
Bootstrap the slope 1000 times. What is the 95% confidence interval?
Does the slope change substantially when you use survey weights?
Glossary¶
least squares — fitting a line by minimizing the sum of squared errors
slope () — change in per unit change in
intercept () — predicted when
residual — ; the error of each individual prediction
SSE — sum of squared errors (residuals); what least squares minimizes
SST — total sum of squares; variance of around its mean
— fraction of variance explained by the model;
homoscedasticity — constant residual variance across the range of
residual plot — plot of residuals vs fitted values; key diagnostic for linear regression
extrapolation — using a model outside the range of observed data; almost always unreliable