2.4 Le package rpart
library(rpart)
treeDef <- rpart(type ~ ., data = spamApp)
print(treeDef, digits = 2)
plot(treeDef)
text(treeDef, xpd = TRUE)
set.seed(601334)
treeMax <- rpart(type ~ ., data = spamApp, minsplit = 2, cp = 0)
plot(treeMax)
treeMax$cptable
cpOpt <- treeMax$cptable[which.min(treeMax$cptable[, 4]), 1]
treeOpt <- prune(treeMax, cp = cpOpt)
plot(treeOpt)
text(treeOpt, xpd = TRUE, cex = 0.8)
thres1SE <- sum(treeMax$cptable[which.min(treeMax$cptable[, 4]), 4:5])
cp1SE <- treeMax$cptable[min(which(treeMax$cptable[, 4] <= thres1SE)), 1]
tree1SE <- prune(treeMax, cp = cp1SE)
plot(tree1SE)
text(tree1SE, xpd = TRUE, cex = 0.8)
errTestTreeMax <- mean(predict(treeMax, spamTest, type = "class") != spamTest$type)
errEmpTreeMax <- mean(predict(treeMax, spamApp, type = "class") != spamApp$type)