我目前正在尝试习惯plm包并尝试使用plm()函数然后使用lm(使用plm()函数来制作具有单独效果的固定效果(仅为了做到这一点,请忽略错误指定)) )功能。我发现当我在lm()回归中为每个单独的N包含一个虚拟变量时,我只能复制plm()回归的结果。据我所知,回归中应该只包含N-1个虚拟变量。有谁知道plm如何处理各个固定效果?时间固定效应btw也是如此。
这是我的代码使用Grunwald 1958的示例数据(也包含在plm包中),请原谅相当笨拙的虚拟变量创建:
################################################################################
## Fixed Effects Estimation with plm() and lm() with individual effects
################################################################################
# Prepare R sheet
library(plm)
library(dplyr)
################################################################################
# Get data
data<-read.csv("http://people.stern.nyu.edu/wgreene/Econometrics/grunfeld.csv")
class(data)
data.tbl<-as.tbl(data)
#I = Investment
#F = Real Value of the Firm
#C = Real Value of the Firm's Capital Stock
################################################################################
# create firm (individual) dummies
firmdum<-rbind(matrix(rep(c(1,0,0,0,0,0,0,0,0,0),20),ncol = 10,byrow = T),
matrix(rep(c(0,1,0,0,0,0,0,0,0,0),20),ncol = 10,byrow = T),
matrix(rep(c(0,0,1,0,0,0,0,0,0,0),20),ncol = 10,byrow = T),
matrix(rep(c(0,0,0,1,0,0,0,0,0,0),20),ncol = 10,byrow = T),
matrix(rep(c(0,0,0,0,1,0,0,0,0,0),20),ncol = 10,byrow = T),
matrix(rep(c(0,0,0,0,0,1,0,0,0,0),20),ncol = 10,byrow = T),
matrix(rep(c(0,0,0,0,0,0,1,0,0,0),20),ncol = 10,byrow = T),
matrix(rep(c(0,0,0,0,0,0,0,1,0,0),20),ncol = 10,byrow = T),
matrix(rep(c(0,0,0,0,0,0,0,0,1,0),20),ncol = 10,byrow = T),
matrix(rep(c(0,0,0,0,0,0,0,0,0,1),20),ncol = 10,byrow = T)
)
colnames(firmdum)<-paste("firm",c(1:10),sep = "")
firmdum.tbl<-tbl_df(firmdum)
firmdum.tbl<-sapply(firmdum.tbl, as.integer)
###############################################################################################
# Estimation with individual fixed effects (plm)
dataset<-tbl_df(cbind(data.tbl,firmdum.tbl))
est1<- plm(I ~ F + C, data = dataset, model = "within", effect = "individual")
summary(est1)
plot(residuals(est1))
# Replication with lm
individualeffects<-tbl_df(cbind(data.tbl,firmdum.tbl))
est2<-lm(I ~ . -1 -FIRM -YEAR, individualeffects)
summary(est2)
plot(residuals(est2))
# Now exclude 1 dummy (as should be done in fixed effects)
individualeffects<-tbl_df(cbind(data.tbl,firmdum.tbl))
est3<-lm(I ~ . -1 -FIRM -YEAR -firm1, individualeffects)
summary(est3)
plot(residuals(est3))
差异很小,但知道plm函数如何处理固定效果会很有趣。当我在模型上运行测试时遇到了一个问题,当我用lm()包进行固定效果估计时不会出现问题,不包括一年和一个假人。 我很感激任何帮助或建议!
答案 0 :(得分:1)
对于您的第3次估算(est3
),排除一个假人并排除拦截将给您不同的结果。当模型中存在截距时变量变为线性相关(如果将所有虚拟列相加,则得到所有1列的列,即截距)。如果没有拦截,你想要你模型中的所有假人:
est4 <- lm(I ~ . -1 -FIRM -YEAR, individualeffects)
summary(est4)
此(est4
)给出与plm()
方法相同的估算值。
顺便说一句:使用因子:
更容易为你创造假人est5 <- lm(I ~ F + C + factor(FIRM), data = individualeffects)
summary(est5)
[...]
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -70.29672 49.70796 -1.414 0.159
F 0.11012 0.01186 9.288 < 2e-16 ***
C 0.31007 0.01735 17.867 < 2e-16 ***
factor(FIRM)2 172.20253 31.16126 5.526 1.08e-07 ***
factor(FIRM)3 -165.27512 31.77556 -5.201 5.14e-07 ***
factor(FIRM)4 42.48742 43.90988 0.968 0.334
factor(FIRM)5 -44.32010 50.49226 -0.878 0.381
factor(FIRM)6 47.13542 46.81068 1.007 0.315
factor(FIRM)7 3.74324 50.56493 0.074 0.941
factor(FIRM)8 12.75106 44.05263 0.289 0.773
factor(FIRM)9 -16.92555 48.45327 -0.349 0.727
factor(FIRM)10 63.72887 50.33023 1.266 0.207
[...]
注意:没有factor(FIRM)1
。
你要求复制的内容太多了。您还询问了如何在plm
包中处理这个问题:不是通过引入虚拟变量,而是通过每个人的数据的意义,因为这是等价的(理论上是Frisch-Waugh-Lovell定理)。 / p>