简体   繁体   中英

How to loop the Holt's Double Exponential Smoothing by sampling beta?

I want to forecast like hundreds records with some various alpha and beta in looping. My goal is to loop the holt result by 2 samples of beta (0.1 and 0.9) in RStudio. Here's the code:

library(forecast)
library(tidyverse)
library(magicfor)
magic_for(silent =  TRUE)
    dataset<- c(100,200,300,400,500,600,700,800,900,800,700)
    x<-c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9)

#-----------HOLT FOR BETA 0.1------------------

for (alpha in x) {
  des<-holt(dataset, alpha = alpha, beta=0.1)
  mape<-mean(abs(des$residuals)/des$x)*100
  put(beta=0.1, mape)
}
result01<-magic_result_as_dataframe()

#-----------HOLT FOR BETA 0.9------------------

for (alpha in x) {
  des<-holt(dataset, alpha = alpha, beta=0.9)
  mape<-mean(abs(des$residuals)/des$x)*100
  put(beta=0.9, mape)
}
result02<-magic_result_as_dataframe()

But I got an error in beta=0.9 and here's the notification:

[1] "Model: ETS(A,A,N)" Error in ets(x, "AAN", alpha = alpha, beta = beta, phi = phi, damped = damped, : No model able to be fitted

I've found the problem is about the dataset where if I put 8 records or less, it could forecast with beta=0.1, 0.9, and various sample of alpha and beta well. but if it has more than 8 records, it only can forecast with beta=0.1 and lower (0.1, 0.01, 0.001, 0.0001 only) but it could be error if the beta is upper than 0.1

could you help me to fix this? Thanks in advance

In the parameterization used by holt() , beta must be less than alpha. See https://otexts.com/fpp2/estimation-and-model-selection.html for a discussion of this.

Here is some code to do what you want for a range of alpha and beta values.

library(forecast)
library(tidyverse)

dataset <- c(100, 200, 300, 400, 500, 600, 700, 800, 900, 800, 700)
get_mape <- function(x, alpha, beta) {
  des <- holt(dataset, alpha=alpha, beta=beta)
  mean(abs(des$residuals) / des$x) * 100
}
holt_mape <- expand.grid(
    alpha = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9),
    betastar = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9)
  ) %>%
  mutate(
    beta = betastar * alpha,
    mape = map2_dbl(alpha, beta, get_mape, x=dataset)
  ) %>%
  as_tibble()
holt_mape
#> # A tibble: 81 x 4
#>    alpha betastar   beta  mape
#>    <dbl>    <dbl>  <dbl> <dbl>
#>  1   0.1      0.1 0.01   18.5 
#>  2   0.2      0.1 0.02   17.9 
#>  3   0.3      0.1 0.03   16.7 
#>  4   0.4      0.1 0.04   14.7 
#>  5   0.5      0.1 0.05   13.6 
#>  6   0.6      0.1 0.06   12.5 
#>  7   0.7      0.1 0.0700 11.5 
#>  8   0.8      0.1 0.08   10.6 
#>  9   0.9      0.1 0.09    9.64
#> 10   0.1      0.2 0.02   19.1 
#> # … with 71 more rows

Created on 2020-06-26 by the reprex package (v0.3.0)

However, note that MAPE is computed here on the training data. Normally it would be computed on the test data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM