固定效果回归与状态特定趋势

时间:2015-12-11 21:27:04

标签: r stata

我正在使用面板数据,我想估计一个具有特定状态趋势的固定效应回归。

在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)

我如何在Stata中以xi的方式包含50(状态x时间)假人?

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

3 个答案:

答案 0 :(得分:2)

您只需将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,]

答案 1 :(得分:1)

这可能是你想要的:

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

答案 2 :(得分:0)

要完成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")