Skip to content

Instantly share code, notes, and snippets.

@toyeiei
Last active Jun 29, 2019
Embed
What would you like to do?
learn how to build model with caret
## install caret and mlbench for datasets
install.packages("caret")
install.packages("mlbench")
library(caret)
library(mlbench)
## load diabetes data
## binary classification problem
data("PimaIndiansDiabetes")
df <- PimaIndiansDiabetes
str(df)
## [1] split dataset into train and test
set.seed(99)
n <- nrow(df)
id <- sample(1:n, 0.8*n, replace=FALSE)
train_df <- df[id, ]
test_df <- df[-id, ]
## [2] train model
set.seed(99)
knn_model <- train(diabetes ~ ., data = train_df, method = "knn")
print(knn_model)
## optional train with cross-validation
## knn_model <- train(diabetes ~ ., data = train_df, method = "knn",
## trControl = trainControl(method = "cv", number = 5))
## [3] test model
p <- predict(knn_model, newdata = test_df)
mean(p == test_df$diabetes)
## we can evaluate model with confusion matrix
table(p, test_df$diabetes, dnn = c("predicted", "actual") )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment