Description Usage Arguments Details Value References Examples
An implementation of the random forest and bagging ensemble algorithms utilizing conditional inference trees as base learners.
1 2 3 4 5 6 7 8 9 10 11 12 13 14  cforest(formula, data, weights, subset, offset, cluster, strata,
na.action = na.pass,
control = ctree_control(teststat = "quad", testtype = "Univ",
mincriterion = 0, saveinfo = FALSE, ...),
ytrafo = NULL, scores = NULL, ntree = 500L,
perturb = list(replace = FALSE, fraction = 0.632),
mtry = ceiling(sqrt(nvar)), applyfun = NULL, cores = NULL,
trace = FALSE, ...)
## S3 method for class 'cforest'
predict(object, newdata = NULL,
type = c("response", "prob", "weights", "node"),
OOB = FALSE, FUN = NULL, simplify = TRUE, scale = TRUE, ...)
## S3 method for class 'cforest'
gettree(object, tree = 1L, ...)

formula 
a symbolic description of the model to be fit. 
data 
a data frame containing the variables in the model. 
subset 
an optional vector specifying a subset of observations to be used in the fitting process. 
weights 
an optional vector of weights to be used in the fitting
process. Nonnegative integer valued weights are
allowed as well as nonnegative real weights.
Observations are sampled (with or without replacement)
according to probabilities 
offset 
an optional vector of offset values. 
cluster 
an optional factor indicating independent clusters. Highly experimental, use at your own risk. 
strata 
an optional factor for stratified sampling. 
na.action 
a function which indicates what should happen when the data contain missing value. 
control 
a list with control parameters, see

ytrafo 
an optional named list of functions to be applied to the response
variable(s) before testing their association with the explanatory
variables. Note that this transformation is only
performed once for the root node and does not take weights into account
(which means, the forest bootstrap or subsetting is ignored,
which is almost certainly not a good idea).
Alternatively, 
scores 
an optional named list of scores to be attached to ordered factors. 
ntree 
Number of trees to grow for the forest. 
perturb 
a list with arguments 
mtry 
number of input variables randomly sampled as candidates
at each node for random forest like algorithms. Bagging, as special case
of a random forest without random input variable sampling, can
be performed by setting 
applyfun 
an optional 
cores 
numeric. If set to an integer the 
trace 
a logical indicating if a progress bar shall be printed while the forest grows. 
object 
An object as returned by 
newdata 
An optional data frame containing test data. 
type 
a character string denoting the type of predicted value
returned, ignored when argument 
OOB 
a logical defining outofbag predictions (only if 
FUN 
a function to compute summary statistics. Predictions for each node have to be
computed based on arguments 
simplify 
a logical indicating whether the resulting list of predictions should be converted to a suitable vector or matrix (if possible). 
scale 
a logical indicating scaling of the nearest neighbor weights by the sum of weights in the corresponding terminal node of each tree. In the simple regression forest, predicting the conditional mean by nearest neighbor weights will be equivalent to (but slower!) the aggregation of means. 
tree 
an integer, the number of the tree to extract from the forest. 
... 
additional arguments. 
This implementation of the random forest (and bagging) algorithm differs
from the reference implementation in randomForest
with respect to the base learners used and the aggregation scheme applied.
Conditional inference trees, see ctree
, are fitted to each
of the ntree
perturbed samples of the learning sample. Most of the hyper parameters in
ctree_control
regulate the construction of the conditional inference trees.
Hyper parameters you might want to change are:
1. The number of randomly preselected variables mtry
, which is fixed
to the square root of the number of input variables.
2. The number of trees ntree
. Use more trees if you have more variables.
3. The depth of the trees, regulated by mincriterion
. Usually unstopped and unpruned
trees are used in random forests. To grow large trees, set mincriterion
to a small value.
The aggregation scheme works by averaging observation weights extracted
from each of the ntree
trees and NOT by averaging predictions directly
as in randomForest
.
See Hothorn et al. (2004) and Meinshausen (2006) for a description.
Predictions can be computed using predict
. For observations
with zero weights, predictions are computed from the fitted tree
when newdata = NULL
.
Ensembles of conditional inference trees have not yet been extensively
tested, so this routine is meant for the expert user only and its current
state is rather experimental. However, there are some things available
in cforest
that can't be done with randomForest
,
for example fitting forests to censored response variables (see Hothorn et al., 2004, 2006a) or to
multivariate and ordered responses. Using the rich partykit
infrastructure allows
additional functionality in cforest
, such as parallel tree growing and probabilistic
forecasting (for example via quantile regression forests). Also plotting of single trees from
a forest is much easier now.
Unlike cforest
, cforest
is entirely written in R which
makes customisation much easier at the price of longer computing times. However, trees
can be grown in parallel with this R only implemention which renders speed less of an issue.
Note that the default values are different from those used in package party
, most
importantly the default for mtry is now datadependent. predict(, type = "node")
replaces
the where
function and predict(, type = "prob")
the
treeresponse
function.
Moreover, when predictors vary in their scale of measurement of number
of categories, variable selection and computation of variable importance is biased
in favor of variables with many potential cutpoints in randomForest
,
while in cforest
unbiased trees and an adequate resampling scheme
are used by default. See Hothorn et al. (2006b) and Strobl et al. (2007)
as well as Strobl et al. (2009).
An object of class cforest
.
Breiman L (2001). Random Forests. Machine Learning, 45(1), 5–32.
Hothorn T, Lausen B, Benner A, RadespielTroeger M (2004). Bagging Survival Trees. Statistics in Medicine, 23(1), 77–91.
Hothorn T, Buehlmann P, Dudoit S, Molinaro A, Van der Laan MJ (2006a). Survival Ensembles. Biostatistics, 7(3), 355–373.
Hothorn T, Hornik K, Zeileis A (2006b). Unbiased Recursive Partitioning: A Conditional Inference Framework. Journal of Computational and Graphical Statistics, 15(3), 651–674.
Hothorn T, Zeileis A (2015). partykit: A Modular Toolkit for Recursive Partytioning in R. Journal of Machine Learning Research, 16, 3905–3909.
Meinshausen N (2006). Quantile Regression Forests. Journal of Machine Learning Research, 7, 983–999.
Strobl C, Boulesteix AL, Zeileis A, Hothorn T (2007). Bias in Random Forest Variable Importance Measures: Illustrations, Sources and a Solution. BMC Bioinformatics, 8, 25. doi: 10.1186/14712105825
Strobl C, Malley J, Tutz G (2009). An Introduction to Recursive Partitioning: Rationale, Application, and Characteristics of Classification and Regression Trees, Bagging, and Random Forests. Psychological Methods, 14(4), 323–348.
Stefan Wager & Susan Athey (2018). Estimation and Inference of Heterogeneous Treatment Effects using Random Forests. Journal of the American Statistical Association, 113(523), 1228–1242. doi: 10.1080/01621459.2017.1319839
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61  ## basic example: conditional inference forest for cars data
cf < cforest(dist ~ speed, data = cars)
## prediction of fitted mean and visualization
nd < data.frame(speed = 4:25)
nd$mean < predict(cf, newdata = nd, type = "response")
plot(dist ~ speed, data = cars)
lines(mean ~ speed, data = nd)
## predict quantiles (aka quantile regression forest)
myquantile < function(y, w) quantile(rep(y, w), probs = c(0.1, 0.5, 0.9))
p < predict(cf, newdata = nd, type = "response", FUN = myquantile)
colnames(p) < c("lower", "median", "upper")
nd < cbind(nd, p)
## visualization with conditional (on speed) prediction intervals
plot(dist ~ speed, data = cars, type = "n")
with(nd, polygon(c(speed, rev(speed)), c(lower, rev(upper)),
col = "lightgray", border = "transparent"))
points(dist ~ speed, data = cars)
lines(mean ~ speed, data = nd, lwd = 1.5)
lines(median ~ speed, data = nd, lty = 2, lwd = 1.5)
legend("topleft", c("mean", "median", "10%  90% quantile"),
lwd = c(1.5, 1.5, 10), lty = c(1, 2, 1),
col = c("black", "black", "lightgray"), bty = "n")
### we may also use predicted conditional (on speed) densities
mydensity < function (y, w) approxfun(density(y, weights = w/sum(w))[1:2], rule = 2)
pd < predict(cf, newdata = nd, type = "response", FUN = mydensity)
## visualization in heatmap (instead of scatterplot)
## with fitted curves as above
dist < 10:150
dens < t(sapply(seq_along(pd), function(i) pd[[i]](dist)))
image(nd$speed, dist, dens, xlab = "speed", col = rev(gray.colors(9)))
lines(mean ~ speed, data = nd, lwd = 1.5)
lines(median ~ speed, data = nd, lty = 2, lwd = 1.5)
lines(lower ~ speed, data = nd, lty = 2)
lines(upper ~ speed, data = nd, lty = 2)
## Not run:
### honest (i.e., outofbag) crossclassification of
### true vs. predicted classes
data("mammoexp", package = "TH.data")
table(mammoexp$ME, predict(cforest(ME ~ ., data = mammoexp, ntree = 50),
OOB = TRUE, type = "response"))
### fit forest to censored response
if (require("TH.data") && require("survival")) {
data("GBSG2", package = "TH.data")
bst < cforest(Surv(time, cens) ~ ., data = GBSG2, ntree = 50)
### estimate conditional KaplanMeier curves
print(predict(bst, newdata = GBSG2[1:2,], OOB = TRUE, type = "prob"))
print(gettree(bst))
}
## End(Not run)

Loading required package: grid
Loading required package: libcoin
Loading required package: mvtnorm
Never Within a Year Over a Year
Never 206 27 1
Within a Year 57 46 1
Over a Year 52 22 0
Loading required package: TH.data
Loading required package: survival
Loading required package: MASS
Attaching package: 'TH.data'
The following object is masked from 'package:MASS':
geyser
$`1`
Call: survfit(formula = y ~ 1, weights = w, subset = w > 0)
records n events median 0.95LCL 0.95UCL
189.0 50.0 29.2 1528.0 891.0 1990.0
$`2`
Call: survfit(formula = y ~ 1, weights = w, subset = w > 0)
records n events median 0.95LCL 0.95UCL
206.0 50.0 26.3 1701.0 1146.0 2018.0
Model formula:
Surv(time, cens) ~ horTh + age + menostat + tsize + tgrade +
pnodes + progrec + estrec
Fitted party:
[1] root
 [2] pnodes <= 3
  [3] tgrade <= I
   [4] tsize <= 22: Inf (n = 15)
   [5] tsize > 22: Inf (n = 16)
  [6] tgrade > I
   [7] horTh in no
    [8] menostat in Pre
     [9] pnodes <= 1
      [10] estrec <= 4: 1477.500 (n = 8)
      [11] estrec > 4
       [12] age <= 43: Inf (n = 10)
       [13] age > 43: Inf (n = 18)
     [14] pnodes > 1
      [15] tsize <= 28
       [16] age <= 45: 2034.000 (n = 14)
       [17] age > 45: Inf (n = 9)
      [18] tsize > 28: 2093.000 (n = 15)
    [19] menostat in Post
     [20] progrec <= 179
      [21] progrec <= 80
       [22] pnodes <= 1
        [23] tsize <= 27: 1192.000 (n = 9)
        [24] tsize > 27: 1525.000 (n = 11)
       [25] pnodes > 1: 1601.000 (n = 15)
      [26] progrec > 80: 889.000 (n = 11)
     [27] progrec > 179: Inf (n = 10)
   [28] horTh in yes
    [29] pnodes <= 2
     [30] progrec <= 5: Inf (n = 12)
     [31] progrec > 5
      [32] tsize <= 24: Inf (n = 18)
      [33] tsize > 24: Inf (n = 16)
    [34] pnodes > 2: 1918.000 (n = 16)
 [35] pnodes > 3
  [36] progrec <= 19
   [37] tgrade <= II
    [38] pnodes <= 9
     [39] pnodes <= 5: 1183.000 (n = 17)
     [40] pnodes > 5
      [41] estrec <= 0: 698.000 (n = 10)
      [42] estrec > 0: Inf (n = 15)
    [43] pnodes > 9
     [44] estrec <= 16: 790.000 (n = 11)
     [45] estrec > 16: 480.500 (n = 10)
   [46] tgrade > II
    [47] age <= 49: 397.500 (n = 14)
    [48] age > 49
     [49] age <= 54: 1587.000 (n = 10)
     [50] age > 54: 548.000 (n = 14)
  [51] progrec > 19
   [52] horTh in no
    [53] tsize <= 34
     [54] pnodes <= 10
      [55] tsize <= 20: 1337.000 (n = 12)
      [56] tsize > 20: 2286.000 (n = 19)
     [57] pnodes > 10: 1170.000 (n = 7)
    [58] tsize > 34
     [59] tsize <= 40: 867.000 (n = 15)
     [60] tsize > 40: 1151.000 (n = 12)
   [61] horTh in yes
    [62] age <= 54
     [63] menostat in Pre: Inf (n = 12)
     [64] menostat in Post: Inf (n = 10)
    [65] age > 54
     [66] age <= 64: 956.000 (n = 14)
     [67] age > 64: Inf (n = 8)
Number of inner nodes: 33
Number of terminal nodes: 34
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.