【问题标题】:How to prevent simulation stopping when lmer model does not convergelmer模型不收敛时如何防止模拟停止
【发布时间】:2022-01-04 09:41:39
【问题描述】:

我运行一个模拟,其中我有一个类似的循环

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)

有时模型不收敛,我收到以下错误消息:

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' 

我不关心错误。我希望循环继续运行,但错误会停止整个循环。 我试图将这样的东西插入到函数中

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

但为时已晚。

我将不胜感激。

【问题讨论】:

  • try函数。此外,错误与无限或缺失值有关,与收敛无关。
  • 如果这是一个可以在lmer 中更好地处理的边缘情况,有一个可重现的示例(使用set.seed() 并在你拥有some_values 的地方填写一些真实的东西)会很有帮助。

标签: r lme4 convergence


【解决方案1】:

尝试tryCatch,因为error= 参数使用NAs 的向量,其长度对应于预期的输出。 例子:

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

您也可以尝试使用REML=FALSE, lmerControl(optCtrl=list(maxit=100L)),看看它是否收敛得更好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多