Home > Software design >  An equivalent for the excel If error for lme4 glmer in r
An equivalent for the excel If error for lme4 glmer in r

Time:04-23

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
}
}
}

CodePudding user response:

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
      }
    }
  }
}

CodePudding user response:

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
}
  • Related