tidyverts 何时识别预测变量转换?

问题描述 投票:0回答:1

我正在尝试在 tidyverts 框架 中完成一个预测项目。其中包括 R 包

tsibble
fable
fabletools
feasts
等。在线书籍预测原理与实践中对此进行了描述。

Q:我如何知道 tidyverts 模型(例如,

ARIMA()
,像
dat %>% model(arima=ARIMA(y)) %>% forecast(h=12)
一样使用)何时会确认并反向转换转换后的目标变量
y

帖子使用寓言进行非高斯预测表明 Box-Cox 变换

fabletools::box_cox(y, lambda)
已被认可并进行反向变换,至少在
fable::ETS()
中用作
dat %>% model(ets=ETS(box_cox(y,lamda)))
时是这样。 复杂季节性部分表明
sqrt(y)
已被确认并进行反向转换,至少在
fable::STL()
内使用时是如此。

Q:更一般地说,我怎样才能简单地看到哪些模型识别哪些转换及其反向转换?它只是记录在某处吗?我在

ARIMA
对象上使用了一个简单的函数来告诉我哪些变换可用吗?也许这是一个简单的 S3 或 R6 的事情?如果是这样,请仍然让我知道,不要遗漏细节,因为我还不知道如何处理 S3 或 R6 对象。

我在下面写了一些代码来演示我是如何感到惊讶的。我的

garima
模型的预测结果是负面的。由于我的
garima
模型使用了
ARIMA(box_cox(y,0.5))
,所以反向变换应该 基本上 是平方函数,不能为负数。所以,这里有些不对劲。也许
ARIMA()
认识
box_cox()
?或者这可能只是因为
inv_box_cox(z,0.5)!=z^2
。无论如何,快速检查
ARIMA()
是否识别
box_cox()
或任何其他转换就好了。

# simulate
set.seed(10)
len=36
aa <- exp(-0.1*(1:len) + rnorm(len,sd=0.1))
ab <- exp(-0.2*(1:len) + rnorm(len,sd=0.1))
tibble(aa=aa,ab=ab) %>%
  mutate(ym=make_yearmonth(year=2021,month=1)+0:(len-1)) %>%
  pivot_longer(cols=c('aa','ab'),names_to='key', values_to='val') %>%
  as_tsibble(index=ym, key=key) ->
  dat

# fit and forecast
dat %>% 
  model(
    garima=ARIMA(box_cox(val,0.5)),
    gets=ETS(box_cox(val,0.5)),
  ) ->
  fit
fit %>% forecast(h=36) -> fcs
fcs %>% autoplot(level=NULL)
fcs %>% filter(.mean<0) %>% distinct(key, .model)
# So, ARIMA doesn't back-transform?
# A tibble: 1 × 2
# key   .model
# <chr> <chr> 
# 1 aa    garima

fit %>% pluck('garima', 1, 'transformation') 
#[[1]]
#Transformation: box_cox(val, 0.5)
#Backtransformation: inv_box_cox(val, 0.5)
## This is nice, it seems ARIMA is recognizing the box_cox transform? 
## But still, what other transforms does it recognize?
## What are my options?

fit %>% pluck('garima', 1, 'model', 'specials') 
#<environment: 0x00000203ba8c0058>
#attr(,"required_specials")
#[1] "pdq" "PDQ"
#attr(,"xreg_specials")
#[1] "trend"   "season"  "fourier"
## This is nice too. It shows me all the available specials, even though I didn't use any.
## Does something like this exist for all the available transformations for use, but may not have been used?
r forecasting fable-r tidyverts fabletools
1个回答
0
投票

应该处理所有转换,甚至是用户定义的转换。有关完整说明,请参阅包插图。 https://fable.tidyverts.org/articles/transformations.html

lambda = 0.5 的 Box-Cox 变换由 z = 2(y^(0.5) - 1) 给出。所以其倒数是 (0.5 z + 1)^2。然而,寓言实际上并没有使用 Box-Cox 变换(尽管函数的名称如此)。由于 Bickel 和 Doksum,它使用的是经过修改的 Box-Cox(请参阅 https://otexts.com/fpp3/transformations.html#mathematical-transformations)。修改后的版本具有逆变换符号(0.5z+1) * (0.5z+1)^2。所以有可能得到负值。

如果要使用平方根,就使用平方根即可。例如,

ARIMA(sqrt(val))

如果您想使用真正的 Box-Cox 变换(而不是修改后的 Bickel-Doksum 版本),您可以按照包 vignette 中的说明编写自己的变换函数。

通过查看预测对象,您始终可以看到预测中的转换已反转。在这种情况下,你会得到


# A fable: 144 x 5 [1M]
# Key:     key, .model [4]
   key   .model       ym                val  .mean
   <chr> <chr>     <mth>             <dist>  <dbl>
 1 aa    garima 2024 Jan  t(N(-1.7, 0.003)) 0.0204
 2 aa    garima 2024 Feb t(N(-1.7, 0.0069)) 0.0178
 3 aa    garima 2024 Mar  t(N(-1.8, 0.012)) 0.0157

val
显示您已转换正态分布。
t()
表示已应用转换。

© www.soinside.com 2019 - 2024. All rights reserved.