How to calculate R Squared value for Lasso regression using glmnet in R
Asked Answered
T

3

7

I am performing lasso regression in R using glmnet package:

fit.lasso <- glmnet(x,y)
plot(fit.lasso,xvar="lambda",label=TRUE)

fit.lasso plot

Then using cross-validation:

cv.lasso=cv.glmnet(x,y)
plot(cv.lasso)

lambda VS MSE

One tutorial (last slide) suggest the following for R^2:

R_Squared =  1 - cv.lasso$cvm/var(y)

But it did not work.

I want to understand the model efficiency/performance in fitting the data. As we usually get R^2 and adjusted R^2 when performing lm() function in r.

Teepee answered 30/5, 2018 at 18:0 Comment(1)
What does "it did not work" mean? This does produce an answer. Do you think the answer is wrong for some reason?Snakebird
S
7

If you are using "gaussian" family, you can access R-squared value by

fit.lasso$glmnet.fit$dev.ratio

Seto answered 13/11, 2019 at 13:57 Comment(2)
From glmnet documentation, dev.ratio is The fraction of (null) deviance explained (for "elnet", this is the R-square).Cabin
This is a highly misleading answer, though, as it does not adjust for overfitting. R-squared should be calculated based on the cross-validated deviance ratio.Snakebird
I
2

I use the example data to demonstrate it

library(glmnet)

load data

data(BinomialExample)
head(x) 
head(y)

For cross validation

cvfit = cv.glmnet(x, y, family = "binomial", type.measure = "class")
rsq = 1 - cvfit$cvm/var(y)
plot(cvfit$lambda,rsq)

enter image description here

Israelite answered 5/10, 2018 at 9:11 Comment(1)
you provided an example with a classification problem, OP is working on a regression problemParsonage
E
0

Firtst fit the Lasso model with the selected lambda

...

lasso.model <- glmnet(x=X,y=Y, family = "binomial", alpha=1, lambda = cv.model$lambda.min )

then you could get the pseudo R2 from the fitted model

`lasso.model$dev.ratio`

this value give the deviance explained by the model/Null deviance

Excommunicative answered 13/3, 2021 at 4:34 Comment(1)
Don't do this, you don't want to refit the glmnet based on cv.model. There is literally already a cv.model$glmnet.fit which mirrors the cv.model exactly. It looks like Woodstock is doing it correctly.Friable

© 2022 - 2024 — McMap. All rights reserved.