Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract tree structure from ctree function?

Tags:

r

I am trying to extract the tree information from the output of ctree. I tried the Class "BinaryTree" info but with no success. Any input is appreciated.

Thank You

like image 280
user1122211 Avatar asked Dec 30 '11 02:12

user1122211


2 Answers

The ctree objects are S4 objects at least at the top, and the tree information is in the "tree" slot. The "tree slot can be access ed with the @ operator. If you take the first example in the help(ctree) page you can get a graphical display with:

plot(airct)

enter image description here

And then you can look are branches of the tree by traversing with list operations. The "leaves" of the tree are descendents of nodes with "terminal"==TRUE:

> airct@tree$right$terminal
[1] FALSE
> airct@tree$left$terminal
[1] FALSE
> airct@tree$right$right$terminal
[1] TRUE
> airct@tree$right$left$terminal
[1] TRUE
> airct@tree$left$left$terminal
[1] TRUE
> airct@tree$left$right$terminal
[1] FALSE

Information at nodes above the leaves can also be recovered:

> airct@tree$left$right
4) Temp <= 77; criterion = 0.997, statistic = 11.599
  5)*  weights = 48 
4) Temp > 77
  6)*  weights = 21 

This is the same information that the nodes function will recover if you know the number of the node:

> nodes(airct,4)
[[1]]
4) Temp <= 77; criterion = 0.997, statistic = 11.599
  5)*  weights = 48 
4) Temp > 77
  6)*  weights = 21 
like image 136
IRTFM Avatar answered Sep 28 '22 05:09

IRTFM


The mlmeta R package converts fitted ctree models to SAS code. It can be easily adapted to other languages and is generally instructive on the internals of the object.

like image 41
Andrew Avatar answered Sep 28 '22 05:09

Andrew