簡體   English   中英

如何解決 Holtwinters 預測 model 和 R 中的錯誤?

[英]How to resolve error in Holtwinters forecast model with R?

我有簡單的每月數據集,只需嘗試以下代碼:

`df2.holtwinters <- subset(df, account_id==loopitem) 
  x.holtwinters <- ts(df2.holtwinters$amount_usd, start = c(2015,1), end = c(2019,5), frequency = 12)
  arima1.holtwinters <- HoltWinters(x.holtwinters)
  forecast1.holtwinters <- predict(arima1.holtwinters, n.ahead=1*1)

數據集如下所示:

`      id     <date>         <dbl>
1     123  2015-01-01       -390
2     123  2015-02-01        944
3     999  2015-01-01        672

它給出以下錯誤:

`In HoltWinters(x.holtwinters) :
  optimization difficulties: ERROR: ABNORMAL_TERMINATION_IN_LNSRCH

由於我看不到您使用的是哪些數據,因此很難說出哪里出了問題,但這里有一個可能有用的示例代碼。 讓我們從 Rob Hyndman 的網站獲取零售數據

library(forecast)

retail <- read.csv("https://robjhyndman.com/data/ausretail.csv",header=FALSE)

刪除日期列並創建 mts 分類數據

retail <- stats::ts(retail[,-1], start = c(1982,4), frequency = 12)

plot 第一個時間序列(第一列)

plot(retail[,1])

將 Holt Winters 擬合到數據中的第一個時間序列

fit <- HoltWinters(retail[,1])

使用 predict function 像您一樣獲得預測

fc <- predict(fit, n.ahead = 12)

plot(fc)

或者您可以使用預測 function 來獲得預測 output,這很好。

fc <- forecast(fit, n.ahead = 12)

plot(fc)

如果由於某些原因您無法獲得零售數據,則使用 AirPassengers 數據

fit <- HoltWinters(AirPassengers)

fc <- predict(fit, n.ahead = 12)

plot(fc)

或者您可以使用預測 function 來獲得預測 output

fc <- forecast(fit, n.ahead = 12)

plot(fc)

許多時間序列的預測循環

nts <- ncol(retail) # number of time series

h = 12 # forecast horizon

fc <- matrix(nrow = h, ncol = nts)

for (i in 1:nts) {

  fc[,i] <- forecast(HoltWinters(retail[,i]), h = h)$mean # it will return point forecast
  
}

colnames(fc) <- colnames(retail)

fc

我建議看看fable package。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM