“:"之间的区别和"|"在R线性建模中 [英] Difference between ":" and "|" in R linear modeling

查看:79
本文介绍了“:"之间的区别和"|"在R线性建模中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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取决于xz之间的交互作用.

: 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.

通常,除非您还同时包括各个术语xz,否则您不会在线性回归中包括这样的相互作用. x * zx + 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆