[R] extracting ctree() output information

Steven J. Clark sclark at nltx.com
Thu Apr 8 01:52:44 CEST 2010

Thank you for the pointer to: help("BinaryTree-class").

It probably isn't the prettiest solution, but I was able to extract what
I needed by doing something like the following:

	nodes = where(fit)
	freq = treeresponse(fit)

	for(i in 1:100)

where the for loop from 1:100 is big enough to find at least one
instance of a datapoint that gets classified in terminal node "40" and
to print out its associated frequency distribution.  I can change the
"40" to whatever terminal node I'm after and make the for loop larger if
1:100 is too small to find at least one instance.


-----Original Message-----
From: Achim Zeileis [mailto:Achim.Zeileis at uibk.ac.at] 
Sent: Wednesday, April 07, 2010 4:57 PM
To: Steven J. Clark
Cc: r-help at r-project.org
Subject: Re: [R] extracting ctree() output information

On Wed, 7 Apr 2010, Steven J. Clark wrote:

> Hi,
> I am new to R and am using the ctree() function to do customer 
> segmentation.  I am using the following code to generate the tree:
> treedata$Response<-factor(treedata$Conversion)
> fit<-ctree(Response ~
> .,controls=ctree_control(mincriterion=0.99,maxdepth=4),data=treedata)
> plot(fit)
> print(fit)
> The variable "Response" above equals 1 if the customer responded to an

> offering and 0 otherwise.  Everything works great, however I am 
> struggling to extract the information I need from the output.  When I 
> look at the output from print(fit) I see things similar to:
> 4) age <=42; criterion 1, statistic = 73.055
>  5)* weights = 5843
> What this is telling me is that 5,843 customers ended up being 
> classified into the group labeled 5.  What I would really like to 
> know, however, is what proportion of this 5,843 had Response=1 and 
> what proportion had Response=0 so that I could make some inference 
> about the
> P(Response) for customers that match the demographic characteristics 
> of each terminal node.

especially the examples section. With where(fit) you can query the node
number and with treeresponse(fit) you get a list with the frequency
distribution. There are also some worked examples how you can compute
other quantities of interest.


> Any help on how to extract this information would be greatly 
> appreciated
> -- thanks!
> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list
> 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