Sharma, Dhruv
2008-Oct-27 16:19 UTC
[R] Displaying number of Y/N affected by tree in rule form RE: R question/request on rules from rpart
Hi Prof. Williams, thanks for your suggestion. The updated code is below. It turns out it was a matter of displaying the second column in yval to get the number of N and subtracting it from the n column in the frame to get the number of Y remaining in a binary example. once this is added now the function returns the rules along with Y and N count affected by the resulting rule. I am ccing the r-help post in case anyone wants to reuse this updated version as well. This is a great little function. Thanks for developing it and suggesting how to make the enhancement. Regards, Dhruv listrules<-function(model) { if (!inherits(model, "rpart")) stop("Not a legitimate rpart tree") # # Get some information. # frm <- model$frame names <- row.names(frm) ylevels <- attr(model, "ylevels") ds.size <- model$frame[1,]$n # # Print each leaf node as a rule. # for (i in 1:nrow(frm)) { if (frm[i,1] == "<leaf>") { # The following [,5] is hardwired - needs work! cat("\n") cat(sprintf(" Rule number: %s ", names[i])) cat(sprintf("[yval=%s cover=%d N=%.0f Y=%.0f (%.0f%%) prob=%0.2f]\n", ylevels[frm[i,]$yval], frm[i,]$n, formatC(frm[i,]$yval2[,2], format = "f", digits = 2), formatC(frm[i,]$n-frm[i,]$yval2[,2], format = "f", digits = 2), round(100*frm[i,]$n/ds.size), frm[i,]$yval2[,5])) pth <- path.rpart(model, nodes=as.numeric(names[i]), print.it=FALSE) cat(sprintf(" %s\n", unlist(pth)[-1]), sep="") } } } listrules(fit) Copyright (c) 2004-2008 Togaware Pty Ltd -----Original Message----- From: Graham Williams [mailto:Graham.Williams at togaware.com] Sent: Friday, October 24, 2008 5:59 PM To: Sharma, Dhruv Subject: Re: R question/request on rules from rpart Hi Dhruv, I would think what you propose is possible, and probably even simple. Would need to explore through the model structure. Might need to multiply the proportions by the total to get the numbers. Regards, Graham Received Sat 25 Oct 2008 5:08am +1100 from Sharma, Dhruv:> Hi Prof. Williams. > I wanted to use your code to convert trees into rules and had a > question. > > Is there anyway to get the code below to print out the number of Y> and N resulting by the rule? > Currently the code prints cover=49 (0%) prob=0.91 but not the > number of Y or N being affected by the rule. > > When I plot large trees using use.n it is hard to read the numbers> and found your rule function to be very neat. > > It would be great if it could be enhanced to print the number of Y> and N affected by the rule. > > thanks > Dhruv > > > http://datamining.togaware.com/survivor/Convert_Tree.html > > list.rules.rpart <- function(model) > { > if (!inherits(model, "rpart")) stop("Not a legitimate rpart tree") > # > # Get some information. > # > frm <- model$frame > names <- row.names(frm) > ylevels <- attr(model, "ylevels") > ds.size <- model$frame[1,]$n > # > # Print each leaf node as a rule. > # > for (i in 1:nrow(frm)) > { > if (frm[i,1] == "<leaf>") > { > # The following [,5] is hardwired - needs work! > cat("\n") > cat(sprintf(" Rule number: %s ", names[i])) > cat(sprintf("[yval=%s cover=%d (%.0f%%) prob=%0.2f]\n", > ylevels[frm[i,]$yval], frm[i,]$n, > round(100*frm[i,]$n/ds.size), frm[i,]$yval2[,5])) > pth <- path.rpart(model, nodes=as.numeric(names[i]), > print.it=FALSE) > cat(sprintf(" %s\n", unlist(pth)[-1]), sep="") > } > } > } > > > > > ________________________________ > > Copyright (c) 2004-2008 Togaware Pty Ltd