简体   繁体   English

R plm时间固定效应模型

[英]R plm time fixed effect model

I found these sample codes online on fixed effect model: 我在固定效果模型上在线找到了这些示例代码:

Code 1 代码1

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

Code 2 代码2

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

What is the difference? 有什么区别? Doesn't index with country,year mean the fixed effect model actually create a dummy variable for year? 不与国家/地区建立索引是否意味着固定效应模型实际上为年份创建了虚拟变量? The documentation does not explain this very clearly. 该文档并没有很清楚地解释这一点。

In plm , specifying the index arguments just formats the data. plm ,指定index参数只是格式化数据。 You want to look at the effect argument, which indicates whether to use individual (the first index you provided), time (the second), or twoways (both) effects. 您要查看effect参数,该参数指示是使用单个效果 (您提供的第一个索引), 时间 (第二个)还是双向 (两个)效果。 If you don't specify anything, individual is the default. 如果您未指定任何内容,则默认为个人

So in your first regression, you (implicitly) used individual , and added time effects yourself. 因此,在您的第一个回归中,您(隐式)使用了personal ,并自己添加了时间效果。 This is equivalent to using twoways . 这等效于使用双向 See code below. 请参见下面的代码。

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

Created on 2018-11-20 by the reprex package (v0.2.1) reprex软件包 (v0.2.1)创建于2018-11-20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM