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)