# Classification and Regression Trees

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


# load data
data(longley)
data <- longley


# fit model con rpart
fit <- rpart(Employed~., data=data,control=rpart.control(minsplit=5))

# summarize the fit
print(fit)

# plot tree
prp(fit)




# trabajando con el k-fold con caret y clasificacion tree
install.packages(c("e1071","rattle"))
library(e1071,rattle)
# fit con caret
fit2 <- train(Employed~Unemployed+Armed.Forces+Year+GNP, data=data, method="rpart",control =rpart.control(minsplit=5) )
# plot tree
library(rattle)
fancyRpartPlot(fit2$finalModel)
