I've been up to developing a backward propagation(BP) neural network to make a time-series data prediction(past two year data to predict the 3rd one).AMORE package and Neural Network package in R have both been applied and I've got my output in AMORE.However,the big problem is that all my testing samples given the input values finally shared exactly the same outputs,which could really be disastrous.I would paste my codes here,hoping someone would do me a favor.I am really anxious with the explanation and really appreciate your help. Mercy!
library(AMORE)
tbincidence<-c(22.37,52.22,73.99,83.51,74.25,79.10,81.19,81.98,80.76,69.29,78.94,80.04,65.80,58.12,116.71,127.01,115.74,116.08,102.58,101.71,94.25,80.27,89.88,89.37,68.37,79.33,113.04,110.72,101.01,102.88,94.41,97.17,88.01,82.37,84.77,86.93,87.80,69.35,107.51,113.10,103.19,102.27,102.16,98.86,90.24,84.33,88.21,90.38,84.49,76.90,118.43,116.34,107.71,99.42,102.91,92.72,93.49,90.30,83.54,91.20,67.86,95.68,105.28,105.19,96.50,106.80,97.26,95.24,93.15,82.00,78.55,90.18,79.31,66.46,103.72,100.13,96.17,95.35,91.61,87.56,83.84,75.73,82.44,78.33,74.26,73.14,101.19,96.31,93.13,88.79,83.77,85.59,79.45,74.57,82.17,77.72,65.21,91.96,102.80,95.35,97.22,87.91,86.85,85.09,78.52,73.98,77.77,72.60,76.95,66.10,92.61,89.73,90.04,78.49,83.61,78.80,77.42,73.74,74.69,73.99,70.57,65.02,82.18,87.19,78.14,74.37,77.50,71.07,70.20,65.12,62.96,67.99,69.54,55.18,82.69,78.82,74.47,74.67,75.07,69.77,69.25,62.63,64.13,65.34,62.70,60.77,83.40,75.24,73.14,68.46,67.54,69.82,65.03,60.49,64.04,65.24,58.91,67.02)
tbincidence
lagtwo<-c(22.37,52.22,73.99,83.51,74.25,79.10,81.19,81.98,80.76,69.29,78.94,80.04,65.80,58.12,116.71,127.01,115.74,116.08,102.58,101.71,94.25,80.27,89.88,89.37,68.37,79.33,113.04,110.72,101.01,102.88,94.41,97.17,88.01,82.37,84.77,86.93,87.80,69.35,107.51,113.10,103.19,102.27,102.16,98.86,90.24,84.33,88.21,90.38,84.49,76.90,118.43,116.34,107.71,99.42,102.91,92.72,93.49,90.30,83.54,91.20,67.86,95.68,105.28,105.19,96.50,106.80,97.26,95.24,93.15,82.00,78.55,90.18,79.31,66.46,103.72,100.13,96.17,95.35,91.61,87.56,83.84,75.73,82.44,78.33,74.26,73.14,101.19,96.31,93.13,88.79,83.77,85.59,79.45,74.57,82.17,77.72,65.21,91.96,102.80,95.35,97.22,87.91,86.85,85.09,78.52,73.98,77.77,72.60,76.95,66.10,92.61,89.73,90.04,78.49,83.61,78.80,77.42,73.74,74.69,73.99,70.57,65.02,82.18,87.19,78.14,74.37,77.50,71.07,70.20,65.12,62.96,67.99,69.54,55.18,82.69,78.82,74.47,74.67,75.07,69.77,69.25,62.63,64.13,65.34,62.70,60.77,83.40,75.24,73.14,68.46,67.54,69.82,65.03,60.49,64.04,65.24)
lagone<-c(52.22,73.99,83.51,74.25,79.10,81.19,81.98,80.76,69.29,78.94,80.04,65.80,58.12,116.71,127.01,115.74,116.08,102.58,101.71,94.25,80.27,89.88,89.37,68.37,79.33,113.04,110.72,101.01,102.88,94.41,97.17,88.01,82.37,84.77,86.93,87.80,69.35,107.51,113.10,103.19,102.27,102.16,98.86,90.24,84.33,88.21,90.38,84.49,76.90,118.43,116.34,107.71,99.42,102.91,92.72,93.49,90.30,83.54,91.20,67.86,95.68,105.28,105.19,96.50,106.80,97.26,95.24,93.15,82.00,78.55,90.18,79.31,66.46,103.72,100.13,96.17,95.35,91.61,87.56,83.84,75.73,82.44,78.33,74.26,73.14,101.19,96.31,93.13,88.79,83.77,85.59,79.45,74.57,82.17,77.72,65.21,91.96,102.80,95.35,97.22,87.91,86.85,85.09,78.52,73.98,77.77,72.60,76.95,66.10,92.61,89.73,90.04,78.49,83.61,78.80,77.42,73.74,74.69,73.99,70.57,65.02,82.18,87.19,78.14,74.37,77.50,71.07,70.20,65.12,62.96,67.99,69.54,55.18,82.69,78.82,74.47,74.67,75.07,69.77,69.25,62.63,64.13,65.34,62.70,60.77,83.40,75.24,73.14,68.46,67.54,69.82,65.03,60.49,64.04,65.24,58.91)
predict<-cbind(lagtwo,lagone)
predict<-matrix(predict,ncol=2,byrow=F)
predict
output<-c(73.99,83.51,74.25,79.10,81.19,81.98,80.76,69.29,78.94,80.04,65.80,58.12,116.71,127.01,115.74,116.08,102.58,101.71,94.25,80.27,89.88,89.37,68.37,79.33,113.04,110.72,101.01,102.88,94.41,97.17,88.01,82.37,84.77,86.93,87.80,69.35,107.51,113.10,103.19,102.27,102.16,98.86,90.24,84.33,88.21,90.38,84.49,76.90,118.43,116.34,107.71,99.42,102.91,92.72,93.49,90.30,83.54,91.20,67.86,95.68,105.28,105.19,96.50,106.80,97.26,95.24,93.15,82.00,78.55,90.18,79.31,66.46,103.72,100.13,96.17,95.35,91.61,87.56,83.84,75.73,82.44,78.33,74.26,73.14,101.19,96.31,93.13,88.79,83.77,85.59,79.45,74.57,82.17,77.72,65.21,91.96,102.80,95.35,97.22,87.91,86.85,85.09,78.52,73.98,77.77,72.60,76.95,66.10,92.61,89.73,90.04,78.49,83.61,78.80,77.42,73.74,74.69,73.99,70.57,65.02,82.18,87.19,78.14,74.37,77.50,71.07,70.20,65.12,62.96,67.99,69.54,55.18,82.69,78.82,74.47,74.67,75.07,69.77,69.25,62.63,64.13,65.34,62.70,60.77,83.40,75.24,73.14,68.46,67.54,69.82,65.03,60.49,64.04,65.24,58.91,67.02)
output<-matrix(output,ncol=1)
output
trainPmatrix<-predict[1:130,]
trainTmatrix<-output[1:130,]
testPmatrix<-predict[131:156,]
testTmatrix<-output[131:156,]
net<-newff(n.neurons=c(2,5,1),learning.rate.global=1e-3,momentum.global=0.5,error.criterium="LMS",Stao=NA,hidden.layer="tansig",output="purelin",method="ADAPTgdwm")
result<- train(net,trainPmatrix,trainTmatrix,error.criterium="LMS",report=TRUE,show.step=100,n.shows=10)
y<-sim(result$net,testPmatrix)
y
The output would be something like this.
y
[,1]
[1,] 84.81958
[2,] 84.81958
[3,] 84.81957
[4,] 84.81958
[5,] 84.81958
[6,] 84.81958
[7,] 84.81958
[8,] 84.81958
[9,] 84.81958
[10,] 84.81958
[11,] 84.81958
[12,] 84.81958
[13,] 84.81958
[14,] 84.81958
[15,] 84.81958
[16,] 84.81958
[17,] 84.81958
[18,] 84.81958
[19,] 84.81958
[20,] 84.81958
[21,] 84.81958
[22,] 84.81958
[23,] 84.81958
[24,] 84.81958
[25,] 84.81958
[26,] 84.81958
How could that be possible!!
Thanks a lot for your sharp ideas!
It's been a while since I used R, and I'm not familiar with AMORE. But, are you normalizing your input in any way? It doesn't look like it. You're using the tansig function for the hidden layer, which returns values between -1 and 1. It might work better if your input were in that range. For time series, you might use the percentage change (expressed as between -1 and 1) from one value to the next instead of the raw values, then scaled if necessary. I'm sure there's a function in an R library which can do that for you. Then, you transform the output in reverse fashion to get the predictions.
You'll probably have to keep tuning the network (learning rate, momentum, etc.), but this might be a place to start.
My data (object called cc) is a 3D array. Each block begins with an identifier which is followed by a square chunk of data with 39 rows and 2 columns. They look like below:
, , 001-10-f
[,1] [,2]
[1,] 188.16863 379.5638
[2,] 188.16863 360.3510
[3,] 131.26338 349.7913
[4,] 164.26256 348.1780
[5,] 145.04971 358.0044
[6,] 147.68964 345.8314
[7,] 149.74292 353.1645
[8,] 212.51469 350.5246
[9,] 245.95385 352.2845
[10,] 229.08761 360.7910
[11,] 230.26091 348.6180
[12,] 227.91430 355.6578
[13,] 164.99587 310.3389
[14,] 211.04806 310.4856
[15,] 171.15572 320.8986
[16,] 204.88821 320.4587
[17,] 177.46223 300.2192
[18,] 199.75501 300.2192
[19,] 188.16863 303.4457
[20,] 188.16863 282.1796
[21,] 180.24883 284.8195
[22,] 199.46168 282.4729
[23,] 160.44932 265.7534
[24,] 215.44795 267.3666
[25,] 177.46223 282.9129
[26,] 205.18154 277.4864
[27,] 187.87530 257.9802
[28,] 187.87530 270.4466
[29,] 95.77094 327.0585
[30,] 282.17961 331.0184
[31,] 107.94397 254.3136
[32,] 272.93984 259.1535
[33,] 189.34193 209.2881
[34,] 113.37050 365.0442
[35,] 129.79675 389.5369
[36,] 172.62235 382.6438
[37,] 201.95495 381.4705
[38,] 244.63388 391.7369
[39,] 265.46003 369.0041
, , 002-10-f
[,1] [,2]
[1,] 194.3285 361.2310
[2,] 191.9819 344.3647
[3,] 141.0898 340.4048
[4,] 172.0357 336.7382
[5,] 153.5562 345.6847
[6,] 155.9028 335.5649
[7,] 158.6894 342.4581
[8,] 214.1280 333.6583
[9,] 245.9539 333.9517
[10,] 229.9676 340.8448
[11,] 230.5542 330.1384
[12,] 228.3543 337.6182
[13,] 170.4224 293.9127
[14,] 210.7547 292.7393
[15,] 173.9423 303.1524
[16,] 208.8481 301.6858
[17,] 180.1022 286.2862
[18,] 200.3417 285.4062
[19,] 190.2219 286.4328
[20,] 188.4620 271.4732
[21,] 182.7421 274.1131
[22,] 195.3551 273.5265
[23,] 167.3425 259.1535
[24,] 213.5413 256.6602
[25,] 180.2488 272.7932
[26,] 199.6083 270.7399
[27,] 188.6086 249.6204
[28,] 189.0486 261.3535
[29,] 111.0239 321.6320
[30,] 274.9931 309.8989
[31,] 126.1302 252.1137
[32,] 259.0069 246.5405
[33,] 188.9019 202.2483
[34,] 126.4235 355.6578
[35,] 139.6232 374.4306
[36,] 182.8888 362.2576
[37,] 204.8882 359.4710
[38,] 248.8871 367.2442
[39,] 264.7267 348.4713
, , 003-10-f
[,1] [,2]
[1,] 195.25311 371.5863
[2,] 194.64767 356.9045
[3,] 139.85572 351.1529
[4,] 172.09518 346.4608
[5,] 151.20764 356.9045
[6,] 155.14298 343.8876
[7,] 156.35385 352.5151
[8,] 215.98929 344.6444
[9,] 248.98555 346.6121
[10,] 235.21189 353.5746
[11,] 236.72548 341.0118
[12,] 232.63878 348.5798
[13,] 167.85713 303.3234
[14,] 214.02163 303.0207
[15,] 172.54926 314.2213
[16,] 210.23765 312.4050
[17,] 179.20906 293.7878
[18,] 204.94009 293.6365
[19,] 190.86370 293.4851
[20,] 190.10690 270.0245
[21,] 182.99303 272.9003
[22,] 199.49116 273.2030
[23,] 160.13782 261.3970
[24,] 217.80560 259.4293
[25,] 179.20906 271.8408
[26,] 204.94009 271.2353
[27,] 189.50147 248.0774
[28,] 189.65283 262.7592
[29,] 98.23199 333.7466
[30,] 283.79812 325.8759
[31,] 118.81681 250.6505
[32,] 260.64020 246.4125
[33,] 187.83652 206.3023
[34,] 119.42225 370.3755
[35,] 138.34213 386.1168
[36,] 177.99818 372.4945
[37,] 212.35668 372.3431
[38,] 247.92604 382.7869
[39,] 265.63504 367.0456
, , 004-10-f
[,1] [,2]
[1,] 195.7082 358.5218
[2,] 194.1993 340.5655
[3,] 141.2358 329.0976
[4,] 171.8671 329.0976
[5,] 156.0234 336.0387
[6,] 155.1180 324.4199
[7,] 157.5323 331.8137
[8,] 216.0788 326.5325
[9,] 247.6154 326.3816
[10,] 232.3752 333.9262
[11,] 236.1475 322.6092
[12,] 232.6770 329.2485
[13,] 168.3966 284.7351
[14,] 214.2681 283.2262
[15,] 174.7341 295.2976
[16,] 209.5904 293.4869
[17,] 178.5064 275.9833
[18,] 205.0636 274.6253
[19,] 191.1814 274.3235
[20,] 191.0305 261.9502
[21,] 183.3350 264.0627
[22,] 197.8207 263.4592
[23,] 161.3046 246.2574
[24,] 218.9457 245.5029
[25,] 178.3555 261.3467
[26,] 204.6109 259.2342
[27,] 190.2761 231.6208
[28,] 190.7288 244.1449
[29,] 112.1135 310.8396
[30,] 278.0958 307.3690
[31,] 125.2412 236.9020
[32,] 262.2520 235.9967
[33,] 189.8234 191.4832
[34,] 124.0340 341.7726
[35,] 144.4046 367.4245
[36,] 180.6189 354.4477
[37,] 209.5904 355.0512
[38,] 247.7663 364.8593
[39,] 265.2699 342.2253
, , 005-10-f
[,1] [,2]
[1,] 227.2118 375.5037
[2,] 226.1675 359.3915
[3,] 171.4159 356.2586
[4,] 202.5959 349.5451
[5,] 185.1411 361.1817
[6,] 185.2903 349.3960
[7,] 185.1411 357.1537
[8,] 242.7272 344.4728
[9,] 273.4598 346.2630
[10,] 254.5130 353.2748
[11,] 264.5086 341.4890
[12,] 258.3919 349.6943
[13,] 196.6285 304.3415
[14,] 244.8159 299.8659
[15,] 203.7894 316.1273
[16,] 240.3403 310.6073
[17,] 207.3699 295.8378
[18,] 235.5663 292.5557
[19,] 221.0951 292.5557
[20,] 219.4541 274.8025
[21,] 212.2931 278.5321
[22,] 226.7642 276.4435
[23,] 191.5561 266.8955
[24,] 242.2797 262.4199
[25,] 204.5354 276.2943
[26,] 232.8809 272.5646
[27,] 216.6195 247.6504
[28,] 218.1114 264.5086
[29,] 134.1191 341.0415
[30,] 298.6724 322.2439
[31,] 145.4573 264.9561
[32,] 274.2057 250.9325
[33,] 213.7850 214.2325
[34,] 155.3037 373.5642
[35,] 175.4439 387.2895
[36,] 211.9947 375.5037
[37,] 237.8041 372.6691
[38,] 267.0447 378.7858
[39,] 287.6325 364.6130
I want to export this array to .xlsx or .csv or .txt format with the array with the following changes:
1. remove the two commas before each block's identifier (e.g., the two commas before "001-10-f"), add "IMAGE=" before and ".JPG" after the identifier, and move each identifier from the beginning to the end of each block;
2. add "LM=39" to the beginning of each block;
3. remove the row ([1,] to [39,]) and column ([,1] and [,2]) labels.
The above changes format the data into TPS format ready for geometric morphometric analysis.
The exported data should look like below:
LM=39
188.16863 379.5638
188.16863 360.351
131.26338 349.7913
164.26256 348.178
145.04971 358.0044
147.68964 345.8314
149.74292 353.1645
212.51469 350.5246
245.95385 352.2845
229.08761 360.791
230.26091 348.618
227.9143 355.6578
164.99587 310.3389
211.04806 310.4856
171.15572 320.8986
204.88821 320.4587
177.46223 300.2192
199.75501 300.2192
188.16863 303.4457
188.16863 282.1796
180.24883 284.8195
199.46168 282.4729
160.44932 265.7534
215.44795 267.3666
177.46223 282.9129
205.18154 277.4864
187.8753 257.9802
187.8753 270.4466
95.77094 327.0585
282.17961 331.0184
107.94397 254.3136
272.93984 259.1535
189.34193 209.2881
113.3705 365.0442
129.79675 389.5369
172.62235 382.6438
201.95495 381.4705
244.63388 391.7369
265.46003 369.0041
IMAGE=001-10-f.JPG
LM=39
194.3285 361.231
191.9819 344.3647
141.0898 340.4048
172.0357 336.7382
153.5562 345.6847
155.9028 335.5649
158.6894 342.4581
214.128 333.6583
245.9539 333.9517
229.9676 340.8448
230.5542 330.1384
228.3543 337.6182
170.4224 293.9127
210.7547 292.7393
173.9423 303.1524
208.8481 301.6858
180.1022 286.2862
200.3417 285.4062
190.2219 286.4328
188.462 271.4732
182.7421 274.1131
195.3551 273.5265
167.3425 259.1535
213.5413 256.6602
180.2488 272.7932
199.6083 270.7399
188.6086 249.6204
189.0486 261.3535
111.0239 321.632
274.9931 309.8989
126.1302 252.1137
259.0069 246.5405
188.9019 202.2483
126.4235 355.6578
139.6232 374.4306
182.8888 362.2576
204.8882 359.471
248.8871 367.2442
264.7267 348.4713
IMAGE=002-10-f.JPG
LM=39
195.25311 371.5863
194.64767 356.9045
139.85572 351.1529
172.09518 346.4608
151.20764 356.9045
155.14298 343.8876
156.35385 352.5151
215.98929 344.6444
248.98555 346.6121
235.21189 353.5746
236.72548 341.0118
232.63878 348.5798
167.85713 303.3234
214.02163 303.0207
172.54926 314.2213
210.23765 312.405
179.20906 293.7878
204.94009 293.6365
190.8637 293.4851
190.1069 270.0245
182.99303 272.9003
199.49116 273.203
160.13782 261.397
217.8056 259.4293
179.20906 271.8408
204.94009 271.2353
189.50147 248.0774
189.65283 262.7592
98.23199 333.7466
283.79812 325.8759
118.81681 250.6505
260.6402 246.4125
187.83652 206.3023
119.42225 370.3755
138.34213 386.1168
177.99818 372.4945
212.35668 372.3431
247.92604 382.7869
265.63504 367.0456
IMAGE=003-10-f.JPG
LM=39
195.7082 358.5218
194.1993 340.5655
141.2358 329.0976
171.8671 329.0976
156.0234 336.0387
155.118 324.4199
157.5323 331.8137
216.0788 326.5325
247.6154 326.3816
232.3752 333.9262
236.1475 322.6092
232.677 329.2485
168.3966 284.7351
214.2681 283.2262
174.7341 295.2976
209.5904 293.4869
178.5064 275.9833
205.0636 274.6253
191.1814 274.3235
191.0305 261.9502
183.335 264.0627
197.8207 263.4592
161.3046 246.2574
218.9457 245.5029
178.3555 261.3467
204.6109 259.2342
190.2761 231.6208
190.7288 244.1449
112.1135 310.8396
278.0958 307.369
125.2412 236.902
262.252 235.9967
189.8234 191.4832
124.034 341.7726
144.4046 367.4245
180.6189 354.4477
209.5904 355.0512
247.7663 364.8593
265.2699 342.2253
IMAGE=004-10-f.JPG
LM=39
227.2118 375.5037
226.1675 359.3915
171.4159 356.2586
202.5959 349.5451
185.1411 361.1817
185.2903 349.396
185.1411 357.1537
242.7272 344.4728
273.4598 346.263
254.513 353.2748
264.5086 341.489
258.3919 349.6943
196.6285 304.3415
244.8159 299.8659
203.7894 316.1273
240.3403 310.6073
207.3699 295.8378
235.5663 292.5557
221.0951 292.5557
219.4541 274.8025
212.2931 278.5321
226.7642 276.4435
191.5561 266.8955
242.2797 262.4199
204.5354 276.2943
232.8809 272.5646
216.6195 247.6504
218.1114 264.5086
134.1191 341.0415
298.6724 322.2439
145.4573 264.9561
274.2057 250.9325
213.785 214.2325
155.3037 373.5642
175.4439 387.2895
211.9947 375.5037
237.8041 372.6691
267.0447 378.7858
287.6325 364.613
IMAGE=005-10-f.JPG
May I know how to perform the conversion in R and export the converted object?
Thank you!
You have to iterate over cc and use string formatting as necessary. You can use nested for loops and R's version of sprintf.
sink ('output-file.txt')
for (i in 1:dim(cc)[3]) {
cat (sprintf ("LM=39\n"))
for (j in 1:dim(cc)[1]) {
cat (sprintf ("%.4f %.4f\n", cc[j,1,i], cc[j,2,i]))
}
cat (sprintf ("IMAGE=%s.JPG\n", dimnames(cc)[[3]][i]))
}