5
$\begingroup$

I recently used rpart for an R-decision tree, but am confused on how to read the results....

library(rpart)
library(rpart.plot)

# Read data
train_dt = read.table("lbr-train.csv", sep=",", header=TRUE, stringsAsFactors = FALSE)

# Classification Tree
ctree <- rpart(LOW ~ RACE+SMOKE+PTL+HT+UI+FTV,
data=train_dt,
method = "class",
cp=.02) 

# plot
rpart.plot(ctree, # middle graph
type=4,
extra=101, 
box.palette="GnBu",
branch.lty=3, 
shadow.col="gray", 
nn=TRUE
)

summary(ctree)

# in looking at the first node, from the summary, it states: 

Node number 1: 151 observations, complexity param=0.125
predicted class=No expected loss=0.3178808 P(node) =1
class counts: **103 48**
probabilities: 0.682 0.318 
left son=2 (**127** obs) right son=3 (**24** obs)
Primary splits:
PTL < 0.5 to the left, improve=5.383050, (0 missing)
UI < 0.5 to the left, improve=3.839328, (0 missing)
SMOKE < 0.5 to the left, improve=2.181188, (0 missing)
RACE < 1.5 to the left, improve=1.589797, (0 missing)
HT < 0.5 to the left, improve=1.081253, (0 missing)

#--- I skipped listing some of the nodes... and just show #3:

Node number 3: 24 observations
predicted class=Yes expected loss=0.375 P(node) =0.1589404
class counts: 9 15
probabilities: 0.375 0.625
  1. For Node 1: Why doesn't the tree split into two 'sons' with numbers equal to the "class counts" ? I would expect the branches to split with 103 one side, and 48 on another... instead, it splits using 127 and 24

  2. For Node 3: Also, for the leaf nodes, what do the number mean? What is a "class count" at a leaf node? What does the 9 and 15 mean?

Thanks

enter image description here

$\endgroup$
7
  • $\begingroup$ Node 1 includes all the rows of your dataset (no split yet), which have 103 "No" and 48 "Yes" in your target variable (This answers your second question). The first split separates your dataset to a node with 33 "Yes" and 94 "No" and a node with 15 "Yes" and 9 "No". Only if your predictor variable (PTL in this case) had a very high correlation with your target variable the split would be a node with all 103 "No" and a node with 48 "Yes" (This answers your first question). $\endgroup$
    – AntoniosK
    Dec 1, 2017 at 14:37
  • $\begingroup$ Note that in the beginning you have a 31.8% of successes (assuming "Yes" is a success). The first split creates a node with 25.98% and a node with 62.5% of successes. The model "thinks" this is a statistically significant split (based on the method it uses). It's very easy to find info, online, on how a decision tree performs its splits (i.e. what metric it tries to optimise). $\endgroup$
    – AntoniosK
    Dec 1, 2017 at 14:42
  • $\begingroup$ Finally, the Yes or No you get on the top of your node is determined by which number is higher (number of No or Yes). The percentage you get in the bottom of the node is the percentage of rows/observations of your dataset (you'll always start with 100%). $\endgroup$
    – AntoniosK
    Dec 1, 2017 at 14:44
  • $\begingroup$ Hey thank.. where do you get "a node with 25.98% and a node with 62.5% of successes" $\endgroup$
    – jrdunson
    Dec 1, 2017 at 16:01
  • 1
    $\begingroup$ That's not something you get printed in your nodes. It's just the number of Yes divided by number of Yes and No. Check if there are ways to show/print more info on the nodes. If I remember correctly the rpart.plot has parameters/options to do that. $\endgroup$
    – AntoniosK
    Dec 1, 2017 at 16:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.