有没有办法在对合并 OLS 使用 plm()
时包含分类变量(具有多个因子水平的因子)?据我了解,plm()
所有变量都必须是数字,这在我的情况下不起作用。我可以为每个因子水平包含一个虚拟变量,但是,这将导致更多的变量,这些变量实际上只是更少因子的水平。
我在交叉验证上提出了类似的问题,并感谢任何形式的帮助。
如果需要,我将包含一个最小示例,但我认为这更像是关于如何使用plm()
和lm()
的一般问题。
您可以轻松地在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
如您所见,在这两种情况下,您都可以同时拥有虚拟变量和分类变量。