Consider a linear regression problem with observations and available covariates. Generate one matrix whose entries are independent random variables, and then keep this realized matrix fixed. Let
and define
Generate one training response and one independent test response from
where
Here is the identity matrix.
For , fit a linear model with an intercept and the first columns of using . If is its fitted mean vector, define
and
Repeat this process independently 200 times, keeping fixed.
Use seed 43202 before generating and the response errors. R and Python use different random-number generators, so their exact numerical values need not agree.
For each , calculate the average training and test MSE over the 200 simulation runs. Plot both curves against . Verify numerically that training MSE does not increase as predictors are added in each simulation run.
For the model containing the first predictors, write
Here and . The model has fitted coefficients, including the intercept. Define its total squared approximation bias as
Thus, is the mean squared approximation bias.
Calculate the two theoretical expectations
and
Add these expectations to your plot and compare them with the simulation averages.
- Add the test MSE curve from one simulation run to the plot. Explain why this single curve is less smooth than the average test MSE. Use the approximation-bias and estimation-variance terms in the expected test MSE to explain why training MSE cannot be used by itself to select the predictor count.