install.packages("zoo")
install.packages("forecast")
install.packages("tseries")
install.packages("e1071")
library("e1071")
library("tseries")
library("zoo")
library("forecast")
dt = read.csv("C://Users//yelang//Desktop//matlabyewu//股票预测//数据.csv") #读入数据
airline2 <- dt[1:55,2]
airts <- ts(airline2,start=c(2011,2),frequency=12)
plot.ts(airts)
airdiff <- diff(diff(airts, differences=1))
plot.ts(airdiff)
adf.test(airdiff, alternative="stationary", k=0) #检验序列平稳性
acf(airdiff, lag.max=20)
acf(airdiff, lag.max=20,plot=FALSE)
pacf(airdiff, lag.max=20)
pacf(airdiff, lag.max=20,plot=FALSE)
auto.arima(airdiff,trace=T)
airarima1 <- arima(airline2,order=c(3,1,0),seasonal=list(order=c(3,1,0),period=12),method="ML")
airarima1
airarima2 <- arima(airline2,order=c(3,1,0),seasonal=list(order=c(1,1,0),period=12),method="ML")
airarima2
airarima3 <- arima(airline2,order=c(1,1,0),seasonal=list(order=c(1,1,0),period=12),method="ML")
airarima3
airforecast <- forecast.Arima(airarima3,h=16,level=c(99.5))
airforecast
plot.forecast(airforecast) #画预测图
ycforecast <- forecast.Arima(airarima1,h=16) #预测后面16个
write.csv(tq[1],file="C://Users//yelang//Desktop//matlabyewu//股票预测//yc1.csv")#写出数据
sub1=dt[56:68,2]-tq[1]
sub1=as.matrix(sub1) #转换成矩阵
#4个数据循环
xindt=matrix(NA,4,length(sub1)-6)
for (i in 1: 10){
xindt[,i]=c(sub1[i],sub1[i+1],sub1[i+2],sub1[i+3])
}
inputData<-cbind(t(xindt[,1:9]),sub1[5:13])
set.seed(100) # for reproducing results
rowIndices <- 1 : nrow(inputData) # prepare row indices
sampleSize <- 0.8 * length(rowIndices) # training sample size
trainingRows <- sample (rowIndices, sampleSize) # random sampling
trainingData <- inputData[trainingRows, ] # training data
testData <- inputData[-trainingRows, ] # test data
#tuned <- tune.svm(V5 ~., data = trainingData, type="eps-regression" , gamma = 10^-1, cost = 10^(1:2)) # tune
#summary (tuned) # to select best gamma and cost
svmfit <- svm (V5 ~ ., data = trainingData, type="eps-regression" , kernel = "radial", cost = 1000, gamma=0.0001, scale = FALSE) # radial svm, scaling turned OFF
print(svmfit)
predict(svmfit, trainingData[,1:4])
compareTable <- cbind(testData[,5], predict(svmfit, testData[,1:4])) # comparison table
View(compareTable)
yc1=predict(svmfit, t(xindt))
write.csv(yc1,file="C://Users//yelang//Desktop//matlabyewu//股票预测//yc2.csv")#写出数据
#预测后面第1个
h1=sub1[10:13,1]
h1=as.matrix(h1)
row.names(h1)<-c("V1","V2","V3","V4")
th1=predict(svmfit, t(h1))
#预测后面第二个
h2=c(sub1[11:13,1],th1)
h2=as.matrix(h2)
row.names(h2)<-c("V1","V2","V3","V4")
th2=predict(svmfit, t(h2))
#预测后面第三个数
h3=c(sub1[12:13,1],th1,th2)
h3=as.matrix(h3)
row.names(h3)<-c("V1","V2","V3","V4")
th3=predict(svmfit, t(h3))
# http://www.cnblogs.com/xuancaoyy/p/5535909.html c参考网址