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
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
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