Dear S-Plus users:
I was able to solve my problem. In short, I needed the "snip.tree" command
rather than the "prune.tree" command.
I enclose my function below, for anyone interested.
Andy Liaw pointed out that the relative measures of importance of variables
based on deviance reduction by internal splits in a tree
should be used with caution. In particular, if the input variables
for a tree are of mixed type (eg continuous versus categorical) such
measure may be severely biased since one type of variable may
favor more splits than the other type.
Thanks again,
Ernst.
# Calculates relative importance of variables in a tree
# Provides a table (vector) of the contribution of each variable
rel.impo <- function(tree1)
{
# Sequence of internal nodes
intern.fr <- tree1$frame[tree1$frame$var != '<leaf>',]
nodes <- row.names(intern.fr)
vars <- as.character(intern.fr$var)
n.intern <- nrow(intern.fr)
# Calculate deviance of recursively snipped subtrees
devs <- rep(deviance(tree1),(n.intern+1))
for (i in 2 : n.intern)
devs[i] <- deviance(snip.tree(tree1,nodes=nodes[(n.intern+2-i)]))
devs[n.intern+1] <- tree1$frame$dev[1]
# Calculate differences in deviances and rearrange terms in correct sequence
impo <- diff(devs)
vars <- vars[n.intern : 1]
tapply(impo,vars,sum)
}
--
****************************************************************
Ernst Linder elinder@math.unh.edu
Department of Mathematics and Statistics 603 - 862- 2687
University of New Hampshire Fax: 603 - 862 - 4096
Durham, NH 03824 www.math.unh.edu/~elinder
****************************************************************
|