Quantum Forest

notes in a shoebox

Linear mixed models in R

A substantial part of my job has little to do with statistics; nevertheless, a large proportion of the statistical side of things relates to applications of linear mixed models. The bulk of my use of mixed models relates to the analysis of experiments that have a genetic structure.

A brief history of time

At the beginning (1992-1995) I would use SAS (first proc glm, later proc mixed), but things started getting painfully slow and limiting if one wanted to move into animal model BLUP. At that time (1995-1996), I moved to DFREML (by Karen Meyer, now replaced by WOMBAT) and AIREML (by Dave Johnson, now defunct—the program I mean), which were designed for the analysis of animal breeding progeny trials, so it was a hassle to deal with experimental design features. At the end of 1996 (or was it the beginning of 1997?) I started playing with ASReml (programed by Arthur Gilmour mostly based on theoretical work by Robin Thompson and Brian Cullis). I was still using SAS for data preparation, but all my analyses went through ASReml (for which I wrote the cookbook), which was orders of magnitude faster than SAS (and could deal with much bigger problems). Around 1999, I started playing with R (prompted by a suggestion from Rod Ball), but I didn’t really use R/S+ often enough until 2003. At the end of 2005 I started using OS X and quickly realized that using a virtual machine or dual booting was not really worth it, so I dropped SAS and totally relied on R in 2009.


As for many other problems, there are several packages in R that let you deal with linear mixed models from a frequentist (REML) point of view. I will only mention nlme (Non-Linear Mixed Effects), lme4 (Linear Mixed Effects) and asreml (average spatial reml). There are also several options for Bayesian approaches, but that will be another post.

nlme is the most mature one and comes by default with any R installation. In addition to fitting hierarchical generalized linear mixed models it also allows fitting non-linear mixed models following a Gaussian distribution (my explanation wasn’t very clear, thanks to ucfagls below for pointing this out). Its main advantages are, in my humble opinion, the ability to fit fairly complex hierarchical models using linear or non-linear approaches, a good variety of variance and correlation structures, and access to several distributions and link functions for generalized models. In my opinion, its main drawbacks are i- fitting cross-classified random factors is a pain, ii- it can be slow and may struggle with lots of data, iii- it does not deal with pedigrees by default and iv- it does not deal with multivariate data.

lme4 is a project led by Douglas Bates (one of the co-authors of nlme), looking at modernizing the code and making room for trying new ideas. On the positive side, it seems to be a bit faster than nlme and it deals a lot better with cross-classified random factors. Drawbacks: similar to nlme’s, but dropping point i- and adding that it doesn’t deal with covariance and correlation structures yet. It is possible to fit pedigrees using the pedigreemm package, but I find the combination a bit flimsy.

ASReml-R is, unsurprisingly, an R package interface to ASReml. On the plus side it i- deals well with cross-classified random effects, ii- copes very well with pedigrees, iii- can work with fairly large datasets, iv-can run multivariate analyses and v- covers a large number of covariance and correlation structures. Main drawbacks are i- limited functionality for non-Gaussian responses, ii- it does not cover non-linear models and iii- it is non-free (as in beer and speech). The last drawback is relative; it is possible to freely use asreml for academic purposes (and there is also a version for developing countries). Besides researchers, the main users of ASReml/ASReml-R are breeding companies.

All these three packages are available for Windows, Linux and OS X.

A (very) simple example

I will use a traditional dataset to show examples of the notation for the three packages: Yates’ variety and nitrogen split-plot experiment. We can get the dataset from the MASS package, after which it is a good idea to rename the variables using meaningful names. In addition, I will follow Bill Venables’s excellent advice and create additional variables for main plot and subplots, as it is confusing to use the same factor for two purposes (e.g. variety as treatment and main plot). Incidentally, if you haven’t read Bill’s post go and read it; it is one of the best explanations I have ever seen for a split-plot analysis.

The nlme code for this analysis is fairly simple: response on the left-hand side of the tilde, followed by the fixed effects (variety, nitrogen and their interaction). Then there is the specification of the random effects (which also uses a tilde) and the data set containing all the data. Notice that 1|block/mainplot is fitting block and mainplot within block. There is no reference to subplot as there is a single assessment for each subplot, which ends up being used at the residual level.

The syntax for lme4 is not that dissimilar, with random effects specified using a (1|something here) syntax. One difference between the two packages is that nlme reports standard deviations instead of variances for the random effects.

For this type of problem, the notation for asreml is also very similar, particularly when compared to nlme.

In this simple example one pretty much gets the same results, independently of the package used (which is certainly comforting). I will soon cover another simple model, but with much larger dataset, to highlight some performance differences between the packages.


  1. Hi there, this looks like to become a very interesting blog! Looking forward to it!

  2. A small clarification: nlme doesn't fit generalized LMMs just linear ones. It is lme4 that implements generalized linear and non-linear MMs (as well as linear ones).

    • Luis

      2011/10/17 at 10:40 am

      Thanks for the clarification. nlme deals only with Gaussian non-linear mixed models; I'll update the post.

  3. I was going to make the same comment as ucfagls. Nice post, though, looking forward to future posts.

  4. MCMCglmm, it may be another choice. It is a Bayesian animal modeler. You may need to turn to it. And, R-INLR package, is also attractive. All these two R packages are free.

    • Luis

      2011/10/17 at 8:50 pm

      Hi Jianfeng, You are right, and I will cover them with more detail in future posts. I used MCMCglmm for a multivariate animal model when working in this paper and am submitting soon another paper using INLA. Both packages are really good, but I am still getting my head around some of their notation.

  5. An alternative to the packages already mentioned is the hglm package on CRAN (developed by me and collaborators). There it is possible to give arbitrary design matrices (both for fixed and random effects) as inputs to the hglm-function, which allows modelling of correlated random effects. In our R Journal publication (The R Journal 2:20-28) we have an example of a an animal model being fitted (see Example 4 in the paper). Note that arbitrary correlation structures can be fitted, allowing for eg modelling of IBD-matrices in QTL analysis and genomic relationship matrices as well.

    • Luis

      2011/10/18 at 7:59 am

      Hi Lars, Thanks for the pointer to hglm and its publication in the R-Journal (PDF). I hope to check it when we finish our semester (I'm trying to keep my head above the water at the moment).

      By the way, it is hard to beat the name for your PhD topic; my childhood dream come true! I visited SLU in Uppsala in Dec 2009, cool place.

      • Well Luis, I'm pretty proud of being one of very few who has been doing research on animal breeding in reindeer. Northern Sweden can be a pretty cool place. Even cooler than Uppsala.
        Another package, recently available on CRAN, is dhglm developed by Noh & Lee. There you can fit GLMs having random effects both in the mean and the dispersion part, which has some interesting applications in genetics (see for instance my recent papers in this field http://www.gsejournal.org/content/pdf/1297-9686-4… and http://www.genetics.org/content/188/2/435.full ) . In the dhglm package you can also fit arbitrary correlation structures.

  6. Antonio Pedro

    2011/11/29 at 11:30 am

    I am very very late to this post but I have a question, if somewhat off topic: how did you collect the output of several lme objects in an useful format for model comparison and, later, for latex table? I've been struggling with that recently. Best, Antonio.

    • You can create a list in R and assign the results of the analyses as one more element of the list (I normally save only the variance components and log-likelihood). Something like this:

      results = list()

      loop iterating i here
      … results[i] = list(summary(den.uni)$varcomp) # in the case of asreml
      end loop

      You can even pre-allocate the list of whatever size to save some time, but in my experience when running this type of analyses the time to growth the list is minimal compared to the time to run a single analysis.

      Concerning Latex tables, I haven't done that in R.

  7. could any one forward syntax for A matrix generation (Y=xB +Zu +e) A is pedgree information

    • Luis

      2015/01/26 at 2:25 pm

      Search for pedigree in the search box and you’ll see examples on how to specify the pedigree for different packages that can be used for genetic analysis.

  8. Thanks Luis, a very suitable post for a beginner. However,the link to “Bill Venables’s post on linear mixed model” is broken. Would you mind update it?

    • Luis

      2016/01/15 at 9:52 pm

      Thanks for pointing out the broken link. It referred to a 16 year old email in the s-plus email list, which is not active anymore. Fortunately there are still a couple of copies circulating in internet, so I have updated the link to one of the surviving copies.

Leave a Reply

© 2016 Quantum Forest

Theme by Anders NorenUp ↑