R - partykit - custom model tree nodes and splits

851 views Asked by At

In package partykit there is a way to build custom trees by specifying predictor and split. For example:

data("WeatherPlay", package = "partykit")

#create a split
sp_o <- partysplit(3L, breaks = 75) 

#create a node
n1 <- partynode(id = 1L, split = sp_o, kids = lapply(2L:3L, partynode))

#and make a "tree" out of it
t2 <- party(
  n1,
  data = WeatherPlay,
  fitted = data.frame(
    "(fitted)" = fitted_node(n1, data = WeatherPlay),
    "(response)" = WeatherPlay$play,
    check.names = FALSE
  ),
  terms = terms(play ~ ., data = WeatherPlay),
)

t2 <- as.constparty(t2)
t2
plot(t2)

Is this possible for model trees (returned by mob())? Can i build the tree node by node, and then fit specified function to terminal nodes?

1

There are 1 answers

2
Achim Zeileis On BEST ANSWER

In principle, it is possible to build a "modelparty" object (as returned by mob()) by hand. However, there is not a simple coercion function like as.constparty() in the example you cite. The reason is that for building the model trees, and for printing and especially predicting with them, much more detailed information about the model is needed.

I would recommend to build the tree structure ("partynode") first, then fill the overall $info slot (with call, formula, Formula, terms, fit, control, dots, and nreg). And then it should be possible to call refit.modelparty() to refit the models in all the terminal nodes. And this can then be used to fill the $node$info (with coefficients, objfun, nobs, ...).

Disclaimer: All of this is completely untested. But instead of mimicking "modelparty" you could, of course, also create your own way of storing the models in the "party" object and just use more basic building blocks provided by partykit.