I would like to make a special kind of hybrid tree model in R, similar to the mob models in the party and partykit packages. But instead of learning splits from the data, I want to specify the splits in advance based on expert opinion, so that I obtain a more nicely structured and interpretable model. Can any existing tools do this in a slick way?

To fit the model, you partition data into $n$ sets according to the value of some factor $X$ with $n$ levels. You then fit some classical regression model, say Cox regression, to each subset. To predict on a new observation, you first determine which set your new observation belongs to, then feed it to the appropriate regression model.

I suppose I could rig this up with S3 without too much trouble, but is there a better way?


It is certainly doable but not in a few lines of code. My recommendation of what to do depends a little bit on what you want to do with it. If you just want to have a way to fit and predict the model, I would probably do something simpler. If you additionally want visualizations like for mob() then it might be worth the effort to mimic a modelparty S3 object from partykit. (Doing it in party with its S4 classes would be very, very cumbersome...even for myself :-))

Quick & dirty: If you know the correct subgroups, I would simply set up a new factor coding the segments, say:

d$seg <- ifelse(d$x1 > 0, ifelse(d$x2 == "a", 1, 2), 3)
    d$seg <- factor(d$seg)

for creating a factor seg with three levels that depend on the interaction of variables x1 and x2. With these you can easily fit your segmented model, e.g.,

m <- lm(y ~ seg * x)


m <- lm(y ~ seg / x)

for different codings of the coefficients. And then for out-of-sample predictions you first need to set up the seg factor on the new data (as above) and then call predict(m, ...). Not very elegant but should be simple and fast enough.

Via partykit: If you want to set up a modelparty object, I would recommend to first call mob() (or lmtree() or so) to create a tree based on the right variables. It can have just a single split but it would already have the correct terms and model.frame etc. Subsequently, you could replace the $node with your pre-specified tree. See vignette("partykit", package = "partykit") on how to do so. The $info elements need to contain the fitted models, parameters, etc. The resulting object will be nice once you have it...but the way to get it is not easy or elegant either.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.