s3alfisc / fwildclusterboot

Fast Wild Cluster Bootstrap Inference for Regression Models / OLS in R. Additionally, R port to WildBootTests.jl via the JuliaConnectoR.
https://s3alfisc.github.io/fwildclusterboot/
GNU General Public License v3.0
24 stars 4 forks source link

Problem with FELM formulas #145

Closed ecrjak closed 1 year ago

ecrjak commented 1 year ago

I am encountering issues when running boottest after felm but only if I supply the formula argument indirectly via a formula object (which I do rather frequently in my workflow). When doing so I get an error message saying Error in x$formula : object of type 'symbol' is not subsettable

Here is an illustration using the example from the reference manual. Note that felm accepts both versions whereas boottest doesn't:

This works:

requireNamespace("lfe")
data(voters)
felm_fit <- felm(proposition_vote ~ treatment + ideology1 + log_income | Q1_immigration, data = voters)
boot1 <- boottest(felm_fit,
                  B = 9999,
                  param = "treatment",
                  clustid = "group_id1"
)

This doesn't work:

requireNamespace("lfe")
data(voters)
for.reg <- formula(proposition_vote ~ treatment + ideology1 + log_income | Q1_immigration)
felm_fit <- felm(for.reg, data = voters)
boot1 <- boottest(felm_fit,
                  B = 9999,
                  param = "treatment",
                  clustid = "group_id1"
)

Thanks a lot in advance for any help!

s3alfisc commented 1 year ago

Hi, boottest calls Formula::Formula internally. What happens if you specify your models via Formula::Formula instead of stats::Formula? I can't debug right now as not close to a laptop unfortunately.

ecrjak commented 1 year ago

Using for.reg <- Formula::Formula(proposition_vote ~ treatment + ideology1 + log_income | Q1_immigration)

produces the error message Error in is.name(callee) && length(object) > 20 : 'length = 2' in coercion to 'logical(1)'

It still creates for.reg but when I use this in boottest, I get the same error message as in my initial post Error in x$formula : object of type 'symbol' is not subsettable without creating boot1.

s3alfisc commented 1 year ago

Thanks, I'll take a closer look this weekend! :)

ecrjak commented 1 year ago

Hi. Just wanted to quickly check if there are already any updates :)

s3alfisc commented 1 year ago

Sorry, it was a busy week and when I had time, some other projects took priority. Will take a look tomorrow though!

ecrjak commented 1 year ago

Sorry, just wanted to quickly follow up on this again...

s3alfisc commented 1 year ago

Hi @ecrjak ,

I just took a look: the problem arises in model_matrix.felm.

library("lfe")
data(voters)
for.reg <- formula(proposition_vote ~ treatment + ideology1 + log_income | Q1_immigration)
felm_fit <- felm(for.reg, data = voters)

model.matrix(felm_fit)
lfe:::model.matrix.felm(felm_fit)

Both stats::model_matrix and lfe::model.matrix.felm produce this error & I currently don't know best solve it within fwildclusterboot.

Basically, what happens is that model.matrix tries to access lfe_fit$call$formula, which returns for.reg, which is not a proper formula.

But if you specify your model as

lfe_fit <- felm(formula = for.reg, data = voters)
#lfe_fit$call$formula <- for.reg

boot1 <- boottest(feols_fit,
                  B = 9999,
                  param = "treatment",
                  clustid = "group_id1"
)

i.e. if you name the formula argument, then boottest runs through, at least for me =) I hope that will work for you as well!

Last, I wanted to point you to the formidable fixest package, which does the same job as lfe but, in many ways, better (in case you are not yet aware of it).

library(fixest)
feols_fit <- feols(for.reg, data = voters)
boot1 <- boottest(feols_fit,
                  B = 9999,
                  param = "treatment",
                  clustid = "group_id1"
)

summary(boot1)

Is there a patricular reason for which you need to stick with lfe? The package is no longer developed and only maintained so that it remains on CRAN, and fixest really is superb (and works well with fwildclusterboot).

And sorry for the delay!

Best, Alex

ecrjak commented 1 year ago

Thanks so much for this! Indeed, when explicitly overwriting lfe_fit$call$formula with the initial formula, the code runs through without any error :)

There is no particular reason for sticking with the lfe package apart from my own laziness in re-writing the codes for many of my research projects. That said, it seems like fixest has a lot of interesting stuff to offer and it may be worth starting the transition if lfe is no longer developed as you say. Thanks for the nudge!