您可以在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
正如您所看到的,您可以在兩個實例中同時具有虛擬變量和分類變量。
謝謝。我已經在某處讀過它不起作用,並且在包含因素時我會收到一條錯誤消息,但顯然這個問題是另一回事...... – Aki 2015-01-15 20:21:30
我會懷疑你是否因爲對比因素而頭疼。見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