[英]How to resolve error in Holtwinters forecast model with R?
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
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让我们从 Rob Hyndman 的网站获取零售数据
library(forecast)
retail <- read.csv("https://robjhyndman.com/data/ausretail.csv",header=FALSE)
remove date column and create a mts classed data删除日期列并创建 mts 分类数据
retail <- stats::ts(retail[,-1], start = c(1982,4), frequency = 12)
plot the first time series (first column) plot 第一个时间序列(第一列)
plot(retail[,1])
fit Holt Winters to first time series in the data将 Holt Winters 拟合到数据中的第一个时间序列
fit <- HoltWinters(retail[,1])
use predict function to get forecast as you did使用 predict function 像您一样获得预测
fc <- predict(fit, n.ahead = 12)
plot(fc)
Or you can use forecast function to get a forecast output which is nice.或者您可以使用预测 function 来获得预测 output,这很好。
fc <- forecast(fit, n.ahead = 12)
plot(fc)
using AirPassengers data if for some reasons you could not get retail data如果由于某些原因您无法获得零售数据,则使用 AirPassengers 数据
fit <- HoltWinters(AirPassengers)
fc <- predict(fit, n.ahead = 12)
plot(fc)
or you can use forecast function to get a forecast output或者您可以使用预测 function 来获得预测 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.我建议看看fable
package。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.