2015-12-22 62 views
5

我真的很茫然。我在這裏發現了一堆關於如何重定向函數輸出的堆棧溢出的線程,但似乎沒有一個線程在我的情況下工作。捕捉庫函數的輸出

我使用library(forecast)的arima生成很多(生成的)時間序列,其中一些具有不良屬性,導致auto.arima()打印出錯誤和警告。無論如何,我無法捕捉到此錯誤,無論是通過tryCatch還是capture.output()(它僅捕獲正常預測)。

目標是捕獲下面示例引發的錯誤消息(和警告)並對其作出反應。所以基本上最後我會有一些可變形式的錯誤和預測(儘管是錯誤的)。

我明白任何意見,下面是小例子,產生錯誤:

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)) 
+0

什麼......究竟......是你想捕捉?我得到:'arima(x,order = c(1,d,0),xreg = xreg)中的錯誤:來自CSS的非靜態AR部分' –

+1

@ 42-OP正試圖捕獲由'auto引發的錯誤。 arima'。這個例子故意不起作用,爲了拋出一個錯誤。 – 2015-12-22 06:32:34

+0

我認爲OP的目標仍然不明確。從調用到'auto.arima'的輸出(到控制檯)包括錯誤消息和模型摘要。由於錯誤報告實際上並未停止執行,因此調用確實會返回一個類爲「c」(「ARIMA」,「arima」)的對象。所以編碼的目標需要澄清。有兩種不同的解釋不同地解釋了這些目標。 –

回答

5

您可以捕獲與type="message"參數錯誤和警告capture.outputtype可以是捕獲功能輸出的「輸出」或捕獲錯誤和警告的「消息」。下面的函數使用sapply來允許您對每個參數運行capture.output一次,並將結果存儲在列表中。

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] "s.e. 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" 

由於捕捉與capture.output模型輸出可能不如捕捉模型對象的「真實」的輸出爲有用的,也許下面的功能會更好。它返回一個列表與模型對象和任何錯誤或警告消息:

capture = function(data) { 
list(model=auto.arima(data), 
    message=capture.output(auto.arima(data), type="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) 
+0

謝謝你的解釋,列表中的捕獲函數正是我所期待的! :) – wlfbck

4

如果我理解正確,那麼您想要禁止打印錯誤消息。 (至少看起來是您致電tryCatch()的目標。)一種方法是在您撥打auto.arima()之前立即使用sink(..., type="message")將任何錯誤消息轉移到臨時文件。然後,在調用之後,通過停止對文件的沉沒然後刪除它進行清理。

這裏你可以實現一個方法:

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 
# s.e. 42.4746 
# 
# sigma^2 estimated as 9020: log likelihood=-29.86 
# AIC=63.73 AICc=69.73 BIC=62.94 
+0

我的印象是,OP想要捕捉錯誤和警告。 – eipi10

+0

@ eipi10 - 可以。我認爲這個問題幾乎完全模糊不清,它是在尋找一種方法來消除或捕捉並檢查錯誤和警告。猜猜我們會發現什麼時候OP編鐘英寸 –

+0

withCallingHandlers可能能夠幫助這個https://stackoverflow.com/questions/19433848/handling-errors-before-warnings-in-trycatch/19446931#19446931 – Hansi