我有一个关于时间序列和SVM的问题.我已经问过强大的互联网,但不幸的是信息很少,而且主要关注的是交易数据.
我的情况如下:
目前,我尝试从Arima预测切换到更复杂的模型.目前,我尝试理解并实现SVM模型.我找到了一些关于美国市场亚洲汽车月销量的数据.现在我试验这些数据.
首先,我使用SVR / SVM和两个不同的例程来进行时间序列预测.
接下来,我对相同的数据实现了绝对简单的auto.arima.
最后,我比较了这3种方法的残差.
我的问题是:
我是否正朝着正确的方向前进?
如何改进SVM模型?
除了财务数据之外,还有更多关于预测的信息吗?
让我们从一个小的解决方法开始构建我的输入矩阵(来自goodcarbadcar.net的数据):
library(zoo)
library(e1071)
library(quantmod)
library(kernlab)
library(caret)
library(forecast)
Date <-c( "2010-01-01", "2010-02-01", "2010-03-01", "2010-04-01", "2010-05-01", "2010-06-01", "2010-07-01", "2010-08-01", "2010-09-01",
"2010-10-01", "2010-11-01", "2010-12-01", "2011-01-01", "2011-02-01", "2011-03-01", "2011-04-01", "2011-05-01", "2011-06-01",
"2011-07-01", "2011-08-01", "2011-09-01", "2011-10-01", "2011-11-01", "2011-12-01", "2012-01-01", "2012-02-01", "2012-03-01",
"2012-04-01", "2012-05-01", "2012-06-01", "2012-07-01", "2012-08-01", "2012-09-01", "2012-10-01", "2012-11-01", "2012-12-01",
"2013-01-01", "2013-02-01", "2013-03-01", "2013-04-01", "2013-05-01", "2013-06-01", "2013-07-01", "2013-08-01", "2013-09-01",
"2013-10-01", "2013-11-01", "2013-12-01", "2014-01-01", "2014-02-01", "2014-03-01", "2014-04-01", "2014-05-01", "2014-06-01",
"2014-07-01", "2014-08-01", "2014-09-01", "2014-10-01", "2014-11-01", "2014-12-01", "2015-01-01", "2015-02-01", "2015-03-01",
"2015-04-01", "2015-05-01", "2015-06-01", "2015-07-01", "2015-08-01", "2015-09-01", "2015-10-01", "2015-11-01", "2015-12-01",
"2016-01-01", "2016-02-01", "2016-03-01")
Nissan <- c( 55861, 63148, 85526, 56558, 75673, 56266, 72573, 67399, 65900, 61843, 63184, 81228, 64442, 83226, 109854, 64765, 69759,
65659, 77191, 82517, 84485, 75484, 76754, 89937, 72517, 97492, 126132, 64200, 81202, 81801, 86722, 87360, 82462, 70928,
84300, 86663, 73793, 90489, 126623, 80003, 106558, 95010, 101279, 108614, 77828, 81866, 93376, 96526, 81472, 105631, 136642,
94764, 125558, 101069, 112914, 125224, 95118, 94072, 91790, 105311, 94449, 106777, 132560, 99869, 124305, 114243, 120439,
122716, 111562, 104904, 95389, 124207, 97220, 120540, 149784,)
Mitsubishi <- c( 4170, 4019, 5434, 3932, 4737, 4198, 5648, 4293, 4961, 5111, 4306, 4874, 5714, 6893, 7560, 8081, 7568, 8299, 7972,
7985, 5803, 4378, 3735, 5032, 4711, 4736, 7160, 5280, 5575, 5411, 4194, 4249, 4806, 3981, 3574, 4113, 4659, 6051,
5286, 4461, 4715, 5297, 5230, 5281, 4001, 4752, 6071, 6423, 4867, 5977, 8996, 6542, 7269, 6021, 6349, 6786, 5558,
6199, 6534, 6545, 1112, 1184, 1715, 1933, 1996, 1982, 2052, 2320, 2066, 1984, 1637, 1403, 1288, 1547, 2123)
mydata <- data.frame(Date, Nissan, Mitsubishi)
mydata$Date <- as.Date(mydata$Date, format = "%Y-%m-%d")
mydata <- xts(mydata[,-1], order.by = mydata[,1])
因此,我有与.csv导入相同的输入.
下一步是定义一个data.frame,它将作为进一步分析的基础.
让我们假设日产在时间t的销售额取决于时间t-1,t-2和t-3的销售额.此外,假设日产在时间t的销售也取决于三菱在t-3
现在到第一个方法.我在这里使用Time Slices
####################
#Use SVR technique#
####################
Nissan <- data$Nissan
Mitsubishi <- data$Mitsubishi
#Assume dependency on Nissan Lag1 + Lag2 and Mitsubishi Lag1
feature = merge(lag(Nissan,1),lag(Nissan,2), lag(Nissan,3),
lag(Mitsubishi,3),
all=FALSE)
colnames(feature) = c("n.lag.1", "n.lag.2", "n.lag.3",
"m.lag.3")
#TARGET to predict: Nissan
dataset = na.trim(merge(feature,Nissan,all=FALSE))
#Label columns of dataset
colnames(dataset) = c("n.lag.1", "n.lag.2", "n.lag.3",
"m.lag.3",
"TARGET")
#################
#Use Time Slices#
#################
myTimeControl <- trainControl(method = "timeslice",
initialWindow = 48,
horizon = 6,
fixedWindow = TRUE)
TimeModel <- train(TARGET ~ .,
data = dataset,
method = "pls",
preProc = c("center", "scale"),
trControl = myTimeControl)
TimeModel
####################################
#Predict with control data set 2016#
####################################
#Define the test set
control.feature <- merge(lag(mydata$Nissan["2010/2016"],1), lag(mydata$Nissan["2010/2016"],2), lag(mydata$Nissan["2010/2016"],3),
lag(mydata$Mitsubishi["2010/2016"],3),
all = FALSE)
colnames(control.feature) = c("n.lag.1", "n.lag.2", "n.lag.3",
"m.lag.3")
#Make a prediction
svr.fc <- predict(TimeModel, control.feature["2016"])
#Show SVR Residuals
现在我想依靠e1071包显示我的第二种方法
####################
#Use Package e1071#
####################
#initialize svm model
nissan.model <- svm(TARGET ~ ., dataset)
#test model on the existing set
nissanY <- predict(nissan.model, dataset)
plot(index(dataset),dataset[,ncol(dataset)], pch=16)
points(index(dataset),nissanY, col="red", pch=4)
#predict 2016 values and compare with actuals
predictY <- predict(nissan.model, control.feature["2016"])
mydata$Nissan["2016"] - predictY
#tune the existing model with grid search
nissan.tuneResult <- tune(svm, TARGET ~ ., data = dataset,
ranges = list(epsilon = seq(0,1, 0.01), cost=2^(2:9)))
print(nissan.tuneResult)
plot(nissan.tuneResult)
#initialize tuned model
tuned.nissan.model <- nissan.tuneResult$best.model
tuned.nissanY <- predict(tuned.nissan.model, dataset)
plot(index(dataset),dataset[,ncol(dataset)], pch=16)
points(index(dataset),tuned.nissanY, col="red", pch=4)
#compare 2016 forecast and actual values
tuned.predictY <- predict(tuned.nissan.model, control.feature["2016"])
最后但并非最不重要的是我展示了我的香草auto.arima.
############################
#Use time series techniques#
############################
#Define time series which should be forecasted
nissan.ts <- ts(data$Nissan, frequency = 12, start = c(2010,1))
#Assume that Nissan depends on Mitsubishi
xreg <- data.frame(data$Mitsubishi)
#auto.arima on Nissan with XREG = Mitsubishi
arima.fit <- auto.arima(nissan.ts, D = 1, xreg = xreg)
arima.fc <- forecast(arima.fit, xreg = xreg)
#How did the ARIMA forecast perform in the first quarter of 2016
comparison.arima <- actual$Nissan - arima.fc$mean[1:3]
rmse.arima <- sqrt(sum((actual$Nissan - arima.fc$mean[1:3])^2/3))
最后比较了这三种方法的残差
#Print the details ARIMA
comparison.arima
#Print details e1071
mydata$Nissan["2016"] - tuned.predictY
#Print details Time Slices
mydata$Nissan["2016"] - svr.fc
我自己的结论是:
> auto.arima使用(2,1,0)模型.我在SVM模型中有3个滞后.从而
区别
> auto.arima不使用滞后(三菱,3)但只是
XREG(三菱)
>两个SVM模型只使用一些假设而无需进一步说明
>最终数据量不高
足够用于SVM
如果你能给我一些关于我的问题的提示或者更多地讨论这些模型,我会很高兴的.最好的祝福
亚历克斯
最佳答案 Alex,你为什么认为三菱对Y有影响.当你强迫不相关的数据在一起时会发生坏事.我在标准化散点图中绘制了Y和X,X向下移动了3个周期(即丢失了前3个观察点).这看起来并不相关,这可能是您问题的一部分.
三菱数据在第61期大幅减少,日产未受影响.这支持没有关系.你可能想先尝试很多真正好的因果例子(Lydia Pinkham或Gas-Furnhane with Air-amp; Methane from Box-Jenkins)