我在 R 中编写了一个代码,它增加了权重并运行附加的 holt-winters 来预测。但是对于我的一些数据,它给出了错误:
Error in etsmodel(y, errortype[i], trendtype[j], seasontype[k], damped[l], :
Parameters out of range
有人可以告诉我为什么要这样做,以及我将来如何阻止它发生。
这是我的代码:
suppressMessages(library(lmtest))
suppressMessages(library(car))
suppressMessages(library(tseries))
suppressMessages(library(forecast))
suppressMessages(library(TTR))
suppressMessages(library(geoR))
suppressMessages(library(MASS))
#-------------------------------------------------------------------------------
Input.data <- matrix(c("08Q1","08Q2","08Q3","08Q4","09Q1","09Q2","09Q3","09Q4","10Q1","10Q2","10Q3","10Q4","11Q1","11Q2","11Q3","11Q4","12Q1","12Q2","12Q3","12Q4","13Q1","13Q2","13Q3","13Q4","14Q1","14Q2","14Q3",73831.11865,84750.47149,85034.80061,99137.19637,62626.50672,72144.77761,74726.1774,122203.5416,84872.02354,96054.77537,93849.93456,136380.3862,94252.32737,101044.518,112453.256,138807.2089,102091.1436,102568.8303,98839.36528,129249.4421,91207.28917,93060.79801,87776.30512,124342.2055,87128.55797,90261.46195,86371.5614),ncol=2,byrow=FALSE)
Frequency <- 1/4
Forecast.horizon <- 4
Start.date <- c(8, 1)
Data.col <- as.numeric(Input.data[, length(Input.data[1, ])])
Data.col.ts <- ts(Data.col, deltat=Frequency, start = Start.date)
trans<- abs(round(BoxCox.lambda(Data.col, method = "loglik"),5))
categ<-as.character( c(cut(trans,c(0,0.25,0.75,Inf),right=FALSE)) )
Data.new<-switch(categ,
"1"=log(Data.col.ts),
"2"=sqrt(Data.col.ts),
"3"=Data.col.ts
)
mape <- function(percent.error)
mean(abs(percent.error))
#----- Weighting ---------------------------------------------------------------
fweight <- function(x){
PatX <- 0.5+x
return(PatX)
}
integvals <- rep(0, length.out = length(Data.new))
for (i in 1:length(Data.new)){
integi <- integrate(fweight, lower = (i-1)/length(Data.new), upper= i/length(Data.new))
integvals[i] <- 2*integi$value
}
HWAW <- ets(Data.new, model = "AAA", damped = FALSE, opt.crit = "mse", ic="aic", lower = c(0.03, 0.03, 0.03, 0.04),
upper = c(0.997, 0.997, 0.997, 0.997), bounds = "usual", restrict = FALSE)
parASW <- round(HWAW$par[1:3], digits=3)
HWAOPT <- function(parASW)
{
HWAddW <- ets(Data.new, model = "AAA", alpha = parASW[1], beta = parASW[2], gamma = parASW[3], damped = FALSE, opt.crit = "mae", ic="aic",
lower = c(0.001, 0.001, 0.001, 0.0001), upper = c(0.999, 0.999, 0.999, 0.999), bounds = "admissible", restrict = FALSE)
error <- c(resid(HWAddW))
error <- t(error) %*% integvals
percent.error <- 100*(error/c(Data.new))
MAPE <- mape(percent.error)
return(MAPE)
}
OPTHWA <- optim(parASW, HWAOPT, method="L-BFGS-B", lower=c(rep(0.01, 3)), upper=c(rep(0.99, 3)), control = list(fnscale= 1, maxit = 3000))
# Alternatively, set method="Nelder-Mead" or method="L-BFGS-B"
parS4 <- OPTHWA$par
HWAW1 <- ets(Data.new, model = "AAA", alpha = parS4[1], beta = parS4[2], gamma = parS4[3], damped = FALSE, opt.crit = "mae", ic="aic",
lower = c(0, 0, 0, 0), upper = c(0.999, 0.999, 0.999, 0.999), bounds = "admissible", restrict = FALSE)
先感谢您
编辑:
即使删除上限和下限的限制,错误仍然存在
编辑
我从中删除了opt.crit
,ets
这使我的代码运行良好。如果还有其他方法,请告诉我
编辑
虽然这适用于这个数据集,但它仍然给不同的数据集带来了错误。所以我必须做一些其他的事情来让这个代码对所有数据集自动运行