Wednesday, 15 April 2015

r - Modified package function produces new error -



r - Modified package function produces new error -

i modify function party / varimp.

the function takes long time run, , print progress within main loop.

i took code function after calling in r; edited code , pasted in in r override bundle original code.

my new code (the modif little part @ bottom marked 'my edit'):

varimp <- 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 multivariate response") if (conditional || pre1.0_0) { if (!all(complete.cases(inp@variables))) stop("cannot compute variable importance measure 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"), " non-unity weights might give misleading results") perror <- matrix(0, nrow = nperm * length(object@ensemble), ncol = length(xnames)) colnames(perror) <- xnames (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, bundle = "party") eoob <- error(p, oob) (j in unique(varids(tree))) { (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, bundle = "party") } else { p <- .call("r_predict", tree, inp, mincriterion, as.integer(j), bundle = "party") } perror[(per + (b - 1) * nperm), j] <- (error(p, oob) - eoob) } } ###################### # edit print(b) flush.console() ###################### } perror <- as.data.frame(perror) return(meandecreaseaccuracy = colmeans(perror)) }

but when using it, error:

> data.cforest.varimp <- varimp(data.cforest, conditional = true) error in unique(varids(tree)) : not find function "varids"

this error comes in part of code did not modify, not understand.

is problem in new code, or in way seek modify existing package?

yes joran's tip worked:

environment(varimp) <- asnamespace('party')

r

No comments:

Post a Comment