2013-02-27 194 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