gpt4 book ai didi

r - 当 R 中的循环中出现警告时,是否可以报告并提取它发生在哪个迭代中?

转载 作者:行者123 更新时间:2023-12-04 11:23:12 25 4
gpt4 key购买 nike

我目前有一些代码,我在其中循环回归模型,并且对于某些迭代,会出现一条警告消息。例如:

for(i in 1:100){
set.seed(i)
x = c(runif(100, min=-3, max=3), 200)
y = rbinom(101, size=1, prob=1/(1+e^(-x))
m = glm(y~x, family=binomial)
}
glm(y~x, family=binomial)会发出警告但它只在最后报告为:
 There were 50 or more warnings (use warnings() to see the first 50)

有没有办法查看哪个迭代导致了哪些警告,并最终能够报告导致警告的结果?

最佳答案

使用 tryCatch

for(i in 1:100){ 
set.seed(i)
x = c(runif(100, min=-3, max=3), 200)
y = rbinom(101, size=1, prob=1/(1+exp(-x)))
tryCatch({m <- glm(y~x, family=binomial)}, warning=function(w) print(i))
}

关于r - 当 R 中的循环中出现警告时,是否可以报告并提取它发生在哪个迭代中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45283731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com