# Simple way to parse the rules of a Tree model

## Problem Description

I am currently working with simple decision tree models to generate rules to classify respondents of a survey.

The output of each model is "translated" to the complete rule-set of each end node. I then select nodes based on some statistical criteria to generate a new segment.

Now I am struggling to find an easy way to summarize the "rules" needed to classify this segment.

## Current Approach

At the moment I extract the rules for each node and then give out the rules for the bigger segment by simply chaining the rules for each node:

library(rpart)
library(rpart.plot)
library(dplyr)

extract_rules <- function(model) {
x = rpart.rules(
model,
style = "wide",
cover = T,
nn = T,
eq = "=",
facsep = ";"
)

rules = rep("", nrow(x))

for (r in 1:nrow(x)) {
for (c in 4:(length(x) - 1)) {
rules[r] = paste0(rules[r], x[r, c])
}
rules[r] = gsub("&", " & ", rules[r], fixed = T)
}
rev(rules)
}

treemodel\$frame %>%
mutate(nn = row.names(.)) %>%
filter(var == "<leaf>") %>%
select(
"Node ID" = nn,
Casecount = n,
PopReach = wt,
TGHitrate = yval
) %>%
arrange(desc(TGHitrate)) %>%
cbind(rules = extract_rules(treemodel))


However this generates unnecessarily complex rules because if a segment consists of all end nodes of a specific leaf the easiest rule would of course be the leaf rule not an "OR-chain" of all end nodes. E.g.

male == yes


is much easier to understand then

male == yes & age < 25 OR male == yes & age >= 25


What would code look like that identifies the overarching rule set needed to classify a bigger segment consisting of several nodes?

• Are you looking to optimize the above example like a compiler? Find the redundant age condition, remove, find the male == yes on both sides of the OR and reduce? However I am guessing the tree split with age at 25 to put a different decision in the leaf nodes and optimizing those into 1 rule would eliminate the different decision. You could look at RuleFit here and here which may do much of what you want. Feb 14, 2022 at 10:30
• @Craig yes a compiler would solve the practical issue I have, as the rules are too complex and in practice would be summed up manually exactly like you describe (i.e. just use male == yes and drop the age). From a theoretical point of view, what I would much rather do is parse the tree model vertically and output the rules of the relevant leafs and nodes based on end node selection. I am looking into the links you posted, thank you. Feb 14, 2022 at 10:37