# NOT RUN {
## data, packages, random seed
data("BBBClub", package = "evtree")
library("rpart")
suppressWarnings(RNGversion("3.5.0"))
set.seed(1090)
## learn trees
ev <- evtree(choice ~ ., data = BBBClub, minbucket = 10, maxdepth = 2)
rp <- as.party(rpart(choice ~ ., data = BBBClub, minbucket = 10, model = TRUE))
ct <- ctree(choice ~ ., data = BBBClub, minbucket = 10, mincrit = 0.99)
## visualization
plot(ev)
plot(rp)
plot(ct)
## accuracy: misclassification rate
mc <- function(obj) 1 - mean(predict(obj) == BBBClub$choice)
c("evtree" = mc(ev), "rpart" = mc(rp), "ctree" = mc(ct))
## complexity: number of terminal nodes
c("evtree" = width(ev), "rpart" = width(rp), "ctree" = width(ct))
## compare structure of predictions
ftable(tab <- table(evtree = predict(ev), rpart = predict(rp),
ctree = predict(ct), observed = BBBClub$choice))
## compare customer predictions only (absolute, proportion correct)
sapply(c("evtree", "rpart", "ctree"), function(nam) {
mt <- margin.table(tab, c(match(nam, names(dimnames(tab))), 4))
c(abs = as.vector(rowSums(mt))[2],
rel = round(100 * prop.table(mt, 1)[2, 2], digits = 3))
})
# }
Run the code above in your browser using DataLab