# Classification and Regression Trees
install.packages(c("rpart","rpart.plot","caret"))
# load the package
library(rpart)
library(rpart.plot)
library(caret)

# load data
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,]

# fit model con rpart
fit <- rpart(Species~., data=data_train, method = "class")

# summarize the fit
print(fit)

# plot tree
prp(fit, type = 2, extra = 104, nn = TRUE, 
    fallen.leaves = TRUE, faclen = 4, varlen = 8,
    shadow.col = "gray")

# make predictions
predictions <- predict(fit, data_test[,1:4], type="class")
predictions.prob <- predict(fit, data_test[,1:4], type="prob")
# summarize accuracy
table(predictions, data_test$Species,dnn = c("Observado", "Esperado"))



# trabajando con el k-fold con caret y clasificacion tree
install.packages(c("e1071","rattle"))
library(e1071,rattle)
# fit con caret
fit2 <- train(Species~., data=data_train, method="rpart", trControl=train_control)
# plot tree
library(rattle)
fancyRpartPlot(fit2$finalModel)

# make predictions
predictions <- predict(fit2, data_test[,1:4])
# summarize accuracy
table(predictions, data_test$Species)
