简体   繁体   English

如何在 R 中进行具有交互效应和滞后的固定效应回归?

[英]How to do a fixed effects regression with interaction effect and lags in R?

I have panel data over the period 2010-2020 for 10 different regions in a country.我有一个国家 10 个不同地区在 2010-2020 年期间的面板数据。 I would like to measure the effect of X1 on Y were Y is the share of the population who voted for a extreme right party at Year s , and X1 is the share of asylum seekers in the region at Year s .我想衡量X1Y的影响,如果Y是在s年投票支持极右政党的人口比例, X1是在s年该地区寻求庇护者的比例。 I would like to measure the effect of X1 on Y during a specific year when asylum seekers in all regions increased drastically to examine if the increase in asylum seekers had any effect on the share of the population who voted on the extreme right party.我想测量X1在特定年份对Y的影响,当时所有地区的寻求庇护者急剧增加,以检查寻求庇护者的增加是否对投票给极右翼政党的人口比例有任何影响。 X2...Xn are a bunch of control variables. X2...Xn 是一堆控制变量。

I have found the package "fixest" and can run a normal FE-regression, but cant seem to understand how to include an interaction effect with a specific year (the "treatment" year, when asylum seekers increased in all regions) for X1.我发现 package “最固定”并且可以运行正常的 FE 回归,但似乎无法理解如何将 X1 的特定年份(“治疗”年份,当所有地区的寻求庇护者增加时)包含交互效应。

This is an example of the equation im trying to estimate: FE regression model , where (1) RHS term is region fixed effects, (2) RHS term is year fixed effects, (3) RHS term is the interaction between the year 2016 and asylum seekers in the previous year, (4) RHS term is all the control variables and (5) RHS term is the error term.这是我试图估计的方程的一个示例: FE 回归 model ,其中 (1) RHS 项是区域固定效应,(2) RHS 项是年份固定效应,(3) RHS 项是 2016 年和(4) RHS 项是所有控制变量,(5) RHS 项是误差项。

Below is a made up head of the data frame.下面是数据框的组成部分。

Year   Region   Y      X1    X2...Xn
2010   A        0.15  0.001
2011   A        0.25  0.05
.      .        .     .
.      .        .     .
2010   B        0.09  0.002
2011   B        0.14  0.04

you can simply use the function i() to create an interaction between a continuous variables (x1) and a factor (the year).您可以简单地使用 function i()在连续变量 (x1) 和因子 (年份) 之间创建交互。 With i 's argument keep , you can select the year you want to keep.使用i的参数keep ,您可以 select 您想要保留的年份。

The second thing is the lag: just use the function l() within a fixest estimation, but it requires to specify the panel identifiers.第二件事是滞后:只需在固定估计中使用fixest l() ,但它需要指定面板标识符。

Here's an example of the expected behavior, it should be easy to adapt to your case:这是预期行为的示例,它应该很容易适应您的情况:

library(fixest)

data(base_did)
est = feols(y ~ i(l(x1), period, keep = 10) | id + period, base_did, panel.id = ~id+period)
#> NOTE: 108 observations removed because of NA values (RHS: 108).
est
#> OLS estimation, Dep. Var.: y
#> Observations: 972 
#> Fixed-effects: id: 108,  period: 9
#> Standard-errors: Clustered (id) 
#>                      Estimate Std. Error t value Pr(>|t|) 
#> l(x1, 1):period::10 -0.263237   0.217412 -1.2108 0.228651 
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 4.8876     Adj. R2: 0.173257
#>                Within R2: 0.002642

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

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