1

I'm writing a loop that fits models across several datasets and outcome variables. I'm saving the results in a matrix. in the current version, I accounted for potential errors such as missing covariates. The more data I include the more errors I get and I need to account for in the loop. I would like to modify the code below so it record "NA" when the model stops due to an error regardless of the error type. I would appreciate any thoughts.

datasets <- list('data_01','data_02','data_03','data_04')
outcomes <- list('var_01','var_02','var_03','var_04','var_05')
results <- vector("list", length(datasets))
for (i in 1:length(datasets)) {
results [[i]] <- matrix(NA, nrow=length(outcomes), ncol=2)
}
for (j in seq_along(outcomes)) {
for (i in seq_along(surveys)) {
if ("TRUE" %in% (!(outcomes[[j]] %in% names(datasets[[i]]))))
{
  results[[i]][j, 1] <- outcomes[[j]]
  results[[i]][j, 2] <- "NA"
} 
else
{
  results[[i]][j, 1] <- outcomes[[j]]

 fit <- glmer(~ RS_AGE + RS_MARITAL + (1|FW_ID) + (1|RS_CLID), data = datasets[[i]], family = 
 binomial, nAGQ=0, control = glmerControl(optimizer = "nloptwrap"))

SI <- getME(fit,"theta")^2
ICC <- SI[[2]] /(SI[[1]]+SI[[2]]+3.29)

  results[[i]][j, 2] <- ICC
}
}
}
M Elkasabi
  • 29
  • 4
  • 1
    You might want to take a look at the `tryCatch` function: https://stackoverflow.com/questions/12193779/how-to-write-trycatch-in-r – Zoe Apr 22 '22 at 12:53

2 Answers2

2

Without the data I can't test, but this should work:

atasets <- list('data_01','data_02','data_03','data_04')
outcomes <- list('var_01','var_02','var_03','var_04','var_05')
results <- vector("list", length(datasets))
for (i in 1:length(datasets)) {
  results [[i]] <- matrix(NA, nrow=length(outcomes), ncol=2)
}
for (j in seq_along(outcomes)) {
  for (i in seq_along(surveys)) {
    if (any(!(outcomes[[j]] %in% names(datasets[[i]]))))
    {
      results[[i]][j, 1] <- outcomes[[j]]
      results[[i]][j, 2] <- NA
    } 
    else
    {
      results[[i]][j, 1] <- outcomes[[j]]
      
      form <- reformulate(c("RS_AGE", "RS_MARITAL", "(1|FW_ID)", "(1|RS_CLID)"), 
                          response = outcomes[[j]])
      fit <- try(glmer(form, data = datasets[[i]], family = 
                     binomial, nAGQ=0, control = glmerControl(optimizer = "nloptwrap")))
      if(!inherits(fit, "try-error")){
        SI <- getME(fit,"theta")^2
        ICC <- SI[[2]] /(SI[[1]]+SI[[2]]+3.29)
        
        results[[i]][j, 2] <- ICC
      }else{
        results[[i]][j,2] <- NA
      }
    }
  }
}
DaveArmstrong
  • 18,377
  • 2
  • 13
  • 25
2

Try replacing

 fit <- glmer(~ RS_AGE + RS_MARITAL + (1|FW_ID) + (1|RS_CLID), data = datasets[[i]], family = 
 binomial, nAGQ=0, control = glmerControl(optimizer = "nloptwrap"))

SI <- getME(fit,"theta")^2
ICC <- SI[[2]] /(SI[[1]]+SI[[2]]+3.29)

  results[[i]][j, 2] <- ICC

by

fit <- tryCatch(glmer(~ RS_AGE + RS_MARITAL + (1|FW_ID) + (1|RS_CLID), data = datasets[[i]], family = 
               binomial, nAGQ=0, control = glmerControl(optimizer = "nloptwrap")),error = function(e) e)
if(!inherits(fit,"error")){
  SI <- getME(fit,"theta")^2
  ICC <- SI[[2]] /(SI[[1]]+SI[[2]]+3.29)
}else{
  ICC <- NA
}
denis
  • 5,580
  • 1
  • 13
  • 40