Skip to content
Snippets Groups Projects
Verified Commit 4826e7bf authored by Gärber, Florian's avatar Gärber, Florian
Browse files

refactor: `getTreeranger()` to use lapply, and include addLayer

parent 12b62e26
No related branches found
No related tags found
No related merge requests found
Type: Package
Package: RFSurrogates
Title: Surrogate Minimal Depth Variable Importance
Version: 0.3.3.9001
Version: 0.3.3.9002
Authors@R: c(
person("Stephan", "Seifert", , "stephan.seifert@uni-hamburg.de", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2567-5728")),
......
......@@ -4,6 +4,8 @@
#'
#' @param RF A [`ranger::ranger`] object which was created with `keep.inbag = TRUE`.
#' @param num.trees (Deprecated) Number of trees to convert (Default: `RF$num.trees`).
#' @param add_layer (Default: `FALSE`) Whether to [addLayer()] in the same loop.
#' @param num.threads (Default: 1) Number of threads to spawn for parallelization.
#'
#' @returns A list of tree data frames of length `RF$num.trees`.
#' Each row of the tree data frames corresponds to a node of the respective tree and the columns correspond to:
......@@ -14,10 +16,15 @@
#' * `splitpoint`: Split point of the split variable.
#' For categorical variables this is a comma separated lists of values, representing the factor levels (in the original order) going to the right.
#' * `status`: `0` for terminal (`splitpoint` is `NA`) and `1` for non-terminal.
#' * `layer`: If `add_layer` is `TRUE`, see [addLayer()]
#'
#' @export
getTreeranger <- function(RF, num.trees = RF$num.trees) {
lapply(1:num.trees, getsingletree, RF = RF)
getTreeranger <- function(RF, num.trees = RF$num.trees, add_layer = FALSE, num.threads = 1) {
parallel::mclapply(1:num.trees, getsingletree,
mc.cores = num.threads,
RF = RF,
add_layer = add_layer
)
}
#' getsingletree
......@@ -26,6 +33,7 @@ getTreeranger <- function(RF, num.trees = RF$num.trees) {
#'
#' @param RF A [`ranger::ranger`] object.
#' @param k Tree index to convert.
#' @param add_layer
#'
#' @returns A tree data frame for the `k`th tree in `RF`.
#' Each row of the tree data frames corresponds to a node of the respective tree and the columns correspond to:
......@@ -38,7 +46,7 @@ getTreeranger <- function(RF, num.trees = RF$num.trees) {
#' * `status`: `0` for terminal (`splitpoint` is `NA`) and `1` for non-terminal.
#'
#' @keywords internal
getsingletree <- function(RF, k = 1) {
getsingletree <- function(RF, k = 1, add_layer = FALSE) {
# here we use the treeInfo function of the ranger package to create extract the trees, in an earlier version this was done with a self implemented function
tree.ranger <- ranger::treeInfo(RF, tree = k)
ktree <- data.frame(
......@@ -55,5 +63,9 @@ getsingletree <- function(RF, k = 1) {
ktree[, 2:4][is.na(ktree[, 2:4])] <- 0
if (add_layer) {
ktree <- add_layer_to_tree(ktree)
}
return(ktree)
}
......@@ -4,12 +4,16 @@
\alias{getTreeranger}
\title{Get a list of structured trees from a ranger object.}
\usage{
getTreeranger(RF, num.trees = RF$num.trees)
getTreeranger(RF, num.trees = RF$num.trees, add_layer = FALSE, num.threads = 1)
}
\arguments{
\item{RF}{A \code{\link[ranger:ranger]{ranger::ranger}} object which was created with \code{keep.inbag = TRUE}.}
\item{num.trees}{(Deprecated) Number of trees to convert (Default: \code{RF$num.trees}).}
\item{add_layer}{(Default: \code{FALSE}) Whether to \code{\link[=addLayer]{addLayer()}} in the same loop.}
\item{num.threads}{(Default: 1) Number of threads to spawn for parallelization.}
}
\value{
A list of tree data frames of length \code{RF$num.trees}.
......@@ -22,6 +26,7 @@ Each row of the tree data frames corresponds to a node of the respective tree an
\item \code{splitpoint}: Split point of the split variable.
For categorical variables this is a comma separated lists of values, representing the factor levels (in the original order) going to the right.
\item \code{status}: \code{0} for terminal (\code{splitpoint} is \code{NA}) and \code{1} for non-terminal.
\item \code{layer}: If \code{add_layer} is \code{TRUE}, see \code{\link[=addLayer]{addLayer()}}
}
}
\description{
......
......@@ -4,12 +4,14 @@
\alias{getsingletree}
\title{getsingletree}
\usage{
getsingletree(RF, k = 1)
getsingletree(RF, k = 1, add_layer = FALSE)
}
\arguments{
\item{RF}{A \code{\link[ranger:ranger]{ranger::ranger}} object.}
\item{k}{Tree index to convert.}
\item{add_layer}{}
}
\value{
A tree data frame for the \code{k}th tree in \code{RF}.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment