I would like to fit a logistic regression with ridge regularization. Here is my code
library(modeldata)
library(glmnet)
# check the data
data(attrition)
head(attrition)
# split the data into training 80%, and test 20%
smp_size <- floor(0.8 * nrow(attrition))
## set the seed to make your partition reproducible
set.seed(123)
# randomly get the index for training data
train_ind <- sample(seq_len(nrow(attrition)), size = smp_size)
# get training and testing data
train <- attrition[train_ind, ]
test <- attrition[-train_ind, ]
# fit the model
X <- model.matrix(Attrition~ ., train)
lm_ridge <- glmnet(X, train$Attrition, family = 'binomial', alpha = 0)
# get predicted values based on ridge regularization
prob_ridge <- predict(lm_ridge, model.matrix(Attrition~ ., test), type = 'response')
The prob_ridge
gives a matrix of 294 * 100. But I am expecting just one column, 294*1. Anything wrong with my code? Why am I getting a matrix from the predict
function?