简体   繁体   中英

Modified package function produces new error

I would like to modify the function party / varimp.

The function takes a very long time to run, and I would like to print the progress inside the main loop.

I took the code from the function after calling it in R; I edited the code and pasted in in R to override the package original code.

My new code is (the only modif is a small part at the 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 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(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)
            }
        }

        ######################
        # MY EDIT
        print(b)
              flush.console()           
        ######################

    }
    perror <- as.data.frame(perror)
    return(MeanDecreaseAccuracy = colMeans(perror))
}

But when using it, I now get an error:

> data.cforest.varimp <- varimp(data.cforest, conditional = TRUE)
Error in unique(varIDs(tree)) : could not find function "varIDs"

This error comes in a part of the code that I did not modify, so I do not understand.

Is the problem in my new code, or in the way I try to modify an existing package?

是的,乔兰的秘诀起作用了:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM