ranger (version 0.12.1)

ranger: Ranger


Ranger is a fast implementation of random forests (Breiman 2001) or recursive partitioning, particularly suited for high dimensional data. Classification, regression, and survival forests are supported. Classification and regression forests are implemented as in the original Random Forest (Breiman 2001), survival forests as in Random Survival Forests (Ishwaran et al. 2008). Includes implementations of extremely randomized trees (Geurts et al. 2006) and quantile regression forests (Meinshausen 2006).


  formula = NULL,
  data = NULL,
  num.trees = 500,
  mtry = NULL,
  importance = "none",
  write.forest = TRUE,
  probability = FALSE,
  min.node.size = NULL,
  max.depth = NULL,
  replace = TRUE,
  sample.fraction = ifelse(replace, 1, 0.632),
  case.weights = NULL,
  class.weights = NULL,
  splitrule = NULL,
  num.random.splits = 1,
  alpha = 0.5,
  minprop = 0.1,
  split.select.weights = NULL,
  always.split.variables = NULL,
  respect.unordered.factors = NULL,
  scale.permutation.importance = FALSE,
  local.importance = FALSE,
  regularization.factor = 1,
  regularization.usedepth = FALSE,
  keep.inbag = FALSE,
  inbag = NULL,
  holdout = FALSE,
  quantreg = FALSE,
  oob.error = TRUE,
  num.threads = NULL,
  save.memory = FALSE,
  verbose = TRUE,
  seed = NULL,
  dependent.variable.name = NULL,
  status.variable.name = NULL,
  classification = NULL,
  x = NULL,
  y = NULL



Object of class formula or character describing the model to fit. Interaction terms supported only for numerical variables.


Training data of class data.frame, matrix, dgCMatrix (Matrix) or gwaa.data (GenABEL).


Number of trees.


Number of variables to possibly split at in each node. Default is the (rounded down) square root of the number variables. Alternatively, a single argument function returning an integer, given the number of independent variables.


Variable importance mode, one of 'none', 'impurity', 'impurity_corrected', 'permutation'. The 'impurity' measure is the Gini index for classification, the variance of the responses for regression and the sum of test statistics (see splitrule) for survival.


Save ranger.forest object, required for prediction. Set to FALSE to reduce memory usage if no prediction intended.


Grow a probability forest as in Malley et al. (2012).


Minimal node size. Default 1 for classification, 5 for regression, 3 for survival, and 10 for probability.


Maximal tree depth. A value of NULL or 0 (the default) corresponds to unlimited depth, 1 to tree stumps (1 split per tree).


Sample with replacement.


Fraction of observations to sample. Default is 1 for sampling with replacement and 0.632 for sampling without replacement. For classification, this can be a vector of class-specific values.


Weights for sampling of training observations. Observations with larger weights will be selected with higher probability in the bootstrap (or subsampled) samples for the trees.


Weights for the outcome classes (in order of the factor levels) in the splitting rule (cost sensitive learning). Classification and probability prediction only. For classification the weights are also applied in the majority vote in terminal nodes.


Splitting rule. For classification and probability estimation "gini", "extratrees" or "hellinger" with default "gini". For regression "variance", "extratrees", "maxstat" or "beta" with default "variance". For survival "logrank", "extratrees", "C" or "maxstat" with default "logrank".


For "extratrees" splitrule.: Number of random splits to consider for each candidate splitting variable.


For "maxstat" splitrule: Significance threshold to allow splitting.


For "maxstat" splitrule: Lower quantile of covariate distribution to be considered for splitting.


Numeric vector with weights between 0 and 1, representing the probability to select variables for splitting. Alternatively, a list of size num.trees, containing split select weight vectors for each tree can be used.


Character vector with variable names to be always selected in addition to the mtry variables tried for splitting.


Handling of unordered factor covariates. One of 'ignore', 'order' and 'partition'. For the "extratrees" splitrule the default is "partition" for all other splitrules 'ignore'. Alternatively TRUE (='order') or FALSE (='ignore') can be used. See below for details.


Scale permutation importance by standard error as in (Breiman 2001). Only applicable if permutation variable importance mode selected.


Calculate and return local importance values as in (Breiman 2001). Only applicable if importance is set to 'permutation'.


Regularization factor (gain penalization), either a vector of length p or one value for all variables.


Consider the depth in regularization.


Save how often observations are in-bag in each tree.


Manually set observations per tree. List of size num.trees, containing inbag counts for each observation. Can be used for stratified sampling.


Hold-out mode. Hold-out all samples with case weight 0 and use these for variable importance and prediction error.


Prepare quantile prediction as in quantile regression forests (Meinshausen 2006). Regression only. Set keep.inbag = TRUE to prepare out-of-bag quantile prediction.


Compute OOB prediction error. Set to FALSE to save computation time, e.g. for large survival forests.


Number of threads. Default is number of CPUs available.


Use memory saving (but slower) splitting mode. No effect for survival and GWAS data. Warning: This option slows down the tree growing, use only if you encounter memory problems.


Show computation status and estimated runtime.


Random seed. Default is NULL, which generates the seed from R. Set to 0 to ignore the R seed.


Name of dependent variable, needed if no formula given. For survival forests this is the time variable.


Name of status variable, only applicable to survival data and needed if no formula given. Use 1 for event and 0 for censoring.


Set to TRUE to grow a classification forest. Only needed if the data is a matrix or the response numeric.


Predictor data (independent variables), alternative interface to data with formula or dependent.variable.name.


Response vector (dependent variable), alternative interface to data with formula or dependent.variable.name. For survival use a Surv() object or a matrix with time and status.


Object of class ranger with elements


Saved forest (If write.forest set to TRUE). Note that the variable IDs in the split.varIDs object do not necessarily represent the column number in R.


Predicted classes/values, based on out of bag samples (classification and regression only).


Variable importance for each independent variable.


Variable importance for each independent variable and each sample, if local.importance is set to TRUE and importance is set to 'permutation'.


Overall out of bag prediction error. For classification this is the fraction of missclassified samples, for probability estimation the Brier score, for regression the mean squared error and for survival one minus Harrell's C-index.


R squared. Also called explained variance or coefficient of determination (regression only). Computed on out of bag data.


Contingency table for classes and predictions based on out of bag samples (classification only).


Unique death times (survival only).


Estimated cumulative hazard function for each sample (survival only).


Estimated survival function for each sample (survival only).


Function call.


Number of trees.


Number of independent variables.


Value of mtry used.


Value of minimal node size used.


Type of forest/tree. classification, regression or survival.


Importance mode used.


Number of samples.


Number of times the observations are in-bag in the trees.


The tree type is determined by the type of the dependent variable. For factors classification trees are grown, for numeric values regression trees and for survival objects survival trees. The Gini index is used as default splitting rule for classification. For regression, the estimated response variances or maximally selected rank statistics (Wright et al. 2016) can be used. For Survival the log-rank test, a C-index based splitting rule (Schmid et al. 2015) and maximally selected rank statistics (Wright et al. 2016) are available. For all tree types, forests of extremely randomized trees (Geurts et al. 2006) can be grown.

With the probability option and factor dependent variable a probability forest is grown. Here, the node impurity is used for splitting, as in classification forests. Predictions are class probabilities for each sample. In contrast to other implementations, each tree returns a probability estimate and these estimates are averaged for the forest probability estimate. For details see Malley et al. (2012).

Note that for classification and regression nodes with size smaller than min.node.size can occur, as in original Random Forests. For survival all nodes contain at min.node.size samples. Variables selected with always.split.variables are tried additionally to the mtry variables randomly selected. In split.select.weights variables weighted with 0 are never selected and variables with 1 are always selected. Weights do not need to sum up to 1, they will be normalized later. The weights are assigned to the variables in the order they appear in the formula or in the data if no formula is used. Names of the split.select.weights vector are ignored. The usage of split.select.weights can increase the computation times for large forests.

Unordered factor covariates can be handled in 3 different ways by using respect.unordered.factors: For 'ignore' all factors are regarded ordered, for 'partition' all possible 2-partitions are considered for splitting. For 'order' and 2-class classification the factor levels are ordered by their proportion falling in the second class, for regression by their mean response, as described in Hastie et al. (2009), chapter 9.2.4. For multiclass classification the factor levels are ordered by the first principal component of the weighted covariance matrix of the contingency table (Coppersmith et al. 1999), for survival by the median survival (or the largest available quantile if the median is not available). The use of 'order' is recommended, as it computationally fast and can handle an unlimited number of factor levels. Note that the factors are only reordered once and not again in each split.

The 'impurity_corrected' importance measure is unbiased in terms of the number of categories and category frequencies and is almost as fast as the standard impurity importance. It is a modified version of the method by Sandri & Zuccolotto (2008), which is faster and more memory efficient. See Nembrini et al. (2018) for details. This importance measure can be combined with the methods to estimate p-values in importance_pvalues.

Regularization works by penalizing new variables by multiplying the splitting criterion by a factor, see Deng & Runger (2012) for details. If regularization.usedepth=TRUE, \(f^d\) is used, where f is the regularization factor and d the depth of the node. If regularization is used, multithreading is deactivated because all trees need access to the list of variables that are already included in the model.

For a large number of variables and data frames as input data the formula interface can be slow or impossible to use. Alternatively dependent.variable.name (and status.variable.name for survival) or x and y can be used. Use x and y with a matrix for x to avoid conversions and save memory. Consider setting save.memory = TRUE if you encounter memory problems for very large datasets, but be aware that this option slows down the tree growing.

For GWAS data consider combining ranger with the GenABEL package. See the Examples section below for a demonstration using Plink data. All SNPs in the GenABEL object will be used for splitting. To use only the SNPs without sex or other covariates from the phenotype file, use 0 on the right hand side of the formula. Note that missing values are treated as an extra category while splitting.

See https://github.com/imbs-hl/ranger for the development version.

With recent R versions, multithreading on Windows platforms should just work. If you compile yourself, the new RTools toolchain is required.


See Also



## Classification forest with default settings
ranger(Species ~ ., data = iris)

## Prediction
train.idx <- sample(nrow(iris), 2/3 * nrow(iris))
iris.train <- iris[train.idx, ]
iris.test <- iris[-train.idx, ]
rg.iris <- ranger(Species ~ ., data = iris.train)
pred.iris <- predict(rg.iris, data = iris.test)
table(iris.test$Species, pred.iris$predictions)

## Quantile regression forest
rf <- ranger(mpg ~ ., mtcars[1:26, ], quantreg = TRUE)
pred <- predict(rf, mtcars[27:32, ], type = "quantiles")

## Variable importance
rg.iris <- ranger(Species ~ ., data = iris, importance = "impurity")

## Survival forest
rg.veteran <- ranger(Surv(time, status) ~ ., data = veteran)
plot(rg.veteran$unique.death.times, rg.veteran$survival[1,])

## Alternative interfaces (same results)
ranger(dependent.variable.name = "Species", data = iris)
ranger(y = iris[, 5], x = iris[, -5])

# }
## Use GenABEL interface to read Plink data into R and grow a classification forest
## The ped and map files are not included
convert.snp.ped("data.ped", "data.map", "data.raw")
dat.gwaa <- load.gwaa.data("data.pheno", "data.raw")
phdata(dat.gwaa)$trait <- factor(phdata(dat.gwaa)$trait)
ranger(trait ~ ., data = dat.gwaa)
# }
# }