How to resolve error in Holtwinters forecast model with R?

809 Views Asked by At

I have simple monthly dataset and simply trying this code:

`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)

The dataset look like this:

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

It is giving following erros:

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

There are 1 best solutions below

0
On

Since I cannot see which data you are using, it is not easy to say what went wrong, but here is an example code which might be helpful. Let's get retail data from Rob Hyndman's website

library(forecast)

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

remove date column and create a mts classed data

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

plot the first time series (first column)

plot(retail[,1])

fit Holt Winters to first time series in the data

fit <- HoltWinters(retail[,1])

use predict function to get forecast as you did

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

plot(fc)

Or you can use forecast function to get a forecast output which is nice.

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

plot(fc)

using AirPassengers data if for some reasons you could not get retail data

fit <- HoltWinters(AirPassengers)

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

plot(fc)

or you can use forecast function to get a forecast output

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

plot(fc)

forecasting loop for many time series

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

I would recommend to take look into fable package.