我有每月的时间序列数据,并希望通过检测异常值来进行预测。
这是我的数据集的示例:
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2006 7.55 7.63 7.62 7.50 7.47 7.53 7.55 7.47 7.65 7.72 7.78 7.81
2007 7.71 7.67 7.85 7.82 7.91 7.91 8.00 7.82 7.90 7.93 7.99 7.93
2008 8.46 8.48 9.03 9.43 11.58 12.19 12.23 11.98 12.26 12.31 12.13 11.99
2009 11.51 11.75 11.87 11.91 11.87 11.69 11.66 11.23 11.37 11.71 11.88 11.93
2010 11.99 11.84 12.33 12.55 12.58 12.67 12.57 12.35 12.30 12.67 12.71 12.63
2011 12.60 12.41 12.68 12.48 12.50 12.30 12.39 12.16 12.38 12.36 12.52 12.63
我已经提到使用R进行时间序列分析的过程和方法,以进行一系列不同的预测模型,但是这似乎并不准确。另外,我不确定如何将tsoutliers也纳入其中。
我有对我的tsoutliers的查询和ARIMA模型和方法在另一篇文章在这里为好。
这些是我当前的代码,类似于链接1。
码:
product<-ts(product, start=c(1993,1),frequency=12)
#Modelling product Retail Price
#Training set
product.mod<-window(product,end=c(2012,12))
#Test set
product.test<-window(product,start=c(2013,1))
#Range of time of test set
period<-(end(product.test)[1]-start(product.test)[1])*12 + #No of month * no. of yr
(end(product.test)[2]-start(product.test)[2]+1) #No of months
#Model using different method
#arima, expo smooth, theta, random walk, structural time series
models<-list(
#arima
product.arima<-forecast(auto.arima(product.mod),h=period),
#exp smoothing
product.ets<-forecast(ets(product.mod),h=period),
#theta
product.tht<-thetaf(product.mod,h=period),
#random walk
product.rwf<-rwf(product.mod,h=period),
#Structts
product.struc<-forecast(StructTS(product.mod),h=period)
)
##Compare the training set forecast with test set
par(mfrow=c(2, 3))
for (f in models){
plot(f)
lines(product.test,col='red')
}
##To see its accuracy on its Test set,
#as training set would be "accurate" in the first place
acc.test<-lapply(models, function(f){
accuracy(f, product.test)[2,]
})
acc.test <- Reduce(rbind, acc.test)
row.names(acc.test)<-c("arima","expsmooth","theta","randomwalk","struc")
acc.test <- acc.test[order(acc.test[,'MASE']),]
##Look at training set to see if there are overfitting of the forecasting
##on training set
acc.train<-lapply(models, function(f){
accuracy(f, product.test)[1,]
})
acc.train <- Reduce(rbind, acc.train)
row.names(acc.train)<-c("arima","expsmooth","theta","randomwalk","struc")
acc.train <- acc.train[order(acc.train[,'MASE']),]
##Note that we look at MAE, MAPE or MASE value. The lower the better the fit.
通过比较红色的“测试集”和蓝色的“预测”集,这是我不同的预测的图,这看起来不太可靠/不准确。 不同预测的情节
各个测试模型和训练集的准确性不同
Test set
ME RMSE MAE MPE MAPE MASE ACF1 Theil's U
theta -0.07408833 0.2277015 0.1881167 -0.6037191 1.460549 0.2944165 0.1956893 0.8322151
expsmooth -0.12237967 0.2681452 0.2268248 -0.9823104 1.765287 0.3549976 0.3432275 0.9847223
randomwalk 0.11965517 0.2916008 0.2362069 0.8823040 1.807434 0.3696813 0.4529428 1.0626775
arima -0.32556886 0.3943527 0.3255689 -2.5326397 2.532640 0.5095394 0.2076844 1.4452932
struc -0.39735804 0.4573140 0.3973580 -3.0794740 3.079474 0.6218948 0.3841505 1.6767075
Training set
ME RMSE MAE MPE MAPE MASE ACF1 Theil's U
theta 2.934494e-02 0.2101747 0.1046614 0.30793753 1.143115 0.1638029 0.2191889194 NA
randomwalk 2.953975e-02 0.2106058 0.1050209 0.31049479 1.146559 0.1643655 0.2190857676 NA
expsmooth 1.277048e-02 0.2037005 0.1078265 0.14375355 1.176651 0.1687565 -0.0007393747 NA
arima 4.001011e-05 0.2006623 0.1079862 -0.03405395 1.192417 0.1690063 -0.0091275716 NA
struc 5.011615e-03 1.0068396 0.5520857 0.18206018 5.989414 0.8640550 0.1499843508 NA
从模型的准确性中,我们可以看到最准确的模型是theta模型。我不确定该预测为什么会非常不准确,我认为原因之一是我没有处理数据集中的“异常值”,从而导致所有模型的预测均不正确。
这是我的异常值
tsoutliers输出
ARIMA(0,1,0)(0,0,1)[12]
Coefficients:
sma1 LS46 LS51 LS61 TC133 LS181 AO183 AO184 LS185 TC186 TC193 TC200
0.1700 0.4316 0.6166 0.5793 -0.5127 0.5422 0.5138 0.9264 3.0762 0.5688 -0.4775 -0.4386
s.e. 0.0768 0.1109 0.1105 0.1106 0.1021 0.1120 0.1119 0.1567 0.1918 0.1037 0.1033 0.1040
LS207 AO237 TC248 AO260 AO266
0.4228 -0.3815 -0.4082 -0.4830 -0.5183
s.e. 0.1129 0.0782 0.1030 0.0801 0.0805
sigma^2 estimated as 0.01258: log likelihood=205.91
AIC=-375.83 AICc=-373.08 BIC=-311.19
Outliers:
type ind time coefhat tstat
1 LS 46 1996:10 0.4316 3.891
2 LS 51 1997:03 0.6166 5.579
3 LS 61 1998:01 0.5793 5.236
4 TC 133 2004:01 -0.5127 -5.019
5 LS 181 2008:01 0.5422 4.841
6 AO 183 2008:03 0.5138 4.592
7 AO 184 2008:04 0.9264 5.911
8 LS 185 2008:05 3.0762 16.038
9 TC 186 2008:06 0.5688 5.483
10 TC 193 2009:01 -0.4775 -4.624
11 TC 200 2009:08 -0.4386 -4.217
12 LS 207 2010:03 0.4228 3.746
13 AO 237 2012:09 -0.3815 -4.877
14 TC 248 2013:08 -0.4082 -3.965
15 AO 260 2014:08 -0.4830 -6.027
16 AO 266 2015:02 -0.5183 -6.442
我想知道如何通过这些相关数据集和离群值的检测来进一步“分析” /预测我的数据。请也帮助我处理离群值以及进行预测。
最后,我想知道如何将不同模型的预测组合在一起,就像@forecaster在链接1中提到的那样,组合不同模型很可能会带来更好的预测/预测。
已编辑
我想将异常值纳入其他模型中也很好。
我尝试了一些代码,例如。
forecast.ets( res$fit ,h=period,xreg=newxreg)
Error in if (object$components[1] == "A" & is.element(object$components[2], : argument is of length zero
forecast.StructTS(res$fit,h=period,xreg=newxreg)
Error in predict.Arima(object, n.ahead = h) : 'xreg' and 'newxreg' have different numbers of columns
产生了一些错误,我不确定将异常值作为回归变量的正确代码。此外,由于没有Forecast.theta或Forecast.rwf,我如何使用thetaf或rwf?