# Estimate accuracy using a k-fold cross validation.

# load the library
library(caret)
# load the iris dataset
data(iris)
# define training control
train_control <- trainControl(method="cv", number=10)

# define an 70%/30% train/test split of the dataset
split=0.70
trainIndex <- createDataPartition(iris$Species, p=split, list=FALSE)
data_train <- iris[ trainIndex,]
data_test  <- iris[-trainIndex,]


# fix the parameters of the algorithm
# train the model
model <- train(Species~., data=data_train, trControl=train_control, method="nb")
# summarize results
print(model)

# make predictions
predictions <- predict(model, data_test[,1:4]) 

# summarize results
confusionMatrix(predictions, data_test$Species)

