“:"之间的区别和"|"在R线性建模中 [英] Difference between ":" and "|" in R linear modeling
问题描述
在R中构建线性模型时,以下两个语句有什么区别:
When constructing a linear model in R, what is the difference between the following two statements:
lm(y ~ x | z)
lm(y ~ x : z)
lm
函数文档记录:
运算符,如下所示:
The lm
function documentation documents the :
operator as follows:
first:second形式的规范表示通过将第一项中的所有术语与第二项中的所有术语进行交互而获得的一组术语.
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?
推荐答案
:
用于交互.在您的示例lm(y ~ x : z)
中,该公式的意思是"y取决于x
和z
之间的交互作用.
:
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
.
通常,除非您还同时包括各个术语x
和z
,否则您不会在线性回归中包括这样的相互作用. x * z
是x + x:z + 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,lm
完全不使用|
.当然,在demo("lm.glm", "stats")
中的任何示例中都没有显示它.在nlme
程序包的混合效果模型中使用.
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.
来自?intervals.lme
的示例:
model <- lme(distance ~ age, Orthodont, random = ~ age | Subject)
ranef(model)
此处|
表示分组依据".也就是说,对每个对象都适用不同的年龄随机效应. (通过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).)
这篇关于“:"之间的区别和"|"在R线性建模中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!