2015-01-15 26 views
2

使用plm()作爲彙總OLS時,是否有辦法包含分類變量(具有多個因子水平的因子)?據我所知,在plm()所有變量必須是數字,這不適用於我的情況。我可以爲每個因子水平包含一個虛擬變量,但是,這會導致更多的變量,實際上這些變量只是水平相對較少的因子。如何使用plm()在合併的OLS中包含分類變量?

我在CrossValidated上提出了類似的問題,並會感謝任何幫助。

如果有要求,我會舉一個例子,但我認爲這是一個關於如何使用plm()lm()的一般性問題。

回答

1

您可以在plm()lm()中輕鬆包含數值和分類變量變量。

require(plm) 
data(Males) 
head(Males[1:6]) 
# nr year school exper union ethn 
# 1 13 1980  14  1 no other 
# 2 13 1981  14  2 yes other 
# 3 13 1982  14  3 no other 
# 4 13 1983  14  4 no other 
# 5 13 1984  14  5 no other 
# 6 13 1985  14  6 no other 

coef(lm(wage ~ school + union + ethn, data=Males)) 
# (Intercept)  school unionyes ethnblack ethnhisp 
# 0.7148  0.0767  0.1930  -0.1523  0.0134 

coef(plm(wage ~ school + union + ethn, data=Males, model="pooling")) 
# (Intercept)  school unionyes ethnblack ethnhisp 
# 0.7148  0.0767  0.1930  -0.1523  0.0134 

正如您所看到的,您可以在兩個實例中同時具有虛擬變量和分類變量。

+0

謝謝。我已經在某處讀過它不起作用,並且在包含因素時我會收到一條錯誤消息,但顯然這個問題是另一回事...... – Aki 2015-01-15 20:21:30

+0

我會懷疑你是否因爲對比因素而頭疼。見http://stackoverflow.com/questions/3445316/factors-in-r-more-than-an-annoyance和http://stackoverflow.com/questions/2352617/how-and-why-do-you-use -contrasts式-R。 – landroni 2015-01-15 20:24:03

相關問題