Forum: help


RE: define tree from list [ Reply ] By: Markus Loecher on 2016-08-07 12:28 | [forum:43501] |
Thanks a lot for this very useful perspective. I think I will simply create a "fake" rpart object then, great pointer. Best Markus |
RE: define tree from list [ Reply ] By: Achim Zeileis on 2016-08-06 13:06 | [forum:43410] |
Markus, there are coercion methods from "partynode" to "list" and vice versa. With the WeatherPlay example you can do R> (pl <- as.list(py$node)) [[1]] [[1]]$id [1] 1 [[1]]$split $varid [1] 1 $breaks NULL $index [1] 1 2 3 $right [1] TRUE $prob NULL $info NULL attr(,"class") [1] "partysplit" [[1]]$kids [1] 2 5 6 [[2]] [[2]]$id [1] 2 ... R> as.partynode(pl) [1] root | [2] V1 in (-Inf,1] | | [3] V3 <= 75 * | | [4] V3 > 75 * | [5] V1 in (1,2] * | [6] V1 in (2, Inf] | | [7] V4 <= 1 * | | [8] V4 > 1 * HOWEVER: This requires that you know in advance the IDs of kid nodes. So I think for the format you have this isn't so simple. Therefore, I would probably recommend to simply grow the tree recursively. See for example partykit:::as.party.rpart on how to do that. The core you will be interested in is the rpart_node() function that is called with rpart_node(1) and from there called recursively until the whole tree has been grown. |
define tree from list [ Reply ] By: Markus Loecher on 2016-08-06 12:15 | [forum:43409] |
I really like the WeatherPlay example on pp.2-5 of the partykit vignette. The explicit code to construct the entire tree on p.4 is also great but maybe a bit too "manual" or static to be generalized easily. So I wonder how one would construct a tree "dynamically" from a file which contains the split informations in a recursive fashion. For example, I am using software that records its generated trees to a file in the following format: split 0 5 1 9 split 1 10 6 14 split 0 3 1 5 leaf 1 0 leaf 1 2 split 1 12 10 14 leaf 1 1 For a split the first number is the variabe index (starting at 0 in the C convention), the second number the split value (e.g. left and right from var1==5 ) and the last two numbers represent the current bounding box of the respective variable. Are there any existing functions that might help reading such a tree into a party structure? Thanks! Markus |