Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boottest looking for the data in the wrong place when running inside a function #155

Open
Yuval-OS opened this issue Oct 9, 2024 · 2 comments

Comments

@Yuval-OS
Copy link

Yuval-OS commented Oct 9, 2024

See this reproducible code:
It works without a function, but when used inside a function it says it cannot find data_obj.
When I define data_obj in the global directory, it works fine.

requireNamespace("fwildclusterboot")
data(voters)

feols_fit <- feols(proposition_vote ~ treatment + ideology1 + log_income | Q1_immigration, data = voters)
boot1 <- boottest(feols_fit, B = 9999, param = "treatment", clustid = "group_id1")
summary(boot1)

boot_func <- function(data_obj) {
feols_fit1 <- feols(proposition_vote ~ treatment + ideology1 + log_income | Q1_immigration, data = data_obj)
boot1 <- boottest(feols_fit1,
B = 9999,
param = "treatment",
clustid = "group_id1")
return(summary(boot1))
}

boot_func(voters)
data_obj <- voters
boot_func(voters)

@ecrjak
Copy link

ecrjak commented Nov 27, 2024

In principle, this should be solved by the new fixest option data.save. However, this still needs to be implemented in boottest, it seems. The following code still throws an error: Error in eval(call("model.frame", ff, data = cl$data, subset = cl$subset, : object 'DATA_SAVED' not found

boot_func <- function(data_obj) {
  feols_fit1 <- feols(proposition_vote ~ treatment + ideology1 + log_income | Q1_immigration, data = data_obj,
                      data.save = TRUE)
  boot1 <- boottest(feols_fit1,
                    B = 9999,
                    param = "treatment",
                    clustid = "group_id1")
  return(summary(boot1))
}

boot_func(voters)

@ecrjak
Copy link

ecrjak commented Nov 27, 2024

Okay, it's a bit hacky but for me it seems to work if you explicitly define DATA_SAVED in the loop:

boot_func <- function(data_obj) {
  feols_fit1 <- feols(proposition_vote ~ treatment + ideology1 + log_income | Q1_immigration, data = data_obj,
                      data.save = TRUE)
  DATA_SAVED <- feols_fit1$data
  boot1 <- boottest(feols_fit1,
                    B = 9999,
                    param = "treatment",
                    clustid = "group_id1")
  return(summary(boot1))
}

boot_func(voters)

Note that you will also need to set the same seeds to ensure results are identical to the non-loop version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants