英文:
Formula quadratic term as interaction of the variable with itself
问题
y ~ x + x:x
似乎等同于 y ~ x
,而 y ~ x + I(x^2)
正确地包括了二次项在模型中。
为什么我不能将二次项写为一个变量与其自身的交互?
英文:
y ~ x +x:x
seems to equal to just y ~ x
, while y ~ x + I(x^2)
correctly include the quadratic term in the model.
Why I cannot write quadratic terms as an interaction of a variable with itself?
答案1
得分: 1
我理解,以下是翻译好的部分:
-
"A start at an answer is that while
:
, in practice, typically multiplies the numeric columns associated with the variable (e.g. ifx
andy
are both numeric,x:y
creates an interaction column that is the product ofx
andy), the root meaning of
:` in R's formula syntax is not "multiply columns" but "form an interaction". The interaction of a variable with itself is just itself."- "一个初步的答案是,虽然
:
在实践中通常将与变量相关的数值列相乘(例如,如果x
和y
都是数字,x:y
会创建一个交互列,该列是x
和y
的乘积),但:
在R的公式语法中的根本含义并不是“相乘列”,而是“形成一个交互作用”。变量与自身的交互作用就是它自己。"
- "一个初步的答案是,虽然
-
"I would love to have a complete formal description of R's version of Wilkinson-Rogers syntax (which is what this is), but I don't know that one exists. The original framing of the formula language is in Wilkinson and Rogers (1973) [where
.
rather than:
was used for the "interaction" operator]; I believe there's a description in the "White Book" (Chambers and Hastie 1992); but other than that, I think the only full definition is the source code ofmodel.matrix()
itself (which is not all that nice to look at ...)"- "我很想拥有R版本的Wilkinson-Rogers语法的完整正式描述(这就是它的名称),但我不知道是否存在这样的描述。公式语言的最初构架出现在Wilkinson和Rogers(1973)中[在那里,用
.
而不是:
来表示“交互作用”操作符];我相信在“白皮书”(Chambers和Hastie 1992)中有一份描述;但除此之外,我认为唯一的完整定义就是model.matrix()
的源代码本身(虽然不太容易阅读...)。"
- "我很想拥有R版本的Wilkinson-Rogers语法的完整正式描述(这就是它的名称),但我不知道是否存在这样的描述。公式语言的最初构架出现在Wilkinson和Rogers(1973)中[在那里,用
-
参考文献:
- Chambers, J. M., and T. Hastie, eds. Statistical Models in S. Wadsworth & Brooks/Cole, 1992.
- Wilkinson, G. N., and C. E. Rogers. "Symbolic Description of Factorial Models for Analysis of Variance." Journal of the Royal Statistical Society. Series C (Applied Statistics) 22, no. 3 (1973): 392–99. https://doi.org/10.2307/2346786.
英文:
Great question. A start at an answer is that while :
, in practice, typically multiplies the numeric columns associated with the variable (e.g. if x
and y
are both numeric, x:y
creates an interaction column that is the product of x
and y
), the root meaning of :
in R's formula syntax is not "multiply columns" but "form an interaction". The interaction of a variable with itself is just itself.
I would love to have a complete formal description of R's version of Wilkinson-Rogers syntax (which is what this is), but I don't know that one exists. The original framing of the formula language is in Wilkinson and Rogers (1973) [where .
rather than :
was used for the "interaction" operator]; I believe there's a description in the "White Book" (Chambers and Hastie 1992); but other than that, I think the only full definition is the source code of model.matrix()
itself (which is not all that nice to look at ...)
Chambers, J. M., and T. Hastie, eds. Statistical Models in S. Wadsworth & Brooks/Cole, 1992.
Wilkinson, G. N., and C. E. Rogers. “Symbolic Description of Factorial Models for Analysis of Variance.” Journal of the Royal Statistical Society. Series C (Applied Statistics) 22, no. 3 (1973): 392–99. https://doi.org/10.2307/2346786.
答案2
得分: 0
为了轻松地构造多项式,我们可以使用 poly
。
lm(Petal.Length ~ Sepal.Length + I(Sepal.Length^2) + I(Sepal.Length^3), iris)$coe
# (Intercept) Sepal.Length I(Sepal.Length^2) I(Sepal.Length^3)
# 19.8028068 -13.5808046 2.8767502 -0.1742277
lm(Petal.Length ~ poly(Sepal.Length, 3, raw=TRUE), iris)$coe
# (Intercept) poly(Sepal.Length, 3, raw = TRUE)1
# 19.8028068 -13.5808046
# poly(Sepal.Length, 3, raw = TRUE)2 poly(Sepal.Length, 3, raw = TRUE)3
# 2.8767502 -0.1742277
英文:
To easily formulate polynomials, we can use poly
.
lm(Petal.Length ~ Sepal.Length + I(Sepal.Length^2) + I(Sepal.Length^3), iris)$coe
# (Intercept) Sepal.Length I(Sepal.Length^2) I(Sepal.Length^3)
# 19.8028068 -13.5808046 2.8767502 -0.1742277
lm(Petal.Length ~ poly(Sepal.Length, 3, raw=TRUE), iris)$coe
# (Intercept) poly(Sepal.Length, 3, raw = TRUE)1
# 19.8028068 -13.5808046
# poly(Sepal.Length, 3, raw = TRUE)2 poly(Sepal.Length, 3, raw = TRUE)3
# 2.8767502 -0.1742277
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论