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 example lm(y ~ x : z)
, the formula means "y is dependent upon an interaction effect between x
and z
.
Usually, you wouldn't include an interaction in a linear regression like this unless you also included the individual terms x
and z
as well. x * z
is short for x + x:z + z
.
AFAIK, |
isn't used by lm
at all. It certainly doesn't show up in any of the examples in demo("lm.glm", "stats")
. It is used in the mixed effects models in the nlme
package.
An example from ?intervals.lme
:
model <- lme(distance ~ age, Orthodont, random = ~ age | Subject)
ranef(model)
Here the |
means "group by". That is, a different random effect for age is fitted for every subject. (Looking at ranef(model)
, you can see that each row corresponds to the random effects for a person (subject).)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With