[R] Export chaid decision tree rules

Rodica Coderie rodikgeorgiana at yahoo.com
Sun Jan 4 00:07:38 CET 2015


Thanks! It answered my question.

Rodica


----- Original Message -----
From: Achim Zeileis <Achim.Zeileis at uibk.ac.at>
To: David Winsemius <dwinsemius at comcast.net>
Cc: Rodica Coderie <rodikgeorgiana at yahoo.com>; "r-help at r-project.org" <r-help at r-project.org>
Sent: Saturday, January 3, 2015 10:58 PM
Subject: Re: [R] Export chaid decision tree rules

On Sat, 3 Jan 2015, David Winsemius wrote:

> On Jan 3, 2015, at 1:21 AM, Rodica Coderie via R-help wrote:
>
>> Hello,
>> Can the decisions tree rules be exported? Along with the probabilities associated with each node?For example, I've created a CHAID decision with a target variable RESPONSE (YES/NO). I have 17 inner nodes with 19 terminal nodes. How which terminal node has the highest probability of YES and which is the probability?
>> An example of a terminal node output is below:clicks_flag in YES: NO (n = 1142, err = 5.3%)
>> Thanks!Rodica
>>     [[alternative HTML version deleted]]
>
> When posting on the weekends it is particularly important to follow the 
> guidelines in the Posting Guide. Many of us who regularly monitor the 
> list will ignore questions that do not have library calls to the 
> packages needed and code to produce a reproducible example. (And you 
> should learn to post in plain text rather than HTML.)

Yes, a reproducible example would have been good.

I assume you are talking about the "CHAID" package from R-Forge. We 
haven't got a nice and ready to use function in "partykit" (which "CHAID" 
is built upon) but we have an unexported .list.rules.party function 
which does a good part of what you want to do.

## package and data
library("CHAID")
ucb <- as.data.frame(UCBAdmissions)
ucb <- ucb[rep(1:nrow(ucb), ucb$Freq), 1:3]

## fit tree
ch <- chaid(Admit ~ Gender + Dept, data = ucb)
plot(ch)
print(ch)

## get rule path
partykit:::.list.rules.party(ch)

And with that information it is not too hard to set something up that is 
close to what you want, I think:

format_rules <- function(object, ...) {
   ft <- fitted(object)
   ns <- tapply(ft[[2]], ft[[1]], length)
   pr <- tapply(ft[[2]], ft[[1]], function(y)
     min(prop.table(table(y))))
   lb <- tapply(ft[[2]], ft[[1]], function(y)
     names(sort(table(y), decreasing = TRUE))[1])
   rl <- partykit:::.list.rules.party(object)
   paste0(rl, ": ", lb, " (n = ", ns, ", ", round(100 * pr, 2), "%)")
}
writeLines(format_rules(ch))

hth,
Z


> -- 
>
> David Winsemius
> Alameda, CA, USA
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

>



More information about the R-help mailing list