choose_split_r: Choose split direction (recursively)

Description Usage Arguments Value Examples

View source: R/choose_split_r.R

Description

This function sends the input observation (pred_row) down a tree to a terminal node. Predicted values for all nodes visited are extracted. These values are required to decompose a single prediction from the gbm into feature contributions plus bias. The tree structure must be provided in a data.frame output from pretty.gbm.tree. The gbm model must also be given to provide variable names and types as well as categorical level names and split directions.

Usage

1
choose_split_r(row, pretty_tree, model, pred_row)

Arguments

row

row index of pretty_tree to determine prediction direction

model

gbm.object

pred_row

single data.frame row (containing explanatory columns) to send down the tree to a terminal node

prett_tree

data.frame output from pretty.gbm.tree giving tree structure.

Value

list of data.frame s showing split decisions for each node visited by the given observation en route to a terminal node. Contains columns;

node_index

index of node observation has passed through

variable

name of the splitting variable (NA for terminal nodes)

type

type for splitting variable, if type > 0 then the variable is categorical otherwise it is ordered or continuous (NA for terminal nodes)

direction

child node to travel down from current node

prediction

prediction at current node

Examples

 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
N <- 1000
X1 <- runif(N)
X2 <- 2*runif(N)
X3 <- ordered(sample(letters[1:4],N,replace=TRUE),levels=letters[4:1])
X4 <- factor(sample(letters[1:6],N,replace=TRUE))
X5 <- factor(sample(letters[1:3],N,replace=TRUE))
X6 <- 3*runif(N) 
mu <- c(-1,0,1,2)[as.numeric(X3)]

SNR <- 10 # signal-to-noise ratio
Y <- X1**1.5 + 2 * (X2**.5) + mu
sigma <- sqrt(var(Y)/SNR)
Y <- Y + rnorm(N,0,sigma)

# introduce some missing values
X1[sample(1:N,size=500)] <- NA
X4[sample(1:N,size=300)] <- NA

data <- data.frame(Y=Y,X1=X1,X2=X2,X3=X3,X4=X4,X5=X5,X6=X6)

# fit initial model
gbm1 <- gbm(Y~X1+X2+X3+X4+X5+X6,        
           data=data,                  
           var.monotone=c(0,0,0,0,0,0),
           distribution="gaussian",   
           n.trees=1000,     
           shrinkage=0.05,  
           interaction.depth=3,
           bag.fraction = 0.5,
           train.fraction = 0.5)

# get deicsion path through tree 1 in gbm1 for first row in data
choose_split_r(row = 1,
               pretty_tree = pretty.gbm.tree(gbm1, i.tree = 1),
               model = gbm1,
               pred_row = data[1, ])

richardangell/GbmExplainR documentation built on May 22, 2019, 12:54 p.m.