R 中的警告 - 做笔记
Warnings in R - making note
我正在尝试 运行 R 中的函数 (glm) 1000 次(每次输入略有不同)。有时我会收到警告(因为发生了分离)——我想做的就是计算这种情况发生的次数(所以在 1000 次迭代中我收到警告的次数 "glm.fit: fitted probabilities numerically 0 or 1 occurred")。
所以我的代码大致如下:
warningCount <- 0
for(i in 1:1000) {
[generate data]
glm(y ~ ., family="binomial", data=generated_data)
if( I got the warning ) warningCount <- warningCount + 1
}
我只是想知道如果(我收到警告)如何正确地写这行。
谢谢
您可以使用 tryCatch
正确获取警告:
lapply(1:1000, function(u){
[generate some data]
tryCatch(glm(y ~ ., family="binomial", data=generated_data),
warning = function(w) list(glm(y ~ ., family="binomial", data=generated_data), w)
})
这里它同时捕获警告和结果,但是如果你想捕获警告就可以了。
我会使用 tryCatch()
捕捉、检查并根据警告采取行动。
## A function that randomly emits one of two warnings
f <- function() if(runif(1) > .7) warning("Ouch") else warning("Hmmm")
## A function to catch and process warnings, adding to a global counter
## when one of them includes the string "Ouch"
saidOuch <- function(w) {
if(grepl("Ouch", as.character(w))) {ww <<- ww+1}
}
## Run your analyses, each wrapped in a tryCatch()
ww <- 0
for(i in 1:100) tryCatch(f(), warning = saidOuch)
ww
## [1] 32
对于您的具体情况,如果您决定不处理 some other way 中的完美分离问题,您可以使用如下函数捕获和计算警告:
perfectSeparation <- function(w) {
if(grepl("fitted probabilities numerically 0 or 1 occurred",
as.character(w))) {ww <<- ww+1}
}
## Test it with a function that will fit glm's that often emit the warning of interest
fitglm <- function() {
dat <- data.frame(x=rnorm(3), y=sample(0:1, 3, replace=TRUE))
glm(y~x, data=dat, family="binomial")
}
ww <- 0
for(i in 1:100) tryCatch(fitglm(), warning = perfectSeparation)
ww
# [1] 45
我正在尝试 运行 R 中的函数 (glm) 1000 次(每次输入略有不同)。有时我会收到警告(因为发生了分离)——我想做的就是计算这种情况发生的次数(所以在 1000 次迭代中我收到警告的次数 "glm.fit: fitted probabilities numerically 0 or 1 occurred")。
所以我的代码大致如下:
warningCount <- 0
for(i in 1:1000) {
[generate data]
glm(y ~ ., family="binomial", data=generated_data)
if( I got the warning ) warningCount <- warningCount + 1
}
我只是想知道如果(我收到警告)如何正确地写这行。
谢谢
您可以使用 tryCatch
正确获取警告:
lapply(1:1000, function(u){
[generate some data]
tryCatch(glm(y ~ ., family="binomial", data=generated_data),
warning = function(w) list(glm(y ~ ., family="binomial", data=generated_data), w)
})
这里它同时捕获警告和结果,但是如果你想捕获警告就可以了。
我会使用 tryCatch()
捕捉、检查并根据警告采取行动。
## A function that randomly emits one of two warnings
f <- function() if(runif(1) > .7) warning("Ouch") else warning("Hmmm")
## A function to catch and process warnings, adding to a global counter
## when one of them includes the string "Ouch"
saidOuch <- function(w) {
if(grepl("Ouch", as.character(w))) {ww <<- ww+1}
}
## Run your analyses, each wrapped in a tryCatch()
ww <- 0
for(i in 1:100) tryCatch(f(), warning = saidOuch)
ww
## [1] 32
对于您的具体情况,如果您决定不处理 some other way 中的完美分离问题,您可以使用如下函数捕获和计算警告:
perfectSeparation <- function(w) {
if(grepl("fitted probabilities numerically 0 or 1 occurred",
as.character(w))) {ww <<- ww+1}
}
## Test it with a function that will fit glm's that often emit the warning of interest
fitglm <- function() {
dat <- data.frame(x=rnorm(3), y=sample(0:1, 3, replace=TRUE))
glm(y~x, data=dat, family="binomial")
}
ww <- 0
for(i in 1:100) tryCatch(fitglm(), warning = perfectSeparation)
ww
# [1] 45