Choosing R packages for mixed effects modelling based on the car you drive
There are many roads you can take to fit a mixed effects model
(sometimes termed hierarchical models) in
R. There are numerous
packages that each deploy different engines to fit mixed effects models.
In this driveability review, I look at some of the dominant packages for mixed effects models The focus of this review is on the practicality of each package, rather than their numerical accuracy. Information on numerical accuracy can be found in the academic literature, see the bibliography below. I will lookat each package’s handling, speed and adaptability for tackling different types of questions.
Summary of the packages strengths and weaknesses. Speeds are relative
lme4 in a test case problem, see below for details.
|Handling- ease of coding1||Straightforward||Hard||Moderate||Hard|
|User manual||Accessible||Brief and technical||Very technical2||Highly accessible|
|Support from developers3||Good||Good||Outstanding||Good|
|Testing for accuracy||Extensive||Extensive for some types of models||Nascent||Nascent|
|Adaptability – types of models you can fit||Limited||Your imagination is the limit||Extensive||Almost as much as JAGS|
|Best for||Simple problems, quick implementation||Customising complex models||Spatio-temporal models||Developing new types of models|
|How you will look driving it||Traditional, but elegant||You like to take the scenic route||Edgy and slightly mysterious||Hanging with the cool kids|
- but see below for some add-on packages which make life easier for STAN and JAGS
- = lots of equations
- In my experience. But, heck these packages are all free and all give better user support than many paid services!
The first is
meaning linear mixed effects models with
lme4 is like an
older model sports-car – fast, respectable, well known and able to
handle common types of questions.
lme4 uses maximum likelihood methods
for estimation, so interpret its statistics as a
The next three packages are Bayesian techniques.
The second package is
which is an interface to the “Just Another Gibbs
rjags is the
Land-Rover of packages for mixed effects models: well tested, able to
tackle just about any terrian, but slow and prone to breaking down
(usually a user error, rather than the package itself).
The third package is
INLA, standing for
“Integrated Nested Laplace Approximation”.
INLA is like an electric
sports car: it is a modern technique that is lightning fast, but there
has been limited testing to date and you better understand what is under
the hood before you attempt to drive it.
The final package is
which is the
R interface to the Stan modelling
RStan is like like BMW’s hybrid,
combining computational speed with flexibility and with exceptional
documentation and support.
Now, let’s look at each packages computational speed, handling (ease of use), driver support (documentation, existing knowledge and forums) and flexibility.
Skip to the end for a biblography of examples and some cool packages that build on these tools.
A note about philosophy
The frequentist and Bayesian approaches I review here come from fundamentally different statistical philosophies. Here I will tackle the approaches pragmatically, looking at estimation speed and so on, however you might exclude one or the other a-prior based on your bent towards Bayesian or frequentist thinking.
I simulated a simple hierarchical data-set to test each of the models. The script is available here. The data-set has 100 binary measurements. There is one fixed covariate (continuous ) and one random effect with five groups. The linear predictor was transformed to binomial probabilities using the logit function. For the Bayesian approaches, slightly different priors were used for each package, depending on what was available. See the script for more details on priors.
For each package I fit the same linear model with binomial errors and a
logit link. To fit the models I used the functions
INLA, custom code for
rjags and the
stan_glmer() from the package
to fit the Stan model (you can also write your own custom code for
RStan, but for convenience I used the canned version of mixed effects
Computational speeds for fitting a binomial Generalised Linear Model to
100 samples with one random and one fixed effect. Relative times are
lme4 is by far the fastest, clocking in at around 6 tenths of a
second (on my 3GHz Intel i7 Macbook Pro).
INLA comes in second, ten
times slower than
lme4, then followed an order of magnitude later by
RStan and finally
rjags came plodding along behind about 100 times
If you run the code I have provided you will see that all packages come up with similar estimates for the model parameters.
Handling – ease of use
lme4 leads the pack when it comes to ease of use. If you have your
data sorted, then you can go from zero to fitted model in one line of
INLA is similar, however you should generally not rely on
default priors but specify your own, which requires additional code and
rjags depends on how you use them. You can write
your own model code, which can lead to quite lengthy scripts, but also
gives you greater number of choices for how you design your model.
However, there are also many packages that provide wrapper’s to
rjags that will fit common types of models (like mixed effects
models). These tend to be as simple as
INLA’s functions, remembering
that you should think carefully about prior
If you are going to write your own models, you will need to get good at debugging, so there is an extra overhead there.
Model checking is another important consideration.
For users familiar with GLMs,
lme4 probably provides the most familiar
support for model checking.
INLA can be more difficult to use, because
it requires the user to hand-code many model checking routines.
RStan also require the user to carefully check that the
fitting algorithms have performed properly.
Each of these packages use different types of algorithms, so the statistics you will use to check algorithm performance are different. See each package’s documentation for further advice.
Driver support – documentation and support
RStan have the highest quality user-manuals. Both are well
written and provide numerous examples that users will find helpful.
The user manual for
rjags and JAGS is somewhat brief, but the user can
easily find many helpful examples on the web, and these packages are
covered by numerous books. So if you are willing to broaden your search
you should have no trouble finding help.
INLA are both relatively new, so fewer examples can be
found on the web. While
RStan has an excellent manual for both the
mathsy and non-mathsy types, documentation of
INLA can be difficult to
follow for novice users, because much of it is equations.
Web support and chat groups are also an important aspect of model development. Help for all packages can be found on sites like Stackoverflow, or you can post your own new questions there.
Personally, I have found that support for
INLA, through their
is exceptional. On several occaisons I have posted questions for which I
could not find documentation and have generally recieved a response from
INLA’s developers before the end of the working day. This is amazing,
because it is better than you would expect from many paid services.
I have not participated in forums for the other packages, so I can’t add comments here.
One final note on support is that
lme4 and JAGS are both pretty well
studied approaches to modelling and there are numerous academic papers
dealing with their biases and accuracy.
relatively new on the scene, so for new and different types of problems
you might want to run your own simulation study to check that your model
is performing as expected.
Adaptability – ability to tackle different types of problems
Your imagination is the limit with
rjags – take it on well driven
routes, like a mixed effects model, or off-road on new adventures with
Bayesian structural equation modelling – it can do it all.
rjags is followed closely by
RStan has a few limitations,
but can basically do anything JAGS can do, and often faster (e.g. this
RStan will probably eventually replace
rjags, but to date
persists because of the extensive range of well documented examples
users can build on.
INLA comes in fourth with a diverse range of built in likelihoods and
model functions (you can even ask the developers to add more and they
might do it!).
INLA is becoming particularly popular for modelling
spatial and temporal auto-correlation, partly due to its speed. You can
do these types of models with
rjags but computations might
be impossibly slow.
lme4 is the least flexible of packages – though there are some options
to customise it’s models. The similar
nlme package also provides a
range of tools for fitting random effects for spatial and temporal
There are however, some clever tricks you can do with
lme4 to fit a
broader range of models. For instance, you can include a random effect
where every sample is a separate group in a Poisson GLM to get a Poisson
with extra variation.
That’s all. I hope this blog helps you make a good choice before investing in learning a new tool for mixed effects models. Let me know how you go and if you found this useful.
Cool applications of mixed effects models in R
rstanarm for mixed effects models coded like
glm() but using Stan for estimation.
glmmBUGS for mixed effects models coded like
glm() but using JAGS for estimation.
A new function in the mgcv package (links to pdf) for auto-writing code for Bayesian Generalised Additive Models.
A new study showing how you can fit spatial models with barriers.
Fit Bayesian Latent Variable models for analysing multivariate data (e.g. ecological communities).
Unless otherwise indicated, these resources are open access.
Stan user manual.
JAGS user manual.
INLA project page.
Mixed Effects Models and Extensions in Ecology with
R my go-to book for
theory and code for fitting likelihood based mixed effects models (e.g.
lme4). You will have to buy it.
Example for INLA for spatial models in ecology.
A review of mixed effects models in fisheries science (good for other disciplines too).
NIMBLE another R package for Bayesian modelling.
glmmADMB another R package giving greater flexibility for fitting using maximum likelihood.