I have scoured lots of help sites and am still confused about how to specify more complicated nested terms in a mixed model as well. I am also confused as the use of : and / and | in specifying interactions and nesting with random factors using lmer() in the lme4 package in R.

For the purpose of this question, let's assume I have accurately portrayed my data with this standard statistical model: $$ Y_{ijk} = u + \text{station}_i + \text{tow}_{j(i)} + \text{day}_k + (\text{station}\times \text{day})_{ik} + (\text{tow}\times\text{day})_{j(i)k} $$ station is fixed, tow and day are random. Tow is (implicitly) nested within station.

In other words, I'm hoping that my model includes Station(i,fixed), Tow(j,random,implicitly nested within Station), Day(k,random), and interaction between Tow and Day, and the interaction between Day and Station. I have consulted with a statistician to create my model and at this time believe it to be representative of my data, but will also add in a description of my data for those who are interested at the bottom of my post so as not to clutter.

So far what I've been able to piece together is the following in lmer:

lmer(y ~ station + (1|station:tow) + (1|Day) + (1|station:day) + (1|tow:day), 

Does this accurately depict my statistical model? Any suggestions for how to improve my code if it does not read correctly?

I've bolded the specific terms I'm having difficulty specifying in my lmer formula

#1. tow nested within station when tow is random and station is fixed
I'm confused, however about differentiating between nested and interaction terms that are random using : and / . In my above example, I have (1|station:tow) in which I'm hoping reads tow nested within station. I've read conflicting comments on various sites whether or not I should be using : or / here within the random (1|...) format of lmer.

#2. The interaction between station and day when station is fixed and day is random
I then have (1|station:day) but this time I'm hoping it reads the interaction between station and day. It seems like I could use station*day to account for the individual effects of station and day as well as their interaction (rather than including each of the three terms separately as I do above), but I don't see how to specify this when one is fixed and the other is random. Would station*(1|day) do that?

#3. The interaction between tow and day (both random) when tow is nested in station (fixed) Then lastly, I have (1|tow:day) which I'm hoping reads the interaction of tow and day, but I'm wondering if I need to specify again that tow is nested (implicitly) in station?

I am new to both R and lmer and statistical modeling and greatly appreciate the trouble of thorough explanations in any responses to my questions if possible.$$$$

More details on my data: I am asking whether concentrations of plankton vary across a physical front in the nearshore ocean. I have three stations, inshore, within, and offshore of this front. Station is thus fixed. At each station, I take three replicate plankton tows (from which I sort, count, and get a concentration in terms of # of bugs per meter cubed of water). Tow is random: in three tows I hope to account for the general variability in plankton at that particular station. Tow is intrinsically nested in station as each tow does not have a unique ID (123,123,123 is the ID for tows at each station). I then did this on multiple, independent days with a new front that had formed. I think I can think of Day as a blocking factor? Day is random as repeating this on multiple independent front days is attempting to capture variability from day to day and be representative of all days where this front is present. I want to know about the interaction terms to see if Tows change in variability from day to day and if stations always yield similar data or does it depend on the day?

Again, thank you for your time and help, I appreciate it!

  • $\begingroup$ I do believe you are missing some subscripts (I didn't want to add them in if I was mistaken) on your standard statistical model. $\endgroup$
    – user25658
    Sep 20, 2013 at 0:40
  • 1
    $\begingroup$ FWIW, for anyone who comes across this thread & wonders about whether it's on-topic, given it's focus on R syntax, IMO, it is sufficiently statistical (wrt understanding how the model specified relates to nesting & interactions, etc) to be on-topic for CV. $\endgroup$ Sep 20, 2013 at 0:48
  • 2
    $\begingroup$ This is 100% on topic in my opinion. $\endgroup$
    – user25658
    Sep 20, 2013 at 0:55
  • 8
    $\begingroup$ These threads might be helpful: 1 R's lmer cheat sheet; 2 Interpreting three forms of a "mixed model"; 3 Questions about how random effects are specified in mer; 4 Questions about specifying linear mixed models in R for repeated measures data with additional nesting structure. (Nonetheless, I think this is sufficiently distinct that it's not a duplicate.) $\endgroup$ Sep 20, 2013 at 1:05
  • 2
    $\begingroup$ According to your lmer() syntax, you've specified a model where there is a fixed effect of station and four random intercepts, shared by individuals with the same (1) combination of station and tow, (2) value of Day, (3) combination of station and day and (4) combination of tow and day, respectively. Is this what you intended? I'm not sure because, as indicated by @BabekP, how you've written your model formulation is not clear. You've written variable names, not parameters. Normally, in a model like this, the variable combinations are captured by the subscripts. $\endgroup$
    – Macro
    Sep 20, 2013 at 1:59

2 Answers 2


Tow nested within station when tow is random and station is fixed

station+(1|station:tow) is correct. As @John said in his answer, (1|station/tow) would expand to (1|station)+(1|station:tow) (main effect of station plus interaction between tow and station), which you don't want because you have already specified station as a fixed effect.

Interaction between station and day when station is fixed and day is random.

The interaction between a fixed and a random effect is always random. Again as @John said, station*day expands to station+day+station:day, which you (again) don't want because you've already specified day in your model. I don't think there is a way to do what you want and collapse the crossed effects of day (random) and station (fixed), but you could if you wanted write station+(1|day/station), which as specified in the previous answer would expand to station + (1|day) + (1|day:station).

interaction between tow and day when tow is nested in station

Because you do not have unique values of the tow variable (i.e. because as you say below tows are specified as 1, 2, 3 at every station, you do need to specify the nesting, as (1|station:tow:day). If you did have the tows specified uniquely, you could use either (1|tow:day) or (1|station:tow:day) (they should give equivalent answers). If you do not specify the nesting in this case, lme4 will try to estimate a random effect that is shared by tow #1 at all stations ...

One way to diagnose whether you've specified the random effects correctly is to look at the number of observations reported for each grouping variable and see whether it agrees with what you expect (for example, the station:tow:day group should have a number of observations corresponding to the total number of station $\times$ tow $\times$ day combinations: if you forgot the nesting with station, you should see that you get fewer observations than you ought.

Are http://bbolker.github.io/mixedmodels-misc/glmmFAQ.html#model-specification and http://bbolker.github.io/mixedmodels-misc/glmmFAQ.html#nested-or-crossed useful to you?

  • $\begingroup$ thank you very much for the helpful reply and references, they are much appreciated. I am confused about the notation (1|a:b) as you describe above, where it appears that the ':' can mean "nested in" as well as an interaction. How can it specify both? In other words, how does lmer know which relationship you are indicating? I must be missing something basic here, I apologize. $\endgroup$
    – wtree
    Sep 24, 2013 at 20:26
  • 4
    $\begingroup$ There is not that much difference, in this context, between interaction and nesting. Whether B is nested in A or just interacts with it depends on whether the main effect of A is included in the model or not. If the main effect of B is also in the model then it's crossed ... $\endgroup$
    – Ben Bolker
    Sep 24, 2013 at 20:35
  • $\begingroup$ Hi all, I asked a related question here: stats.stackexchange.com/questions/272377/… if anyone (especially @BenBolker) has a chance to look and provide an answer. $\endgroup$ Apr 7, 2017 at 0:17

Some of the things in formula are a bit confusing. The : is for interactions between two terms while the * is for main effects and interactions. The / is another one for interactions but what it does is generate an interaction between the numerator and all of the terms in the denominator (e.g. A/(B+C) = A:B + A:C). The | is for something like "grouped by". So, 1|station would be intercept grouped by station and in parentheses it's random (1|station). That's how you would do nesting.

Hopefully that's help. It's a bit odd to have a random effect nested within a fixed effect and I'm not sure how you'd represent that. I can't even imagine the situation. You might get a better response if you explain just what your variables are and what you want to accomplish. Lots of times people ask questions and are using the terminology wrong and it's hard to communicate. Explain what the variables represent and what you want to know about them.

Focusing on your your description in your last paragraph it sounds like your tow is simply an indicator of the samples you gathered and not something you need estimates of in the sense that you expect tow 1 to be consistently different from tow 2 in some way. Tow is just indicating a sample. Unless you really believe the order of tows mattered you don't even bother with that variable. And if they mattered then it's a fixed effect (and maybe random, but not a solely random effect). You say that you want to know if tows change in variability from day to day. How about the answer is yes? It's not in the realm of realistic probability that they don't vary from day to day. It's just the variance of your measures. You're not allowed to try to account for every spec of variance because then you wind up not having any variance left over for error. You'd have an over-specified model. You'd be at the point of just reporting each measure.

You make a similar statement about wondering if station varies by day; of course it does. But maybe you mean specific days? Were the days grouped in some way by season, lunar cycle, etc? Unless you have something other than just this is day 1, this is day 2, etc how does knowing that stations vary day to day tell you anything other than stations vary? So the answer to that question is, of course stations vary day to day. And of course tows vary day to day and station to station. You end up left with a simple model:

aov(y ~ station, data = dat)

The one fixed effect you have here, station, just is sampled over multiple tows and multiple days. I'm not sure you really need multi-level modelling here at all. It sounds like you're over-specifying your model.

If you really do want random day and tow effects and there's information that you haven't specified here then you might expand it out to a multi-level model. That would be:

lmer(y ~ station + (two*day|station), data = dat)

You need multiple tows at each station and day to use that model though.

  • $\begingroup$ I agree with everything you've said but I think this is probably more of a comment than an answer. $\endgroup$
    – Macro
    Sep 20, 2013 at 3:36
  • $\begingroup$ @John I was with you until "that's how you would do nesting". I think I missed the actual point of how you do nesting. Would you be willing to explain in more detail? I think I am still confused by the | and will look more into it. But from your response, I'm still not sure how to, for example, indicate that tow (random) is nested within station (fixed)? $\endgroup$
    – wtree
    Sep 20, 2013 at 17:11
  • $\begingroup$ @John Oh and station is fixed as sites/locations of interest in the ocean and tow is random because I am taking plankton tows at these sites that are random in that they are attempting to account for variability in plankton at each site and then be extrapolated to represent the plankton population at a station. $\endgroup$
    – wtree
    Sep 20, 2013 at 17:12
  • 1
    $\begingroup$ Not every label of a sample is a random variable, see edits. $\endgroup$
    – John
    Sep 22, 2013 at 17:56
  • 1
    $\begingroup$ I still think tow shouldn't be in the model at all given that description. Day sounds fine though. $\endgroup$
    – John
    Sep 25, 2013 at 0:02

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.