Skip to content

Instantly share code, notes, and snippets.

@toyeiei
Last active June 29, 2019 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toyeiei/d995a6270b81aee5cf897269f2245299 to your computer and use it in GitHub Desktop.
Save toyeiei/d995a6270b81aee5cf897269f2245299 to your computer and use it in GitHub Desktop.
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