The easiest way to do so is to consider only partitioning variables at site level. If these site variables are not constant across the observations at the site, it might make sense to average them for each site. For example, to average a regressor x2
to x2m
in data d
:
d$x2m <- tapply(d$x2, d$site, mean)[d$site]
If you have additional variables at observation level rather than at site level, it might make sense to include them (a) in the regression part of the formula so that the corresponding coefficients are site-specific in the tree,
or (b) in the random effect part of the formula so that only a global coefficient is estimated. For example, if you have a single observation-level regressor z
and two site-level regressors x1
and x2
that you want to use for partitioning, you might consider
## (a)
y ~ treatment + z | site | x1 + x2
## (b)
y ~ treatment | (1 + site) + z | x1 + x2
Finally, we discovered recently that in the case of having cluster-level (aka site-level) covariates with strong random effects it might make sense to initialize the estimation of the model with the random effect and not with the tree. The simple reason is that if we start estimation with the tree, this will then capture the random intercepts through splits in the cluster-level variables. We plan to adapt the package accordingly but haven't done so yet. However, it is easy to do so yourself. You simply start with estimating the null model for only the random intercept:
null_lmm <- lmer(y ~ (1 | site), data = d)
Then you extract the random intercept and include it in your data:
d$ranef <- ranef(null_lmm)$site[[1]][d$site]
And include it as the starting value for the random effect:
tree_lmm <- lmertree(y ~ treatment | site | x1 + x2 + ...,
data = d, ranefstart = d$ranef, ...)
You can try to additionally cluster the covariances at site level by setting cluster = site
.