我正在尝试使用fixest::feols输出模型的多个版本。我的目标是为数据集中的每个限制列独立运行模型,并获得所有回归摘要的列表或etable。棘手的部分是,我正在与Year交互限制变量,并且我不确定如何在fixest的I((功能中使用sw((。我还尝试编写一个函数,并使用map通过向其提供列表来输出模型,但由于我对NSE的了解有限,我也遇到了麻烦。以下是我的数据集的简化版本:
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))
我尝试了两种方法来做到这一点。第一种是在feals:中使用sw((
require(fixest)
res <- feols(fml = pct ~
i(sw(registration, conditional_ban), Year) | factor(Year) + factor(fipscode),
data = df)
etable(res)
这就产生了错误:";不能将逐步函数与任何其他函数组合元素"在这个发现之后,我试图为我的模型创建一个函数,然后在地图中使用它:
# 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)
这导致错误:";feols(fml=pct~i(.x[[i]],Year(| factor(Year(+中的错误:变量".x"one_answers"i"在公式的RHS(第一部分(中,但不在数据集中">
现在,我知道我正在向函数model_restr提供一个引用的参数,并且我已经尝试过使用!!或noquote((取消对参数的引用,如下所示:
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和编程还很陌生,但我渴望了解这一点,以便加快我的工作流程。我感谢你的帮助!!
只需将i()
插入sw()
即可:
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()
(这只会使估计速度变慢(。