# getTree: Extract a single tree from a forest. In randomForest: Breiman and Cutler's Random Forests for Classification and Regression

## Description

This function extract the structure of a tree from a `randomForest` object.

## Usage

 `1` ```getTree(rfobj, k=1, labelVar=FALSE) ```

## Arguments

 `rfobj` a `randomForest` object. `k` which tree to extract? `labelVar` Should better labels be used for splitting variables and predicted class?

## Details

For numerical predictors, data with values of the variable less than or equal to the splitting point go to the left daughter node.

For categorical predictors, the splitting point is represented by an integer, whose binary expansion gives the identities of the categories that goes to left or right. For example, if a predictor has four categories, and the split point is 13. The binary expansion of 13 is (1, 0, 1, 1) (because 13 = 1*2^0 + 0*2^1 + 1*2^2 + 1*2^3), so cases with categories 1, 3, or 4 in this predictor get sent to the left, and the rest to the right.

## Value

A matrix (or data frame, if `labelVar=TRUE`) with six columns and number of rows equal to total number of nodes in the tree. The six columns are:

 `left daughter` the row where the left daughter node is; 0 if the node is terminal `right daughter` the row where the right daughter node is; 0 if the node is terminal `split var` which variable was used to split the node; 0 if the node is terminal `split point` where the best split is; see Details for categorical predictor `status` is the node terminal (-1) or not (1) `prediction` the prediction for the node; 0 if the node is not terminal

## Author(s)

Andy Liaw andy\_liaw@merck.com

## See Also

`randomForest`

## Examples

 ```1 2 3``` ```data(iris) ## Look at the third trees in the forest. getTree(randomForest(iris[,-5], iris[,5], ntree=10), 3, labelVar=TRUE) ```

### Example output

```randomForest 4.6-12
Type rfNews() to see new features/changes/bug fixes.
left daughter right daughter    split var split point status prediction
1              2              3 Petal.Length        2.60      1       <NA>
2              0              0         <NA>        0.00     -1     setosa
3              4              5 Petal.Length        4.85      1       <NA>
4              6              7  Petal.Width        1.70      1       <NA>
5              8              9  Petal.Width        1.75      1       <NA>
6              0              0         <NA>        0.00     -1 versicolor
7              0              0         <NA>        0.00     -1  virginica
8             10             11  Sepal.Width        2.45      1       <NA>
9              0              0         <NA>        0.00     -1  virginica
10             0              0         <NA>        0.00     -1  virginica
11             0              0         <NA>        0.00     -1 versicolor
```

randomForest documentation built on May 2, 2019, 3:26 p.m.