Is it possible to include mixed effects into a random forest model in R? I know about the lmer (from lme4) and randomForest (from randomForest) functions but it would be nice if I could combine the two in some way, if that makes sense.

I'm afraid that if I don't include random effects in my random forest model, it will be incorrect.

  • $\begingroup$ Can you provide some information about your data & problem. Obviously @utobi presumed that your study is longitudinal but I'm not sure how the information in the question supports this guess. $\endgroup$
    – dipetkov
    Commented Apr 7, 2023 at 16:53

1 Answer 1


The current main popular implementation of Random Forests (RF) (i.e. the randomForest package) is available only for univariate (continuous or discrete) responses. On the other hand, mixed models are inherently multivariate models, that is models that deal with vector-valued responses. Fortunately, extensions of RF for multivariate responses, in particular for handling longitudinal data, do exist.

LongitudiRF is one of the R packages that implement Random Forests for longitudinal data of which I am aware. A lot more information can be found at this recent review paper on longitudinal data with Random Forests.

Related posts:

How can I include random effects (or repeated measures) into a randomForest

How to deal with hierarchical / nested data in machine learning

Random forest for binary panel data

  • $\begingroup$ I read it as “luckily…do not exist”. $\endgroup$
    – Dave
    Commented Apr 6, 2023 at 20:58
  • 1
    $\begingroup$ replaced "luckily". hopefully, it sounds better now :-) $\endgroup$
    – utobi
    Commented Apr 6, 2023 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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