0

Hi I am having issues regarding a foreach loop where in every iteration I estimate a regression on a subset of the data with a different list of controls on several outcomes. The problem is that for some outcomes in some countries I only have missing values and therefore the regression function returns an error message. I would like to be able to run the loop, get the output with NAs or a string saying "Error" for example instead of the coefficient table. I tried several things but they don't quite work with the .combine = rbind option and if I use .combine = c I get a very messy output. Thanks in advance for any help.

reg <- function(y, d, c){
  if (missing(c))
    feols(as.formula(paste0(y, "~ 0 + treatment")), data = d)
  else {
    feols(as.formula(paste0(y, "~ 0 + treatment + ", c)), data = d) 
  }
}


# Here we set up the parallelization to run the code on the server
n.cores <- 9 #parallel::detectCores() - 1

#create the cluster
my.cluster <- parallel::makeCluster(
  n.cores, 
  type = "PSOCK"
)
# print(my.cluster)

#register it to be used by %dopar%
doParallel::registerDoParallel(cl = my.cluster)

# #check if it is registered (optional)
# foreach::getDoParRegistered()
# #how many workers are available? (optional)
# foreach::getDoParWorkers()

# Here is the cycle to parallel regress each outcome on the global treatment
# variable for each RCT with strata control

tables <- foreach(
  n = 1:9, .combine = rbind, .packages = c('data.table', 'fixest'),
  .errorhandling = "pass"
) %dopar% {
  
  dt_target <- dt[country == n]
  c <- controls[n]
  est <- lapply(outcomes,  function(x) reg(y = x, d = dt_target, c))
  
  table <- etable(est, drop = "!treatment", cluster = "uid", fitstat = "n")
  table
}

0 Answers0