# Estimate accuracy using a train/test split.
install.packages(c("caret","klaR"))
# load the libraries
library(caret)
library(klaR)

# importar datos
# load the iris dataset
data(iris)
data <- iris

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

# Calculamos el algoritmos / modelo de predicción
# train a naive bayes model
model <- NaiveBayes(Species~., data=data_train)

# Predictions and Accuracy
# make predictions
x_test <- data_test[,1:4]
y_test <- data_test[,5]
predictions <- predict(model, x_test)
predictions$class
# summarize results
confusionMatrix(predictions$class, y_test)

