-
Notifications
You must be signed in to change notification settings - Fork 30
/
Handle-Missing-Value-II.Rmd
3138 lines (2571 loc) · 127 KB
/
Handle-Missing-Value-II.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "<img src='www/binary-logo-resize.jpg' width='240'>"
subtitle: "[binary.com](https://github.com/englianhu/binary.com-interview-question) 面试试题 I - 多变量数据缺失值管理 II"
author: "[®γσ, Lian Hu(黄联富)](https://englianhu.github.io/) <img src='www/RYO.jpg' width='24'> <img src='www/RYU.jpg' width='24'> <img src='www/ENG.jpg' width='24'>®"
date: "`r lubridate::today('Asia/Tokyo')`"
output:
html_document:
number_sections: yes
toc: yes
toc_depth: 4
toc_float:
collapsed: yes
smooth_scroll: yes
code_folding: hide
---
```{r setup}
suppressPackageStartupMessages(require('BBmisc'))
## 读取程序包
pkg <- c('devtools', 'tidyverse', 'timetk', 'lubridate', 'plyr', 'dplyr', 'magrittr', 'purrr', 'stringr', 'reshape', 'formattable', 'microbenchmark', 'knitr', 'kableExtra', 'VIM', 'mice', 'miceAdds', 'mi', 'mitools', 'Amelia', 'missForest', 'Hmisc', 'DMwR', 'imputeTS', 'tidyimpute', 'mtsdi', 'xts', 'forecast', 'marima', 'missMDA')
suppressAll(lib(pkg))
funs <- c('convertOHLC.R')
l_ply(funs, function(x) source(paste0('./function/', x)))
algo <- c('interpolation', 'locf', 'mean', 'random', 'kalman', 'ma')
rm(pkg, funs)
```
# 简介
## 介绍弥补数据
由于在科研[binary.com Interview Question I - Interday High Frequency Trading Models Comparison](https://rpubs.com/englianhu/binary-Q1Inter-HFT)测试高频率量化交易时,从[fxcm/MarketData](https://github.com/fxcm/MarketData)下载的数据并不完整^[欲知更多详情,请查阅[binary.com Interview Question I - Interday High Frequency Trading Models Comparison](https://rpubs.com/englianhu/binary-Q1Inter-HFT)。],[binary.com 面试试题 I - 单变量数据缺失值管理](http://rpubs.com/englianhu/handle-missing-value)尝试弥补缺失值不果,单变量无法辨认开市价、最高价、最低价和闭市价之间的关系。
- [How to use auto.arima to impute missing values](https://stats.stackexchange.com/questions/104565/how-to-use-auto-arima-to-impute-missing-values)使用`auto.arima()`来弥补缺失值。
- [What should be the allowed percentage of Missing Values?](https://discuss.analyticsvidhya.com/t/what-should-be-the-allowed-percentage-of-missing-values/2456)讨论着一个数据最多可以允许20%~30%的缺失值,过多的缺失值的话,该数据基本上就无法使用了。一些统计学家有本事将50%缺失值的数据复原,不过是基于许多附属变量和数据才能弥补回数据。
- [Principled Missing Data Methods for Researchers](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3701793/)讲述许多弥补数据缺失值的方法与数学模式。
- [Imputation methods for time series data](https://stats.stackexchange.com/questions/261271/imputation-methods-for-time-series-data)
- [Imputing Missing Observation in Multivariate Time Series](https://stats.stackexchange.com/questions/103968/imputing-missing-observation-in-multivariate-time-series)
- [`imputeTS`: Time Series Missing Value Imputation in R](https://journal.r-project.org/archive/2017/RJ-2017-009/index.html)
- [How to Handle Missing Data](https://towardsdatascience.com/how-to-handle-missing-data-8646b18db0d4)
## `impueTS`程序包
<span style='color:goldenrod'>*imputeTS - Time Series Missing Value Imputation in R*</span>讲述`mice`、`Amelia`、`missMDA`与`VIM`都是多变量弥补数据程序包,而`imputeTS`乃单变量弥补数据程序包,不过程序包中的`seadec()`函数乃弥补季节性数据。
| Simple | Imputation Imputation | Plots & Statistics | Datasets |
|:----------:|:---------------------:|:----------------------:|:-----------------:|
| na.locf | na.interpolation | plotNA.distribution | tsAirgap |
| na.mean | na.kalman | plotNA.distributionBar | tsAirgapComplete |
| na.random | na.ma | plotNA.gapsize | tsHeating |
| na.replace | na.seadec | plotNA.imputations | tsHeatingComplete |
| na.remove | na.seasplit | statsNA | tsNH4 |
| | | | tsNH4Complete |
*Table 1: General Overview imputeTS package*
| Function | Option | Description |
|:-----------------:|:-----------:|:---------------------------------------------------------------:|
| na.interpolation | linear | Imputation by Linear Interpolation |
| | spline | Imputation by Spline Interpolation |
| | stine | Imputation by Stineman Interpolation |
| | | |
| na.kalman | StructTS | Imputation by Structural Model & Kalman Smoothing |
| | auto.arima | Imputation by ARIMA State Space Representation & Kalman Sm. |
| | | |
| na.locf | locf | Imputation by Last Observation Carried Forward |
| | nocb | Imputation by Next Observation Carried Backward |
| | | |
| na.ma | simple | Missing Value Imputation by Simple Moving Average |
| | linear | Missing Value Imputation by Linear Weighted Moving Average |
| | exponential | Missing Value Imputation by Exponential Weighted Moving Average |
| | | |
| na.mean | mean | MissingValue Imputation by Mean Value |
| | median | Missing Value Imputation by Median Value |
| | mode | Missing Value Imputation by Mode Value |
| | | |
| na.random | | Missing Value Imputation by Random Sample |
| na.replace | | Replace Missing Values by a Defined Value |
| na.seadec | | Seasonally Decomposed Missing Value Imputation |
| na.seasplit | | Seasonally Splitted Missing Value Imputation |
| na.remove | | Remove Missing Values |
*Table 3: Overview Imputation Algorithms*
## `Amelia`程序包
[Amelia II: A Program for Missing Data](https://gking.harvard.edu/amelia)介绍`Amelia`程序包,而<span style='color:goldenrod'>*AMELIA II - A Program for Missing Data*</span>教导如何使用该程序包。[Error in as.POSIXct.numeric(value) : 'origin' must be supplied #18](https://github.com/IQSS/Amelia/issues/18)显示时间变量无法弥补,故此对于`Amelia`缺失值,僕得省略掉时间变量,仅设置价格变量为缺失值而已。
## 其它程序包
`mice`程序包可以使用`lm`函数将弥补数据线型化,`tidyr`程序包中有个`fill()`函数可以。而`dendextend::na_locf()`会比`zoo::na.locf()`高效率,不过弥补数据时会遇到一些参数问题。
# 数据
## 读取数据
### 1分钟数据
和之前的单变量一样,首先僕随机导入每分钟为1个时间单位的数据。
```
Error in optim(init[mask], getLike, method = "L-BFGS-B", lower = rep(0, : L-BFGS-B needs finite values of 'fn'
17. optim(init[mask], getLike, method = "L-BFGS-B", lower = rep(0, np + 1L), upper = rep(Inf, np + 1L), control = optim.control)
16. StructTS(data, ...)
15. na.kalman(data, ...)
14. apply.base.algorithm(data, algorithm = algorithm, ...)
13. .f(.x[[i]], ...)
12. map(., na.seadec, algorithm = x)
11. function_list[[i]](value)
10. freduce(value, `_function_list`)
9. `_fseq`(`_lhs`)
8. eval(quote(`_fseq`(`_lhs`)), env, env)
7. eval(quote(`_fseq`(`_lhs`)), env, env)
6. withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
5. data_m1_NA %>% dplyr::select(starts_with("Ask"), starts_with("Bid")) %>% map(na.seadec, algorithm = x) %>% as.tibble
4. FUN(X[[i]], ...)
3. lapply(pieces, .fun, ...)
2. structure(lapply(pieces, .fun, ...), dim = dim(pieces))
1. llply(algo, function(x) { data_m1_NA %>% dplyr::select(starts_with("Ask"), starts_with("Bid")) %>% map(na.seadec, algorithm = x) %>% as.tibble })
```
由于频频出现错误信息[#imputeTS/issues/26](https://github.com/SteffenMoritz/imputeTS/issues/26),于此僕使用sort(sample(length(fls), 1))随机筛选1个文件。
```{r warning=FALSE, message=FALSE}
pth <- 'C:/Users/scibr/Documents/GitHub/scibrokes/real-time-fxcm/data/USDJPY/'
fls <- list.files(pth, pattern = '^Y[0-9]{4}W[1-9]{1,2}_m1.rds$')
## 1分钟数据
## 由于频频出现错误信息,于此僕使用sort(sample(length(fls), 1))随机筛选4个文件。
data_m1 <- llply(fls[sort(sample(length(fls), 1))], function(x) {
y <- readRDS(paste0(pth, x)) %>%
dplyr::rename(index = DateTime) %>%
mutate(index = index %>% mdy_hms %>%
.POSIXct(tz = 'Europe/Athens') %>%
force_tz())
yw <- x %>% str_extract_all('Y[0-9]{4}W[0-9]{1,2}') %>%
str_split_fixed('[A-Z]{1}', 3) %>% .[,-1]
nch <- y$index[1] %>% substr(nchar(.)+2, nchar(.)+3)
y %<>% mutate(
year = as.numeric(yw[1]), week = as.numeric(yw[2]),
nch = nch, index = if_else(
nch == '23', index + hours(1), index)) %>%
dplyr::select(-nch)
}) %>% bind_rows %>% tbl_df %>% arrange(index)
dim(data_m1)
data_m1
## 检验原始数据是否存在偏差。
data_m1 %<>% mutate(
bias.open = if_else(AskOpen>AskHigh|AskOpen<AskLow, 1, 0),
bias.high = if_else(AskHigh<AskOpen|AskHigh<AskLow|AskHigh<AskClose, 1, 0),
bias.low = if_else(AskLow>AskOpen|AskLow>AskHigh|AskLow>AskClose, 1, 0),
bias.close = if_else(AskClose>AskHigh|AskClose<AskLow, 1, 0))
data_m1 %>%
dplyr::filter(bias.open==1|bias.high==1|bias.low==1|bias.close==1) %>%
kable(caption = 'Bias Imputation') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%', height = '400px')
```
### Tick数据转为1分钟数据
接着,导入Tick数据^[欲知更多详情,请参阅[一、什么是Tick Data](https://www.fmz.com/bbs-topic/457)。],并且转为每分钟为1时间单位。
```{r, warning=FALSE, message=FALSE}
pth <- 'C:/Users/scibr/Documents/GitHub/scibrokes/real-time-fxcm/data/USDJPY/'
fls <- list.files(pth, pattern = '^Y[0-9]{4}W[1-9]{1,2}.rds$')
## Tick数据转为1分钟数据
## 由于频频出现错误信息,于此僕使用sort(sample(length(fls), 1))随机筛选2个文件。
data_tm1 <- llply(fls[sort(sample(length(fls), 1))], function(x) {
y <- readRDS(paste0(pth, x)) %>%
convertOHLC(combine = TRUE)
yw <- x %>% str_extract_all('Y[0-9]{4}W[0-9]{1,2}') %>%
str_split_fixed('[A-Z]{1}', 3) %>% .[,-1]
y %<>% mutate(
year = as.numeric(yw[1]), week = as.numeric(yw[2]), .)
}) %>% bind_rows %>% tbl_df %>% arrange(index)
dim(data_tm1)
data_tm1
## 检验原始数据是否存在偏差。
data_tm1 %<>% mutate(
bias.open = if_else(AskOpen>AskHigh|AskOpen<AskLow, 1, 0),
bias.high = if_else(AskHigh<AskOpen|AskHigh<AskLow|AskHigh<AskClose, 1, 0),
bias.low = if_else(AskLow>AskOpen|AskLow>AskHigh|AskLow>AskClose, 1, 0),
bias.close = if_else(AskClose>AskHigh|AskClose<AskLow, 1, 0))
data_tm1 %>%
dplyr::filter(bias.open==1|bias.high==1|bias.low==1|bias.close==1) %>%
kable(caption = 'Bias Imputation') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%', height = '400px')
```
## 设置缺失值
### 1分钟数据
现在尝试随机设置缺失值。
```{r warning=FALSE}
data_m1_NA <- data_m1 %>%
dplyr::select(index, BidOpen, BidHigh, BidLow, BidClose, AskOpen, AskHigh, AskLow, AskClose) %>%
prodNA(noNA = 0.01)
data_m1_NA
data_m1_NA %>% md.pattern
data_m1_NA %>% md.pairs
```
### Tick数据转为1分钟数据
```{r warning=FALSE}
data_tm1_NA <- data_tm1 %>%
dplyr::select(index, BidOpen, BidHigh, BidLow, BidClose, AskOpen, AskHigh, AskLow, AskClose) %>%
prodNA(noNA = 0.01)
data_tm1_NA
data_tm1_NA %>% md.pattern
data_tm1_NA %>% md.pairs
```
# 统计模式
## 弥补缺失值
- [Imputing missing data with R; MICE package](https://www.r-bloggers.com/imputing-missing-data-with-r-mice-package/)
- [mice - Multivariate Imputation by Chained Equations in R](https://github.com/englianhu/binary.com-interview-question/blob/master/reference/mice%20Multivariate%20Imputation%20by%20Chained%20Equations%20in%20R.pdf)
- [mice : Multivariate Imputation by Chained Equations](https://github.com/stefvanbuuren/mice)
- [HOW DO I PERFORM MULTIPLE IMPUTATION USING PREDICTIVE MEAN MATCHING IN R? | R FAQ](https://stats.idre.ucla.edu/r/faq/how-do-i-perform-multiple-imputation-using-predictive-mean-matching-in-r/)
- [Imputing missing observation in multivariate time series](https://stats.stackexchange.com/questions/103968/imputing-missing-observation-in-multivariate-time-series)
- [arima method in mtsdi](https://stackoverflow.com/questions/29472532/arima-method-in-mtsdi)
- [Dealing with Missing Data using R](https://medium.com/coinmonks/dealing-with-missing-data-using-r-3ae428da2d17)
- [How to use auto.arima to impute missing values](https://stats.stackexchange.com/questions/104565/how-to-use-auto-arima-to-impute-missing-values)
- [How to Fill in Missing Data in Time Series?](https://stats.stackexchange.com/questions/245615/how-to-fill-in-missing-data-in-time-series)
- [Forecasting Multivariate Data with `auto.arima`](https://stackoverflow.com/questions/15495465/forecasting-multivariate-data-with-auto-arima)
- [Multivariate Time Series Model](https://stackoverflow.com/questions/44376808/multivariate-time-series-model)
- [`auto.arima` using `xreg` and Forecasting Several ts Together](https://stackoverflow.com/questions/25036986/auto-arima-using-xreg-and-forecasting-several-ts-together)
- [`auto.arima` Forecast with Multivariate `xreg` - unexpected Results](https://stackoverflow.com/questions/15054800/auto-arima-forecast-with-multivariate-xreg-unexpected-results)
- [`auto.arima` Warns `NaNs` Produced on Std Error](https://stats.stackexchange.com/questions/26999/auto-arima-warns-nans-produced-on-std-error)
- [Arima time series forecast (auto.arima) with multiple exogeneous variables in R](https://stats.stackexchange.com/questions/122803/arima-time-series-forecast-auto-arima-with-multiple-exogeneous-variables-in-r)
- [Multivariate ARIMA with regression](https://stats.stackexchange.com/questions/45993/multivariate-arima-with-regression)
- [I am trying to do a multivariate time series analysis on r. how to use auto.arima with Xreg?](https://www.researchgate.net/post/I_am_trying_to_do_a_multivariate_time_series_analysis_on_r_how_to_use_autoarima_with_Xreg)
```{r warning=FALSE}
tttt <- data_m1_NA[-1] %>% amelia
llply(tttt$imputations, function(x) {
x %>% mutate(
VA = if_else(AskOpen <= AskHigh & AskOpen >= AskLow &
AskClose <= AskHigh & AskClose >= AskLow &
AskHigh >= AskLow, 1, 0),
VB = if_else(BidOpen <= BidHigh & BidOpen >= BidLow &
BidClose <= BidHigh & BidClose >= BidLow &
BidHigh >= BidLow, 1, 0)) %>%
dplyr::filter(VA == 0|VB == 0)
})
```
经过测试以上数据,结果发现`amelia`也是单变量数据弥补。
**注释:单变量弥补的数据将会与之前单变量预测数据一样,就是出现偏差,例如:**
- 开市价高于最高价
- 开市价低于最低价
- 最高价低于开市价
- 最高价低于最低价
- 最高价低于闭市价
- 最低价高于开市价
- 最低价高于最高价
- 最低价高于闭市价
- 闭市价高于最高价
- 闭市价低于最低价
## 1% 缺失值
### 1分钟数据
以下使用`imputeTS::na.seadec()`弥补1%数据缺失值。
```{r warning=FALSE}
data_m1_NA <- data_m1 %>%
dplyr::select(BidOpen, BidHigh, BidLow, BidClose,
AskOpen, AskHigh, AskLow, AskClose) %>%
prodNA(noNA = 0.01) %>%
cbind(data_m1[1], .) %>% tbl_df
data_m1_1_impTS <- llply(algo, function(x) {
data_m1_NA %>%
dplyr::select(starts_with('Ask'), starts_with('Bid')) %>%
map(na.seadec, algorithm = x) %>% as.tibble
})
names(data_m1_1_impTS) <- algo
data_m1_1_impTS %<>% ldply %>% tbl_df
data_m1_1_impTS %<>% mutate(
bias.open = if_else(AskOpen>AskHigh|AskOpen<AskLow, 1, 0),
bias.high = if_else(AskHigh<AskOpen|AskHigh<AskLow|AskHigh<AskClose, 1, 0),
bias.low = if_else(AskLow>AskOpen|AskLow>AskHigh|AskLow>AskClose, 1, 0),
bias.close = if_else(AskClose>AskHigh|AskClose<AskLow, 1, 0))
data_m1_1_impTS %>%
dplyr::filter(bias.open==1|bias.high==1|bias.low==1|bias.close==1)
data_m1_1_impTS %<>%
ddply(.(.id), summarise,
AskOpen = mean((AskOpen - data_m1$AskOpen)^2),
AskHigh = mean((AskHigh - data_m1$AskHigh)^2),
AskLow = mean((AskLow - data_m1$AskLow)^2),
AskClose = mean((AskClose - data_m1$AskClose)^2),
Mean.HLC = (AskHigh + AskLow + AskClose)/3,
Mean.OHLC = (AskOpen + AskHigh + AskLow + AskClose)/4,
bias.open = sum(bias.open)/length(bias.open),
bias.high = sum(bias.high)/length(bias.high),
bias.low = sum(bias.low)/length(bias.low),
bias.close = sum(bias.close)/length(bias.close)) %>% tbl_df
data_m1_1_impTS %>%
kable(caption = 'MSE') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%')#, height = '400px')
```
以下使用`Amelia::amelia()`弥补1%数据缺失值。
```{r warning=FALSE}
data_m1_1_amelia <- data_m1_NA %>%
amelia %>%
.$imputations %>%
ldply %>% tbl_df
data_m1_1_amelia %>% anyNA
data_m1_1_amelia %<>% mutate(
bias.open = if_else(AskOpen>AskHigh|AskOpen<AskLow, 1, 0),
bias.high = if_else(AskHigh<AskOpen|AskHigh<AskLow|AskHigh<AskClose, 1, 0),
bias.low = if_else(AskLow>AskOpen|AskLow>AskHigh|AskLow>AskClose, 1, 0),
bias.close = if_else(AskClose>AskHigh|AskClose<AskLow, 1, 0))
data_m1_1_amelia %>%
dplyr::filter(bias.open==1|bias.high==1|bias.low==1|bias.close==1)
data_m1_1_amelia %<>%
ddply(.(.id), summarise,
AskOpen = mean((AskOpen - data_m1$AskOpen)^2),
AskHigh = mean((AskHigh - data_m1$AskHigh)^2),
AskLow = mean((AskLow - data_m1$AskLow)^2),
AskClose = mean((AskClose - data_m1$AskClose)^2),
Mean.HLC = (AskHigh + AskLow + AskClose)/3,
Mean.OHLC = (AskOpen + AskHigh + AskLow + AskClose)/4,
bias.open = sum(bias.open)/length(bias.open),
bias.high = sum(bias.high)/length(bias.high),
bias.low = sum(bias.low)/length(bias.low),
bias.close = sum(bias.close)/length(bias.close)) %>% tbl_df
data_m1_1_amelia %>%
kable(caption = 'MSE') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%')#, height = '400px')
```
以下使用`tidyr::fill()`弥补1%数据缺失值。
```{r warning=FALSE}
data_m1_1_tidyr <- data_m1_NA %>%
fill(BidOpen, BidHigh, BidLow, BidClose,
AskOpen, AskHigh, AskLow, AskClose) %>% #default direction down
fill(BidOpen, BidHigh, BidLow, BidClose,
AskOpen, AskHigh, AskLow, AskClose, .direction = 'up')
data_m1_1_tidyr %>% anyNA
data_m1_1_tidyr %<>% mutate(
bias.open = if_else(AskOpen>AskHigh|AskOpen<AskLow, 1, 0),
bias.high = if_else(AskHigh<AskOpen|AskHigh<AskLow|AskHigh<AskClose, 1, 0),
bias.low = if_else(AskLow>AskOpen|AskLow>AskHigh|AskLow>AskClose, 1, 0),
bias.close = if_else(AskClose>AskHigh|AskClose<AskLow, 1, 0))
data_m1_1_tidyr %>%
dplyr::filter(bias.open==1|bias.high==1|bias.low==1|bias.close==1)
data_m1_1_tidyr %<>%
summarise(
AskOpen = mean((AskOpen - data_m1$AskOpen)^2),
AskHigh = mean((AskHigh - data_m1$AskHigh)^2),
AskLow = mean((AskLow - data_m1$AskLow)^2),
AskClose = mean((AskClose - data_m1$AskClose)^2),
Mean.HLC = (AskHigh + AskLow + AskClose)/3,
Mean.OHLC = (AskOpen + AskHigh + AskLow + AskClose)/4,
bias.open = sum(bias.open)/length(bias.open),
bias.high = sum(bias.high)/length(bias.high),
bias.low = sum(bias.low)/length(bias.low),
bias.close = sum(bias.close)/length(bias.close)) %>% tbl_df
data_m1_1_tidyr %>%
kable(caption = 'MSE') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%')#, height = '400px')
```
### Tick数据转为1分钟数据
以下使用`imputeTS::na.seadec()`弥补1%数据缺失值。
```{r warning=FALSE}
data_tm1_NA <- data_tm1 %>%
dplyr::select(BidOpen, BidHigh, BidLow, BidClose,
AskOpen, AskHigh, AskLow, AskClose) %>%
prodNA(noNA = 0.01) %>%
cbind(data_tm1[1], .) %>% tbl_df
data_tm1_1_impTS <- llply(algo, function(x) {
data_tm1_NA %>%
dplyr::select(starts_with('Ask'), starts_with('Bid')) %>%
map(na.seadec, algorithm = x) %>% as.tibble
})
names(data_tm1_1_impTS) <- algo
data_tm1_1_impTS %<>% ldply %>% tbl_df
data_tm1_1_impTS %<>% mutate(
bias.open = if_else(AskOpen>AskHigh|AskOpen<AskLow, 1, 0),
bias.high = if_else(AskHigh<AskOpen|AskHigh<AskLow|AskHigh<AskClose, 1, 0),
bias.low = if_else(AskLow>AskOpen|AskLow>AskHigh|AskLow>AskClose, 1, 0),
bias.close = if_else(AskClose>AskHigh|AskClose<AskLow, 1, 0))
data_tm1_1_impTS %>%
dplyr::filter(bias.open==1|bias.high==1|bias.low==1|bias.close==1)
data_tm1_1_impTS %<>%
ddply(.(.id), summarise,
AskOpen = mean((AskOpen - data_m1$AskOpen)^2),
AskHigh = mean((AskHigh - data_m1$AskHigh)^2),
AskLow = mean((AskLow - data_m1$AskLow)^2),
AskClose = mean((AskClose - data_m1$AskClose)^2),
Mean.HLC = (AskHigh + AskLow + AskClose)/3,
Mean.OHLC = (AskOpen + AskHigh + AskLow + AskClose)/4,
bias.open = sum(bias.open)/length(bias.open),
bias.high = sum(bias.high)/length(bias.high),
bias.low = sum(bias.low)/length(bias.low),
bias.close = sum(bias.close)/length(bias.close)) %>% tbl_df
data_tm1_1_impTS %>%
kable(caption = 'MSE') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%')#, height = '400px')
```
以下使用`Amelia::amelia()`弥补1%数据缺失值。
```{r warning=FALSE}
data_tm1_1_amelia <- data_tm1_NA %>%
amelia %>%
.$imputations %>%
ldply %>% tbl_df
data_tm1_1_amelia %>% anyNA
data_tm1_1_amelia %<>% mutate(
bias.open = if_else(AskOpen>AskHigh|AskOpen<AskLow, 1, 0),
bias.high = if_else(AskHigh<AskOpen|AskHigh<AskLow|AskHigh<AskClose, 1, 0),
bias.low = if_else(AskLow>AskOpen|AskLow>AskHigh|AskLow>AskClose, 1, 0),
bias.close = if_else(AskClose>AskHigh|AskClose<AskLow, 1, 0))
data_tm1_1_amelia %>%
dplyr::filter(bias.open==1|bias.high==1|bias.low==1|bias.close==1)
data_tm1_1_amelia %<>%
ddply(.(.id), summarise,
AskOpen = mean((AskOpen - data_m1$AskOpen)^2),
AskHigh = mean((AskHigh - data_m1$AskHigh)^2),
AskLow = mean((AskLow - data_m1$AskLow)^2),
AskClose = mean((AskClose - data_m1$AskClose)^2),
Mean.HLC = (AskHigh + AskLow + AskClose)/3,
Mean.OHLC = (AskOpen + AskHigh + AskLow + AskClose)/4,
bias.open = sum(bias.open)/length(bias.open),
bias.high = sum(bias.high)/length(bias.high),
bias.low = sum(bias.low)/length(bias.low),
bias.close = sum(bias.close)/length(bias.close)) %>% tbl_df
data_tm1_1_amelia %>%
kable(caption = 'MSE') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%')#, height = '400px')
```
以下使用`tidyr::fill()`弥补1%数据缺失值。
```{r warning=FALSE}
data_tm1_1_tidyr <- data_tm1_NA %>%
fill(BidOpen, BidHigh, BidLow, BidClose,
AskOpen, AskHigh, AskLow, AskClose) %>% #default direction down
fill(BidOpen, BidHigh, BidLow, BidClose,
AskOpen, AskHigh, AskLow, AskClose, .direction = 'up')
data_tm1_1_tidyr %>% anyNA
data_tm1_1_tidyr %<>% mutate(
bias.open = if_else(AskOpen>AskHigh|AskOpen<AskLow, 1, 0),
bias.high = if_else(AskHigh<AskOpen|AskHigh<AskLow|AskHigh<AskClose, 1, 0),
bias.low = if_else(AskLow>AskOpen|AskLow>AskHigh|AskLow>AskClose, 1, 0),
bias.close = if_else(AskClose>AskHigh|AskClose<AskLow, 1, 0))
data_tm1_1_tidyr %>%
dplyr::filter(bias.open==1|bias.high==1|bias.low==1|bias.close==1)
data_tm1_1_tidyr %<>%
summarise(
AskOpen = mean((AskOpen - data_m1$AskOpen)^2),
AskHigh = mean((AskHigh - data_m1$AskHigh)^2),
AskLow = mean((AskLow - data_m1$AskLow)^2),
AskClose = mean((AskClose - data_m1$AskClose)^2),
Mean.HLC = (AskHigh + AskLow + AskClose)/3,
Mean.OHLC = (AskOpen + AskHigh + AskLow + AskClose)/4,
bias.open = sum(bias.open)/length(bias.open),
bias.high = sum(bias.high)/length(bias.high),
bias.low = sum(bias.low)/length(bias.low),
bias.close = sum(bias.close)/length(bias.close)) %>% tbl_df
data_tm1_1_tidyr %>%
kable(caption = 'MSE') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%')#, height = '400px')
```
## 10% 缺失值
### 1分钟数据
以下使用`imputeTS::na.seadec()`弥补10%数据缺失值。
```{r warning=FALSE}
data_m1_NA <- data_m1 %>%
dplyr::select(BidOpen, BidHigh, BidLow, BidClose,
AskOpen, AskHigh, AskLow, AskClose) %>%
prodNA(noNA = 0.1) %>%
cbind(data_m1[1], .) %>% tbl_df
data_m1_10_impTS <- llply(algo, function(x) {
data_m1_NA %>%
dplyr::select(starts_with('Ask'), starts_with('Bid')) %>%
map(na.seadec, algorithm = x) %>% as.tibble
})
names(data_m1_10_impTS) <- algo
data_m1_10_impTS %<>% ldply %>% tbl_df
data_m1_10_impTS %<>% mutate(
bias.open = if_else(AskOpen>AskHigh|AskOpen<AskLow, 1, 0),
bias.high = if_else(AskHigh<AskOpen|AskHigh<AskLow|AskHigh<AskClose, 1, 0),
bias.low = if_else(AskLow>AskOpen|AskLow>AskHigh|AskLow>AskClose, 1, 0),
bias.close = if_else(AskClose>AskHigh|AskClose<AskLow, 1, 0))
data_m1_10_impTS %>%
dplyr::filter(bias.open==1|bias.high==1|bias.low==1|bias.close==1)
data_m1_10_impTS %<>%
ddply(.(.id), summarise,
AskOpen = mean((AskOpen - data_m1$AskOpen)^2),
AskHigh = mean((AskHigh - data_m1$AskHigh)^2),
AskLow = mean((AskLow - data_m1$AskLow)^2),
AskClose = mean((AskClose - data_m1$AskClose)^2),
Mean.HLC = (AskHigh + AskLow + AskClose)/3,
Mean.OHLC = (AskOpen + AskHigh + AskLow + AskClose)/4,
bias.open = sum(bias.open)/length(bias.open),
bias.high = sum(bias.high)/length(bias.high),
bias.low = sum(bias.low)/length(bias.low),
bias.close = sum(bias.close)/length(bias.close)) %>% tbl_df
data_m1_10_impTS %>%
kable(caption = 'MSE 10% 缺失值') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%')#, height = '400px')
```
以下使用`Amelia::amelia()`弥补10%数据缺失值。
```{r warning=FALSE}
data_m1_10_amelia <- data_m1_NA %>%
amelia %>%
.$imputations %>%
ldply %>% tbl_df
data_m1_10_amelia %>% anyNA
data_m1_10_amelia %<>% mutate(
bias.open = if_else(AskOpen>AskHigh|AskOpen<AskLow, 1, 0),
bias.high = if_else(AskHigh<AskOpen|AskHigh<AskLow|AskHigh<AskClose, 1, 0),
bias.low = if_else(AskLow>AskOpen|AskLow>AskHigh|AskLow>AskClose, 1, 0),
bias.close = if_else(AskClose>AskHigh|AskClose<AskLow, 1, 0))
data_m1_10_amelia %>%
dplyr::filter(bias.open==1|bias.high==1|bias.low==1|bias.close==1)
data_m1_10_amelia %<>%
ddply(.(.id), summarise,
AskOpen = mean((AskOpen - data_m1$AskOpen)^2),
AskHigh = mean((AskHigh - data_m1$AskHigh)^2),
AskLow = mean((AskLow - data_m1$AskLow)^2),
AskClose = mean((AskClose - data_m1$AskClose)^2),
Mean.HLC = (AskHigh + AskLow + AskClose)/3,
Mean.OHLC = (AskOpen + AskHigh + AskLow + AskClose)/4,
bias.open = sum(bias.open)/length(bias.open),
bias.high = sum(bias.high)/length(bias.high),
bias.low = sum(bias.low)/length(bias.low),
bias.close = sum(bias.close)/length(bias.close)) %>% tbl_df
data_m1_10_amelia %>%
kable(caption = 'MSE') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%')#, height = '400px')
```
以下使用`tidyr::fill()`弥补10%数据缺失值。
```{r warning=FALSE}
data_m1_10_tidyr <- data_m1_NA %>%
fill(BidOpen, BidHigh, BidLow, BidClose,
AskOpen, AskHigh, AskLow, AskClose) %>% #default direction down
fill(BidOpen, BidHigh, BidLow, BidClose,
AskOpen, AskHigh, AskLow, AskClose, .direction = 'up')
data_m1_10_tidyr %>% anyNA
data_m1_10_tidyr %<>% mutate(
bias.open = if_else(AskOpen>AskHigh|AskOpen<AskLow, 1, 0),
bias.high = if_else(AskHigh<AskOpen|AskHigh<AskLow|AskHigh<AskClose, 1, 0),
bias.low = if_else(AskLow>AskOpen|AskLow>AskHigh|AskLow>AskClose, 1, 0),
bias.close = if_else(AskClose>AskHigh|AskClose<AskLow, 1, 0))
data_m1_10_tidyr %>%
dplyr::filter(bias.open==1|bias.high==1|bias.low==1|bias.close==1)
data_m1_10_tidyr %<>%
summarise(
AskOpen = mean((AskOpen - data_m1$AskOpen)^2),
AskHigh = mean((AskHigh - data_m1$AskHigh)^2),
AskLow = mean((AskLow - data_m1$AskLow)^2),
AskClose = mean((AskClose - data_m1$AskClose)^2),
Mean.HLC = (AskHigh + AskLow + AskClose)/3,
Mean.OHLC = (AskOpen + AskHigh + AskLow + AskClose)/4,
bias.open = sum(bias.open)/length(bias.open),
bias.high = sum(bias.high)/length(bias.high),
bias.low = sum(bias.low)/length(bias.low),
bias.close = sum(bias.close)/length(bias.close)) %>% tbl_df
data_m1_10_tidyr %>%
kable(caption = 'MSE') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%')#, height = '400px')
```
### Tick数据转为1分钟数据
以下使用`imputeTS::na.seadec()`弥补10%数据缺失值。
```{r warning=FALSE}
data_tm1_NA <- data_tm1 %>%
dplyr::select(BidOpen, BidHigh, BidLow, BidClose,
AskOpen, AskHigh, AskLow, AskClose) %>%
prodNA(noNA = 0.1) %>%
cbind(data_tm1[1], .) %>% tbl_df
data_tm1_10_impTS <- llply(algo, function(x) {
data_tm1_NA %>%
dplyr::select(starts_with('Ask'), starts_with('Bid')) %>%
map(na.seadec, algorithm = x) %>% as.tibble
})
names(data_tm1_10_impTS) <- algo
data_tm1_10_impTS %<>% ldply %>% tbl_df
data_tm1_10_impTS %<>% mutate(
bias.open = if_else(AskOpen>AskHigh|AskOpen<AskLow, 1, 0),
bias.high = if_else(AskHigh<AskOpen|AskHigh<AskLow|AskHigh<AskClose, 1, 0),
bias.low = if_else(AskLow>AskOpen|AskLow>AskHigh|AskLow>AskClose, 1, 0),
bias.close = if_else(AskClose>AskHigh|AskClose<AskLow, 1, 0))
data_tm1_10_impTS %>%
dplyr::filter(bias.open==1|bias.high==1|bias.low==1|bias.close==1)
data_tm1_10_impTS %<>%
ddply(.(.id), summarise,
AskOpen = mean((AskOpen - data_m1$AskOpen)^2),
AskHigh = mean((AskHigh - data_m1$AskHigh)^2),
AskLow = mean((AskLow - data_m1$AskLow)^2),
AskClose = mean((AskClose - data_m1$AskClose)^2),
Mean.HLC = (AskHigh + AskLow + AskClose)/3,
Mean.OHLC = (AskOpen + AskHigh + AskLow + AskClose)/4,
bias.open = sum(bias.open)/length(bias.open),
bias.high = sum(bias.high)/length(bias.high),
bias.low = sum(bias.low)/length(bias.low),
bias.close = sum(bias.close)/length(bias.close)) %>% tbl_df
data_tm1_10_impTS %>%
kable(caption = 'MSE 10% 缺失值') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%')#, height = '400px')
```
以下使用`Amelia::amelia()`弥补10%数据缺失值。
```{r warning=FALSE}
data_tm1_10_amelia <- data_tm1_NA %>%
amelia %>%
.$imputations %>%
ldply %>% tbl_df
data_tm1_10_amelia %>% anyNA
data_tm1_10_amelia %<>% mutate(
bias.open = if_else(AskOpen>AskHigh|AskOpen<AskLow, 1, 0),
bias.high = if_else(AskHigh<AskOpen|AskHigh<AskLow|AskHigh<AskClose, 1, 0),
bias.low = if_else(AskLow>AskOpen|AskLow>AskHigh|AskLow>AskClose, 1, 0),
bias.close = if_else(AskClose>AskHigh|AskClose<AskLow, 1, 0))
data_tm1_10_amelia %>%
dplyr::filter(bias.open==1|bias.high==1|bias.low==1|bias.close==1)
data_tm1_10_amelia %<>%
ddply(.(.id), summarise,
AskOpen = mean((AskOpen - data_m1$AskOpen)^2),
AskHigh = mean((AskHigh - data_m1$AskHigh)^2),
AskLow = mean((AskLow - data_m1$AskLow)^2),
AskClose = mean((AskClose - data_m1$AskClose)^2),
Mean.HLC = (AskHigh + AskLow + AskClose)/3,
Mean.OHLC = (AskOpen + AskHigh + AskLow + AskClose)/4,
bias.open = sum(bias.open)/length(bias.open),
bias.high = sum(bias.high)/length(bias.high),
bias.low = sum(bias.low)/length(bias.low),
bias.close = sum(bias.close)/length(bias.close)) %>% tbl_df
data_tm1_10_amelia %>%
kable(caption = 'MSE') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%')#, height = '400px')
```
以下使用`tidyr::fill()`弥补10%数据缺失值。
```{r warning=FALSE}
data_tm1_10_tidyr <- data_tm1_NA %>%
fill(BidOpen, BidHigh, BidLow, BidClose,
AskOpen, AskHigh, AskLow, AskClose) %>% #default direction down
fill(BidOpen, BidHigh, BidLow, BidClose,
AskOpen, AskHigh, AskLow, AskClose, .direction = 'up')
data_tm1_10_tidyr %>% anyNA
data_tm1_10_tidyr %<>% mutate(
bias.open = if_else(AskOpen>AskHigh|AskOpen<AskLow, 1, 0),
bias.high = if_else(AskHigh<AskOpen|AskHigh<AskLow|AskHigh<AskClose, 1, 0),
bias.low = if_else(AskLow>AskOpen|AskLow>AskHigh|AskLow>AskClose, 1, 0),
bias.close = if_else(AskClose>AskHigh|AskClose<AskLow, 1, 0))
data_tm1_10_tidyr %>%
dplyr::filter(bias.open==1|bias.high==1|bias.low==1|bias.close==1)
data_tm1_10_tidyr %<>%
summarise(
AskOpen = mean((AskOpen - data_m1$AskOpen)^2),
AskHigh = mean((AskHigh - data_m1$AskHigh)^2),
AskLow = mean((AskLow - data_m1$AskLow)^2),
AskClose = mean((AskClose - data_m1$AskClose)^2),
Mean.HLC = (AskHigh + AskLow + AskClose)/3,
Mean.OHLC = (AskOpen + AskHigh + AskLow + AskClose)/4,
bias.open = sum(bias.open)/length(bias.open),
bias.high = sum(bias.high)/length(bias.high),
bias.low = sum(bias.low)/length(bias.low),
bias.close = sum(bias.close)/length(bias.close)) %>% tbl_df
data_tm1_10_tidyr %>%
kable(caption = 'MSE') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%')#, height = '400px')
```
## 20% 缺失值
### 1分钟数据
以下使用`imputeTS::na.seadec()`弥补20%数据缺失值。
```{r warning=FALSE}
data_m1_NA <- data_m1 %>%
dplyr::select(BidOpen, BidHigh, BidLow, BidClose,
AskOpen, AskHigh, AskLow, AskClose) %>%
prodNA(noNA = 0.2) %>%
cbind(data_m1[1], .) %>% tbl_df
data_m1_20_impTS <- llply(algo, function(x) {
data_m1_NA %>%
dplyr::select(starts_with('Ask'), starts_with('Bid')) %>%
map(na.seadec, algorithm = x) %>% as.tibble
})
names(data_m1_20_impTS) <- algo
data_m1_20_impTS %<>% ldply %>% tbl_df
data_m1_20_impTS %<>% mutate(
bias.open = if_else(AskOpen>AskHigh|AskOpen<AskLow, 1, 0),
bias.high = if_else(AskHigh<AskOpen|AskHigh<AskLow|AskHigh<AskClose, 1, 0),
bias.low = if_else(AskLow>AskOpen|AskLow>AskHigh|AskLow>AskClose, 1, 0),
bias.close = if_else(AskClose>AskHigh|AskClose<AskLow, 1, 0))
data_m1_20_impTS %>%
dplyr::filter(bias.open==1|bias.high==1|bias.low==1|bias.close==1)
data_m1_20_impTS %<>%
ddply(.(.id), summarise,
AskOpen = mean((AskOpen - data_m1$AskOpen)^2),
AskHigh = mean((AskHigh - data_m1$AskHigh)^2),
AskLow = mean((AskLow - data_m1$AskLow)^2),
AskClose = mean((AskClose - data_m1$AskClose)^2),
Mean.HLC = (AskHigh + AskLow + AskClose)/3,
Mean.OHLC = (AskOpen + AskHigh + AskLow + AskClose)/4,
bias.open = sum(bias.open)/length(bias.open),
bias.high = sum(bias.high)/length(bias.high),
bias.low = sum(bias.low)/length(bias.low),
bias.close = sum(bias.close)/length(bias.close)) %>% tbl_df
data_m1_20_impTS %>%
kable(caption = 'MSE 20% 缺失值') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%')#, height = '400px')
```
以下使用`Amelia::amelia()`弥20%数据缺失值。
```{r warning=FALSE}
data_m1_20_amelia <- data_m1_NA %>%
amelia %>%
.$imputations %>%
ldply %>% tbl_df
data_m1_20_amelia %>% anyNA
data_m1_20_amelia %<>% mutate(
bias.open = if_else(AskOpen>AskHigh|AskOpen<AskLow, 1, 0),
bias.high = if_else(AskHigh<AskOpen|AskHigh<AskLow|AskHigh<AskClose, 1, 0),
bias.low = if_else(AskLow>AskOpen|AskLow>AskHigh|AskLow>AskClose, 1, 0),
bias.close = if_else(AskClose>AskHigh|AskClose<AskLow, 1, 0))
data_m1_20_amelia %>%
dplyr::filter(bias.open==1|bias.high==1|bias.low==1|bias.close==1)
data_m1_20_amelia %<>%
ddply(.(.id), summarise,
AskOpen = mean((AskOpen - data_m1$AskOpen)^2),
AskHigh = mean((AskHigh - data_m1$AskHigh)^2),
AskLow = mean((AskLow - data_m1$AskLow)^2),
AskClose = mean((AskClose - data_m1$AskClose)^2),
Mean.HLC = (AskHigh + AskLow + AskClose)/3,
Mean.OHLC = (AskOpen + AskHigh + AskLow + AskClose)/4,
bias.open = sum(bias.open)/length(bias.open),
bias.high = sum(bias.high)/length(bias.high),
bias.low = sum(bias.low)/length(bias.low),
bias.close = sum(bias.close)/length(bias.close)) %>% tbl_df
data_m1_20_amelia %>%
kable(caption = 'MSE') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%')#, height = '400px')
```
以下使用`tidyr::fill()`弥补20%数据缺失值。
```{r warning=FALSE}
data_m1_20_tidyr <- data_m1_NA %>%
fill(BidOpen, BidHigh, BidLow, BidClose,
AskOpen, AskHigh, AskLow, AskClose) %>% #default direction down
fill(BidOpen, BidHigh, BidLow, BidClose,
AskOpen, AskHigh, AskLow, AskClose, .direction = 'up')
data_m1_20_tidyr %>% anyNA
data_m1_20_tidyr %<>% mutate(
bias.open = if_else(AskOpen>AskHigh|AskOpen<AskLow, 1, 0),
bias.high = if_else(AskHigh<AskOpen|AskHigh<AskLow|AskHigh<AskClose, 1, 0),
bias.low = if_else(AskLow>AskOpen|AskLow>AskHigh|AskLow>AskClose, 1, 0),
bias.close = if_else(AskClose>AskHigh|AskClose<AskLow, 1, 0))
data_m1_20_tidyr %>%
dplyr::filter(bias.open==1|bias.high==1|bias.low==1|bias.close==1)
data_m1_20_tidyr %<>%
summarise(
AskOpen = mean((AskOpen - data_m1$AskOpen)^2),
AskHigh = mean((AskHigh - data_m1$AskHigh)^2),
AskLow = mean((AskLow - data_m1$AskLow)^2),
AskClose = mean((AskClose - data_m1$AskClose)^2),
Mean.HLC = (AskHigh + AskLow + AskClose)/3,
Mean.OHLC = (AskOpen + AskHigh + AskLow + AskClose)/4,
bias.open = sum(bias.open)/length(bias.open),
bias.high = sum(bias.high)/length(bias.high),
bias.low = sum(bias.low)/length(bias.low),
bias.close = sum(bias.close)/length(bias.close)) %>% tbl_df
data_m1_20_tidyr %>%
kable(caption = 'MSE') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%')#, height = '400px')
```
### Tick数据转为1分钟数据
以下使用`imputeTS::na.seadec()`弥补20%数据缺失值。
```{r warning=FALSE}
data_tm1_NA <- data_tm1 %>%
dplyr::select(BidOpen, BidHigh, BidLow, BidClose,
AskOpen, AskHigh, AskLow, AskClose) %>%
prodNA(noNA = 0.2) %>%
cbind(data_tm1[1], .) %>% tbl_df
data_tm1_20_impTS <- llply(algo, function(x) {
data_tm1_NA %>%
dplyr::select(starts_with('Ask'), starts_with('Bid')) %>%
map(na.seadec, algorithm = x) %>% as.tibble
})
names(data_tm1_20_impTS) <- algo
data_tm1_20_impTS %<>% ldply %>% tbl_df
data_tm1_20_impTS %<>% mutate(
bias.open = if_else(AskOpen>AskHigh|AskOpen<AskLow, 1, 0),
bias.high = if_else(AskHigh<AskOpen|AskHigh<AskLow|AskHigh<AskClose, 1, 0),
bias.low = if_else(AskLow>AskOpen|AskLow>AskHigh|AskLow>AskClose, 1, 0),
bias.close = if_else(AskClose>AskHigh|AskClose<AskLow, 1, 0))
data_tm1_20_impTS %>%
dplyr::filter(bias.open==1|bias.high==1|bias.low==1|bias.close==1)
data_tm1_20_impTS %<>%
ddply(.(.id), summarise,
AskOpen = mean((AskOpen - data_m1$AskOpen)^2),
AskHigh = mean((AskHigh - data_m1$AskHigh)^2),
AskLow = mean((AskLow - data_m1$AskLow)^2),
AskClose = mean((AskClose - data_m1$AskClose)^2),
Mean.HLC = (AskHigh + AskLow + AskClose)/3,
Mean.OHLC = (AskOpen + AskHigh + AskLow + AskClose)/4,
bias.open = sum(bias.open)/length(bias.open),
bias.high = sum(bias.high)/length(bias.high),
bias.low = sum(bias.low)/length(bias.low),
bias.close = sum(bias.close)/length(bias.close)) %>% tbl_df
data_tm1_20_impTS %>%
kable(caption = 'MSE 20% 缺失值') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%')#, height = '400px')
```
以下使用`Amelia::amelia()`弥补20%数据缺失值。
```{r warning=FALSE}
data_tm1_20_amelia <- data_tm1_NA %>%
amelia %>%
.$imputations %>%
ldply %>% tbl_df
data_tm1_20_amelia %>% anyNA
data_tm1_20_amelia %<>% mutate(
bias.open = if_else(AskOpen>AskHigh|AskOpen<AskLow, 1, 0),
bias.high = if_else(AskHigh<AskOpen|AskHigh<AskLow|AskHigh<AskClose, 1, 0),
bias.low = if_else(AskLow>AskOpen|AskLow>AskHigh|AskLow>AskClose, 1, 0),
bias.close = if_else(AskClose>AskHigh|AskClose<AskLow, 1, 0))
data_tm1_20_amelia %>%
dplyr::filter(bias.open==1|bias.high==1|bias.low==1|bias.close==1)
data_tm1_20_amelia %<>%
ddply(.(.id), summarise,
AskOpen = mean((AskOpen - data_m1$AskOpen)^2),
AskHigh = mean((AskHigh - data_m1$AskHigh)^2),
AskLow = mean((AskLow - data_m1$AskLow)^2),
AskClose = mean((AskClose - data_m1$AskClose)^2),
Mean.HLC = (AskHigh + AskLow + AskClose)/3,
Mean.OHLC = (AskOpen + AskHigh + AskLow + AskClose)/4,
bias.open = sum(bias.open)/length(bias.open),
bias.high = sum(bias.high)/length(bias.high),
bias.low = sum(bias.low)/length(bias.low),
bias.close = sum(bias.close)/length(bias.close)) %>% tbl_df
data_tm1_20_amelia %>%
kable(caption = 'MSE') %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%')#, height = '400px')
```
以下使用`tidyr::fill()`弥补20%数据缺失值。
```{r warning=FALSE}
data_tm1_20_tidyr <- data_tm1_NA %>%