Coercion Functions

Description

Functions coercing various forest objects to objects of class “stabletree”.

Usage

  as.stabletree(x, ...)
  ## S3 method for class 'randomForest'
as.stabletree(x, applyfun = NULL, cores = NULL, ...)
  ## S3 method for class 'RandomForest'
as.stabletree(x, applyfun = NULL, cores = NULL, ...)
  ## S3 method for class 'cforest'
as.stabletree(x, applyfun = NULL, cores = NULL, savetrees = FALSE, ...)
  ## S3 method for class 'ranger'
as.stabletree(x, applyfun = NULL, cores = NULL, ...)

Arguments

x an object of class randomForest, RandomForest-class, cforest, or ranger.
applyfun a lapply-like function. The default is to use lapply unless cores is specified in which case mclapply is used (for multicore computations on platforms that support these).
cores integer. The number of cores to use in multicore computations using mclapply (see above).
savetrees logical. If TRUE, trees of the forests are returned.
additional arguments (currently not used).

Details

Random forests fitted using randomForest, cforest, cforest or ranger are coerced to “stabletree” objects.

Note that when plotting a randomForest or ranger, the gray areas of levels of a nominal variable do not mimic exactly the same behavior as for classical “stabletree” objects, due to randomForest and ranger, not storing any information whether any individuals were left fulfilling the splitting criterion in the subsample. Therefore, gray areas only indicate that this level of this variable has already been used in a split before in such a way that it could not be used for any further splits.

For ranger, interaction terms are (currently) not supported.

Value

as.stabletree returns an object of class “stabletree” which is a list with the following components:

call the call from the model object x,
B the number of trees of the random forest,
sampler the random forest fitting function,
vs0 numeric vector of the variable selections of the original tree, here always a vector of zeros because there is no original tree,
br0 always NULL (only included for consistency),
vs numeric matrix of the variable selections for each tree of the random forest,
br list of the break points (only the breaks for each variable over all trees of the random forest,
classes character vector indicating the classes of all partitioning variables,
trees a list of tree objects of class “party”, or NULL.

See Also

stabletree, plot.stabletree

Examples

library("stablelearner")



## build a randomForest using randomForest
library(randomForest)
set.seed(1)
rf <- randomForest(Species ~ ., data = iris)

## coerce to a stabletree
srf <- as.stabletree(rf)
print(srf)

Call:
randomForest(formula = Species ~ ., data = iris)

Sampler:
B = 500 
Method = randomForest::randomForest
summary(srf, original = FALSE) # there is no original tree

Call:
randomForest(formula = Species ~ ., data = iris)

Sampler:
B = 500 
Method = randomForest::randomForest

Variable selection overview:

              freq  mean
Petal.Length 0.988 2.566
Petal.Width  0.988 2.514
Sepal.Length 0.816 1.542
Sepal.Width  0.736 1.126
barplot(srf)

image(srf)

plot(srf)

## build a RandomForest using party
library("party")
set.seed(2)
cf_party <- cforest(Species ~ ., data = iris,
  control = cforest_unbiased(mtry = 2))

## coerce to a stabletree
scf_party <- as.stabletree(cf_party)
print(scf_party)

Call:

A ModelEnvFormula with 

  response variable(s):   Species 
  input variable(s):      Sepal.Length Sepal.Width Petal.Length Petal.Width 
  number of observations: 150 


Sampler:
B = 500 
Method = party::cforest
summary(scf_party, original = FALSE)

Call:

A ModelEnvFormula with 

  response variable(s):   Species 
  input variable(s):      Sepal.Length Sepal.Width Petal.Length Petal.Width 
  number of observations: 150 


Sampler:
B = 500 
Method = party::cforest

Variable selection overview:

              freq  mean
Petal.Width  0.928 1.690
Petal.Length 0.904 1.696
Sepal.Length 0.492 0.590
Sepal.Width  0.170 0.192
barplot(scf_party)

image(scf_party)

plot(scf_party)

## build a cforest using partykit
library("partykit")
set.seed(3)
cf_partykit <- cforest(Species ~ ., data = iris)

## coerce to a stabletree
scf_partykit <- as.stabletree(cf_partykit)
print(scf_partykit)

Call:
cforest(formula = Species ~ ., data = iris)

Sampler:
B = 500 
Method = partykit::cforest
summary(scf_partykit, original = FALSE)

Call:
cforest(formula = Species ~ ., data = iris)

Sampler:
B = 500 
Method = partykit::cforest

Variable selection overview:

              freq  mean
Petal.Width  0.924 1.680
Petal.Length 0.906 1.680
Sepal.Length 0.492 0.598
Sepal.Width  0.180 0.200
barplot(scf_partykit)

image(scf_partykit)

plot(scf_partykit)

## build a random forest using ranger
library("ranger")
set.seed(4)
rf_ranger <- ranger(Species ~ ., data = iris)

## coerce to a stabletree
srf_ranger <- as.stabletree(rf_ranger)
print(srf_ranger)

Call:
ranger(Species ~ ., data = iris)

Sampler:
B = 500 
Method = ranger::ranger
summary(srf_ranger, original = FALSE)

Call:
ranger(Species ~ ., data = iris)

Sampler:
B = 500 
Method = ranger::ranger

Variable selection overview:

              freq  mean
Petal.Length 0.982 2.616
Petal.Width  0.974 2.512
Sepal.Length 0.812 1.580
Sepal.Width  0.732 1.210
barplot(srf_ranger)

image(srf_ranger)

plot(srf_ranger)