“:”和“:”之间的区别和“|”在 R 线性建模中
在 R 中构建线性模型时,以下两条语句有什么区别:
lm(y ~ x | z)
lm(y ~ x : z)
lm
函数文档 记录了 :
运算符,如下所示:
first:second 形式的规范表示通过将第一个中的所有项与第二个中的所有项进行交互而获得的项集。
该页面上没有提及 |
语法。有什么区别?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
:
用于交互。在您的示例lm(y ~ x : z)
中,该公式表示“y 取决于x
和z
之间的交互作用。通常,除非您还包含单独的项
x
和z
,否则您不会在这样的线性回归中包含交互作用。 > 是x + x:z + z
的缩写。AFAIK,
|
根本没有被lm
使用,它当然不会出现在demo("lm.glm",它用于
nlme
包中的混合效果模型。来自
?intervals.lme
的示例:这里是
|
表示“分组依据”。每个受试者都适合不同的年龄随机效应(查看ranef(model)
,您可以看到每一行对应于一个人(受试者)的随机效应。):
is used for interactions. In your examplelm(y ~ x : z)
, the formula means "y is dependent upon an interaction effect betweenx
andz
.Usually, you wouldn't include an interaction in a linear regression like this unless you also included the individual terms
x
andz
as well.x * z
is short forx + x:z + z
.AFAIK,
|
isn't used bylm
at 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 thenlme
package.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).)