如何在并行循环中忽略 R tryCatch 中的警告
How to ignore the warnings in R tryCatch in do parallel loop
我正在 R 中构建 Arima 模型。我正在尝试使用 tryCatch 来处理模型训练中的异常——特别是在我没有足够数据来构建模型的情况下。因为我训练了很多模型(大约 380 个),所以我尝试使用 R 中的 doParallel 包来实现这个。
但我注意到有些警告我只需要忽略即可。下面给出的是我尝试过的。但我可以看到一些线程被卡住了。我只是想知道警告处理代码是否将线程置于无限循环中,因为我试图在警告中调用相同的表达式。
#some sample data
ops <- ['Android', 'iOS']
country <- ['US', 'CA']
apps <- ['A', 'B', 'C']
nCores <- detectCores()
registerDoParallel(cores = nCores)
foreach(os=ops)%:% foreach(country=countries) %:% foreach(app=apps)%dopar%{
modeling <- function(y_data)
{
#the function model is implemented inside the models.R #source file
#it basically uses auto.arima with some fourier regressors
#and returns to model
source("models.R")
#for each condition get data as y_data
tryCatch(
{
suppressWarnings (
out <- model(data=y_data)
)
},
warning=function(w)
{
print(w)
suppressWarnings (
out <- model(data=y_data)
)
},
error=function(e) {
print(e)
return(NULL) }
write.table(out$AICC, "results.csv")
}
modeling(y_data)
}
请注意,首先我尝试了 4 次循环迭代,第一个 运行 非常快,但其他 3 次迭代是 运行 永远。
您想忽略警告但在 suppressWarning
中打印警告?
...
warning=function(w)
{
print(w)
suppressWarnings (
...
您没有提供可重现的示例,但我会尝试从最少的几行开始:
...
nCores <- detectCores()
registerDoParallel(cores = nCores)
source("models.R")
modeling <- function(y_data) {
tryCatch({
suppressWarnings(out <- model(data=y_data))
})
}
foreach(os=ops) %:% foreach(country=countries) %:% foreach(app=apps) %dopar% {
modeling(y_data)
}
我正在 R 中构建 Arima 模型。我正在尝试使用 tryCatch 来处理模型训练中的异常——特别是在我没有足够数据来构建模型的情况下。因为我训练了很多模型(大约 380 个),所以我尝试使用 R 中的 doParallel 包来实现这个。 但我注意到有些警告我只需要忽略即可。下面给出的是我尝试过的。但我可以看到一些线程被卡住了。我只是想知道警告处理代码是否将线程置于无限循环中,因为我试图在警告中调用相同的表达式。
#some sample data
ops <- ['Android', 'iOS']
country <- ['US', 'CA']
apps <- ['A', 'B', 'C']
nCores <- detectCores()
registerDoParallel(cores = nCores)
foreach(os=ops)%:% foreach(country=countries) %:% foreach(app=apps)%dopar%{
modeling <- function(y_data)
{
#the function model is implemented inside the models.R #source file
#it basically uses auto.arima with some fourier regressors
#and returns to model
source("models.R")
#for each condition get data as y_data
tryCatch(
{
suppressWarnings (
out <- model(data=y_data)
)
},
warning=function(w)
{
print(w)
suppressWarnings (
out <- model(data=y_data)
)
},
error=function(e) {
print(e)
return(NULL) }
write.table(out$AICC, "results.csv")
}
modeling(y_data)
}
请注意,首先我尝试了 4 次循环迭代,第一个 运行 非常快,但其他 3 次迭代是 运行 永远。
您想忽略警告但在 suppressWarning
中打印警告?
...
warning=function(w)
{
print(w)
suppressWarnings (
...
您没有提供可重现的示例,但我会尝试从最少的几行开始:
...
nCores <- detectCores()
registerDoParallel(cores = nCores)
source("models.R")
modeling <- function(y_data) {
tryCatch({
suppressWarnings(out <- model(data=y_data))
})
}
foreach(os=ops) %:% foreach(country=countries) %:% foreach(app=apps) %dopar% {
modeling(y_data)
}