In [1]:
#准备工作环境,设置路径

In [2]:
getwd()


Out[2]:
'/Users/zhoudan'

In [3]:
setwd('/Users/zhoudan/Neural Network')

In [4]:
A_index<-data.frame(read.csv("A_index.csv",header=T))

In [5]:
head(A_index)


Out[5]:
Xdateopenhighcloselowvolumeindex
112012-01-042317.2652323.0822272.6712271.8894906726000623
222012-01-052263.7522287.4612250.8462247.8115852715200622
332012-01-062250.5592267.5112266.5272234.2975037613200621
442012-01-092267.922332.2462331.8932250.8197640185600620
552012-01-102327.6172397.6692394.6022323.87410914484800619
662012-01-112391.6222399.7672384.4412373.1028411556800618

In [6]:
A_index$X<-NULL

In [7]:
A_index$index<-NULL

In [8]:
head(A_index)


Out[8]:
dateopenhighcloselowvolume
12012-01-042317.2652323.0822272.6712271.8894906726000
22012-01-052263.7522287.4612250.8462247.8115852715200
32012-01-062250.5592267.5112266.5272234.2975037613200
42012-01-092267.922332.2462331.8932250.8197640185600
52012-01-102327.6172397.6692394.6022323.87410914484800
62012-01-112391.6222399.7672384.4412373.1028411556800

In [9]:
dates<-as.Date(A_index[,1])

In [10]:
A_index$date<-NULL

In [11]:
head(A_index)


Out[11]:
openhighcloselowvolume
12317.2652323.0822272.6712271.8894906726000
22263.7522287.4612250.8462247.8115852715200
32250.5592267.5112266.5272234.2975037613200
42267.922332.2462331.8932250.8197640185600
52327.6172397.6692394.6022323.87410914484800
62391.6222399.7672384.4412373.1028411556800

In [12]:
nrow(A_index)


Out[12]:
623

In [13]:
#数据分成训练集和测试集两个部分,并且进行数据标准化

In [14]:
train<-A_index[c(0:500),]

In [15]:
test<-A_index[c(501:623),]

In [16]:
scaled<-as.data.frame(scale(A_index))

In [17]:
train_<-scaled[c(0:500),]

In [18]:
test_<-scaled[c(501:623),]

In [19]:
head(train_)


Out[19]:
openhighcloselowvolume
10.33841130.2620399-0.0069163110.1140257-1.436619
2-0.05891628-0.001623227-0.1693022-0.06641021-1.140394
3-0.1568727-0.1492912-0.05262989-0.1676815-1.395633
4-0.027969380.3298710.4337168-0.04386884-0.580669
50.41527370.81412560.90029440.50359130.4446388
60.89050310.82965480.82469290.8724965-0.3391233

In [20]:
head(test_)


Out[20]:
openhighcloselowvolume
501-0.9986313-1.041272-0.9550341-0.9143496-0.6667944
502-0.9697931-1.085173-1.08617-0.979733-1.0199
503-1.153314-1.094322-0.9970349-1.111287-0.678129
504-0.9379626-0.7582605-0.6724269-0.83415830.9052118
505-0.6559731-0.5784609-0.5343858-0.57846961.465564
506-0.5178555-0.5794824-0.485205-0.46748630.9697979

In [21]:
#调入neuralnet包

In [22]:
library(neuralnet)


Loading required package: grid
Loading required package: MASS

In [23]:
n<-names(train_)

In [24]:
f <- as.formula(paste("close ~", paste(n[!n %in% "close"], collapse = " + ")))

In [25]:
nn<-neuralnet(f,data=train_,hidden=3,rep=10,linear.output=FALSE)

In [26]:
nn


Out[26]:
Call: neuralnet(formula = f, data = train_, hidden = 3, rep = 10, linear.output = FALSE)

10 repetitions were calculated.

         Error Reached Threshold Steps
6  103.8621395    0.009798719338  1395
3  103.8669148    0.009529707445  1491
2  103.8767040    0.009052110366   614
4  103.8845496    0.008858636744  1073
1  103.8867198    0.009363340683   592
5  103.8932209    0.009000414092   360
8  103.8974423    0.009971031803   861
10 103.9061371    0.009862469403  1272
9  103.9138978    0.009959824298   938
7  103.9235842    0.009970904557   920

In [27]:
par(mfrow=c(2,1))

In [28]:
plot.nn(nn,rep='best')



In [29]:
ls(nn)


Out[29]:
  1. 'act.fct'
  2. 'call'
  3. 'covariate'
  4. 'data'
  5. 'err.fct'
  6. 'generalized.weights'
  7. 'linear.output'
  8. 'model.list'
  9. 'net.result'
  10. 'response'
  11. 'result.matrix'
  12. 'startweights'
  13. 'weights'

In [30]:
nn$result.matrix


Out[30]:
12345678910
error103.8867198103.8767040103.8669148103.8845496103.8932209103.8621395103.9235842103.8974423103.9138978103.9061371
reached.threshold0.0093633406830.0090521103660.0095297074450.0088586367440.0090004140920.0097987193380.0099709045570.0099710318030.0099598242980.009862469403
steps 592 61414911073 3601395 920 861 9381272
Intercept.to.1layhid1-1.69761850218 7.94609834663-2.19339138500-2.59707694293-1.37816035823 8.90594847345-0.05755141287-1.78821307201 5.20985121563 5.82591012830
open.to.1layhid1 17.583131305 4.683449555 19.915932672 -1.722725610 3.682266482 6.989949903-17.583733959 26.232853160 2.598657659 1.707973811
high.to.1layhid1-16.227664507 -3.196241143-17.832404925 2.303917301 -4.969902613 -8.025058918 16.541925201-23.276634368 -4.652671127 -1.684725645
low.to.1layhid1-21.4617588652 -8.8515119423-24.8122217860 0.6968969244 -6.3360978289 -7.5027809269 21.6155925599-30.3170968960 -2.7869557396 -4.6876690526
volume.to.1layhid1-0.16951587697-0.11682786444-0.20179668062-0.03051839343-0.15793731568-0.02890981941 0.19636753895-0.06007886479 0.03110639258-0.04696936063
Intercept.to.1layhid2-2.036362459-1.68591183911.097968396-1.646779617-2.205766089-1.914114147 2.930743302 2.165782299-1.625376489-1.329258600
open.to.1layhid2 -1.721493474 12.205126887 2.402626291 16.844302993 -3.053840541 16.086288141 2.286509743 1.610662172 10.237048889-32.338433141
high.to.1layhid2 2.315146275-11.279671713 -2.282100375-15.517838182 2.558473720-13.568256308 -2.264832916 -1.155958206-13.773351878 26.914012457
low.to.1layhid2 0.9516900863-15.6324170104-10.0886556712-20.5726092388 0.8966736609-20.6095452028 -2.8456206948 -1.9314681822-13.8170454790 43.8515535206
volume.to.1layhid2 0.00809074319-0.19723170028-0.24062430268-0.18238185863-0.09633252872-0.15705365544-0.09080585749-0.11007256489-0.41765289756 0.22137456748
Intercept.to.1layhid3-3.8607712480-2.0112504074-1.8013899825-3.6961584467-4.9867762551 0.6079590065 3.7318986503-3.4317047698-0.1351187651-1.6014167877
open.to.1layhid3-1.961545622-1.440895546-1.673788228-1.798549864-1.447882201 2.043860892 2.560990375-1.692734607 2.874971213-1.447344278
high.to.1layhid3 2.027661525 2.044065153 1.943586177 2.059381032 2.417644937-3.185322613-3.489540798 2.421038006-1.912491503 1.742945872
low.to.1layhid3 2.667148544 1.203246246 1.592941486 2.251347289 3.035674980-1.950168329-2.517405422 1.597900096-3.254660833 1.192712578
volume.to.1layhid3 0.0015562748862-0.0026053572487 0.0002293357626 0.0153494919084 0.0552749671845-0.0147301711947 0.0738440860254-0.0590297258053-0.0285081181970-0.0010160751380
Intercept.to.close-2.517939530 1.699163784 2.180445103-2.370187295-1.490058603 8.559550259 1.422659981 2.495964718 8.957374021-1.692783711
1layhid.1.to.close-30.363580712 -4.518801235-46.350768715 4.921756775-17.320952349 -7.207842316 8.663877690-37.715147681 -8.464762980 -8.134557988
1layhid.2.to.close 5.165624150-23.436384111 -5.547068907-28.826617442 6.666164034-25.473077271 -4.705411037 -5.324456928-25.859916051 6.099405261
1layhid.3.to.close16.89688561410.84748187211.14483885520.82631792415.091430428-5.169634412-7.17730708018.247670762-4.90454275011.407954355

In [31]:
nn$net.result[[1]]


Out[31]:
10.0113894953791424
20.0000980643650960887
30.000000000000780546175479125
40.309181979545015
50.902761577272782
60.870097637380536
70.950524557570007
80.495102822972547
90.356362792178865
100.910639722927125
110.866264776064548
120.968241043991596
130.994011179856257
140.947095307243932
150.943642849450324
160.91717912988762
170.983742028468541
180.998342591201809
190.998026319675548
200.924748048896727
210.999453376742579
220.999873182127215
230.999962802364632
240.999902785802704
250.999397444149298
260.999967239246804
270.99988382066342
280.999873087557832
290.999975959882312
300.999952065569294
4710.00000000000566038309890904
4720.00000000000144061064382376
4730.0000000000000126875961992262
4740.00000000000000714612575555598
4750.00000000000000680398194354019
4760.00000000000000735095584723684
4770.00000000000000735029256730368
4780.00000000000000663446945345699
4790.00000000000000772201457252841
4800.00000000000000726828989768875
4810.00000000000000830095042678633
4820.00000000000000737220909891984
4830.00000000000000673322089482767
4840.00000000000000610822483212092
4850.00000000000000619520192066893
4860.00000000000000628717436354434
4870.00000000000000615539803403282
4880.00000000000000583507493946449
4890.00000000000000584027106916194
4900.00000000000000591779168581069
4910.00000000000000582003199311539
4920.00000000000000594731761806429
4930.00000000000000573319216335164
4940.00000000000000566371824938691
4950.00000000000000584249293742415
4960.00000000000000635930645596984
4970.00000000000000610753014998517
4980.00000000000000637339217687909
4990.00000000000000596725450199856
5000.00000000000000606369038690613

In [32]:
nn$covariate


Out[32]:
0.3384113015445410.26203994247854 0.114025698385096-1.43661923021568
-0.0589162759352383 -0.00162322652561283-0.0664102107293942 -1.14039399235431
-0.15687271137505 -0.14929118142954 -0.167681532092183-1.39563346425449
-0.02796937811721710.32987097700183 -0.0438688362540909-0.580668950662351
0.4152736542825820.8141256443541950.5035913074604640.444638800577592
0.890503067632481 0.829654835802237 0.872496487869473 -0.339123257329483
0.780354980029234 0.864917499319146 0.87270631449358 -0.741413246434682
0.845820142711932 0.759521809600998 0.563144599666331 -0.743895053568626
0.4830182549763460.4469764764772710.408300044884675-1.54280122035511
0.297039872887631 0.890009948799061 0.329952282205384 -0.235444993989304
1.01504057173557 0.992496690834138 0.814929054784604 -0.165799829284631
0.759520768110484 0.946738129570176 0.826244704870277 -0.711262809429234
1.02807123528418 1.0806015363089 1.09804512116659 -0.792004836010591
1.21419811496319 1.09205968138114 1.02275483215283 -1.15069999270664
0.9148715222003190.87478423535358 0.965929286060333-1.45249708423442
0.9312285317715990.9481074815329430.858078401270242-1.30474889539641
0.820152334228993 1.00009844470313 0.899916331355225 -0.992840324889022
1.07589488423952 1.18413194659668 1.15379155890655 -0.509639465151969
1.29082287155676 1.22519770167473 1.28274500703522 -0.610294568252677
1.17469033106452 1.05516119741142 0.979215807650994 -0.872082508850229
0.959695519831648 1.29803982419149 1.04176662305836 -0.253948698656876
1.36745505302984 1.39561170296561 1.41181086228395 0.0857970952620493
1.35036298038658 1.43703274936373 1.45839237283531 0.0358573339048257
1.27232007180568 1.40874267800318 1.34163884413675 -0.27928081746163
1.40339146988186 1.30870596569609 1.39828453883716 -0.689534534111805
1.3377481100908 1.47765439029168 1.42064606192037 0.110870687271382
1.51985812995227 1.47363515723339 1.47496867613962 -0.00813255350081768
1.54127148247318 1.46387204783398 1.49589138808611 -0.730602718014552
1.67368678374155 1.58356821318493 1.63122206682598 -0.199069873056911
1.53181961085074 1.53789847475099 1.49774235866304 -0.496948605213985
-0.0673657888231713-0.154672364548093 -0.075710026462058 -0.518857498573563
-0.154400226496598-0.224287257574232-0.104501236812498-0.968792072195247
-0.128984863917161-0.204761038775408-0.233686992989286-0.692452980978712
-0.325966917446932-0.417484314764581-0.575329699880623-0.264793296499658
-0.629050499244336-0.667912882096138-0.687676869472964-0.847064780673627
-0.593425927331943-0.57302049202755 -0.599542193540764-0.783863246038533
-0.586802934804668-0.609075159112114-0.532899758963343-0.942217672015988
-0.527827116817977-0.644574683509067-0.672966524361589-0.521401682812216
-0.746601191723921-0.585018975982402-0.680460332365347-0.577865738607356
-0.479721322441067-0.572413536022429-0.480540522441087-0.659006327331654
-0.622078537379865-0.508128012797336-0.540903145911358-0.460117304732696
-0.454684628716892-0.563886544340762-0.433914049041702-0.833753656835003
-0.537368086994597-0.648934404082421-0.63107613762058 -0.336471353592738
-0.714778158123144-0.830947186593079-0.959087607753081-0.179631263350085
-1.05977518478205 -1.0352396956331 -0.99637679637978 -0.996077170551452
-0.958277081813638-0.952145938151821-0.934777694588889-0.735993202107437
-1.00147503083121 -0.997193916775647-1.01912799747919 -0.602325329477566
-1.14388421992661-1.21395863153061-1.16374350433572-0.61258398335282
-1.20995822272953 -1.22993933903124 -1.22321436465354 -0.896921122047445
-1.27079768547446 -1.2281702843334 -1.21766145292276 -0.774725626474816
-1.13835268468802 -1.22848116423846 -1.14679251063122 -0.865427197441377
-1.15164321901966 -1.17180479678485 -1.11387970587871 -0.700360594180496
-1.19083915797871 -1.30268523681549 -1.21667227026626 -0.869958430654134
-1.31260718202274-1.39544587896366-1.34649500012337-1.21621899961347
-1.38953635867642-1.3318043212562 -1.289924243503 -1.10474184461144
-1.24997832331473-1.0357430250032 -1.157298829452480.110837118869651
-0.951520441455095-1.03415901786789 -0.921041544517999-0.344912053054847
-1.0347013666603 -0.968681788340059 -0.95704929197606 -0.0724245601712463
-0.983291500899267-1.09398859578711 -0.99496046666707 -0.199138512923137
-1.04440568418625 -1.07612040314861 -1.01564337675744 -0.708839621743089

In [33]:
out<-cbind(nn$covariate,nn$net.result[[1]])

In [34]:
n


Out[34]:
  1. 'open'
  2. 'high'
  3. 'close'
  4. 'low'
  5. 'volume'

In [35]:
dimnames(out)<-list(NULL,c("open","high","low","volume","nn-output"))

In [36]:
head(out)


Out[36]:
openhighlowvolumenn-output
0.33841130154 0.26203994248 0.11402569839-1.43661923022 0.01138949538
-0.0589162759352-0.0016232265256-0.0664102107294-1.1403939923543 0.0000980643651
-0.1568727113750498225464-0.1492911814295400374508-0.1676815320921834773227-1.3956334642544943047682 0.0000000000007805461755
-0.02796937812 0.32987097700-0.04386883625-0.58066895066 0.30918197955
0.41527365430.81412564440.50359130750.44463880060.9027615773
0.8905030676 0.8296548358 0.8724964879-0.3391232573 0.8700976374