I run a simulation in which I have a loop similar to this
simulate_study <- function(params){
pats <- simulate_pats(params) # a function that simulate the pats data frame
model <- lmer(SFD ~ group*month + (1 + month|id), data=pats, REML=TRUE)
reject <- as.numeric(confint(model, method="Wald")[8, 1] > 0)
return(reject)
}
res <- sapply(1:1000, FUN=simulate_study, params=some_values)
Sometimes the model does not converge, and I get the following error message:
Error in eigen(Sigma, symmetric = TRUE) :
infinite or missing values in 'x'
In addition: Warning message:
In Ops.factor(sd, 2) :
Error in eigen(Sigma, symmetric = TRUE) :
infinite or missing values in 'x'
I don't care about the error. I want the loop to keep running, but the error stops the whole loop. I tried to insert the something like this into the function
if(is.null(summary(model)$optinfo$message) == FALSE) {return(NA)}
But its too late.
I will appreciate any help.