I am trying to get the prediction accuracy, precision and recall when running Lasso logistic model but it turns out that no matter what lambda I set, the recall is always 1 (even when I set lambda = 0)
Below is my code. Here I'm using Smartket dataset from ISLR package.
library(ISLR)
k = 10 #10 folds cv
set.seed(42)
folds <- sample(1:k, nrow(Smarket), replace = T)
library(glmnet)
x = model.matrix(Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Lag5 + Volume
, Smarket)[,-1]
y = Smarket$Direction
results <- data.frame(matrix(NA, nrow = 10, ncol = 3))
colnames(results) <- c("Accuracy", "Precision", "Recall")
for(j in 1:k){
lasso.mod <- glmnet(x[folds != j,], y[folds != j], alpha = 1,
lambda = 0.01, family = "binomial")
preds <- predict(lasso.mod, newx = x[folds == j,], type = "response")
class <- ifelse(preds >= 0.5, "Up", "Down")
accuracy <- sum(class == y[folds == j])/length(class)
precision <- sum(class == "Up" & y[folds == j] == "Up")/sum(class == "Up")
recall <- sum(class == "Up" & y[folds == j] == "Up")/sum(y[folds ==
j] == "Up")
results[j,] <- c(accuracy, precision, recall)
}
Is there anything wrong in my model. I think something's wrong in my function call glmnet, not the loop because everything's good when I use glm to call the logistic model instead of glmnet.