如何使用plm()在分类OLS中包含分类变量?

时间:2015-01-15 15:34:01

标签: r lm categorical-data plm

在将plm()用于池化OLS时,是否有办法包含分类变量(具有多个因子级别的因子)?据我所知,在plm()中,所有变量都必须是数字,这在我的情况下是行不通的。我可以为每个因子级别包含一个虚拟变量,但是,这会导致更多的变量,实际上只有相当少的因子水平。

我在CrossValidated上提出了类似的问题,并感谢任何帮助。

如果需要,我会包含一个最小的示例,但我认为这是关于如何使用plm()lm()的更一般性问题。

1 个答案:

答案 0 :(得分: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 

如您所见,您可以在两个实例中同时拥有虚拟变量和分类变量。

相关问题