简体   繁体   中英

R plm time fixed effect model

I found these sample codes online on fixed effect model:

Code 1

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

Code 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. 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. If you don't specify anything, individual is the default.

So in your first regression, you (implicitly) used individual , and added time effects yourself. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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