R/varUsed.R

Defines functions varUsed

Documented in varUsed

varUsed <- function(x, by.tree=FALSE, count=TRUE) {
    if (!inherits(x, "randomForest"))
        stop(deparse(substitute(x)), "is not a randomForest object")
    if (is.null(x$forest))
        stop(deparse(substitute(x)), "does not contain forest")
    
    p <- length(x$forest$ncat)  # Total number of variables.
    if (count) {
        if (by.tree) {
            v <- apply(x$forest$bestvar, 2, function(x) {
                xx <- numeric(p)
                y <- table(x[x>0])
                xx[as.numeric(names(y))] <- y
                xx
            })
        } else {
            v <- numeric(p)
            vv <- table(x$forest$bestvar[x$forest$bestvar > 0])
            v[as.numeric(names(vv))] <- vv
        }
    } else {
        v <- apply(x$forest$bestvar, 2, function(x) sort(unique(x[x>0])))
        if(!by.tree) v <- sort(unique(unlist(v)))
    }
    v
}

Try the extendedForest package in your browser

Any scripts or data that you put into this service are public.

extendedForest documentation built on Dec. 12, 2023, 3 p.m.