1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
validate.tree <- function(fit, method, B, bw, rule, type, sls, aics,
pr=TRUE, k, rand, xval = 10,
FUN, ...) {
wm <- if(inherits(fit,'tree'))'tree' else
if(inherits(fit,'rpart'))'rpart' else 'none'
if(wm=='none') stop("Not legitimate tree")
# if(.R. && wm=='rpart') require('maptree') # for prune.Rpart
if(missing(FUN))
FUN <- switch(wm,
tree=prune.tree,
rpart=function(...,k)prune.rpart(...,cp=k))
act <- (fit$call)$na.action
if(!length(act))
act <- function(x) x
m <- model.frame(fit, na.action = act)
if(!is.data.frame(m)) stop('you must specify model=T in the fit')
y <- model.extract(m, response)
binary <- is.logical(y) ||
((length(un <- sort(unique(y[!is.na(y)]))) ==
2) && un[1] == 0 && un[2] == 1)
if(binary && is.factor(y)) y <- as.numeric(y) - 1 ## 12dec02
call <- match.call()
method <- call$method
size <- NULL
if(missing(k)) {
if(wm=='tree') {
ff <- FUN(fit, ...)
k <- ff$k
size <- ff$size[is.finite(k)]
k <- k[is.finite(k)] # tree makes first k -Inf 7dec02
} else {
k <- fit$cptable[,'CP']
size <- fit$cptable[,'nsplit']
}
}
if(missing(rand))
rand <- sample(xval, length(m[[1]]), replace = TRUE)
which <- unique(rand)
pdyx.app <- pdyx.val <- pb.app <- pb.val <- double(length(k))
l <- 0
for(kk in k) {
l <- l + 1
dyx.val <- dyx.app <- b.val <- b.app <- double(length(which))
j <- 0
for(i in which) {
j <- j + 1
s <- rand != i
tlearn <- switch(wm,tree=tree(model=m[s,]),rpart=rpart(model=m[s,]))
papp <- if(kk == 0) tlearn else FUN(tlearn, k = kk, ...)
if(nrow(papp$frame) == 1) {
dyx.app[j] <- dyx.val[j] <- 0 #no splits
b.app[j] <- b.val[j] <- mean((y - mean(y))^2, na.rm = TRUE)
}
else {
yhat <- predict(papp, newdata = m[s, ])
if(is.matrix(yhat) && ncol(yhat) > 1)
yhat <- yhat[,ncol(yhat),drop=TRUE]
## tree with factor binary y 7dec02
b.app[j] <- mean((yhat - y[s])^2)
dyx.app[j] <- if(binary) somers2(yhat, y[s])["Dxy"] else
rcorr.cens(yhat, y[s])["Dxy"]
s <- rand == i
yhat <- predict(papp, newdata = m[s, ])
b.val[j] <- mean((yhat - y[s])^2)
dyx.val[j] <- if(binary) somers2(yhat, y[s])["Dxy"] else
rcorr.cens(yhat, y[s])["Dxy"]
}
}
pdyx.app[l] <- mean(dyx.app)
pdyx.val[l] <- mean(dyx.val)
pb.app[l] <- mean(b.app)
pb.val[l] <- mean(b.val)
if(pr) {
dyx.app <- c(dyx.app, pdyx.app[l])
dyx.val <- c(dyx.val, pdyx.val[l])
b.app <- c(b.app, pb.app[l])
b.val <- c(b.val, pb.val[l])
cat("\n\nk=", format(kk), ":\n\n")
dyx <- cbind(dyx.app, dyx.val, b.app, b.val)
dimnames(dyx) <- list(c(as.character(1:j), "Mean"),
c("Dxy Training", "Dxy Test", "MSE Training",
"MSE Test"))
print(dyx)
}
}
structure(list(k = k, size = size, dxy.app = pdyx.app, dxy.val =
pdyx.val, mse.app = pb.app, mse.val = pb.val,
binary = binary, xval = xval),
class = "validate.tree")
}
print.validate.tree <- function(x, ...) {
cat(x$xval, "-fold cross-validation\n\n", sep = "")
w <- cbind(k = x$k, size = x$size, Dxy.apparent = x$dxy.app,
Dxy.val = x$dxy.val, MSE.apparent = x$mse.app, MSE.val =
x$mse.val)
if(x$binary)
dimnames(w) <- list(NULL, c("k", if(length(x$size)) "size",
"Dxy.apparent", "Dxy.val", "Brier.apparent",
"Brier.val"))
invisible(print(w))
}
plot.validate.tree <- function(x, what = c("mse", "dxy"),
legendloc = locator, ...) {
obj <- x
if(length(obj$size)) {
x <- obj$size
xlab <- "Number of Nodes"
}
else {
x <- obj$k
xlab <- "Cost/Complexity Parameter"
}
if("mse" %in% what) {
blab <- if(obj$binary) "Brier Score" else "Mean Squared Error"
ylim <- range(c(obj$mse.app, obj$mse.val))
plot(x, obj$mse.app, xlab = xlab, ylab = blab, ylim = ylim,
type = "n")
lines(x, obj$mse.app, lty = 3)
lines(x, obj$mse.val, lty = 1)
title(sub = paste(obj$xval, "-fold cross-validation", sep = ""),
adj = 0)
if(is.function(legendloc))
legend(legendloc(1), c("Apparent", "Cross-validated"),
lty = c(3, 1), bty = "n") else {
par(usr=c(0,1,0,1))
legend(legendloc[1],legendloc[2],
c("Apparent", "Cross-validated"),
lty = c(3, 1), bty = "n")
}
}
if("dxy" %in% what) {
ylim <- range(c(obj$dxy.app, obj$dxy.val))
plot(x, obj$dxy.app, xlab = xlab, ylab = "Somers' Dxy", ylim =
ylim, type = "n")
lines(x, obj$dxy.app, lty = 3)
lines(x, obj$dxy.val, lty = 1)
title(sub = paste(obj$xval, "-fold cross-validation", sep = ""),
adj = 0)
if(is.function(legendloc))
legend(legendloc(1), c("Apparent", "Cross-validated"),
lty = c(3, 1), bty = "n") else {
par(usr=c(0,1,0,1))
legend(legendloc[1],legendloc[2],
c("Apparent", "Cross-validated"),
lty = c(3, 1), bty = "n")
}
}
invisible()
}
validate.rpart <- function(fit, ...) validate.tree(fit, ...)
|