首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Fixest::feols:使用sw进行交互或使用map输出模型的多个RHS版本

Fixest::feols:使用sw进行交互或使用map输出模型的多个RHS版本
EN

Stack Overflow用户
提问于 2021-04-02 01:21:11
回答 1查看 101关注 0票数 1

我正在尝试使用fixest::feols输出一个模型的多个版本。我的目标是为我的数据集中的每个限制列独立运行模型,并获得所有回归摘要的列表或表格。棘手的部分是我正在与Year交互限制变量,并且我不确定如何在fixest的i()功能中使用sw()。我还尝试编写一个函数,并使用map通过向其提供列表来输出模型,但由于我对NSE的了解有限,在那里我也遇到了麻烦。以下是我的数据集的简化版本:

代码语言:javascript
复制
df <- data.frame(Year = rep(2000:2007), fipscode = c("001", "002"), 
                 conditional_ban = rep(0:1), registration = rep(0:1), pct = rnorm(n = 16, mean = .02, sd = .005))

我试着用两种方法来做这件事。第一种是在feols中使用sw():

代码语言:javascript
复制
require(fixest)

res <- feols(fml = pct ~ 
                     i(sw(registration, conditional_ban), Year) | factor(Year) + factor(fipscode), 
             data = df)

etable(res)

这会产生错误:“您不能将逐步函数与任何其他元素组合在一起。”在这个发现之后,我尝试为我的模型创建一个函数,然后在map中使用它:

代码语言:javascript
复制
# function that takes a restriction as its argument and results in a table summary with the name 
# of the restriction 
model_restr <- function(restr){
        mod <- eval(substitute(feols(fml = pct ~
                                             i(restr, Year) | factor(Year) +
                                             factor(fipscode), 
                                     data = df)))
        modsum <- summary(mod)
        varName <- deparse(substitute(restr))
        . <- etable(modsum, signifCode = c("***" = 0.01, "**" = 0.05, "*" = 0.1))
        assign(varName, ., envir = globalenv())
}

# Then, I create a list with the restrictions
restrs <- list(names = c("conditional_ban", "registration")

# Next, I try to use map to loop the function over the list
map(restrs$names, model_restr)

这会导致错误:"Error in feols(fml = pct ~ i(.x[i],Year) | factor(Year) +:变量'.x‘和'i’在公式的RHS (第一部分)中,但不在数据集中。“

现在,我知道我正在向函数model_restr提供一个带引号的参数,并且我尝试使用!!或者使用noquote()来取消引用参数,比如:

代码语言:javascript
复制
 model_restr <- function(restr){
        restr <- noquote(restr)
        mod <- eval(substitute(feols(fml = pct ~
                                             i(restr, Year) | factor(Year) +
                                             factor(fipscode), 
                                     data = df)))
        modsum <- summary(mod)
        varName <- deparse(substitute(restr))
        . <- etable(modsum, signifCode = c("***" = 0.01, "**" = 0.05, "*" = 0.1))
        assign(varName, ., envir = globalenv())
}

我知道当涉及到NSE时,我可能遗漏了一些重要的东西,但我不知道到底是哪里出了问题。我对R和编程非常陌生,但我渴望了解这一点,这样我就可以加快我的工作流程。感谢您的帮助!!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-02 18:12:11

只需将i()插入到sw()中,就可以了:

代码语言:javascript
复制
data(base_did) ; base = base_did
# Alternative treatment indicator, purely random
base$treat_bis = 1 * (base$id %in% sample(108, 50))

# 2 estimations
res = feols(y ~ sw(i(treat, period, 5), i(treat_bis, period, 5)) | id + period, base)
etable(res)
#>                                  model 1         model 2
#> Dependent Var.:                        y               y
#>                                                         
#> treat x period = 1        -2.015 (1.342)                
#> treat x period = 2        -1.664 (1.389)                
#> treat x period = 3        0.5041 (1.323)                
#> treat x period = 4       -0.8846 (1.416)                
#> treat x period = 6         1.159 (1.227)                
#> treat x period = 7       4.335** (1.308)                
#> treat x period = 8        3.826* (1.514)                
#> treat x period = 9      4.640*** (1.293)                
#> treat x period = 10     6.947*** (1.426)                
#> treat_bis x period = 1                    -1.565 (1.358)
#> treat_bis x period = 2                    -1.200 (1.425)
#> treat_bis x period = 3                   -3.350* (1.301)
#> treat_bis x period = 4                   -3.538* (1.385)
#> treat_bis x period = 6                   -2.660* (1.227)
#> treat_bis x period = 7                    -2.274 (1.382)
#> treat_bis x period = 8                   -3.399* (1.529)
#> treat_bis x period = 9                   -2.724. (1.374)
#> treat_bis x period = 10                   -2.177 (1.615)
#> Fixed-Effects:          ---------------- ---------------
#> id                                   Yes             Yes
#> period                               Yes             Yes
#> _______________________ ________________ _______________
#> S.E.: Clustered                   by: id          by: id
#> Observations                       1,080           1,080
#> R2                               0.32343         0.26744
#> Within R2                        0.08710         0.01155

我擅自更改了示例,因为交互与固定效果是共线的。

顺便说一句,在管道的RHS中不需要factor() (它只会使估计变慢)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66908988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档