R caret

caret (Classification And REgression Training) is a library of functions for model training.


Installation

install.packages('caret')


Usage

The createDataPartition function randomly partitions a vector of outcomes.

data(mtcars)

parts = createDataPartition(mtcars$mpg, p = .8)
train = mtcars[parts, ]
test = mtcars[-parts, ]

createDataPartition returns a list object. To instead return a matrix, pass list = FALSE as an option.

The confusionMatrix function prepares the confusion matrix for a prediction, such as that from an XGBoost model.

my.data <- xgb.DMatrix(data = df[,predictors], label=df$outcome)
my.model <- xgboost(data = my.data, objective = "binary:logistic", ...)
pred <- predict(my.model, my.data)
pred <- ifelse(pred > 0.5, 1, 0)

confusionMatrix(as.factor(pred), as.factor(df$outcome))


CategoryRicottone

R/Caret (last edited 2025-03-20 15:05:45 by DominicRicottone)