R plm时间固定效应模型

JSS*_*JSS 2 r plm

我在固定效应模型上在线找到了这些示例代码:

代码1

fixed.time <- plm(y ~ x1 + factor(year), data=Panel, index=c("country", "year"), model="within")
Run Code Online (Sandbox Code Playgroud)

代码2

fixed.time <- plm(y ~ x1, data=Panel, index=c("country", "year"), model="within")
Run Code Online (Sandbox Code Playgroud)

有什么不同?与国家、年份建立索引是否意味着固定效应模型实际上为年份创建了虚拟变量?文档对此没有解释得很清楚。

Mat*_*fou 6

在 中plm,指定index参数只是格式化数据。您想要查看effect参数,该参数指示是否使用单独(您提供的第一个索引)、时间(第二个)或双向(两者)效果。如果您不指定任何内容,则默认为individual 。

因此,在您的第一次回归中,您(隐式)使用了individual,并自己添加了时间效应。这相当于使用twoways。请参阅下面的代码。

library(plm)
#> Loading required package: Formula
Panel <- data.frame(y <-  rnorm(120), x1 = rnorm(120), 
                    country = rep(LETTERS[1:20], each = 6),
                    year = rep(1:6, 20))
## this computes just individual FE
mod2 <- plm(y ~ x1, data=Panel, index=c("country", "year"), model="within")

## this computes individual FE, and you added time FE:
fixed.time <- plm(y ~ x1 + factor(year), data=Panel, index=c("country", "year"), model="within")

## this computes individual and time FE
mod3 <- plm(y ~ x1, data=Panel, index=c("country", "year"), model="within", effect = "twoways")

## second and third model should be identical:
all.equal(coef(fixed.time)["x1"], coef(mod3)["x1"])
#> [1] TRUE
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.2.1)于 2018 年 11 月 20 日创建