Size: 1078
Comment: confusion matrix
|
← Revision 3 as of 2025-03-20 15:05:45 ⇥
Size: 1032
Comment: Accidentally left junk in last edit
|
Deletions are marked like this. | Additions are marked like this. |
Line 35: | Line 35: |
The '''`confusionMatrix`''' function prepares the confusion matrix for a prediction, such as that from an [[R/XGBoost|XGBoost]] model using the option objective = "binary:logistic" | The '''`confusionMatrix`''' function prepares the confusion matrix for a prediction, such as that from an [[R/XGBoost|XGBoost]] model. |
R caret
caret (Classification And REgression Training) is a library of functions for model training.
Contents
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))