0

I am using LASSO model for prediction and in the prediction, I get the following error when running predict function. Can someone help me to overcome this?

ERROR MSG: Error in predict(lasso_model, x, type = "response")[, 2] : subscript out of bounds

#convert data to matrix format
x <- model.matrix(St_recurrence~.,tr)

#convert class to numerical variable
y <- tr$St_recurrence

#Cross validation - perform grid search to find optimal value of lambda
cv_out <- cv.glmnet(x, y, alpha=1, family = 'binomial', nfolds = 5, type.measure = "auc")

#best value of lambda
best_lambda <- cv_out$lambda.1se

# Rebuilding the model with best lamda value identified
lasso_model <- glmnet(x, y, family = "binomial", alpha = 1, lambda = best_lambda)
coef(lasso_model)

# odds ratio
exp(coef(lasso_model))


library(ROCR)
# Calculate the probability of new observations belonging to "yes"
predprob <- predict(lasso_model, x, type = "response")[,2]      ## error comes in this line

# prediction is ROCR function
pred <- prediction(predprob, tr$Structural_recurrence)

# ROC curve (x-axis: fpr, y-axis: tpr)
perf <- performance(pred, measure = "tpr", x.measure = "fpr")
plot(perf, main="ROC Curve for LASSO model", col=rainbow(10))

#compute area under curve
aucperf <- performance(pred, measure="auc")
print(aucperf@y.values)
SAN
  • 25
  • 4
  • https://stackoverflow.com/questions/15031338/subscript-out-of-bounds-general-definition-and-solution – Peter Jun 13 '20 at 22:12
  • 3
    Does this answer your question? [Subscript out of bounds - general definition and solution?](https://stackoverflow.com/questions/15031338/subscript-out-of-bounds-general-definition-and-solution) – Peter Jun 13 '20 at 22:12

1 Answers1

0

For glmnet, if you do ?glmnet::predict.glmnet you can see under details:

 type: Type of prediction required. Type ‘"link"’ gives the linear
          predictors for ‘"binomial"’, ‘"multinomial"’, ‘"poisson"’ or
          ‘"cox"’ models; for ‘"gaussian"’ models it gives the fitted
          values. Type ‘"response"’ gives the fitted probabilities for
          ‘"binomial"’ or ‘"multinomial"

And it returns a vector of being 1, unlike caret which returns 2 columns.

So you can do:

library(glmnet)
library(ROCR)

data(Sonar)
y= as.numeric(Sonar$Class)-1
x=as.matrix(Sonar[,-ncol(Sonar)])
lasso_model = glmnet(x=x,y=y,family="binomial",alpha=1,lambda=0.001)

predprob <- predict(lasso_model, x, type = "response")
pred <- prediction(predprob, y)
perf <- performance(pred, measure = "tpr", x.measure = "fpr")
plot(perf, main="ROC Curve for LASSO model")
StupidWolf
  • 45,075
  • 17
  • 40
  • 72