---
title: randomForestSRC Package Overview
url: https://www.emergentmind.com/topics/randomforestsrc-package
type: topic
---

# randomForestSRC Package Overview

The randomForestSRC package is a unified random forest implementation in R supporting survival, regression, and classification analysis based on ensemble decision tree methodology. Originating from Breiman’s nonparametric tree ensembles, with extensions for right-censored time-to-event data by Ishwaran and Kogalur, randomForestSRC provides a single functional and algorithmic framework for a variety of supervised learning scenarios, with extensive tools for error estimation, variable importance, interpretability, and missing data [1612.08974].

## 1. Core Functionality

The central function `rfsrc()` grows random forests for three response families:

- **"surv"**: Time-to-event outcomes, using the `Surv(time,status)` format.
- **"regr"**: Continuous regression.
- **"class"**: Categorical classification.

Default parameterization is set by the detected response type. Defaults include:
- **Split Rule**: “logrank” for survival, “gini” for classification, “mse” (mean squared error) or “md” for regression.
- **mtry**: $\lfloor\sqrt{p}\rfloor$ for survival/classification, $p/3$ for regression.
- **ntree**: 1000.
- **nodesize**: 1 (classification/regression), 3 (survival).
- **nsplit** (survival): 10, i.e., random split selection per candidate covariate.
- **samptype**: “swr” (with replacement; bagging), “swor” (without).
- **na.action**: “na.impute” (adaptive tree imputation), or “na.omit.”

Users are encouraged to tune:
- **ntree** until OOB error stabilizes (typically 500–2000).
- **mtry**: Lower values promote tree heterogeneity (diversity), higher values yield lower individual tree bias.
- **nodesize**: Smaller values permit deeper trees (greater variance, lower bias); for large data, moderate increases (5–10) may improve generalization.
- **splitrule** in survival forests can be “logrank,” “logrankrandom,” or “random.”

## 2. Algorithmic Foundations

RandomForestSRC employs the classical forest approach:
- **Bootstrap & Aggregation**: For $b=1, \dots, B$:
  - Draw bootstrap sample $D_b$ (size $n$) from full data $D$, fit tree $T_b$.
  - The OOB (out-of-bag) set, $OOB_b$, comprises $\sim 36.8\%$ of data not used in $D_b$.
- **Prediction**:
  - **Regression**:
    $$
    \hat{f}_{\mathrm{RF}}(x_i) = \frac{1}{B_i}\sum_{b: i\in OOB_b} \hat{f}_b(x_i)
    $$
  - **Classification**: Majority vote from OOB predictions.
  - **Survival**: Forest survival estimate is ensemble of terminal node Kaplan–Meier curves.

- **Splitting Criteria**:
  - **Survival**: Maximize two-sample log-rank statistic:
    $$
    LR(s) = \frac{\Big[\sum_{t\in\mathcal T}\big(d_L(t) - Y_L(t)\frac{d(t)}{Y(t)}\big)\Big]^2}
    {\sum_{t\in\mathcal T}\frac{Y_L(t)Y_R(t)d(t)[Y(t)-d(t)]}{Y(t)^2 [Y(t)-1]}}
    $$
    maximizing separation of event times post-split.
  - **Regression**: Split yielding largest impurity reduction $\Delta I = \sum(y_i-\bar y)^2$.
  - **Classification**: Gini impurity or entropy reduction.

- **Survival Functions**:
  - **Kaplan–Meier within terminal nodes**:
    $$
    \hat S_{b,j}(t) = \prod_{u\le t}\left(1-\frac{d_{b,j}(u)}{Y_{b,j}(u)}\right)
    $$
    Forest ensemble:
    $$
    \hat S_{\mathrm{RF}}(t\mid x_i) = \frac{1}{B}\sum_{b=1}^B \hat S_{b,j_b}(t)
    $$
  - **Cumulative hazard** via Nelson–Aalen is analogous.

## 3. Error Estimation and Performance Metrics

- **Classification**: OOB error as proportion of misclassified OOB samples; tracked via `$err.rate`.
- **Regression**: OOB mean squared error:
  $$
  \mathrm{OOB\_MSE} = \frac{1}{n}\sum_i(y_i-\hat{f}_{\mathrm{OOB}}(x_i))^2
  $$
- **Survival**:
  - **Integrated Brier Score** at time $t$:
    $$
    BS(t) = \frac{1}{n} \sum_{i=1}^n w_i(t)\left(I\{T_i>t\} - \hat S_{\mathrm{RF}}(t\mid x_i)\right)^2
    $$
    with $w_i(t)$ as inverse-probability-of-censoring weights.
  - **Concordance index (C-index)**:
    $$
    C = \frac{\sum_{i<j} I(T_i<T_j,\delta_i=1)I(\hat S(t\mid x_i)<\hat S(t\mid x_j))}
    {\sum_{i<j} I(T_i<T_j,\delta_i=1)}
    $$
    quantifying agreement between predicted and observed event orderings.

## 4. Variable Importance and Interpretability

- **Permutation Variable Importance (VIMP)**:
  - For variable $v$, permute OOB values in each tree, anticipate increase in OOB error.
  - $VIMP(v) = \text{Err}_{\text{permuted}}(v) - \text{Err}_{\text{original}}$.
  - Values are stored in `$importance` within model objects and can be visualized.
- **Minimal Depth**:
  - $\mathrm{depth}_b(v)$: level of rootmost split on $v$ in tree $b$ (root=0).
  - Minimal depth = average over all trees.
  - Variables with small minimal depth generally have greatest predictive relevance.
  - `var.select()` computes minimal depths and an analytic mean-depth threshold to guide variable selection.

- **Interaction Assessment**: `find.interaction()` returns a $p \times p$ matrix indexed by variable pairs indicating candidate interactive effects, based on minimal depth.

## 5. Handling Right-Censored Data and Missingness

- **Censoring**: Input via `Surv(time, status)` response, with status 1 for event, 0 for censoring.
- **Split handling**: Log-rank statistic and node-specific Kaplan–Meier estimators enable robust nonparametric management of right-censoring.
- **Missing Data**: Adaptive “na.impute” option imputes missing entries at each node, using draws from in-node non-missing data in split search and final OOB-aggregated filling.

## 6. Model Object Structure and Methodology

Objects returned by `rfsrc()` possess a standardized S3 structure:
- **Primary slots**: `$family`, `$n`, `$p`, `$mtry`, `$ntree`, `$nodesize`.
- **Forest representation**: `$forest` contains tree memberships ($ndbigtree), node data, variables and split points.
- **Predictions and error rates**: `$predicted`, `$predicted.oob`, `$err.rate`.
- **Survival-specific components**: `$survival` ($n\times T$ matrix), `$chf` (cumulative hazard), `$time.interest$` (unique event times).
- **Variable selection and interaction**: Integrated methods include `var.select()` and `find.interaction()` for in-depth model interrogation.

Methods for visualization and exploration include `print.rfsrc()`, `plot.rfsrc()`, variable importance and depth plots, and heatmaps of interaction metrics.

## 7. Representative R Usage and Workflow Examples

Canonical workflow involves:

```r
library(randomForestSRC)
set.seed(42)
# Fit a random survival forest
rf <- rfsrc(
  formula   = Surv(time, status) ~ .,
  data      = pbc.trial,
  ntree     = 1000,
  mtry      = floor(sqrt(ncol(pbc.trial)-2)),
  nodesize  = 3,
  nsplit    = 10,
  na.action = "na.impute",
  importance= TRUE
)
# Plot OOB error
plot(rf)
# Variable importance
barplot(sort(rf$importance, decreasing=TRUE), main="VIMP (permutation)")
# Minimal depth variable selection
varsel <- var.select(rf)
plot(varsel)
# OOB survival curves for first 50 subjects
matplot(rf$time.interest, t(rf$survival[1:50, ]), type="l", col=rainbow(50))
# Out-of-sample prediction
pred <- predict(rf, newdata=pbc.test, na.action="na.impute", importance=TRUE)
# C-index computation (requires additional packages)
library(pec)
cidx <- concordance.index(
  x = rowMeans(rf$chf),
  surv.time = rf$yvar[,1],
  surv.event= rf$yvar[,2]
)
print(cidx$c.index)
# Interaction heatmap
heatmap(find.interaction(rf), symm=TRUE)
```

The table below summarizes key object components:

| Component            | Description                                             | Applies to         |
|----------------------|--------------------------------------------------------|--------------------|
| `$err.rate`          | OOB error by tree                                      | All                |
| `$importance`        | Permutation VIMP                                       | All                |
| `$survival`          | OOB survival estimates $n\times T$                     | Survival           |
| `$chf`               | OOB cumulative hazard $n\times T$                      | Survival           |
| `$forest`            | Underlying trees: node data, splits, memberships       | All                |
| `var.select()`       | Computes minimal depth, analytic depth threshold        | All                |
| `find.interaction()` | Pairwise minimal depth interactions                    | All                |

The randomForestSRC package thus operationalizes nonparametric ensemble learning with rigorous error control and interpretability set within a unified R framework for categorical, continuous, and survival outcomes [1612.08974].

Source: https://www.emergentmind.com/topics/randomforestsrc-package