从@Alex 的建议开始,我在party:::varimp
. 此命令计算标准(平均降低精度)和条件变量重要性(VI),cforest
并且可以轻松修改以计算森林中每棵树的 VI。
set.seed(12345)
y <- cforest(score ~ ., data = readingSkills,
control = cforest_unbiased(mtry = 2, ntree = 10))
varimp_ctrees <- function (object, mincriterion = 0, conditional = FALSE,
threshold = 0.2, nperm = 1, OOB = TRUE, pre1.0_0 = conditional) {
response <- object@responses
if (length(response@variables) == 1 && inherits(response@variables[[1]],
"Surv"))
return(varimpsurv(object, mincriterion, conditional,
threshold, nperm, OOB, pre1.0_0))
input <- object@data@get("input")
xnames <- colnames(input)
inp <- initVariableFrame(input, trafo = NULL)
y <- object@responses@variables[[1]]
if (length(response@variables) != 1)
stop("cannot compute variable importance measure for multivariate response")
if (conditional || pre1.0_0) {
if (!all(complete.cases(inp@variables)))
stop("cannot compute variable importance measure with missing values")
}
CLASS <- all(response@is_nominal)
ORDERED <- all(response@is_ordinal)
if (CLASS) {
error <- function(x, oob) mean((levels(y)[sapply(x, which.max)] !=
y)[oob])
} else {
if (ORDERED) {
error <- function(x, oob) mean((sapply(x, which.max) !=
y)[oob])
} else {
error <- function(x, oob) mean((unlist(x) - y)[oob]^2)
}
}
w <- object@initweights
if (max(abs(w - 1)) > sqrt(.Machine$double.eps))
warning(sQuote("varimp"), " with non-unity weights might give misleading results")
perror <- matrix(0, nrow = nperm * length(object@ensemble),
ncol = length(xnames))
colnames(perror) <- xnames
for (b in 1:length(object@ensemble)) {
tree <- object@ensemble[[b]]
if (OOB) {
oob <- object@weights[[b]] == 0
} else {
oob <- rep(TRUE, length(y))
}
p <- .Call("R_predict", tree, inp, mincriterion, -1L,
PACKAGE = "party")
eoob <- error(p, oob)
for (j in unique(party:::varIDs(tree))) {
for (per in 1:nperm) {
if (conditional || pre1.0_0) {
tmp <- inp
ccl <- create_cond_list(conditional, threshold,
xnames[j], input)
if (is.null(ccl)) {
perm <- sample(which(oob))
} else {
perm <- conditional_perm(ccl, xnames, input,
tree, oob)
}
tmp@variables[[j]][which(oob)] <- tmp@variables[[j]][perm]
p <- .Call("R_predict", tree, tmp, mincriterion,
-1L, PACKAGE = "party")
} else {
p <- .Call("R_predict", tree, inp, mincriterion,
as.integer(j), PACKAGE = "party")
}
perror[(per + (b - 1) * nperm), j] <- (error(p,
oob) - eoob)
}
}
}
perror <- as.data.frame(perror)
return(list(MeanDecreaseAccuracy = colMeans(perror), VIMcTrees=perror))
}
VIMcTrees
是一个矩阵,其行数等于森林树的数量,并且每个解释变量都有一列。该矩阵的(i,j)元素是第i个树中第j个变量的 VI。
varimp_ctrees(y)$VIMcTrees
nativeSpeaker age shoeSize
1 4.853855 30.06969 52.271824
2 15.740311 70.55825 5.409772
3 17.022082 113.86020 0.000000
4 22.003119 19.62134 50.634286
5 6.070659 28.58817 47.049866
6 16.508634 105.50321 2.302387
7 11.487349 31.80002 46.147677
8 19.250631 27.78282 43.589832
9 19.669478 98.73722 0.483079
10 11.748669 85.95768 5.812538