Last active
June 29, 2019 22:54
-
-
Save toyeiei/d995a6270b81aee5cf897269f2245299 to your computer and use it in GitHub Desktop.
learn how to build model with caret
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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