具有状态特定趋势的固定效应回归

Fixed effects regression with state specific trends

我正在处理面板数据,我想估计具有州特定趋势的固定效应回归。

在 Stata 中,我可以通过以下方式完成此操作,

xi i.state i.year i.state*time
reg y x _I*

以上将创建州虚拟人、年份虚拟人和 50(州 x 时间)虚拟人,其中时间以数字表示趋势(即 1、2、3...)

在 R 中,我可以 运行 使用 plm 或 lm 的固定效应模型,例如,

plm(y ~ x, index = c("state", "year"), effect = "twoways", data = df)
 lm(y ~ x + factor(state) + factor(year), data = df)

我如何像 xi 在 Stata 中那样包含 50 个(状态 x 时间)虚拟变量?

我知道 interaction() 不是我想要的,因为它创建了一个具有 n 个级别的新因子变量,其中 n =(状态数)x(时间段数)。我想要做的是创建 50 个(状态 x 时间)变量,使得 state1xtime 为 1,2,3...当 state == 1 否则为零时,重复 state2xtime,其中 state == 2,等等

这会是您要找的吗:

dta <- data.frame(state = rep(LETTERS[1:3], 10), 
              time = rep(1:3, each = 10))
dta <- cbind(dta, model.matrix( ~ state - 1, data = dta) * dta$time)

head(dta, 1)
#     state time stateA stateB stateC
# 1     A    1      1      0      0

tail(dta, 1)
#      state time stateA stateB stateC
# 30     C    3      0      0      3

您只需 stateyear 进行交互即可完成此操作。正确的运算符是 :,它只包含交互项。

请注意 lmplm 之间存在细微差别:

  • 对于 lm,只需 state:year
  • 对于 plm,年份已隐式转换为因子,state:as.integer(year) 也是如此(state:year 将为您提供州和年份的所有组合)。

检查:

library(plm)

data("Produc", package = "plm")
produc_plm <- pdata.frame(Produc, index = c("state","year"))

### simple state-specific time trend ###
fe1_Ttrend_lm <- lm(log(gsp) ~ -1 + log(pcap) + log(pc) + log(emp) + unemp + state +state:year, data = Produc)
fe1_Ttrend_plm <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp + state : as.integer(year), data = produc_plm)

summary(fe1_Ttrend_lm)$coef[1:4,]
summary(fe1_Ttrend_plm)$coef[1:4,]

要完成 Matifou 的回答,您也可以使用包 fixest:

library(fixest)
data("Produc", package = "plm")
fe_fixest = feols(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp + year::state | state, data = Produc)
# Notice the double colon (on the left the numeric variable, on the right the factor). The alternative also works,
# fe_fixest = feols(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp + interact(year, state) | state, data = Produc)
# fe_fixest = feols(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp + i(year, state) | state, data = Produc)

# Requesting ("standard" standard-errors, otherwise, clustered at state level by default)
coeftable(fe_fixest, se = "standard")[1:4, ]

请注意,如果您不关心交互系数的标准误差,您可以使用以下语法将它们投影出来:

fe_fixest_bis = feols(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp | state[year], data = Produc)
# state[year] means 'state' fixed-effects and 'state x year' interactions
# The interacted terms are projected out, and the estimation is faster

coeftable(fe_fixest_bis, se = "s")