【问题标题】:In fable's ARIMA function, is it possible to capture the output from trace = TRUE?在 fable 的 ARIMA 函数中,是否可以捕获 trace = TRUE 的输出?
【发布时间】:2021-12-04 15:53:23
【问题描述】:

fableARIMA 函数中,我们可以选择查看使用trace = TRUE 选项评估的所有模型。 (下面的示例。)此输出仅打印到控制台。

此模型评估历史记录是否保存在任何地方,或者有什么方法可以保存打印的控制台输出?

library(dplyr)
library(fable)
library(tsibble)
library(tsibbledata)

df <- aus_livestock |> 
    filter(Animal == 'Pigs', State == 'Queensland')

fcst <- df |>
    model(arima = ARIMA(Count, trace = TRUE))

# Prints all models tried, but only saves final selected model:
# Model specification       Selection metric
# ARIMA(2,1,2)(1,0,1)[12]+c Inf
# ARIMA(0,1,0)(0,0,0)[12]+c 21811.280078
# ARIMA(1,1,0)(1,0,0)[12]+c 21524.227259
# ARIMA(0,1,1)(0,0,1)[12]+c 21470.955343
# Search iteration complete: Current best fit is  0 1 1 0 0 1 1 
# ARIMA(0,1,1)(0,0,0)[12]+c 21562.904816
# ARIMA(0,1,0)(0,0,1)[12]+c 21710.467789
# ARIMA(0,1,1)(0,0,1)[12]   21469.103988
# Search iteration complete: Current best fit is  0 1 1 0 0 1 0 
# ...
# ...
# ...

【问题讨论】:

  • 一种选择是使用来自stackoverflow.com/questions/48118977/…capture &lt;- capture.output(fcst &lt;- df |&gt; model(arima = ARIMA(Count, trace = TRUE)))。但是你必须解析文本输出。
  • 为什么不把它写到filecapture.outputcapture.output(fcst &lt;- df |&gt; model(arima = ARIMA(Count, trace = TRUE)), file = file.path(getwd(), 'arimaout.text')) 然后检查输出

标签: r arima fable-r


【解决方案1】:

有一些选项可以读取输出

  1. capture.output 的输出写入file
capture.output(fcst <- df |>
        model(arima = ARIMA(Count, trace = TRUE)), 
       file = file.path(getwd(),  'arimaout.text'))
  1. 也可以使用包 (logger) 将其写入日志文件
library(logger)
log_formatter(formatter_glue)
 t <- tempfile()
 log_appender(appender_file(t))
 log_info('{capture.output(fcst <- df |> model(arima = ARIMA(Count, trace = TRUE)))}') 
 log_appender()

-读取日志文件

readLines(t) |> 
     head()
[1] "INFO [2021-12-04 12:20:58] Model specification\t\tSelection metric"                       
[2] "INFO [2021-12-04 12:20:58] ARIMA(2,1,2)(1,0,1)[12]+c\tInf"                                
[3] "INFO [2021-12-04 12:20:58] ARIMA(0,1,0)(0,0,0)[12]+c\t21811.280078"                       
[4] "INFO [2021-12-04 12:20:58] ARIMA(1,1,0)(1,0,0)[12]+c\t21524.227259"                       
[5] "INFO [2021-12-04 12:20:58] ARIMA(0,1,1)(0,0,1)[12]+c\t21470.955343"                       
[6] "INFO [2021-12-04 12:20:58] Search iteration complete: Current best fit is  0 1 1 0 0 1 1 "

unlink如果是临时文件

unlink(t)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多