Splits and Root node of binary decision tree(CART)

2.3k views Asked by At

How to find a split and root node in a regression tree, I made a regression tree from multiple vectors now I have to extract root node of rpart of multiple vectors.file contains numeric value of multiple vectors A,B,C,D,E,F,G,H ex. A vector contains 4,3,6,7,2,4,5,...and so on similarly others B,C,D,E,F,G,H .so want to extract F (which is a root node in my case) as an output from this input an after creating a tree .thank you.sorry unable to put any image :(

Here's what I've done so far

log_data <- read.csv(file="C:\\Users\\AASHU\\Desktop\\CART\\syn.csv",
                      header=T, as.is=T)
library(rpart)

fit <- rpart(A ~ B+C+D+E+F+G+H, log_data)
# plot(fit)
plot(fit, compress=TRUE, branch=0)
text(fit, xpd = NA, cex = 0.7)

summary(fit)
Call:
rpart(formula = A ~ B + C + D + E + F + G + H, data = log_data)
n=52 (1 observation deleted due to missingness)

          CP nsplit rel error   xerror      xstd
1 0.09798662      0 1.0000000 1.065250 0.1888568

2 0.09347624      1 0.9020134 1.198999 0.1842667

3 0.03632980      2 0.8085371 1.154558 0.1859743

4 0.02297130      3 0.7722073 1.254874 0.2029423

5 0.01000000      4 0.7492360 1.274024 0.2118272

Node number 1: 52 observations,    complexity param=0.09798662

 mean=4.403846, MSE=1.509985 

 left son=2 (7 obs) right son=3 (45 obs)

 Primary splits:

F < 5.5 to the right, improve=0.09798662, (0 missing)

........... Now I have to extract root node F(F>=5.5) from fit (regression tree) and its split,can anyone help me?.

1

There are 1 answers

0
Aashu On BEST ANSWER

find the lables of that tree so that we can extract any of the vector

when the root node is character(ex.-A)

nodes<-labels(fit, digits=4, minlength=1L, pretty, collapse=TRUE)
root<-substr(nodes[2], 1, 1)

from the path we can extract the root node of a tree,below one is best to extract root node name by going through its second split which is nothing but an root node.

nodes<-labels(fit, digits=4, minlength=1L, pretty, collapse=TRUE)
path<-path.rpart(fit, node_no, pretty=0, print.it=FALSE) 
path[[2]][1]