Solved – How to interpret this glmnet() code and its output in R

glmnetr

I am not quite sure how to interpret the output of this code:

 coef(ridge_model, s = cv.glmnet(model, y, k=k)$lambda.min)

ridge_model is the output of glmnet()

What role does the argument 's' play?

Output:

7 x 1 sparse Matrix of class "dgCMatrix"
                    1
(Intercept) 86.825637
(Intercept)  .       
x1           3.924821
x2           9.816783
x3          11.770995
x4B         22.385858
x4C         -6.438195
  1. My confusion is in understanding how the coef() function works. ridge_model is
    the output of glmnet() so it represents the fitted
    model for different lambda values. Each lambda would have its set of
    coefficients.
  2. Then there is the cv.glmnet() that gives the k-fold cross validation
    output and gives the minimum lambda value. We are giving this lambda
    as an input to the 's' argument.
  3. How would this then affect the ridge model which already has its
    lambda values?
    coef(ridge_model, s = cv.glmnet(model, y, k=k)$lambda.min)

Best Answer

This smells incorrect, you probably wanted:

fit <- cv.glmnet(model, y, k=k)
coef(fit, "lambda.min")

which will return the coefficients using the internal fit from the cross validation.

Unless ridge_model has the same predictors, weights, mixing parameter, etc, plugging in a penalty parameter from one model into another seems odd; but if that were the same, ridge_model would be the same as fit$glmnet.fit above and redundant.