在lapply()&地图():环境混乱与fixest :: feols和lm
我正在尝试在lapply()
中使用explion.model.frame()
都使用lm
和fix> fixest :: feols()
。
碰巧的是,我不断遇到fixest :: feols
的错误,这很可能是因为我对环境感到困惑。
这是一个原型示例:
library(fixest)
library(lfe)
library(estimatr)
fun <- function(object, clustid){
fml <- object$call$formula
if(!inherits(clustid, "formula")){
clustid <- reformulate(clustid)
}
expand.model.frame(
model = object,
extras = clustid,
#envir = environment(terms(object)),
#envir = .GlobalEnv,
na.expand = FALSE
)
}
data(mtcars)
tmp_lm <- lapply(list(mtcars, mtcars), function(x){lm(mpg ~ qsec ,data = x)})
lapply(tmp_lm, function(x) fun(x, clustid = "carb"))
tmp_feols <- lapply(list(mtcars, mtcars), function(x){feols(mpg ~ qsec ,data = x)})
lapply(tmp_feols, function(x) fun(x, clustid = ~carb))
# Error in eval(model$call$data, envir) : object 'x' not found
tmp_felm <- lapply(list(mtcars, mtcars), function(x){felm(mpg ~ qsec ,data = x)})
lapply(tmp_felm, function(x) fun(x, clustid = ~carb))
tmp_estimatr <- lapply(list(mtcars, mtcars), function(x){lm_robust(mpg ~ qsec ,data = x)})
lapply(tmp_estimatr, function(x) fun(x, clustid = ~carb))
是否有人建议我如何不得不调整fun()
,以便我不再遇到以下错误?
I am trying to use expand.model.frame()
within lapply()
after estimating models with both lm
and fixest::feols()
.
As it happens, I continuously run into errors for fixest::feols
, likely because I am confused about environments.
Here's a prototypical example:
library(fixest)
library(lfe)
library(estimatr)
fun <- function(object, clustid){
fml <- object$call$formula
if(!inherits(clustid, "formula")){
clustid <- reformulate(clustid)
}
expand.model.frame(
model = object,
extras = clustid,
#envir = environment(terms(object)),
#envir = .GlobalEnv,
na.expand = FALSE
)
}
data(mtcars)
tmp_lm <- lapply(list(mtcars, mtcars), function(x){lm(mpg ~ qsec ,data = x)})
lapply(tmp_lm, function(x) fun(x, clustid = "carb"))
tmp_feols <- lapply(list(mtcars, mtcars), function(x){feols(mpg ~ qsec ,data = x)})
lapply(tmp_feols, function(x) fun(x, clustid = ~carb))
# Error in eval(model$call$data, envir) : object 'x' not found
tmp_felm <- lapply(list(mtcars, mtcars), function(x){felm(mpg ~ qsec ,data = x)})
lapply(tmp_felm, function(x) fun(x, clustid = ~carb))
tmp_estimatr <- lapply(list(mtcars, mtcars), function(x){lm_robust(mpg ~ qsec ,data = x)})
lapply(tmp_estimatr, function(x) fun(x, clustid = ~carb))
Does anyone have a suggestion how I would have to tweak fun()
so that I would no longer run into the error below?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的评论代码中,我可以看到您一直在尝试
envir
参数。您在此处的正确轨道。 “ Fixest”类包含一个称为call_env
的对象,以确保您可以访问正确的评估环境。这意味着您需要根据传递的对象类型来更改代码以获取正确的调用环境:这允许:
但是仍然可以在
lm
版本上使用。当然,做到这一点的一种更复杂的方法是创建一个新的通用函数,该功能根据类别进行分发,而不是在一个大函数中处理类。
由
I can see from your commented code that you have been experimenting with the
envir
argument. You are on the right track here. The "fixest" class contains an object calledcall_env
to ensure you can access the correct evaluating environment. That means you need to alter your code to get the correct calling environment according to the type of object passed:This allows:
But will still work on the
lm
version.Of course, a more sophisticated way to do this would be to create a new generic function that dispatches according to class, rather than handling classes inside one big function.
Created on 2022-05-28 by the reprex package (v2.0.1)