kap kap - 1 year ago 104
R Question

J48 partykit access flat list

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

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

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


Thanks

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download