简体   繁体   English

lmer model不收敛时如何防止模拟停止

[英]How to prevent simulation stopping when lmer model does not converge

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:有时 model 不收敛,我收到以下错误消息:

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我试图将这样的东西插入 function

if(is.null(summary(model)$optinfo$message) == FALSE) {return(NA)}

But its too late.但为时已晚。

I will appreciate any help.我将不胜感激。

Try tryCatch , as error= argument use a vector of NA s with length corresponding to the expected output.尝试tryCatch ,因为error=参数使用NA的向量,其长度对应于预期的 output。 Example:例子:

library(lme4)
simulate_study <- function(params) { 
  # pats <- simulate_pats(params) # a function that simulate the pats data frame
  reject <- tryCatch({
    # stop()  ## uncomment line to produce error and see the effect
    model <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy, REML=TRUE)
    as.numeric(confint(model, method="Wald")[5:6, ] > 0)
  }, error=function(e) rep(NA, 4L))
  return(reject)
}

replicate(10L, simulate_study(params=0))  ## more suitable than `sapply` here

You could also try to use REML=FALSE, lmerControl(optCtrl=list(maxit=100L)) and look if it converges better.您也可以尝试使用REML=FALSE, lmerControl(optCtrl=list(maxit=100L))并查看它是否收敛得更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM