我很困惑如何计算 glmnet 拟合的 r 平方(LASSO、elastic-net 等)。我见过的一种方法是通过对应于 lambdas 之一的 cvm:
cvfit2 <- glmnet::cv.glmnet(datam, fundm,alpha=1,nfolds=10)
cf<-coef(cvfit2, s = "lambda.1se")
i<-which(cvfit2$lambda == cvfit2$lambda.1se)
e<-cvfit2$cvm[i]
r2<-1-e/var(fundm)
r2
#[1] 0.4571688
通过计算残差方差的经典方法:
datam2<-as.matrix(datam)
cc2<-as.matrix(cf[-1,]) #removing the intercept row
predict<-datam2 %*% cc2
err<-predict - fundm
View(err)
r2b<-1-var(err)/var(fundm)
r2b
#[1] 0.6100457
差异很大,我不确定第一种计算的方法是否正确。
我的问题
计算r平方的正确方法是什么?
glmnet 对象具有组件 dev.ratio 和 nulldev。来自 glmnet 文档:
“解释的(空)偏差分数(对于“elnet”,这是 R 平方)。”
我们是否应该将 dev.ratio 用于计算?如果是,如何为给定的 lambda 索引提取它?dev.ratio 数组有 100 个值,但只有 88 个值。cvfit2$lambda
我真的很困惑,非常感谢您的反馈。