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
-
Are you talking about ctree found in the 'party' package?Dason– Dason2011年12月30日 03:57:27 +00:00Commented Dec 30, 2011 at 3:57
3 Answers 3
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
-
@user1122211 - To show that DWin answered your question -- quite nicely I'd say -- and as another form of thanks to him, you can 'accept' it by clicking the checkmark directly to its left. Thanks.Josh O'Brien– Josh O'Brien2011年12月31日 05:50:21 +00:00Commented Dec 31, 2011 at 5:50
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.
Let's say your ctree model is named ct
. Then
print(ct)
worked for me to see the tree structure.