Tutorials › Exam PA - Predictive Analytics › A Tuning Workflow for Penalized Regression

Exam PA · Tuning and Regularized Model Use · Tutorial 457 of 966

A Tuning Workflow for Penalized Regression

Learn how to tune penalized regression by cross-validation without letting the final test set influence model selection.

Advanced 10 min read

What You'll Learn

  • Define a prediction target in terms of outcome, prediction time and unit of analysis
  • Separate predictors available at prediction time from information that would cause leakage
  • Set up a train/test split and resampling plan appropriate to the data structure
  • Compare ridge, lasso and elastic net using a consistent cross-validation loss
  • Apply a pre-specified selection rule to choose an alpha and lambda
  • Calculate and interpret final test-set error without using it to retune the model

Why Penalized-Regression Tuning Needs a Workflow

A penalized regression model is not selected by its coefficient estimates alone. The analysis must also decide what is being predicted, which information is available when the prediction is made, how candidate penalty settings will be compared, and which observations will provide the final assessment. If the same data are used repeatedly to make all of these decisions, the reported performance can be too optimistic.

A disciplined workflow separates model development from final assessment. Resampling within the development data compares candidate settings; an independent test set is reserved until the model-selection rule has been applied. This workflow is aligned with the SOA Exam PA (Predictive Analytics) syllabus.

Definition: The prediction target specifies the outcome to predict, the unit receiving a prediction, and the time or decision point at which the prediction is made. The development data are used to fit and tune models. The test data are held aside during those decisions and used for a final assessment of the selected procedure.

Start With the Prediction Target and Candidate Predictors

Write the target in operational terms before fitting anything. State one row’s meaning, the response variable, its measurement period, and when the prediction must be produced. For instance, “predict the total dollars of covered repair costs incurred per policy during the 12 months after renewal, using information known at renewal” is more precise than “predict claims.”

The timing clause is essential. A variable recorded after the prediction date may be strongly associated with the response but unavailable for a real prediction. Including it can make validation results misleading. Candidate predictors should be defined by their availability and intended role, not selected merely because they correlate with the outcome in the complete data.

For a numerical response, a common tuning loss is mean squared error. If \(y_i\) is the observed response and \(\hat{y}_i\) is its prediction, the squared error is \((y_i-\hat{y}_i)^2\); the mean squared error averages these losses. Root mean squared error is the square root of that average and is expressed in the response’s units. This tutorial uses squared-error loss in its regression examples.

The candidate predictor set should be fixed before comparing penalty settings. In practice, a penalized regression routine operates on a numerical predictor matrix. This workflow assumes that such a matrix is available; the details of constructing it are a separate modeling decision. Any data-dependent preparation must be handled so that validation observations do not influence the fitted model or its preparation.

Worked Example: Define the Target Before Splitting the Data

A hypothetical insurer wants to predict repair costs for small watercraft. There are 2,500 policy records, each representing one policy renewed on a particular date. The available fields include boat age, coverage type, prior-year repair cost, and repair cost during the 12 months after renewal. A separate field records whether a repair was reported during that future period. Specify a prediction target, decide which fields are eligible, and set up a development/test split with five-fold resampling.

Step 1: State the target. Use the total repair cost in dollars per policy during the 12 months after renewal as the response. The prediction is made at renewal, and the row is one policy. The target is therefore a future policy-level cost, not a claim-level amount.

Step 2: Check predictor timing. Boat age, coverage type, and prior-year repair cost are known at renewal, so they are eligible candidates. The future-period repair indicator is observed after renewal and depends on what happens during the target period. It is not eligible for a prediction made at renewal.

Step 3: Reserve a test set. Choose an 80% development and 20% test split. The development count is \(2{,}500 \times 0.80=2{,}000\), and the test count is \(2{,}500-2{,}000=500\).

Step 4: Plan resampling within development data. Five-fold cross-validation divides the 2,000 development rows into five folds of \(2{,}000/5=400\) rows each. Each fold can serve as validation data while the other 1,600 rows in the development set are used to fit that fold’s model.

This design keeps the 500 test policies out of model tuning. It also avoids the common mistake of including a future-period field simply because it is present in the data table.

Use Resampling to Compare Candidate Settings

For each candidate setting, fit on part of the development data and calculate predictions for the held-out fold. In \(K\)-fold cross-validation, the data are partitioned into \(K\) folds. Each fold is held out once, and its observations are predicted by a model fitted without that fold. The cross-validation estimate is the average validation loss across the folds.

For penalized regression, the candidate setting includes the mixing parameter \(\alpha\) and penalty strength \(\lambda\). With the usual glmnet convention, \(\alpha=0\) gives ridge, \(\alpha=1\) gives lasso, and a value strictly between 0 and 1 gives elastic net. The earlier material on elastic-net penalties explains these roles. Tuning asks which candidate setting performs best under the selected resampling plan and loss.

$$ \widehat{L}_{CV}(\alpha,\lambda) = \frac{1}{n}\sum_{i=1}^{n} \ell\!\left(y_i,\hat{f}_{-k(i),\alpha,\lambda}(x_i)\right) $$

Here \(k(i)\) is the fold containing observation \(i\), and \(\hat{f}_{-k(i),\alpha,\lambda}\) is the fitted model that excludes that fold. For squared-error loss, \(\ell(y,\hat{y})=(y-\hat{y})^2\). The formula averages one held-out loss per development observation; it does not average training error.

Justification: Each observation contributes a loss from a model that was fitted without that observation. Averaging those held-out losses estimates how the specified fitting procedure performs on observations drawn from the same prediction setting. This interpretation depends on an appropriate resampling design: if observations are dependent or prediction is for a later time period, ordinary random folds may not represent the intended future use.

A practical comparison should use the same development data, the same folds, and the same loss for all candidate \(\alpha\) values. The lambda paths need not contain identical values across fits; the comparison is between the cross-validation results produced for each candidate procedure. A simple selection rule is to choose the \((\alpha,\lambda)\) pair with the smallest mean cross-validation loss. State the rule before looking at the final test results.

For a numerical response, a concise R sketch using glmnet is shown below. It assumes that x_train is a numeric predictor matrix and y_train is the corresponding numerical response. The object foldid assigns each development observation to one of five folds.

R sketch: alphas <- c(0, 0.5, 1); cvfits <- lapply(alphas, function(a) cv.glmnet(x_train, y_train, family = "gaussian", alpha = a, foldid = foldid, type.measure = "mse")). For each fit, obtain its minimum cross-validation loss from min(cvfit$cvm) and its corresponding penalty from cvfit$lambda.min. Compare the three minimum losses, select the alpha with the smallest one, and retain that fit and its selected lambda.

The code is a workflow sketch, not a complete data-preparation recipe. The target and candidate features must already be defined, the folds must reflect the data structure, and the test observations must remain unused. If several tuning decisions are made after inspecting validation results, those decisions are part of model selection and should be made within development data rather than by repeatedly consulting the test set.

Worked Example: Select a Candidate by Cross-Validation

A development sample is used to tune three penalized-regression candidates for hypothetical equipment-repair cost. All use the same five folds and mean squared error. The table reports each candidate’s minimum mean cross-validation MSE at the listed lambda. Choose the candidate under the minimum-MSE rule and express its error as RMSE.

CandidateAlphaLambdaMean CV MSE (dollars squared)
Ridge00.0818,600
Elastic net0.50.1017,200
Lasso10.0417,500

Step 1: Apply the stated rule. Compare \(18{,}600\), \(17{,}200\), and \(17{,}500\). The smallest value is \(17{,}200\), so select the elastic-net candidate with \(\alpha=0.5\) and \(\lambda=0.10\).

Step 2: Convert to RMSE for interpretation. The cross-validation RMSE is \(\sqrt{17{,}200}=\$131.1488\ldots\). Rounded to the nearest cent, it is \(\$131.15\).

Check. Squaring the unrounded RMSE gives approximately \(17{,}200\) dollars squared. The mistake to avoid is selecting the largest lambda or choosing lasso automatically because it produces fewer nonzero coefficients; the stated predictive-loss rule selects the elastic-net candidate here.

Reserve the Test Set for Final Assessment

After choosing the candidate setting using development data, fit that selected procedure using all development observations. Then predict the untouched test observations and calculate the pre-specified test metric. Do not choose a different alpha, lambda, predictor set, or preprocessing option because the test result looks disappointing. Changing the model in response to test performance turns the test data into part of model development.

For test observations \(i=1,\ldots,m\), test RMSE is \(\sqrt{\frac{1}{m}\sum_{i=1}^{m}(y_i-\hat{y}_i)^2}\). Test MAE is \(\frac{1}{m}\sum_{i=1}^{m}|y_i-\hat{y}_i|\). RMSE penalizes large errors more strongly because the errors are squared; MAE is in the response units and gives a linear penalty to error magnitude. Neither metric is universally preferable, so the loss should reflect the purpose of the prediction.

A final test estimate is informative only for the population and prediction setting represented by the test data. A random split may be suitable when future observations resemble a random sample from the same population. If the intended task is to predict later periods, a chronological split may be more appropriate. If several records belong to one policyholder, household, or provider, splitting related records across development and test sets can make assessment unrealistically easy.

Worked Example: Calculate Final Test Error Without Retuning

A selected penalized regression model is fitted on development data and evaluated on four independent hypothetical test policies. Costs and predictions are in thousands of dollars. Calculate test RMSE and MAE, then compare RMSE with a fixed baseline prediction of 13 for every test policy.

PolicyObserved costModel predictionModel residualSquared residualAbsolute residual
11011-111
21211111
31413111
41618-242

Step 1: Calculate model RMSE. The squared residual total is \(1+1+1+4=7\). The test MSE is \(7/4=1.75\), so test RMSE is \(\sqrt{1.75}=1.322875\ldots\) thousand dollars. Rounded to four decimal places, this is 1.3229 thousand dollars, or \(\$1{,}322.88\) when rounded to the nearest cent.

Step 2: Calculate model MAE. The absolute residual total is \(1+1+1+2=5\). Thus MAE is \(5/4=1.25\) thousand dollars, or \(\$1{,}250.00\).

Step 3: Compare with the baseline. The baseline prediction is 13 for each policy. Its residuals are \(-3,-1,1,3\), so its squared residual total is \(9+1+1+9=20\). Its test MSE is \(20/4=5\), and its test RMSE is \(\sqrt{5}=2.2361\) thousand dollars, rounded to four decimal places.

The selected model has lower test RMSE than this baseline on these four observations. This is a descriptive final comparison, not a reason to change the selected penalty and report the same test set again as independent evidence. The common mistake is to use the test result to select a new model; the correction is to retain the preselected model and report the test assessment transparently.

Common Workflow Traps

  • Vague target definition. “Predict cost” is incomplete unless the response period, unit of analysis, and prediction time are clear.
  • Predictor availability ignored. A field recorded after the prediction date is not a legitimate predictor merely because it improves validation error.
  • Different comparisons for different candidates. Changing the folds or loss while comparing alpha values makes the comparison harder to interpret.
  • Training error treated as validation error. A model’s fitted-data error is not a substitute for held-out predictions.
  • Repeated test-set inspection. Choosing among models based on test results uses the test set for tuning and weakens its role as a final assessment.
  • Random splitting despite dependence or time order. The split must represent how predictions will actually be made; random folds can be unsuitable for grouped or time-ordered data.
Key takeaway: Define the prediction target and eligible predictors first, compare candidate alpha and lambda settings by a consistent resampling rule within development data, then assess the selected procedure once on an untouched test set.

Check Your Understanding

For each problem, identify the appropriate workflow decision or calculate the requested quantity.

  1. A data set contains 1,200 independent records. You reserve 25% for testing and use five-fold cross-validation on the remainder. How many records are in the test set, development set, and each validation fold? (Answer: 300 test records, 900 development records, and 180 records per fold.)
  2. Three candidates have minimum cross-validation MSE values of 12.1, 11.6, and 11.8, for ridge, elastic net, and lasso respectively. Under the minimum-MSE rule, which candidate is selected, and what is its cross-validation RMSE? (Answer: Elastic net; \(\sqrt{11.6}=3.4059\), to four decimal places.)
  3. On three test observations, actual values are 20, 24, and 28, while predictions are 18, 27, and 27. Calculate test MSE, RMSE, and MAE. (Answer: MSE \(=14/3=4.6667\); RMSE \(=2.1602\); MAE \(=2.0000\), with values rounded to four decimal places.)
  4. A model predicts next quarter’s claim cost, and several quarterly records belong to each policyholder. Why might a random row-level split give an unsuitable test assessment, and what split is more appropriate? (Answer: Related records could appear in both sets, and random splitting may not represent prediction into a later quarter; keep policyholders together and use a time-ordered assessment for future-quarter prediction.)
  5. After evaluating the selected model on the test set, you find that another lambda would have lower error on those same test observations. Should you replace the selected lambda and report the same test result as final evidence? (Answer: No. That uses the test set for selection; retain the preselected model or obtain a new independent test set for a fresh final assessment.)
Clinical Biostats