Tutorials › Exam PA - Predictive Analytics › Predicting with a Fitted Ridge Model in R

Exam PA · Penalties and Ridge Regression · Tutorial 412 of 966

Predicting with a Fitted Ridge Model in R

Use a fitted ridge model to generate predictions at a specified penalty, then verify the prediction scale, dimensions, and calculations.

Intermediate 11 min read

What You'll Learn

  • Supply a numeric newx matrix with the same predictor columns and order used for fitting
  • Use the s argument to request predictions at a particular lambda value
  • Interpret the rows and columns of predict.glmnet output
  • Distinguish response-scale predictions from link-scale predictions for a binomial ridge model
  • Check predictions with the fitted linear predictor and inverse link
  • Avoid common errors involving dimensions, lambda selection, and predictor scaling

From a Fitted Ridge Model to Predictions

A fitted ridge model is useful only if it can be applied to new cases. In R, the predict method for a glmnet fit does this calculation: it combines the fitted coefficients with a matrix of new predictor values. A prediction call can look simple, but its result is only meaningful when the penalty value, predictor columns, and output scale are all understood.

This tutorial focuses on numeric predictions from a fitted ridge model at a chosen value of lambda. It shows how to check the input and output dimensions, interpret response-scale predictions, and verify the calculations independently. The examples use hypothetical data and fitted coefficient values.

Definition: For a fitted ridge model, a prediction is the model's estimated outcome for a new case, calculated using that case's predictor values and the fitted coefficients at a specified penalty value \( \lambda \). In glmnet, the new predictor data are supplied through newx, and the requested penalty value or values are supplied through s.

What the Prediction Call Requires

For an ordinary Gaussian ridge model, suppose the fitted conditional mean has the form

$$ \widehat{y}(x;\lambda)=\widehat{\beta}_0(\lambda)+\sum_{j=1}^{p}x_j\widehat{\beta}_j(\lambda). $$

Here \(p\) is the number of predictors, \(x_j\) is the value of predictor \(j\) for the new case, and the coefficients depend on the selected \( \lambda \). The model is fitted using a squared-error criterion with a ridge penalty on the predictor coefficients; the intercept is not penalized. For interpreting the fitted mean, the usual linear-model conditions include a correctly specified conditional mean and errors with mean zero. Additional conditions, such as constant error variance or normal errors, matter for some inferential procedures, but are not needed simply to calculate a fitted prediction.

In R, a typical call is predict(fit, newx = x_new, s = 0.4, type = "response"). The object fit is a fitted glmnet model. The object x_new must be a numeric matrix with one row per new case and one column for each predictor used in fitting. Its columns must have the same meaning and order as the training matrix. The s argument identifies the requested penalty value. When type = "response" is used with a Gaussian model, the result is on the outcome's original response scale.

If fitting used standardize = TRUE, predictions still use new predictor values in the same units as the training matrix. The fitted model applies its internal standardization consistently. If the analyst manually transformed the training predictors before fitting, the new predictors must receive that same transformation before prediction. predict.glmnet does not infer what a column means from its name; matching the number of columns alone is not enough.

Key dimension rule: If newx has \(n_{\text{new}}\) rows and s requests \(K\) penalty values, the prediction result has \(n_{\text{new}}\) rows and \(K\) columns. Each row corresponds to a new case; each column corresponds to a requested value of \( \lambda \).

Why the Output Has This Form

For a Gaussian model with an identity link, the response-scale prediction is the fitted linear predictor. For one new case, the model computes the intercept plus the sum of each predictor value multiplied by its fitted coefficient. With multiple new cases, the same calculation is performed row by row. With multiple penalty values, each value selects a different coefficient vector, so the calculations form separate columns.

$$ \widehat{\mathbf{y}}_{\text{new}}(\lambda) = \mathbf{1}_{n_{\text{new}}}\widehat{\beta}_0(\lambda) + \mathbf{X}_{\text{new}}\widehat{\boldsymbol{\beta}}(\lambda), \qquad \widehat{\mathbf{y}}_{\text{new}}(\lambda)\in\mathbb{R}^{n_{\text{new}}}. $$

Stacking these prediction vectors for \(K\) requested penalty values gives an \(n_{\text{new}}\)-by-\(K\) matrix. This explains both the dimensions and the contents of the result. In particular, a single requested penalty value generally still gives a one-column matrix rather than a plain vector.

1
Check the fitted object.
Confirm that it is the intended ridge fit, typically fitted with alpha = 0, and identify the penalty value to use.
2
Prepare newx.
Confirm it is numeric, has one row per case, and has the same predictor columns in the same order as the training input.
3
Request predictions.
Call predict with the fitted object, newx, the chosen s, and an appropriate type.
4
Verify the result.
Check the number of rows and columns, then confirm whether the values are outcomes, probabilities, or linear predictors.

Worked Predictions for a Gaussian Ridge Fit

Worked Example: One Penalty and Three New Cases

Method and trap avoided. Use the fitted coefficient equation at a single specified \( \lambda \), and check the one-column output. A common mistake is to expect a vector and overlook the matrix dimensions.

Suppose a hypothetical Gaussian ridge fit was made with alpha = 0. At \( \lambda=0.4 \), its reported coefficients are an intercept of 12, a coefficient of 1.5 for \(x_1\), and a coefficient of \(-2\) for \(x_2\). The new-data matrix has three rows, in the column order \(x_1,x_2\): \((4,1)\), \((0,3)\), and \((2,-1)\). The R call is predict(fit, newx = x_new, s = 0.4, type = "response").

For the first case, \(x_1=4\) and \(x_2=1\), so the response-scale prediction is \(12+(1.5)(4)+(-2)(1)=12+6-2=16\).

For the second case, \(x_1=0\) and \(x_2=3\), giving \(12+(1.5)(0)+(-2)(3)=12+0-6=6\).

For the third case, \(x_1=2\) and \(x_2=-1\), giving \(12+(1.5)(2)+(-2)(-1)=12+3+2=17\).

The result is a \(3\)-by-\(1\) matrix, with entries 16, 6, and 17 in row order. The dimension check is \(3\) new cases by \(1\) requested penalty value. As a final check, all three values were calculated using the same intercept, coefficients, and predictor-column order.

Worked Example: Comparing Predictions at Two Penalty Values

Method and trap avoided. Calculate a separate prediction column for each value in s. The trap is to mistake the columns for different new cases rather than different penalty values.

Suppose a Gaussian ridge fit has two predictors \(x_1,x_2\). At \( \lambda=0.2 \), let the fitted intercept and coefficients be \(5,2,-1\). At \( \lambda=0.8 \), let them be \(5,1,-0.5\). These are hypothetical values from the fitted model. The two rows of x_new are \((1,2)\) and \((3,0)\), and the call requests s = c(0.2, 0.8).

At \( \lambda=0.2 \), the first case predicts \(5+(2)(1)+(-1)(2)=5+2-2=5\). The second predicts \(5+(2)(3)+(-1)(0)=5+6+0=11\).

At \( \lambda=0.8 \), the first case predicts \(5+(1)(1)+(-0.5)(2)=5+1-1=5\). The second predicts \(5+(1)(3)+(-0.5)(0)=5+3+0=8\).

Therefore, the prediction matrix is

$$ \begin{pmatrix} 5 & 5 \\ 11 & 8 \end{pmatrix}. $$

The first column corresponds to \( \lambda=0.2 \), and the second to \( \lambda=0.8 \), because those values were requested in that order. The matrix has two rows for the two cases and two columns for the two penalty values. Each entry agrees with the corresponding coefficient calculation.

Response Scale and Link Scale

For Gaussian ridge regression, the identity link means the fitted linear predictor and response-scale prediction have the same numerical value. That equivalence does not hold for every family. For example, in a binomial model the linear predictor is a log-odds value, while a response-scale prediction is a probability. The requested type therefore matters when the model has a non-identity link.

For a binomial model with linear predictor \( \eta \), the inverse-logit transformation converts the linear predictor to the estimated probability \(p\):

$$ p=\frac{1}{1+\exp(-\eta)}. $$

Worked Example: Binomial Ridge Predictions as Probabilities

Method and trap avoided. Compute the linear predictor first, then apply the inverse-logit transformation for response-scale probabilities. A common mistake is to report a log-odds value as if it were a probability.

Suppose a hypothetical binomial ridge fit at \( \lambda=0.6 \) has intercept 0 and coefficient \( \log(3) \) for one predictor \(z\). The three new values are \(z=0,1,2\). The call requests type = "response", so the output is on the probability scale.

For \(z=0\), \( \eta=0+\log(3)(0)=0 \), and \(p=1/(1+\exp(0))=1/2=0.5\).

For \(z=1\), \( \eta=\log(3) \), so \( \exp(-\eta)=1/3 \) and \(p=1/(1+1/3)=3/4=0.75\).

For \(z=2\), \( \eta=2\log(3)=\log(9) \), so \( \exp(-\eta)=1/9 \) and \(p=1/(1+1/9)=9/10=0.9\).

The output is a \(3\)-by-\(1\) matrix of probabilities: 0.5, 0.75, and 0.9. Each lies between 0 and 1, as a probability must. If the call instead requested the link scale, it would return 0, \( \log(3) \), and \( \log(9) \), which are not probabilities.

Choosing Lambda and Avoiding Prediction Errors

The value supplied through s is the penalty at which predictions are requested; it is not automatically the value that produced the best predictive performance. The choice of \( \lambda \) should come from the modeling decision already made, such as a specified value or a value selected using a separate evaluation procedure. If a cross-validation fit object is used, values such as lambda.min can identify a selected penalty; predictions should still be checked to ensure the intended value and object were supplied.

A requested value of \( \lambda \) need not be one of the exact values stored in the fitted model's path. glmnet can interpolate coefficients for requested values between fitted values. For a question that supplies a particular penalty, use that value in s; do not silently replace it with a nearby entry from the fit.

The most consequential errors are often data-preparation errors rather than arithmetic errors. A matrix with the right dimensions can still be wrong if its columns are reversed or if a factor's dummy columns do not match the training design. Similarly, manually standardized training predictors require new data transformed using the same training means and scales. Check column names and order before prediction, and avoid relying on a data frame that may silently coerce columns to an unintended type.

Common exam trap: Suppose a Gaussian fit has intercept 12 and coefficients 1.5 and \(-2\), and the case has \(x_1=4,x_2=1\). The wrong calculation \(12+(1.5)(4)+(-2)(1)=12+6-2=16\) is not the error; the error occurs if the predictor values are accidentally assigned to reversed columns, producing \(12+(1.5)(1)+(-2)(4)=12+1.5-8=5.5\). The correction is to preserve the training column order: with \(x_1=4,x_2=1\), the correct prediction is 16. Always verify column meanings as well as the matrix dimensions.

In practice, a reliable prediction check has three parts: confirm the fitted model and \( \lambda \), confirm that every new-data column matches the training input, and confirm that the result has the expected scale and dimensions. These checks make a short R call auditable and reduce the risk of reporting a technically valid but incorrectly interpreted number. This treatment is aligned with the SOA Exam PA (Predictive Analytics) syllabus.

Check Your Understanding

For each question, determine the requested prediction or output structure before checking the answer.

  1. A Gaussian ridge fit at \( \lambda=0.3 \) has intercept 7 and coefficients 2 for \(x_1\) and \(-1\) for \(x_2\). A new case has \(x_1=3,x_2=4\). What is its response-scale prediction, and what are the dimensions of the output when one penalty is requested? (Answer: \(7+2(3)-4=9\); a \(1\)-by-\(1\) matrix.)
  2. A fitted Gaussian ridge model has intercept 4. At \( \lambda=0.1 \), its coefficients are 1 and 2; at \( \lambda=0.5 \), they are 0.5 and 1. For the case \(x_1=2,x_2=1\), give the prediction column in the order \(s=c(0.1,0.5)\). (Answer: \(8\) at \(0.1\), \(6\) at \(0.5\); a \(1\)-by-\(2\) matrix with row \((8,6)\).)
  3. A binomial ridge fit has linear predictor \( \eta=-\log(4) \) for a new case. What probability should a response-scale prediction report? (Answer: \(1/(1+4)=0.2\).)
  4. A new-data matrix has 5 rows and 3 columns. A prediction call requests four values of \(s\). What are the output dimensions, and what do rows and columns represent? (Answer: \(5\)-by-\(4\); rows represent new cases and columns represent requested penalty values in order.)
  5. A Gaussian model has intercept 10 and coefficients 3 for \(x_1\) and \(-2\) for \(x_2\). The training columns are ordered \(x_1,x_2\), but a new row containing \(x_1=1,x_2=4\) is mistakenly supplied in reverse order as \((4,1)\). Compare the incorrect and correct predictions. (Answer: Incorrect \(10+3(4)-2(1)=20\); correct \(10+3(1)-2(4)=5\).)
Clinical Biostats