【问题标题】:How to make a model equation in R fixest and choose factor interactions如何在 R fixst 中制作模型方程并选择因子交互
【发布时间】:2020-12-10 00:36:44
【问题描述】:

我有一个差分模型,我想用对 R 的固定效应来估计它。我想包括暴露和治疗的相互作用来计算领先和滞后效应。

基本上,我有共线的变量,我想选择要从我的模型中排除的变量。几天前,使用 lfe 软件包可以做到这一点非常简单:

library(data.table)
library(fixest)

crime <- fread("crime.csv")

# Time as a factor for fixed effects
crime[, time := factor(time)]

# Model
xx <- model.matrix(any_crime ~ treatment:time - 1, data = crime) # time is a factor

# Set period 12 as base
xx[, "treatment:time12"] <- 0

# Model with leads and lags
m1 <- felm(any_crime ~ xx | id + time, data = crime)

但是,既然 lfe 已存档,我已转向更快的 fixest 包。但是我无法让feols() 接受我的model.matrix。与上面做同样的事情,但将felm() 替换为feols() 会出现以下错误:

> feols(any_crime ~ xx | id + time, data = crime)
Error in feols(any_crime ~ xx | id + time, data = crime)
  The variable xx is in the RHS of the formula but not in the dataset.

我已阅读?feols(),公式参数没有说明模型矩阵,详细信息部分只有一句话。

fixest 也有model.matrix.fixest,我还检查了这个函数的帮助,它说它需要一个fixest 对象作为它的第一个参数。我无法使用它来创建将周期 12 设置为基准周期的模型。


Here 是用于重现性的小数据样本。

谢谢大家。

【问题讨论】:

    标签: r estimation


    【解决方案1】:

    fixest估计公式中的所有变量都必须在数据集中:与lmfelm相反,没有来自全局环境的评估。

    但是您的情况可以通过函数i() 轻松处理。这是来自vignette 的示例:

    library(fixest)
    data(base_did)
    est_did = feols(y ~ x1 + i(treat, period, 5) | id + period, base_did)
    est_did
    #> OLS estimation, Dep. Var.: y
    #> Observations: 1,080 
    #> Fixed-effects: id: 108,  period: 10
    #> Standard-errors: Clustered (id) 
    #>                   Estimate Std. Error   t value  Pr(>|t|)    
    #> x1                0.973490   0.045678 21.312000 < 2.2e-16 ***
    #> treat:period::1  -1.403000   1.110300 -1.263700  0.209084    
    #> treat:period::2  -1.247500   1.093100 -1.141200  0.256329    
    #> treat:period::3  -0.273206   1.106900 -0.246813  0.805526    
    #> treat:period::4  -1.795700   1.088000 -1.650500  0.101769    
    #> treat:period::6   0.784452   1.028400  0.762798  0.447262    
    #> treat:period::7   3.598900   1.101600  3.267100  0.001461 ** 
    #> treat:period::8   3.811800   1.247500  3.055500  0.002837 ** 
    #> treat:period::9   4.731400   1.097100  4.312600   3.6e-05 ***
    #> treat:period::10  6.606200   1.120500  5.895800   4.4e-08 ***
    #> ---
    #> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    #> Log-likelihood: -2,984.58   Adj. R2: 0.48783 
    #>                           R2-Within: 0.38963
    

    函数i() 执行连续变量(尽管它也可以是一个因子)和一个因子(第二个元素始终被视为一个因子)之间的交互。然后,您可以添加参数ref 和/或drop 和/或keep 以指定要保留的因子水平。 (refdrop 之间的区别很微妙:ref 只接受一个值 [drop 接受多个值],并且在估计时使用 coefplot 时会突出显示该引用。)

    在前面的例子中,我们有ref = 5,所以第5个周期将被排除在交互之外。

    回到你的例子,以下应该可以工作(不创建任何额外的数据):

    feols(any_crime ~ i(treatment, time, 12) | id + time, data = crime)
    

    然后很容易变基或删除几个:

    feols(any_crime ~ i(treatment, time, 10) | id + time, data = crime)
    feols(any_crime ~ i(treatment, time, drop = 10:12) | id + time, data = crime)
    

    【讨论】:

    • 非常感谢您的回答!这完美地工作,很难过分说明这是多么有用。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 2020-10-31
    • 2020-01-18
    • 2013-07-28
    • 1970-01-01
    相关资源
    最近更新 更多