0

重写原始问题

我想要一个函数,它采用向量和预定义的指数平滑模型(在本例中,alpha = 0.5 的简单指数平滑),并对输入向量进行一步预测。下面的代码虽然笨拙,但可以满足我的要求。有没有更好的方法来做到这一点,或者内置函数/包函数可以做到这一点?

这个问题的第二部分是:如果我用{fable}包来拟合 ETS 或 ARIMA 模型,是否有一个函数可以采用拟合的 ETS/ARIMA 模型并对向量进行相同的单步预测,像第一部分一样?

# Ref: # https://stats.stackexchange.com/questions/44984/how-do-you-use-simple-exponential-smoothing-in-r
# NOT fully tested, just in this case.  Gives one-step-ahead forecasts for an exponential smoothing model
ses <- function(x, alpha, beta = FALSE, gamma = FALSE) {
    ## Populate this vector with return values
    result_vec <- numeric(length(x))
    result_vec[1] <- NA

    for (i in 2:length(x)) {
        mod <- HoltWinters(x[seq(i)], alpha=alpha, beta=beta, gamma=gamma)
        result_vec[i] <- predict(mod, n.ahead = 1)
    }
    result_vec
}

new_wt <- c(1, 0, 0, 0, 0, 1, 1, 1)
ses(new_wt, 0.5)
#> [1]        NA 0.5000000 0.2500000 0.1250000 0.0625000 0.5312500 0.7656250
#> [8] 0.8828125

reprex 包于 2020-10-22 创建(v0.3.0)

4

1 回答 1

0

这是我正在寻找的问题和帖子,它为我解决了这个问题:

https://stats.stackexchange.com/questions/265115/one-step-ahead-forecast https://robjhyndman.com/hyndsight/out-of-sample-one-step-forecasts/

然后我能够利用forecast::ets()模型构建器来构建我想要的过滤器,然后使用重新调用的技术ets()与我想要对(和原始模型)进行一步预测的新数据,然后调用fitted()该新模型来获得领先一步的预测。它与我想要过滤器执行的操作非常接近(输出中存在一些我可以很容易处理的差异)。

它主要与{fable}ETS()调用一起使用。但是,我不知道如何指定起始值以确保我们匹配。但总体上非常接近,并且随着时间的推移,应该大致匹配。

以下是我所拥有的示例代码以及最终的解决方案。

# Ref: # https://stats.stackexchange.com/questions/44984/how-do-you-use-simple-exponential-smoothing-in-r
# Hand-rolled solution
# NOT fully tested, just in this case.  Gives one-step-ahead forecasts for an exponential smoothing model
ses <- function(x, alpha, beta = FALSE, gamma = FALSE) {
    ## Populate this vector with return values
    result_vec <- numeric(length(x))
    result_vec[1] <- NA

    for (i in 2:length(x)) {
        ## One method
        mod <- HoltWinters(x[seq(i)], alpha=alpha, beta=beta, gamma=gamma)
        result_vec[i] <- predict(mod, n.ahead = 1)
        
        ## A similar method
        # result_vec[i] <- forecast::ses(x[seq(i)], alpha=alpha, beta=beta, gamma=gamma, h=1)$mean
    }
    result_vec
}

new_wt <- c(1, 0, 0, 0, 0, 1, 1, 1)
ses(new_wt, 0.5)
#> [1]        NA 0.5000000 0.2500000 0.1250000 0.0625000 0.5312500 0.7656250
#> [8] 0.8828125


## From: https://robjhyndman.com/hyndsight/out-of-sample-one-step-forecasts/
## I can do one-step-ahead forecasts with the forecast package
library(forecast)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
#> 
#> Attaching package: 'forecast'
#> The following object is masked _by_ '.GlobalEnv':
#> 
#>     ses

## Values to do one-step-ahead forecasts on
new_wt <- c(1, 0, 0, 0, 0, 1, 1, 1)

## Fit a model with hand-picked parameters, in this case, and walk the forecast
## ahead a step at a time.
initial_value <- 0
fit <- forecast::ets(initial_value, model="ANN", alpha=0.5, use.initial.values = T)
fit2 <- forecast::ets(new_wt, model=fit, use.initial.values = T)
fitted(fit2) %>% as.vector()
#> [1] 0.0000000 0.5000000 0.2500000 0.1250000 0.0625000 0.0312500 0.5156250
#> [8] 0.7578125



## With fable, I  can't seem to make it work:
library(fable)
#> Loading required package: fabletools
#> 
#> Attaching package: 'fabletools'
#> The following objects are masked from 'package:forecast':
#> 
#>     accuracy, forecast
library(tibble)

new_wt_ts <-
    tibble(value = new_wt, idx = seq(length(new_wt))) %>%
    as_tsibble(index = idx)

myfable <-
    model(new_wt_ts, ets = ETS(value ~ error("A") + trend("N", alpha=0.5) + season("N")))

## This is close to the others, and goes in the right direction, not sure why it doesn't match?
fitted(myfable, new_wt_ts)
#> # A tsibble: 8 x 3 [1]
#> # Key:       .model [1]
#>   .model   idx .fitted
#>   <chr>  <int>   <dbl>
#> 1 ets        1  0.531 
#> 2 ets        2  0.765 
#> 3 ets        3  0.383 
#> 4 ets        4  0.191 
#> 5 ets        5  0.0957
#> 6 ets        6  0.0478
#> 7 ets        7  0.524 
#> 8 ets        8  0.762

reprex 包于 2020-10-22 创建(v0.3.0)

于 2020-10-22T20:45:55.090 回答