Homework 04
Before you begin
Homework 04 contains four required questions on variable selection under correlation, the coordinate-descent computation of a lasso path, and penalty tuning by cross-validation. It is evaluated as Complete or Incomplete. Use either R or Python. You are not expected to use both languages.
Attempt each question before looking at the solutions or asking an AI tool for help. You may use AI tools, but you are responsible for checking the work and being able to explain your reasoning, code, and results.
Download the materials
The ZIP file contains the two editable QMD files, data/diabetes.csv, and data/diabetes-split-folds.csv. The simulations in Questions 1, 2, and 3 must be generated from the models stated below.
Question 2: Elastic net with an equal penalty mix
This question continues the simulation of Question 1. Generate the data exactly as in Question 1, with the same seed 43246, so that the 200 repetitions produce the same datasets and the only change is the fitting method. Now fit the elastic net model with mixing parameter , which places equal weight on the and squared penalties. Use the glmnet package in R or sklearn.linear_model.ElasticNet in Python, with the same four penalties and the same fitting details as in Question 1. In R, additionally set family = gaussian() to avoid the response-rescaling convention of the default Gaussian solver and match the stated penalty mix. Make sure you understand which argument controls this mix in the package you use.
For every covariate and penalty, report the selection frequency, and draw the selection-frequency figure. Compare the frequencies with the lasso results from Question 1: which covariates change the most, and in which direction?
For the pair , report the frequency of four mutually exclusive outcomes at each penalty: only the first covariate is selected, only the second is selected, both are selected, and neither is selected. Compare these frequencies with Question 1. Are the two covariates now properly selected together? Explain why the squared part of the penalty encourages the fit to keep both members of the pair.
Compare the average selection frequency among the noise covariates with the lasso results at each penalty. Are the noise covariates still screened out as the penalty increases? At the same numerical value of , the elastic net applies a weaker threshold than the lasso; use this to explain any difference. State the penalties at which the elastic net selects both members of the pair while screening out most noise covariates.
Question 3: Coordinate descent and the lasso path
This question asks you to implement coordinate descent yourself and use it to compute a complete lasso path. Create one simulated dataset with observations and covariates. Generate three covariates as independent standard normal vectors of length , and generate the response as
where is a vector of independent standard normal errors, independent of the covariates. The third covariate does not appear in the generating equation. Use seed 43247. Center the response and center and standardize each covariate using divisor . The intercept is fitted separately as the mean of the response and is not penalized.
The lasso objective is
where is the centered response and is the standardized covariate matrix. Coordinate descent updates one slope at a time while holding the other slopes fixed. For coordinate , form the residual that leaves covariate out of the current fit, and compute the score as the mean of the elementwise product of covariate and that residual. Because each standardized column satisfies , the update is the soft-thresholding rule from the Week 4 lecture:
Follow this procedure:
- Fix a value of and loop over the indices until the coefficients stop changing. For each , form the residual without covariate , compute its score, and save the soft-thresholded value as the new before moving to the next index.
- Check convergence after each complete cycle: stop when the largest absolute coefficient change is below a small tolerance, such as . Record the fitted vector for that .
- Then reduce to the next grid value and start the next fit from the current coefficients (a warm start). Repeat for the whole grid
Implement the procedure. Report the fitted coefficient vector at each grid value in a small table, and verify that every fit converged. Compute and check that all slopes are zero for grid values at or above it.
Plot the coefficient path: one curve per covariate, with the fitted slope on the vertical axis and on the horizontal axis. Describe the order in which the covariates enter the model and explain that order using the sizes of the true coefficients.
The third covariate has a zero population coefficient. Does it enter the path at small penalties in your run? Compare the fit at the smallest grid value with a least-squares fit on the same standardized data, and explain why the two are close but not identical.
Question 4: Tuning and comparing penalized regressions for diabetes prediction
The supplied data/diabetes.csv contains 442 observations of ten baseline covariates and the quantitative response y. The file data/diabetes-split-folds.csv assigns 353 observations to the training set and 89 observations to the final test set; it also supplies ten fold labels for the training observations.
Tune three penalized regressions (lasso, ridge, and elastic net with mixing parameter ) for predicting y from the ten covariates. Using only the training observations, perform ten-fold cross-validation with the supplied folds for each method and select each penalty by the smallest mean cross-validation error. In R, use cv.glmnet with its default Gaussian solver and package-generated penalty grids. In Python, use GridSearchCV with 61 logarithmically spaced penalty values from to for each method, placing StandardScaler inside a Pipeline. Make sure you understand which argument selects the penalty family. In every fit, estimate the covariate means and scales from the observations used for that fit, center the response, and leave the intercept unpenalized. The package-specific grids and penalty conventions can give different tuning results across languages.
Select the method with the smallest minimum cross-validation error. Produce the cross-validation plot for each of the three fits (in R, the default plot of a cv.glmnet object) and a table comparing the three selected penalties and their cross-validation errors. Then refit the chosen model on all training observations, evaluate the final test set once, and report the selected method, its penalty, and the test error.