When constructing a linear model in R, what is the difference between the following two statements:
lm(y ~ x | z)
lm(y ~ x : z)
The lm function documentation documents the : operator as follows:
A specification of the form first:second indicates the set of terms obtained by taking the interactions of all terms in first with all terms in second.
There’s no mention of | syntax on that page. What is the difference?
:is used for interactions. In your examplelm(y ~ x : z), the formula means “y is dependent upon an interaction effect betweenxandz.Usually, you wouldn’t include an interaction in a linear regression like this unless you also included the individual terms
xandzas well.x * zis short forx + x:z + z.AFAIK,
|isn’t used bylmat all. It certainly doesn’t show up in any of the examples indemo("lm.glm", "stats"). It is used in the mixed effects models in thenlmepackage.An example from
?intervals.lme:Here the
|means “group by”. That is, a different random effect for age is fitted for every subject. (Looking atranef(model), you can see that each row corresponds to the random effects for a person (subject).)