r -r- rycatch()funtion-如何保存错误/警告消息?

发布于 2025-01-23 21:05:12 字数 255 浏览 4 评论 0原文

我正在模拟多级数据并将其拟合到各种多级模型中。

我想制作一个函数,如果存在任何错误(例如“无法收敛”或“单数拟合”),我想保存它。

例如,我的模型是

lmer(y〜x1 + x2 +(1 | pid),data = sim_data)。

这是许多条件,因此将在此模型中拟合各种数据。

如何在数据框架或列表中保存错误或整个警告消息?

(例如,第一个数据集 - >无错误,第二个数据集 - 汇聚错误...等)

I am simulating multilevel data and fitting it to various Multilevel models.

I want to make a function that if there is any error (such as "failed to converge" or "singular fit"), I want to save it.

For example, my model is

lmer(y~ x1 + x2 + (1|pid), data=sim_data).

and here are many conditions so various data will be fitted into this model.

How can I save the error or warning message as a whole in the dataframe or list?

(like, first dataset -> no error, second dataset -> converge error...etc)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油焖大侠 2025-01-30 21:05:12

您可以使用for循环在条件下迭代或“各种数据”

dummyfunction <- function(x) {
  if (i == 0) {
    stop("Error occurred")
  }
  return(i * 100)
}

0

result <- list()

for (i in c(-1, 0, 1)) {

  # Using tryCatch to handle errors 
  # that might occur when executing dummyfunction (i.e. put them in the result list)
  #
  result[[as.character(i)]] <- tryCatch(dummyfunction(i),
    error = function(e) e
  )  
  
}

You could use a for loop to iterate through the conditions or "various data":

0.Setting up a dummy function that might throw error:

dummyfunction <- function(x) {
  if (i == 0) {
    stop("Error occurred")
  }
  return(i * 100)
}

1.Preparing a list to receive results

result <- list()

2.Iterating through different "conditions":

for (i in c(-1, 0, 1)) {

  # Using tryCatch to handle errors 
  # that might occur when executing dummyfunction (i.e. put them in the result list)
  #
  result[[as.character(i)]] <- tryCatch(dummyfunction(i),
    error = function(e) e
  )  
  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文