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.

radumanolescu
  • 4,059
  • 2
  • 31
  • 44
AKD
  • 149
  • 1
  • 2
  • 10

3 Answers3

7

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

fit.lasso$glmnet.fit$dev.ratio

woodstck
  • 106
  • 1
  • 6
  • From glmnet documentation, dev.ratio is `The fraction of (null) deviance explained (for "elnet", this is the R-square).` – Quazi Irfan Apr 08 '20 at 07:50
  • 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. – Patrick Breheny May 10 '23 at 18:53
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

Bhavin Solanki
  • 1,364
  • 11
  • 27
Yi Xiong
  • 39
  • 2
  • 1
    you provided an example with a classification problem, OP is working on a regression problem – David Apr 26 '22 at 23:56
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

  • 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. – profPlum Jul 11 '23 at 20:26