kap kap - 1 year ago 73
R Question

J48 partykit access flat list

I would like to access individual nodes in the flat list representation of the

object. That is, I would like to get a node, its split and kids and any other properties. I am new to
(just started exploring) so not sure what I am doing wrong.

data("PimaIndiansDiabetes", package = "mlbench")
j48 <- J48(diabetes ~ ., data=PimaIndiansDiabetes)
j48_party <- as.party(j48)
j48_party_list <- as.list(j48_party)
## Error: length(i) == 1 & is.numeric(i) is not TRUE


Answer Source

In partykit there is only an as.list() method for partynode objects but not for the whole party object. The former contains only a recursive list node IDs, variable IDs, splits, etc. All the actual variable names, data, fitted values, etc. are not stored in the partynode but the party.

Therefore, the as.list(j48_party) does not actually produce the flat list but it returns the j48_party objects "as is" because it is internally a list (containing the node, data, fitted, terms, etc.).

So the flat list can only be produced for as.list(j48_party$node) but I guess that this will not have the information you are looking for. Possibly, nodeapply() will help you to get what you want...or some of the other examples in the vignettes.