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?
In principle, it is possible to build a
"modelparty"
object (as returned bymob()
) by hand. However, there is not a simple coercion function likeas.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 (withcall
,formula
,Formula
,terms
,fit
,control
,dots
, andnreg
). And then it should be possible to callrefit.modelparty()
to refit the models in all the terminal nodes. And this can then be used to fill the$node$info
(withcoefficients
,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 bypartykit
.