0
votes

I am using this code to fit a model using LASSO regression.

library(glmnet)
IV1 <- data.frame(IV1 = rnorm(100))
IV2 <- data.frame(IV2 = rnorm(100))
IV3 <- data.frame(IV3 = rnorm(100))
IV4 <- data.frame(IV4 = rnorm(100))
IV5 <- data.frame(IV5 = rnorm(100))
DV <- data.frame(DV = rnorm(100))

data<-data.frame(IV1,IV2,IV3,IV4,IV5,DV)

x <-model.matrix(DV~.-IV5 , data)[,-1]
y <- data$DV

AB<-glmnet(x=x, y=y, alpha=1)
plot(AB,xvar="lambda")

lambdas = NULL
for (i in 1:100)
{
  fit <- cv.glmnet(x,y)
  errors = data.frame(fit$lambda,fit$cvm)
  lambdas <- rbind(lambdas,errors)
}

lambdas <- aggregate(lambdas[, 2], list(lambdas$fit.lambda), mean)


bestindex = which(lambdas[2]==min(lambdas[2]))
bestlambda = lambdas[bestindex,1]


fit <- glmnet(x,y,lambda=bestlambda)

I would like to calculate some sort of R2 using the training data. I assume that one way to do this is using the cross-validation that I performed in choosing lambda. Based off of this post it seems like this can be done using

r2<-max(1-fit$cvm/var(y))

However, when I run this, I get this error:

Warning message:
In max(1 - fit$cvm/var(y)) :
no non-missing arguments to max; returning -Inf

Can anyone point me in the right direction? Is this the best way to compute R2 based off of the training data?

1

1 Answers

1
votes

The function glmnet does not return cvm as a result on fit

?glmnet

What you want to do is use cv.glmnet

?cv.glmnet 

The following works (note you must specify more than 1 lambda or let it figure it out)

fit <- cv.glmnet(x,y,lambda=lambdas[,1])

r2<-max(1-fit$cvm/var(y))

I'm not sure I understand what you are trying to do. Maybe do this?

for (i in 1:100)
{
  fit <- cv.glmnet(x,y)
  errors = data.frame(fit$lambda,fit$cvm)
  lambdas <- rbind(lambdas,errors)
  r2[i]<-max(1-fit$cvm/var(y))
}

lambdas <- aggregate(lambdas[, 2], list(lambdas$fit.lambda), mean)


bestindex = which(lambdas[2]==min(lambdas[2]))
bestlambda = lambdas[bestindex,1]
r2[bestindex]