Capturing library function output

I am really at a loss. I found a couple of threads here, in a stack overflow, on how to redirect function output, but none of this works in my case.

I use arima from library(forecast) for a large number of (generated) time series, and some of them have bad properties, which causes auto.arima() print an error and a warning. In any case, I cannot catch this error, whether through tryCatch or capture.output() (which only captures the normal forecast).

The goal is to capture and respond to the error message (and warning) in the example below. Thus, basically at the end I would have a mistake and a forecast (despite the fallacy) in some variable form.

I appreciate any suggestions, below is a minimal example for getting an error:

 library(forecast) testt <- c(826,816,839,995,697) testend <- c(2015,164) testseries <- ts(testt,end=testend,frequency=365) auto.arima(testseries) #tryCatch not working: testfc <- tryCatch(forecast(auto.arima(testseries),h=1), error=function(e) NA) #capture.output not working: result <- capture.output(auto.arima(testseries)) 
+5
source share
2 answers

You can commit errors and warnings with the argument type="message" before capture.output . type can be either "output", which captures the output of the function, or "message", which captures errors and warnings. The function below uses sapply so that you can run capture.output once with each argument, storing the results in a list.

 capture.errors = function(type, data) { sapply(type, function(type) { capture.output(auto.arima(data), type=type) }, simplify=FALSE) } out = capture.errors(c("output","message"), testseries) out $output [1] "Series: data " [2] "ARIMA(0,0,0) with non-zero mean " [3] "" [4] "Coefficients:" [5] " intercept" [6] " 834.6000" [7] "se 42.4746" [8] "" [9] "sigma^2 estimated as 9020: log likelihood=-29.86" [10] "AIC=63.73 AICc=69.73 BIC=62.94" $message [1] "Error in arima(x, order = c(1, d, 0), xreg = xreg) : " [2] " non-stationary AR part from CSS" [3] "In addition: Warning message:" [4] "In auto.arima(data) : Unable to calculate AIC offset" 

Since capturing model output using capture.output is probably not as useful as capturing β€œreal” output in a model object, the function below might be better. It returns a list with the model object and any error or warning messages:

 capture = function(data) { list(model=auto.arima(data), message=capture.output(auto.arima(data), type="message")) } 

The model object is accessible in the usual way, so below I just look at the output of the message.

 out1 = capture(testseries) # Show any errors and warnings out1[["message"]] [1] "Error in arima(x, order = c(1, d, 0), xreg = xreg) : " [2] " non-stationary AR part from CSS" [3] "In addition: Warning message:" [4] "In auto.arima(data) : Unable to calculate AIC offset" out2 = capture(cumsum(rnorm(100))) # No errors or warnings with this data set out2[["message"]] character(0) 
+5
source

If I understand correctly, you want to suppress the printing of the error message. (This seems to be the purpose of your tryCatch() call.) One way to do this is to forward any error messages to a temporary file with sink(..., type="message") just before your call to auto.arima() sink(..., type="message") . Then, immediately after the call, clean by stopping immersion in the file, and then deleting it.

Here you can implement this:

 muffleMessages <- function(expr) { f <- tempfile() ff <- file(f, open="w") sink(ff, type="message") on.exit({sink(); unlink(f)}) expr } muffleMessages(auto.arima(testseries)) # Series: testseries # ARIMA(0,0,0) with non-zero mean # # Coefficients: # intercept # 834.6000 # se 42.4746 # # sigma^2 estimated as 9020: log likelihood=-29.86 # AIC=63.73 AICc=69.73 BIC=62.94 
+4
source

Source: https://habr.com/ru/post/1238914/


All Articles