我目前正在尝试习惯plm包并尝试使用plm()函数然后使用lm()函数来制作具有单独效果的固定效果(仅为了做到这一点,请忽略错误指定) . 我发现当我在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()包进行固定效果估计时不会出现问题,不包括一年和一个假人 . 我很感激任何帮助或建议!
1 回答
对于您的第3次估算(
est3
),排除一个假人并排除截距将给出不同的结果 . 当模型中存在截距时,排除一个虚拟(取n-1个虚拟对象)的做法是有意义的,因为变量变为线性相关(如果你将所有虚拟列加起来,则得到所有1的列,即截距) . 如果没有拦截,你想要你模型中的所有假人:这(
est4
)给出了与plm()
方法相同的估计值 .顺便说一句:使用一个因素让你更容易为你创造假人:
注意:没有
factor(FIRM)1
.你所要求的复制品非常多 . 您还询问了如何在
plm
包中处理这个问题:不是通过引入虚拟变量,而是通过每个人的数据的含义,因为这是等价的(理论上是Frisch-Waugh-Lovell定理) .