2013-02-27 129 views
-1

我发现互联网I()的功能是什么?

mod1 <- lm(mpg ~ weight + I(weight^2) + foreign, auto)

的功能是什么I()在下面的代码?看起来weight^2的结果与I(weight^2)相同。

+9

你读过'help(I)'吗?如果没有,请阅读。如果是这样,请告诉我们你不了解的内容。 – 2013-02-27 14:35:03

+2

请注意,只是在命令提示符下比较'weight^2'与'I(weight^2)** **并不是在公式中使用它们时的相似性测试。在那里,因为应用了附加的公式解析和解释约定,所以两个调用*是不同的。 – 2013-02-27 15:05:17

+0

'?formula'是另外一个很好的资源,它提供了关于如何在公式中使用'I()'的例子。 – Alex 2017-02-21 22:21:35

回答

12

I()功能是隔离在式术语从通常的公式解析&语法。在数据框中还有其他用途I()可帮助创建具有或继承自类"AsIs"的对象,该对象允许嵌入对象而无需进行常规转换。

式的情况下,因为这是你问具体,^是一个特殊的配方操作指示条款穿越到n给出像这样的运算符之后的第n度:^n。因此^在公式中没有它通常的算术解释。 (同样地,-+/*运营商也具有特殊配方的含义并且作为结果I()是需要使用它们,将它们从下式解析工具隔离。)

在你给特定示例(我如果你忘记使用围绕二次项的I(),在这种情况下,R将完全忽略该项,因为Volume(在本例中为weight)已经在模型中,并且你正在寻求变量与其自身的多方相互作用,这不是一个二次项。

首先不I()

> lm(Height ~ Volume + Volume^2, data = trees) 

Call: 
lm(formula = Height ~ Volume + Volume^2, data = trees) 

Coefficients: 
(Intercept)  Volume 
    69.0034  0.2319 

通知的Volume项公式中如何只?二次模型的正确说明(实际上它可能不是,见下文)是

> lm(Height ~ Volume + I(Volume^2), data = trees) 

Call: 
lm(formula = Height ~ Volume + I(Volume^2), data = trees) 

Coefficients: 
(Intercept)  Volume I(Volume^2) 
    65.33587  0.47540  -0.00314 

我说可能不正确;这是由于Volume和volume^2 . An identical but more stable fit can be achieved by the use of orthogonal polynomials, which poly()`可以为您生成的相关性。所以更稳定speficiation是:

> lm(Height ~ poly(Volume, 2), data = trees) 

Call: 
lm(formula = Height ~ poly(Volume, 2), data = trees) 

Coefficients: 
    (Intercept) poly(Volume, 2)1 poly(Volume, 2)2 
      76.000   20.879   -5.278 

注意,配合等同于早期型号虽然不同的系数估计值作为输入数据是不同的(正交多项式VS原多项式)。你可以通过自己的summary()输出中看到这一点,如果你不相信我:

> summary(lm(Height ~ poly(Volume, 2), data = trees)) 

Call: 
lm(formula = Height ~ poly(Volume, 2), data = trees) 

Residuals: 
    Min  1Q Median  3Q  Max 
-11.2266 -3.6728 -0.0745 2.4073 9.9954 

Coefficients: 
       Estimate Std. Error t value Pr(>|t|)  
(Intercept)  76.0000  0.9322 81.531 < 2e-16 *** 
poly(Volume, 2)1 20.8788  5.1900 4.023 0.000395 *** 
poly(Volume, 2)2 -5.2780  5.1900 -1.017 0.317880  
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 5.19 on 28 degrees of freedom 
Multiple R-squared: 0.3808, Adjusted R-squared: 0.3365 
F-statistic: 8.609 on 2 and 28 DF, p-value: 0.001219 

> summary(lm(Height ~ Volume + I(Volume^2), data = trees)) 

Call: 
lm(formula = Height ~ Volume + I(Volume^2), data = trees) 

Residuals: 
    Min  1Q Median  3Q  Max 
-11.2266 -3.6728 -0.0745 2.4073 9.9954 

Coefficients: 
      Estimate Std. Error t value Pr(>|t|)  
(Intercept) 65.335867 4.110886 15.893 1.52e-15 *** 
Volume  0.475398 0.246279 1.930 0.0638 . 
I(Volume^2) -0.003140 0.003087 -1.017 0.3179  
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 5.19 on 28 degrees of freedom 
Multiple R-squared: 0.3808, Adjusted R-squared: 0.3365 
F-statistic: 8.609 on 2 and 28 DF, p-value: 0.001219 

注意在 -tests在模型的线性和二次项的差异。这是输入多项式项的正交性有用的地方。

要真正看到什么^做一个公式(如果在?formula的术语是不是你所熟悉的,可以考虑这种模式:

> lm(Height ~ (Girth + Volume)^2, data = trees) 

Call: 
lm(formula = Height ~ (Girth + Volume)^2, data = trees) 

Coefficients: 
(Intercept)   Girth  Volume Girth:Volume 
    75.40148  -2.29632  1.86095  -0.05608 

由于有两个术语在(...)^2,公式解析代码将其转化为两个变量的主效应加上它们的二阶交互作用,该模型可以更简洁地编写为Height ~ Girth * Volume,但^可以帮助您在更多的变量之间获得更高阶的交互作用或交互作用

+0

'I()'是否允许为像I(log(Volume + 1))这样的回归构造额外的变量?您是否可以在交互中使用这些新变量?例如'(I(log(Volume + 1))+ Girth)^ 2' – Alex 2017-02-21 22:19:14

+0

嗯,好吧,它看起来像是用'公式'来解释,而不是'?I'。 – Alex 2017-02-21 22:20:53