student_R123
student_R123

Reputation: 1002

Find the predicted probabilities for logistic/LASSO using caret package in R (using cross validation)

I fitted a lasso logistic regression model using caret package in R. My code as follows,

require(ISLR)
require(caret)
set.seed(123)
fitControl <- trainControl(method = "cv",number = 5,savePredictions = T,classProbs=TRUE)
mod_fitg <- train(Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Volume,
                  data=Smarket[1:100,], method = "glmnet", 
                  trControl = fitControl,
                  tuneGrid=expand.grid(
                    .alpha=1,
                    .lambda=10^seq(-5, 5, length =2)),
                  family="binomial")

When i extract the predicted values, it will only show the predicted class (under the column pred) as follows,

mod_fitg$pred

enter image description here

Is there a way to extract the predicted probabilities instead of the predicted class ? Somehow i needed to obtain the predicted probabilities based on cross validation.

Thank you

Upvotes: 2

Views: 730

Answers (1)

Peter_Evan
Peter_Evan

Reputation: 947

I believe your predicted probabilities are there under the Down and Up columns. The model gives many observations an even chance and seems to defer to Up in such cases. However, there is variation further down the list. mod_fit$pred is a data frame and you can just extract the values directly:

pre_prob <- mod_fitg$pred[3:5]
pre_prob

#output- keeping index if we care about a certain observation 
    rowIndex      Down        Up
1          4 0.5000000 0.5000000
2          8 0.5000000 0.5000000

Upvotes: 1

Related Questions