Cross-validation and logistic regression

Our goal here is to build a model using 5-fold cross-validation. We'll utilize the caret package to establish our sampling scheme and to produce the final model. Start by building a separate trainControl() function:

> glm_control <-    caret::trainControl(method = "cv",    number = 5,    returnResamp = "final")

This object is passed as an argument to train the algorithm. We now produce our input features, response variable (must be a factor for caret to train as logistic regression), set our random seed, and train the model. For the train() function, specify glm for Generalized Linear Model (GLM):

> x <- train_reduced[, -22]> y <- as.factor(train_reduced$y)> set.seed(1988)> glm_fit <- caret::train(x, y, method ...

Get Advanced Machine Learning with R now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.