# Formula symbols for mixed model using lme4

Edit Note: Since I posted this question, it has been suggested that I read some documents and I am still thinking on the subject. I have added some new understanding as marked between ** asterisks: please do correct them if I am wrong and add remaining questions.*

I often confuse four symbols ":", "|", "/" and "*" in the formula, particularly while doing mixed model. Can somebody explain clearly the differences between them? I have the following working example:

require(lme4)
mydata <- expand.grid(xvar1 =factor(1:10), xvar2 =factor(1:3),replication = factor(1:3))
mydata$yvar <- rnorm(nrow(mydata), 10, 5) fm1 <- lmer(yvar ~ 1 + (1|xvar1) + (1|xvar2) + (1|xvar1:xvar2), mydata) ***My understanding: Cross classification between xvar1, xvar2*** fm2 <- lmer(yvar ~ 1 + (1|xvar1) + (1|xvar2) + (1|xvar1/xvar2), mydata) ***is not correct model, should remove 1|xvar1, 1|xvar2*** ***** has fixed intercept 1 , I do not know if that is correct technically** **fm2 <- lmer(yvar ~ 1 + (1|xvar1/xvar2), mydata)***** *is xvar1 is nested in xvar2 and is essentially same as fm1* fm3 <- lmer(yvar ~ 1 + (1|xvar1) + (1|xvar2) + (xvar1|xvar2), mydata) Warning message: In mer_finalize(ans) : singular convergence (7) ***I do not have idea on "|",*** fm4 <- lmer(yvar ~ 1 + (1|xvar1) + (1|xvar2) + xvar1:xvar2, mydata) Error in mer_finalize(ans) : Downdated X'X is not positive definite, 31 ; ***xvar1:xvar2 interaction term, technically correct !*** fm5 <- lmer(yvar ~ 1 + (1|xvar1) + (1|xvar2) + (1|xvar1*xvar2), mydata) Error in validObject(.Object) : invalid class "ngTMatrix" object: all row indices must be between 0 and nrow-1 In addition: Warning message: In Ops.factor(xvar1, xvar2) : * not meaningful for factors **again (1|xvar1) and (1|xvar2) are not necessary** ***fm5 <- lmer(yvar ~ 1 + (1|xvar1*xvar2), mydata) is equal to*** ***fm5 <- lmer(yvar ~ 1 + (1|xvar1) + (1|xvar2)+ (1|xvar1*xvar2)*** fm6 <- lmer(yvar ~ 1 + (1|xvar1) + (1|xvar2) + (xvar1/xvar2), mydata) ***same as fm2*** fm7 <- lmer(yvar ~ 1 + (1|xvar1) + (1|xvar2) + (0 + xvar1|xvar2), mydata) Warning message: In mer_finalize(ans) : singular convergence (7) ***I have no idea on xvar1|xvar2, 0 means no intercept I believe***  Edit: Here is something I learned from R documentation on linear model formula (1) yvar ~ xvar1 + xvar2 + xvar1:xvar2 - cross over classification is same as yvar ~ xvar1 * xvar2 (2) yvar ~ xvar1 / xvar2 - nested classification ( however means xvar1 + xvar2 + xvar1:xvar2) is same as yvar ~ xvar1 %in% xvar2  I am not sure those applicable to lm model hold true for mixed model. I am still not on track on use of "|" which is I believe unique in mixed models. - Just to link to a related thread where Mike Lawrence provided a pretty nice overview of R's formula for mixed-effects models. – chl Nov 15 '11 at 21:19 thanks for the suggestion, I am trying to grasp something of it. – John Nov 16 '11 at 1:58 CAN SOMEBODY HELP TO MOVE THIS QUESTION TO STACKOVERFLOW ...It might be suitable question for there.. – John Nov 17 '11 at 2:14 John, I think this should stay here. You may have to wait some days before getting a response. – chl Nov 17 '11 at 8:22 ok..waiting for response – John Nov 18 '11 at 2:40 add comment ## 2 Answers The general trick is, as mentioned in another answer, is that the formula follows the form dependent ~ independent | grouping. The groupingis generally a random factor, you can include fixed factors without any grouping and you can have additional random factors without any fixed factor (an intercept-only model). A + between factors indicates no interaction, a * indicates interaction. For random factors, you have three basic variants: 1. Intercepts only by random factor: (1 | random.factor) 2. Slopes only by random factor: (0 + fixed.factor | random.factor) 3. Intercepts and slopes by random factor: (1 + fixed.factor | random.factor) Note that variant 3 has the slope and the intercept calculated in the same grouping, i.e. at the same time. If we want the slope and the intercept calculated independently, i.e. without any assumed correlation between the two, we need a fourth variant: • Intercept and slope, separately, by random factor: (1 | random.factor) + (0 + fixed.factor | random.factor) There's also a nice summary in response to a related question that you should look at. If you're up to digging into the math a bit, Barr et al. (2013) summarize the lmer syntax quite nicely in their Table 1, adapted here to meet the constraints of tableless markdown. That paper dealt with psycholinguistic data, so the two random effects are Subjectand Item. Models and equivalent lme4 formula syntax: •$Y_{si} = β_0 + β_{1}X_{i} + e_{si}$• n/a (Not a mixed-effects model) •$Y_{si} = β_0 + S_{0s} + β_{1}X_{i} + e_{si} $• Y ∼ X+(1∣Subject) •$Y_{si} = β_0 + S_{0s} + (β_{1} + S_{1s})Xi + e_{si}$• Y ∼ X+(1 + X∣Subject) •$Y_{si} = β_0 + S_{0s} + I_{0i} + (β_{1} + S_{1s})Xi + e_{si}$• Y ∼ X+(1 + X∣Subject)+(1∣Item) •$Y_{si} = β_0 + S_{0s} + I_{0i} + β_{1}X_{i} + e_{si}$• Y ∼ X+(1∣Subject)+(1∣Item) • As (4), but$S_{0s}$,$S_{1s}$independent • Y ∼ X+(1∣Subject)+(0 + X∣ Subject)+(1∣Item) •$Y_{si} = β_0 + I_{0i} + (β_{1} + S_{1s})Xi + e_{si}\$
• Y ∼ X+(0 + X∣Subject)+(1∣Item)

References:

Barr, Dale J, R. Levy, C. Scheepers und H. J. Tily (2013). Random effects structure for confirmatory hypothesis testing: Keep it maximal. Journal of Memory and Language, 68:255– 278.

-

The | symbol indicates a grouping factor in mixed methods.

As per Pinheiro & Bates:

...The formula also designates a response and, when available, a primary covariate. It is given as

response ~ primary | grouping


where response is an expression for the response, primary is an expression for the primary covariate, and grouping is an expression for the grouping factor.

Depending on which method you use to perform mixed methods analysis in R, you may need to create a groupedData object to be able to use the grouping in the analysis (see the nlme package for details, lme4 doesn't seem to need this). I can't speak to the way you have specified your lmer model statements because I don't know your data. However, having multiple (1|foo) in the model line is unusual from what I have seen. What are you trying to model?

-