Solved – Obtaining residual variance from glmnet

glmnetr

I am working with glmnet and I'd like to see how we can obtain a residual variance for a given penalty parameter in prediction. I use glmnet's QuickStartExample example (here) and focus on L1 penalty with a given lambda. My code is as follows:

require(glmnet)
data(QuickStartExample)
fit <- glmnet(x, y, intercept = F, standardize = F, thresh = 1e-20, alpha = 1)

Following, I extracted coefficient vector using predict function to get the exact solution for 25th lambda as follows:

beta_glmnet = as.matrix(predict(fit, s = fit$lambda[25]/(2*dim(x)[1]), type = "coefficients", exact = T)[-1,])

My question is how we can obtain an estimate (preferably an unbiased one) of residual variance for the above model. My initial thought was fit$dev.ratio[25]/fit$df[25].

Any ideas?

Best Answer

In my understanding it is in deviance function of glmnet

data.table(l=fit$lambda, d=deviance(fit))[abs(l-reg.lambda) == min(abs(l-reg.lambda))]$d[1]