R语言svm预测股票走势

时间:2022-05-27 04:49:02
library(e1071)

a<-read.table("gu.txt")

文件样式:

R语言svm预测股票走势

xtrain<-seq(1,4680,by=1)
xtest<-seq(4681,4695,by=1)

y<-as.vector(a[2:4696,5])   

从开始是因为,如果从1开始结果是这样,svm中必须是数值型的向量,要改变类型

//head(ytrain)
//[1] "收"     "21.150" "21.000" "21.600" "21.620" "21.330"

y<-as.numeric(y)   
ytrain<-y[2:4681]
ytest<-y[4682:4696]
m<-svm(xtrain, ytrain)

new<-predict(m, xtest)

s<-0
for(i in 1:15)
{
if(abs(ytest[i]-new[i])<1.35)
s<-s+1
}
s
[1] 14

相对误差0.1141648
1.35/mean(new)
[1] 0.1141648


aa<-0
bb<-0
for(i in 1:14)
{
aa[i]<-ytest[i+1]-ytest[i]
bb[i]<-new[i+1]-new[i]
}




ss<-0
for(j in 1:14)
{
if(aa[j]*bb[j]>0)
{
ss<-ss+1
}
}
ss
[1] 9

趋势正确率是60%