# Small example to illustrate rpart() on the kyphosis data library("rpart") set.seed(784) kyphosis[sample(nrow(kyphosis), 4), ] fit <- rpart(Kyphosis ~ ., data = kyphosis, method = "class") fit printcp(fit) summary(fit) names(fit) if(FALSE) { postscript("kyphosis.eps", horiz = FALSE, onefile = FALSE, print.it = FALSE, width = 9, height = 5) } else { pdf("kyphosis.pdf", onefile = FALSE, width = 9, height = 5) } par(mar = c(5, 4, 2, 2) + 0.8, mfrow = c(1, 1), xpd = TRUE) plot(fit) text(fit) dev.off() pfit <- predict(fit, kyphosis, type = "class") tab <- table(with(kyphosis, Kyphosis), pfit) # Error checking: the table must be square if(!all(dimnames(tab)[[1]] == dimnames(tab)[[2]])) stop("not all dimnames are the same") # Obtain the (resubstitution) misclassification rate 1 - sum(diag(tab)) / sum(tab) # Other ways of obtaining the (resubstitution) misclassification rate rev(fit$cptable[,"rel error"])[1] * rev(fit$parms$prior)[1] tail(fit$cptable[,"rel error"], 1) * tail(fit$parms$prior, 1)