-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation_code_new.Rmd
More file actions
1542 lines (1199 loc) · 58.9 KB
/
simulation_code_new.Rmd
File metadata and controls
1542 lines (1199 loc) · 58.9 KB
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
```{r}
#install.packages("CondIndTests")
#install.packages("GeneralisedCovarianceMeasure")
#install.packages("ppcor")
library("CondIndTests")
library("GeneralisedCovarianceMeasure")
library("ppcor")
library("ggplot2")
library("boot")
```
```{r}
##Adding in kernel width based on the median of the data as in RCIT and paper I read.
##We let GCM cross validate regression parameter lambda (sigma in my essay) as it wants to, but use the same kernel width as KCI
normalize <- function(mat){
if (is.null(nrow(mat))){mat = matrix(mat);}
mat = apply(mat, 2, function(x) if (sd(x)>0){(x - mean(x)) / sd(x)} else{x-mean(x);})
}
GCM <- function(X,Y,Z){
X=normalize(X);
Y=normalize(Y);
Z=normalize(Z);
if (length(X)>500){
width = median(as.vector(dist(cbind(X[1:500],Y[1:500]))));
}
else {
width = median(as.vector(dist(cbind(X,Y))));
}
gcm.test(X, Y, Z, regr.method = "kernel.ridge", regr.pars = list("sigma" = width))
}
KCIT <- function(X, Y, Z, GP = FALSE){
X=normalize(X);
Y=normalize(Y);
Z=normalize(Z);
if (length(X)>500){
width = median(as.vector(dist(cbind(X[1:500],Y[1:500]))));
}
else {
width = median(as.vector(dist(cbind(X,Y))));
}
KCI(X,Y,Z, gammaApprox = FALSE, width = width, GP = GP)
}
```
```{r}
#CONDITIONALLY INDEPENDENT DATA GENERATION PROCEDURES
linear_model <- function(n, p = 1, a = NULL){
#Generates X = bZ + e, Y = cZ + e
#n = number of datapoints generated, p = dim(Z)
#b,c are random vectors of dimension p, each entry ~Unif[-coef_size, coef_size]
#error_stdev controls stdev of e
#if same_coef==TRUE, set b=c
err_X <- rnorm(n)
err_Y <- rnorm(n)
Z <- c()
for (i in 1:p){
Z <- cbind(Z, rnorm(n))
}
beta_1 <- runif(n=p, min=-5, max=5)
beta_2 <- runif(n=p, min=-5, max=5)
X <- Z%*%beta_1 + err_X
Y <- Z%*%beta_2 + err_Y
vars <- list(X, Y, Z)
return(vars)
}
nonlinear_functions <- list(function(x){return(x^2)},
function(x){return(x^3)},
tanh,
function(x){return(exp(-abs(x)))})
nonlinear_model <- function(n, p = 1, a = NULL){
err_X <- rnorm(n)
err_Y <- rnorm(n)
Z <- c()
for (i in 1:p){
Z <- cbind(Z, rnorm(n))
}
#Selecting random regression function
random_nums <- sample(length(nonlinear_functions),2)
random_func1 <- nonlinear_functions[[random_nums[[1]]]]
random_func2 <- nonlinear_functions[[random_nums[[2]]]]
X <- random_func1(rowMeans(Z) + err_X)
Y <- random_func2(rowMeans(Z) + err_Y)
vars <- list(X, Y, Z)
return(vars)
}
Z_error_model <- function(n, p = NULL, a = NULL){
err_X <- rnorm(n)
err_Y <- rnorm(n)
Z <- rnorm(n)
X <- Z*err_X
Y <- Z*err_Y
vars <- list(X,Y,Z)
return(vars)
}
###TRYING TO SHOW NONUNIFORM CONVERGENCE OF KCI COMPARED TO GCM
wavepacket <- function(x, a){
return(exp(-x^2/2)*sin(a*x))
}
nonconvergent_model1 <- function(n, p = NULL, a = NULL){
err_X <- rnorm(n)
err_Y <- rnorm(n)
Z <- rnorm(n)
X <- wavepacket(Z, 2) + 0.4*err_X
Y <- wavepacket(Z, 2) + 0.4*err_Y
vars <- list(X,Y,Z)
return(vars)
}
nonconvergent_model2<- function(n, p = NULL, a = NULL){
err_X <- rnorm(n)
err_Y <- rnorm(n)
Z <- rnorm(n)
X <- wavepacket(Z,2)*err_X
Y <- wavepacket(Z,2)*err_Y
vars <- list(X,Y,Z)
return(vars)
}
nonconvergent_model3<- function(n, p = NULL, a = NULL){
err_X <- rnorm(n)
err_Y <- rnorm(n)
Z <- rnorm(n)
X <- cos(2*Z)*err_X
Y <- cos(2*Z)*err_Y
vars <- list(X,Y,Z)
return(vars)
}
nonconvergent_model4<- function(n, p = NULL, a = NULL){
err_X <- rnorm(n)
err_Y <- rnorm(n)
Z1 <- rnorm(n)
Z2 <- rnorm(n)
Z <- cbind(Z1, Z2)
X <- (wavepacket(Z1, 2) + wavepacket(Z2, 2))*err_X
Y <- (wavepacket(Z1, 2) - wavepacket(Z2, 2))*err_Y
vars <- list(X,Y,Z)
return(vars)
}
```
```{r}
#CONDITIONALLY DEPENDENT DATA GENERATION PROCEDURES
linear_model2 <- function(n, p = 1, a = NULL){
#Generates X = bZ + e, Y = cZ + e
#n = number of datapoints generated, p = dim(Z)
#b,c are random vectors of dimension p, each entry ~Unif[-coef_size, coef_size]
#error_stdev controls stdev of e
#if same_coef==TRUE, set b=c
err_X <- error_stdev*rnorm(n)
err_Y <- error_stdev*rnorm(n)
err <- error_stdev*0.7*rnorm(n)
Z <- c()
for (i in 1:p){
Z <- cbind(Z, rnorm(n))
}
beta_1 <- runif(n=p, min=-5, max=5)
beta_2 <- runif(n=p, min=-5, max=5)
X <- Z%*%beta_1 + err_X + err
Y <- Z%*%beta_2 + err_Y + err
vars <- list(X, Y, Z)
return(vars)
}
nonlinear_model2 <- function(n, p = 1, a = NULL){
err_X <- rnorm(n)
err_Y <-rnorm(n)
err <- 0.7*rnorm(n)
Z <- c()
for (i in 1:p){
Z <- cbind(Z, rnorm(n))
}
#Selecting random regression function
random_nums <- sample(length(nonlinear_functions),2)
random_func1 <- nonlinear_functions[[random_nums[[1]]]]
random_func2 <- nonlinear_functions[[random_nums[[2]]]]
X <- random_func1(rowMeans(Z) + err_X + err)
Y <- random_func2(rowMeans(Z) + err_Y + err)
vars <- list(X, Y, Z)
return(vars)
}
zero_cond_covariance1 <- function(n, p = NULL, a = NULL){
Z <- rnorm(n)
U <- runif(n, min = -1, max = 1)
X <- U*Z
Y <- X^2 + Z
vars <- list(X, Y, Z)
return(vars)
}
zero_cond_covariance2 <- function(n, p = NULL, a = NULL){
Z <- rnorm(n)
U <- runif(n, min = 0, max = 2*pi)
Y <- Z*sin(U)
X <- Z*cos(U)
vars <- list(X,Y,Z)
return(vars)
}
zero_cond_covariance3 <- function(n,p = NULL, a = NULL){
err_X <- rnorm(n)
err_Y <- rnorm(n)
Z <- rnorm(n)
X <- Z*sinh(err_X)
Y <- cos(X) + err_Y
vars <- list(X, Y, Z)
return(vars)
}
zero_cond_covariance4 <- function(n, p = NULL, a = NULL){
err_X <- rnorm(n)
err_Y <- rnorm(n)
Z <- rnorm(n)
X <- Z^3*sin(err_X)
Y <- exp(-X^2)*cos(X)
vars <- list(X, Y, Z)
return(vars)
}
Z_error_model2 <- function(n, p = NULL, a = NULL){
err_X <- rnorm(n)
err_Y <- rnorm(n)
err <- 0.7*rnorm(n)
Z <- rnorm(n)
X <- Z*(err_X + err)
Y <- Z*(err_Y + err)
vars <- list(X,Y,Z)
return(vars)
}
```
```{r}
#Generates 100 sets of n datapoints using the data generation procedure func, and generates p values and cpu times
#for KCI, GCM and pcor for each of the 100 sets. p is the dimension of Z, a is now a depreciated parameter which has no influence.
simulate <- function(N = 100, n, func, p = 1, a = NULL){
p_values_KCI <- c()
p_values_GCM <- c()
p_values_pcor <- c()
KCI_times <- c()
GCM_times <- c()
pcor_times <- c()
for (i in 1:N){
if( i %% 5 == 0){
print(i)
}
vars <- func(n, p, a)
X <- vars[[1]]
Y <- vars[[2]]
Z <- vars[[3]]
#KCI
KCI_start_time <- Sys.time()
KCI_result <- KCIT(X, Y, Z, GP = FALSE)
KCI_end_time <- Sys.time()
KCI_times <- c(KCI_times, KCI_end_time - KCI_start_time)
p_values_KCI <- c(p_values_KCI, KCI_result$pvalue)
#GCM
GCM_start_time <- Sys.time()
GCM_result <- GCM(X, Y, Z)
GCM_end_time <- Sys.time()
GCM_times <- c(GCM_times, GCM_end_time - GCM_start_time)
p_values_GCM <- c(p_values_GCM, GCM_result$p.value)
#PCOR
pcor_start_time <- Sys.time()
pcor_result <- pcor.test(X, Y, Z)
pcor_end_time <- Sys.time()
pcor_times <- c(pcor_times, pcor_end_time - pcor_start_time)
p_values_pcor <- c(p_values_pcor, pcor_result$p.value)
}
output <- list(p_values_KCI, p_values_GCM, p_values_pcor, KCI_times, GCM_times, pcor_times)
return(output)
}
#same as function simulate, but only uses KCI with GP regression to generate p values.
simulateGP <- function(N = 100, n, func, p = 1, a = NULL){
p_values_KCI <- c()
KCI_times <- c()
for (i in 1:N){
if( i %% 5 == 0){
print(i)
}
vars <- func(n, p, a)
X <- vars[[1]]
Y <- vars[[2]]
Z <- vars[[3]]
#KCI
KCI_start_time <- Sys.time()
KCI_result <- KCIT(X, Y, Z, GP = TRUE)
KCI_end_time <- Sys.time()
KCI_times <- c(KCI_times, KCI_end_time - KCI_start_time)
p_values_KCI <- c(p_values_KCI, KCI_result$pvalue)
}
output <- list(p_values_KCI, KCI_times)
return(output)
}
```
```{r}
#install.packages("DescTools")
library("DescTools")
#install.packages("gridExtra")
library("gridExtra")
#Computes AUPC of a test given its p values
AUPC <- function(p_values){
x <- c(p_values, 1)
y <- c(ecdf(p_values)(p_values), 1)
return(AUC(x,y))
}
```
```{r}
#computes the rejection rate
rej_rate <- function(p_values, indices){
p_values <- p_values[indices]
return(sum(p_values <= 0.05)/sum(p_values <=1))
}
#computes the mean
my_mean <- function(p_values, indices){
return(mean(p_values[indices]))
}
#outputs ks statistic, ks p value, rejection rate, cpu time, aupc, and 95% bootstrapped confidence intervals
diagnostics <- function(p_values, times = NULL, bootstrap = TRUE){
n <- length(p_values)
kstest <- ks.test(p_values, punif)
rejection_rate <- rej_rate(p_values, 1:length(p_values))
if (bootstrap){
bootstrapped_rejection_rate <- boot(p_values, rej_rate, R = 10000)
rejection_CI <- boot.ci(bootstrapped_rejection_rate, type = "bca")
rejection_CI <- list(rejection_CI$bca[[4]], rejection_CI$bca[[5]])
}
else{
rejection_CI <- NULL
}
if (!is.null(times)){
average_time <- mean(times)
if (bootstrap){
bootstrapped_average_time <- boot(times, my_mean, R = 10000)
time_CI <- boot.ci(bootstrapped_average_time, type = "bca")
time_CI <- list(time_CI$bca[[4]], time_CI$bca[[5]])
}
else{
time_CI <- NULL
}
#ks statistic, ks p value, rejection rate, rejection 95% confidence interval, average time, average time 95%
#confidence interval, #AUPC
outputs <- list(kstest[[1]], kstest[[2]], rejection_rate, rejection_CI, average_time, time_CI, AUPC(p_values))
}
else{
outputs <- list(kstest[[1]], kstest[[2]], rejection_rate, rejection_CI, NULL, NULL, AUPC(p_values))
}
return(outputs)
}
```
```{r}
#5.2: LINEAR VS NONLINEAR
funcs <- c(linear_model, nonlinear_model, Z_error_model)
#Data matrices we'll store the p values in
linear_mat <- matrix(0, nrow = 3, ncol = 100)
nonlinear_mat <- matrix(0, nrow = 3, ncol = 100)
Zerror_mat <- matrix(0, nrow = 3, ncol = 100)
#Data matrices we'll store rejection rates in
linear_rej <- matrix(0, nrow = 3, ncol = 3)
nonlinear_rej <- matrix(0, nrow = 3, ncol = 3)
Zerror_rej <- matrix(0, nrow = 3, ncol = 3)
#Data matrices we'll store ks statistic and p-value in
linear_ks <- matrix(0, nrow = 3, ncol = 2)
nonlinear_ks <- matrix(0, nrow = 3, ncol = 2)
Zerror_ks <- matrix(0, nrow = 3, ncol = 2)
for (i in c(1,2,3)) {
#Simulating p values
results <- simulate(100, 200, funcs[[i]])
#Computing rejection rates with bootstrapped confidence intervals, and ks statistic
type1_resultsKCI <- diagnostics(results[[1]], results[[4]])
type1_resultsGCM <- diagnostics(results[[2]], results[[5]])
type1_resultspcor <- diagnostics(results[[3]], results[[6]])
if (i == 1){
#p values
linear_mat[1,] <- results[[1]]
linear_mat[2,] <- results[[2]]
linear_mat[3,] <- results[[3]]
#lower 95% rejection rate, rejection rate, and upper 95% rejection rate
linear_rej[1,] <- c(type1_resultsKCI[[4]][[1]], type1_resultsKCI[[3]], type1_resultsKCI[[4]][[2]])
linear_rej[2,] <- c(type1_resultsGCM[[4]][[1]], type1_resultsGCM[[3]], type1_resultsGCM[[4]][[2]])
linear_rej[3,] <- c(type1_resultspcor[[4]][[1]], type1_resultspcor[[3]], type1_resultspcor[[4]][[2]])
#ks statistic, and p value
linear_ks[1,] <- c(type1_resultsKCI[[1]], type1_resultsKCI[[2]])
linear_ks[2,] <- c(type1_resultsGCM[[1]], type1_resultsGCM[[2]])
linear_ks[3,] <- c(type1_resultspcor[[1]], type1_resultspcor[[2]])
}
else if(i==2){
#p values
nonlinear_mat[1,] <- results[[1]]
nonlinear_mat[2,] <- results[[2]]
nonlinear_mat[3,] <- results[[3]]
#lower 95% rejection rate, rejection rate, and upper 95% rejection rate
nonlinear_rej[1,] <- c(type1_resultsKCI[[4]][[1]], type1_resultsKCI[[3]], type1_resultsKCI[[4]][[2]])
nonlinear_rej[2,] <- c(type1_resultsGCM[[4]][[1]], type1_resultsGCM[[3]], type1_resultsGCM[[4]][[2]])
nonlinear_rej[3,] <- c(type1_resultspcor[[4]][[1]], type1_resultspcor[[3]], type1_resultspcor[[4]][[2]])
#ks statistic, and p value
nonlinear_ks[1,] <- c(type1_resultsKCI[[1]], type1_resultsKCI[[2]])
nonlinear_ks[2,] <- c(type1_resultsGCM[[1]], type1_resultsGCM[[2]])
nonlinear_ks[3,] <- c(type1_resultspcor[[1]], type1_resultspcor[[2]])
}
else{
#p values
Zerror_mat[1,] <- results[[1]]
Zerror_mat[2,] <- results[[2]]
Zerror_mat[3,] <- results[[3]]
#lower 95% rejection rate, rejection rate, and upper 95% rejection rate
Zerror_rej[1,] <- c(type1_resultsKCI[[4]][[1]], type1_resultsKCI[[3]], type1_resultsKCI[[4]][[2]])
Zerror_rej[2,] <- c(type1_resultsGCM[[4]][[1]], type1_resultsGCM[[3]], type1_resultsGCM[[4]][[2]])
Zerror_rej[3,] <- c(type1_resultspcor[[4]][[1]], type1_resultspcor[[3]], type1_resultspcor[[4]][[2]])
#ks statistic, and p value
Zerror_ks[1,] <- c(type1_resultsKCI[[1]], type1_resultsKCI[[2]])
Zerror_ks[2,] <- c(type1_resultsGCM[[1]], type1_resultsGCM[[2]])
Zerror_ks[3,] <- c(type1_resultspcor[[1]], type1_resultspcor[[2]])
}
}
```
```{r}
#Putting the data in a form that ggplot can read
barcharts <- data.frame(Test = as.factor(c(rep('KCI',100),rep('GCM', 100),rep('pcor',100),rep('KCI',100),rep('GCM', 100),rep('pcor',100), rep('KCI',100),rep('GCM', 100),rep('pcor',100))),
model = as.factor(c(rep('Linear', 300), rep('Nonlinear', 300), rep('Zerror', 300))),
p_values = c(linear_mat[1,], linear_mat[2,], linear_mat[3,], nonlinear_mat[1,], nonlinear_mat[2,], nonlinear_mat[3,], Zerror_mat[1,], Zerror_mat[2,], Zerror_mat[3,]))
rejection_charts <- data.frame(Test = as.factor(c(rep('KCI',3), rep('GCM',3), rep('pcor',3))),
model = as.factor(c('Linear', 'Nonlinear', 'Zerror', 'Linear', 'Nonlinear', 'Zerror', 'Linear', 'Nonlinear', 'Zerror')),
rej_rate = c(linear_rej[1,2], nonlinear_rej[1,2], Zerror_rej[1,2], linear_rej[2,2], nonlinear_rej[2,2], Zerror_rej[2,2], linear_rej[3,2], nonlinear_rej[3,2], Zerror_rej[3,2]),
lower_rej = c(linear_rej[1,1], nonlinear_rej[1,1], Zerror_rej[1,1], linear_rej[2,1], nonlinear_rej[2,1], Zerror_rej[2,1], linear_rej[3,1], nonlinear_rej[3,1], Zerror_rej[3,1]),
upper_rej = c(linear_rej[1,3], nonlinear_rej[1,3], Zerror_rej[1,3], linear_rej[2,3], nonlinear_rej[2,3], Zerror_rej[2,3], linear_rej[3,3], nonlinear_rej[3,3], Zerror_rej[3,3]))
```
```{r}
#Constructing the plots and saving
dat_text <- data.frame(
Test = c("KCI", "GCM", "pcor", "KCI", "GCM", "pcor", "KCI", "GCM", "pcor"),
model = c("Linear", "Linear", "Linear", "Nonlinear", "Nonlinear", "Nonlinear", "Zerror", "Zerror", "Zerror"),
label = c(paste("ks stat = ", toString(round(linear_ks[1,1], 3)), ", ks p-value = ", toString(round(linear_ks[1,2], 3))),
paste("ks stat = ", toString(round(linear_ks[2,1],3)), ", ks p-value = ", toString(round(linear_ks[2,2],3))),
paste("ks stat = ", toString(round(linear_ks[3,1],3)), ", ks p-value = ", toString(round(linear_ks[3,2],3))),
paste("ks stat = ", toString(round(nonlinear_ks[1,1],3)), ", ks p-value = ", toString(round(nonlinear_ks[1,2],3))),
paste("ks stat = ", toString(round(nonlinear_ks[2,1],3)), ", ks p-value = ", toString(round(nonlinear_ks[2,2],3))),
paste("ks stat = ", toString(round(nonlinear_ks[3,1],3)), ", ks p-value = ", toString(round(nonlinear_ks[3,2],3))),
paste("ks stat = ", toString(round(Zerror_ks[1,1],3)), ", ks p-value = ", toString(round(Zerror_ks[1,2],3))),
paste("ks stat = ", toString(round(Zerror_ks[2,1],3)), ", ks p-value = ", toString(round(Zerror_ks[2,2],3))),
paste("ks stat = ", toString(round(Zerror_ks[3,1],3)), ", ks p-value = ", toString(round(Zerror_ks[3,2],3)))
)
)
g4 <- ggplot(data = barcharts, aes(x = p_values))+
geom_histogram(binwidth = 0.1, color = "white", boundary = 0) +
labs(x = 'p-values', y = 'Count') +
facet_grid(Test ~ model) +
geom_text(
size = 3,
data = dat_text,
mapping = aes(x = Inf, y = Inf, label = label),
hjust = 1.05,
vjust = 1.5
)
g4
g5 <- ggplot(data = rejection_charts, aes(x=model, y=rej_rate, fill=Test)) +
labs(x = 'Model', y = 'Rejection Rate') +
geom_bar(stat="identity", color="white", position=position_dodge())+
theme_minimal() +
scale_fill_manual(values=c('#003f5c','#bc5090', '#58508d')) +
geom_errorbar(aes(ymin=lower_rej, ymax=upper_rej), width=.1,
position=position_dodge(.9))
g5
ggsave('linvnon_type1_pvalues.png', g4, 'png')
ggsave('linvnon_type1_rej.png', g5, 'png')
```
```{r}
#5.12 LINEAR VS NONLINEAR CONDITIONALLY DEPENDEDENT
funcs2 <- c(linear_model2, nonlinear_model2, Z_error_model2)
#Data matrices we'll store the p values in
linear_mat2 <- matrix(0, nrow = 3, ncol = 100)
nonlinear_mat2 <- matrix(0, nrow = 3, ncol = 100)
Zerror_mat2 <- matrix(0, nrow = 3, ncol = 100)
#Data matrices we'll store rejection rates in
linear_rej2 <- matrix(0, nrow = 3, ncol = 3)
nonlinear_rej2 <- matrix(0, nrow = 3, ncol = 3)
Zerror_rej2 <- matrix(0, nrow = 3, ncol = 3)
#Data matrices we'll store AUPC in
linear_AUPC <- matrix(0, nrow = 3, ncol = 1)
nonlinear_AUPC <- matrix(0, nrow = 3, ncol = 1)
Zerror_AUPC <- matrix(0, nrow = 3, ncol = 1)
for (i in c(1,2,3)) {
#Simulating p values
results <- simulate(100, 200, funcs2[[i]])
#Computing rejection rates with bootstrapped confidence intervals, and ks statistic
type2_resultsKCI <- diagnostics(results[[1]], results[[4]])
type2_resultsGCM <- diagnostics(results[[2]], results[[5]])
type2_resultspcor <- diagnostics(results[[3]], results[[6]])
if (i == 1){
#p values
linear_mat2[1,] <- results[[1]]
linear_mat2[2,] <- results[[2]]
linear_mat2[3,] <- results[[3]]
#lower 95% rejection rate, rejection rate, and upper 95% rejection rate
linear_rej2[1,] <- c(type2_resultsKCI[[4]][[1]], type2_resultsKCI[[3]], type2_resultsKCI[[4]][[2]])
linear_rej2[2,] <- c(type2_resultsGCM[[4]][[1]], type2_resultsGCM[[3]], type2_resultsGCM[[4]][[2]])
linear_rej2[3,] <- c(type2_resultspcor[[4]][[1]], type2_resultspcor[[3]], type2_resultspcor[[4]][[2]])
#AUPC
linear_AUPC[1,] <- type2_resultsKCI[[7]]
linear_AUPC[2,] <- type2_resultsGCM[[7]]
linear_AUPC[3,] <- type2_resultspcor[[7]]
}
else if(i==2){
#p values
nonlinear_mat2[1,] <- results[[1]]
nonlinear_mat2[2,] <- results[[2]]
nonlinear_mat2[3,] <- results[[3]]
#lower 95% rejection rate, rejection rate, and upper 95% rejection rate
nonlinear_rej2[1,] <- c(type2_resultsKCI[[4]][[1]], type2_resultsKCI[[3]], type2_resultsKCI[[4]][[2]])
nonlinear_rej2[2,] <- c(type2_resultsGCM[[4]][[1]], type2_resultsGCM[[3]], type2_resultsGCM[[4]][[2]])
nonlinear_rej2[3,] <- c(type2_resultspcor[[4]][[1]], type2_resultspcor[[3]], type2_resultspcor[[4]][[2]])
#AUPC
nonlinear_AUPC[1,] <- type2_resultsKCI[[7]]
nonlinear_AUPC[2,] <- type2_resultsGCM[[7]]
nonlinear_AUPC[3,] <- type2_resultspcor[[7]]
}
else{
#p values
Zerror_mat2[1,] <- results[[1]]
Zerror_mat2[2,] <- results[[2]]
Zerror_mat2[3,] <- results[[3]]
#lower 95% rejection rate, rejection rate, and upper 95% rejection rate
Zerror_rej2[1,] <- c(type1_resultsKCI[[4]][[1]], type2_resultsKCI[[3]], type2_resultsKCI[[4]][[2]])
Zerror_rej2[2,] <- c(type1_resultsGCM[[4]][[1]], type2_resultsGCM[[3]], type2_resultsGCM[[4]][[2]])
Zerror_rej2[3,] <- c(type1_resultspcor[[4]][[1]], type2_resultspcor[[3]], type2_resultspcor[[4]][[2]])
#AUPC
Zerror_AUPC[1,] <- type2_resultsKCI[[7]]
Zerror_AUPC[2,] <- type2_resultsGCM[[7]]
Zerror_AUPC[3,] <- type2_resultspcor[[7]]
}
}
```
```{r}
#Putting data in a format that ggplot can read
barcharts2 <- data.frame(Test = as.factor(c(rep('KCI',100),rep('GCM', 100),rep('pcor',100),rep('KCI',100),rep('GCM', 100),rep('pcor',100), rep('KCI',100),rep('GCM', 100),rep('pcor',100))),
model = as.factor(c(rep('Linear', 300), rep('Nonlinear', 300), rep('Zerror', 300))),
p_values = c(linear_mat2[1,], linear_mat2[2,], linear_mat2[3,], nonlinear_mat2[1,], nonlinear_mat2[2,], nonlinear_mat2[3,], Zerror_mat2[1,], Zerror_mat2[2,], Zerror_mat2[3,]))
rejection_charts2 <- data.frame(Test = as.factor(c(rep('KCI',3), rep('GCM',3), rep('pcor',3))),
model = as.factor(c('Linear', 'Nonlinear', 'Zerror', 'Linear', 'Nonlinear', 'Zerror', 'Linear', 'Nonlinear', 'Zerror')),
rej_rate = c(linear_rej2[1,2], nonlinear_rej2[1,2], Zerror_rej2[1,2], linear_rej2[2,2], nonlinear_rej2[2,2], Zerror_rej2[2,2], linear_rej2[3,2], nonlinear_rej2[3,2], Zerror_rej2[3,2]),
lower_rej = c(linear_rej2[1,1], nonlinear_rej2[1,1], Zerror_rej2[1,1], linear_rej2[2,1], nonlinear_rej2[2,1], Zerror_rej2[2,1], linear_rej2[3,1], nonlinear_rej2[3,1], Zerror_rej2[3,1]),
upper_rej = c(linear_rej2[1,3], nonlinear_rej2[1,3], Zerror_rej2[1,3], linear_rej2[2,3], nonlinear_rej2[2,3], Zerror_rej2[2,3], linear_rej2[3,3], nonlinear_rej2[3,3], Zerror_rej2[3,3]))
```
```{r}
#plotting and saving
dat_text2 <- data.frame(
Test = c("KCI", "GCM", "pcor", "KCI", "GCM", "pcor", "KCI", "GCM", "pcor"),
model = c("Linear", "Linear", "Linear", "Nonlinear", "Nonlinear", "Nonlinear", "Zerror", "Zerror", "Zerror"),
label = c(paste("AUPC = ", toString(round(linear_AUPC[1,1], 3))),
paste("AUPC = ", toString(round(linear_AUPC[2,1],3))),
paste("AUPC = ", toString(round(linear_AUPC[3,1],3))),
paste("AUPC = ", toString(round(nonlinear_AUPC[1,1],3))),
paste("AUPC = ", toString(round(nonlinear_AUPC[2,1],3))),
paste("AUPC = ", toString(round(nonlinear_AUPC[3,1],3))),
paste("AUPC = ", toString(round(Zerror_AUPC[1,1],3))),
paste("AUPC = ", toString(round(Zerror_AUPC[2,1],3))),
paste("AUPC = ", toString(round(Zerror_AUPC[3,1],3)))
)
)
g6 <- ggplot(data = barcharts2, aes(x = p_values))+
geom_histogram(binwidth = 0.05, color = "white", boundary = 0) +
labs(x = 'p-values', y = 'Count') +
facet_grid(Test ~ model) +
geom_text(
size = 3,
data = dat_text2,
mapping = aes(x = 1, y = Inf, label = label),
hjust = 1.05,
vjust = 1.5
)
g6
g7 <- ggplot(data = rejection_charts2, aes(x=model, y=rej_rate, fill=Test)) +
labs(x = 'Model', y = 'Rejection Rate') +
geom_bar(stat="identity", color="white", position=position_dodge())+
theme_minimal() +
scale_fill_manual(values=c('#003f5c','#bc5090', '#58508d')) +
geom_errorbar(aes(ymin=lower_rej, ymax=upper_rej), width=.1,
position=position_dodge(.9))
g7
ggsave('linvnon_type2_pvalues.png', g6, 'png')
ggsave('linvnon_type2_rej.png', g7, 'png')
```
```{r}
#5.2: SAMPLE SIZE
#CONDITIONALLY INDEPENDENT TESTS
sizes <- c(10, 50, 100, 200, 500, 800)
#Data matrices we'll store the p values in
sample_mat <- matrix(0, nrow = 12, ncol = 100)
#rejection rate
sample_rej <- matrix(0, nrow = 12, ncol = 3)
#ks statistic
sample_ks <- matrix(0, nrow = 12, ncol = 2)
#cpu time
sample_cpu <- matrix(0, nrow = 12, ncol = 3)
for (i in 1:6) {
n <- sizes[[i]]
#Simulating p values
results <- simulate(100, n, nonlinear_model)
#Computing rejection rates and cpu time with bootstrapped confidence intervals, and ks statistic
type1_resultsKCI <- diagnostics(results[[1]], results[[4]])
type1_resultsGCM <- diagnostics(results[[2]], results[[5]])
#p values
sample_mat[2*i - 1,] <- results[[1]]
sample_mat[2*i,] <- results[[2]]
#lower 95% rejection rate, rejection rate, and upper 95% rejection rate
sample_rej[2*i - 1,] <- c(type1_resultsKCI[[4]][[1]], type1_resultsKCI[[3]], type1_resultsKCI[[4]][[2]])
sample_rej[2*i,] <- c(type1_resultsGCM[[4]][[1]], type1_resultsGCM[[3]], type1_resultsGCM[[4]][[2]])
#lower 95% cpu time, cpu time, and upper 95% cpu time
sample_cpu[2*i - 1,] <- c(type1_resultsKCI[[6]][[1]], type1_resultsKCI[[5]], type1_resultsKCI[[6]][[2]])
sample_cpu[2*i,] <- c(type1_resultsGCM[[6]][[1]], type1_resultsGCM[[5]], type1_resultsGCM[[6]][[2]])
#ks statistic, and p value
sample_ks[2*i - 1,] <- c(type1_resultsKCI[[1]], type1_resultsKCI[[2]])
sample_ks[2*i,] <- c(type1_resultsGCM[[1]], type1_resultsGCM[[2]])
}
```
```{r}
#This section is for the purpose of bootstrapping 95% confidence intervals for the ks statistic
#ks statistic
my_ks_stat <- function(p_values, indices){
p <- unique(p_values[indices])
ks <- ks.test(p, punif)
return(unname(ks[[1]]))
}
#ks p value
my_ks_p <- function(p_values, indices){
p <- unique(p_values[indices])
ks <- ks.test(p, punif)
return(unname(ks[[2]]))
}
#Function that outputs the bootstrapped confidence intervals of the ks statistic and p value.
ks_bootstrap <- function(p_values){
bootstrapped_ks_stat <- boot(p_values, my_ks_stat, R = 1000)
bootstrapped_ks_p <- boot(p_values, my_ks_p, R = 1000)
ks_stat_CI <- boot.ci(bootstrapped_ks_stat, type = "bca")
ks_stat_CI <- list(ks_stat_CI$bca[[4]], ks_stat_CI$bca[[5]])
ks_p_CI <- boot.ci(bootstrapped_ks_p, type = "bca")
ks_p_CI <- list(ks_p_CI$bca[[4]], ks_p_CI$bca[[5]])
return(list(ks_stat_CI, ks_p_CI))
}
```
```{r}
#Bootstrapping confidence intervals for the ks statistics
sample_ks_CI <- matrix(0, nrow = 12, ncol = 4)
for (i in 1:12){
print(i)
bootstrap <- ks_bootstrap(sample_mat[i,])
sample_ks_CI[i,] <- c(bootstrap[[1]][[1]], bootstrap[[1]][[2]], bootstrap[[2]][[1]], bootstrap[[2]][[2]])
}
```
```{r}
#Rearranging the data in a way that is compatible with ggplot...
sample_size_ks_data <- data.frame(Test = as.factor(c(rep("KCI", 6), rep("GCM", 6), rep("KCI", 6), rep("GCM", 6))),
Statistic = as.factor(c(rep("ks statistic", 12), rep("ks p-value", 12))),
sample_size = c(10, 50, 100, 200, 500, 800, 10, 50, 100, 200, 500, 800, 10, 50, 100, 200, 500, 800, 10, 50, 100, 200, 500, 800),
ks = c(sample_ks[1,1], sample_ks[3,1], sample_ks[5,1], sample_ks[7,1], sample_ks[9,1], sample_ks[11,1], sample_ks[2,1], sample_ks[4,1], sample_ks[6,1], sample_ks[8,1], sample_ks[10,1], sample_ks[12,1], sample_ks[1,2], sample_ks[3,2], sample_ks[5,2], sample_ks[7,2], sample_ks[9,2], sample_ks[11,2], sample_ks[2,2], sample_ks[4,2], sample_ks[6,2], sample_ks[8,2], sample_ks[10,2], sample_ks[12,2]),
lower_ks = c(sample_ks_CI[1,1], sample_ks_CI[3,1], sample_ks_CI[5,1], sample_ks_CI[7,1], sample_ks_CI[9,1], sample_ks_CI[11,1], sample_ks_CI[2,1], sample_ks_CI[4,1], sample_ks_CI[6,1], sample_ks_CI[8,1], sample_ks_CI[10,1], sample_ks_CI[12,1], sample_ks[1,2], sample_ks[3,2], sample_ks[5,2], sample_ks[7,2], sample_ks[9,2], sample_ks[11,2], sample_ks[2,2], sample_ks[4,2], sample_ks[6,2], sample_ks[8,2], sample_ks[10,2], sample_ks[12,2]),
upper_ks = c(sample_ks_CI[1,2], sample_ks_CI[3,2], sample_ks_CI[5,2], sample_ks_CI[7,2], sample_ks_CI[9,2], sample_ks_CI[11,2], sample_ks_CI[2,2], sample_ks_CI[4,2], sample_ks_CI[6,2], sample_ks_CI[8,2], sample_ks_CI[10,2], sample_ks_CI[12,2], sample_ks[1,2], sample_ks[3,2], sample_ks[5,2], sample_ks[7,2], sample_ks[9,2], sample_ks[11,2], sample_ks[2,2], sample_ks[4,2], sample_ks[6,2], sample_ks[8,2], sample_ks[10,2], sample_ks[12,2])
)
sample_size_rejtime_data <- data.frame(Test = as.factor(c(rep("KCI", 6), rep("GCM", 6))),
sample_size = c(10, 50, 100, 200, 500, 800, 10, 50, 100, 200, 500, 800),
rej_rate = c(sample_rej[1,2], sample_rej[3,2], sample_rej[5,2], sample_rej[7,2], sample_rej[9,2], sample_rej[11,2], sample_rej[2,2], sample_rej[4,2], sample_rej[6,2], sample_rej[8,2], sample_rej[10,2], sample_rej[12,2]),
lower_rej_rate = c(sample_rej[1,1], sample_rej[3,1], sample_rej[5,1], sample_rej[7,1], sample_rej[9,1], sample_rej[11,1], sample_rej[2,1], sample_rej[4,1], sample_rej[6,1], sample_rej[8,1], sample_rej[10,1], sample_rej[12,1]),
upper_rej_rate = c(sample_rej[1,3], sample_rej[3,3], sample_rej[5,3], sample_rej[7,3], sample_rej[9,3], sample_rej[11,3], sample_rej[2,3], sample_rej[4,3], sample_rej[6,3], sample_rej[8,3], sample_rej[10,3], sample_rej[12,3]),
cpu_time = c(sample_cpu[1,2], sample_cpu[3,2], sample_cpu[5,2], sample_cpu[7,2], sample_cpu[9,2], sample_cpu[11,2], sample_cpu[2,2], sample_cpu[4,2], sample_cpu[6,2], sample_cpu[8,2], sample_cpu[10,2], sample_cpu[12,2]),
lower_cpu_time = c(sample_cpu[1,1], sample_cpu[3,1], sample_cpu[5,1], sample_cpu[7,1], sample_cpu[9,1], sample_cpu[11,1], sample_cpu[2,1], sample_cpu[4,1], sample_cpu[6,1], sample_cpu[8,1], sample_cpu[10,1], sample_cpu[12,1]),
upper_cpu_time = c(sample_cpu[1,3], sample_cpu[3,3], sample_cpu[5,3], sample_cpu[7,3], sample_cpu[9,3], sample_cpu[11,3], sample_cpu[2,3], sample_cpu[4,3], sample_cpu[6,3], sample_cpu[8,3], sample_cpu[10,3], sample_cpu[12,3])
)
```
```{r}
#plotting and saving
p1 <- ggplot(sample_size_ks_data, aes(x = sample_size, y = ks, color = Test, linetype = Statistic, shape = Statistic, group = interaction(Test, Statistic))) +
labs(x = 'Sample Size', y = 'K-S Statistic/p-value') +
geom_point(size = 2) +
geom_line(size = 0.6) +
scale_colour_manual(values = c('#003f5c','#bc5090')) +
scale_linetype_manual(values = c('blank','solid')) +
scale_shape_manual(values = c(18,16)) +
theme(legend.position = "right") +
annotate('rect', xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=0.05, alpha=.5, fill='grey') +
geom_hline(yintercept = 0.05, size = 0.3) +
geom_errorbar(aes(ymin=lower_ks, ymax=upper_ks),
position=position_dodge(.9))
p1
p2 <- ggplot(sample_size_rejtime_data, aes(x = sample_size, y = cpu_time, color = Test)) +
labs(x = 'Sample Size', y = 'CPU Runtime (s)') +
geom_point(size = 2) +
stat_smooth(method = "lm",
formula = y ~ poly(x, 3),
se = FALSE,
size = 0.6) +
scale_colour_manual(values = c('#003f5c','#bc5090')) +
theme(legend.position = "right") +
geom_errorbar(aes(ymin=lower_cpu_time, ymax=upper_cpu_time),
position=position_dodge(.9))
p2
p3 <- ggplot(data = sample_size_rejtime_data, aes(x=sample_size, y=rej_rate, color=Test)) +
labs(x = 'Sample Size', y = 'Rejection Rate') +
geom_point(size = 2) +
geom_line(size = 0.6) +
theme(legend.position = "right") +
scale_color_manual(values=c('#003f5c','#bc5090')) +
annotate('rect', xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=0.05, alpha=.5, fill='grey') +
geom_hline(yintercept = 0.05, size = 0.3) +
geom_errorbar(aes(ymin=lower_rej_rate, ymax=upper_rej_rate),
position=position_dodge(.9)) +
ylim(0, 0.5)
p3
ggsave('samplesize_type1_ks.png', p1, 'png')
ggsave('samplesize_type1_cpu.png', p2, 'png')
ggsave('samplesize_type1_rej.png', p3, 'png')
```
```{r}
#5.2: SAMPLE SIZE
#CONDITIONALLY DEPENDENT TESTS
sizes <- c(10, 50, 100, 200, 500, 800)
#Data matrices we'll store the p values in
sample_mat2 <- matrix(0, nrow = 12, ncol = 100)
#rejection rate
sample_rej2 <- matrix(0, nrow = 12, ncol = 3)
#ks statistic
sample_AUPC <- matrix(0, nrow = 12, ncol = 1)
#cpu time
sample_cpu2 <- matrix(0, nrow = 12, ncol = 3)
for (i in 1:6) {
n <- sizes[[i]]
#Simulating p values
results2 <- simulate(100, n, nonlinear_model2)
#Computing rejection rates and cpu time with bootstrapped confidence intervals, and ks statistic
type2_resultsKCI <- diagnostics(results2[[1]], results2[[4]])
type2_resultsGCM <- diagnostics(results2[[2]], results2[[5]])
#p values
sample_mat2[2*i - 1,] <- results2[[1]]
sample_mat2[2*i,] <- results2[[2]]
#lower 95% rejection rate, rejection rate, and upper 95% rejection rate
sample_rej2[2*i - 1,] <- c(type2_resultsKCI[[4]][[1]], type2_resultsKCI[[3]], type2_resultsKCI[[4]][[2]])
sample_rej2[2*i,] <- c(type2_resultsGCM[[4]][[1]], type2_resultsGCM[[3]], type2_resultsGCM[[4]][[2]])
#lower 95% cpu time, cpu time, and upper 95% cpu time
sample_cpu2[2*i - 1,] <- c(type2_resultsKCI[[6]][[1]], type2_resultsKCI[[5]], type2_resultsKCI[[6]][[2]])
sample_cpu2[2*i,] <- c(type2_resultsGCM[[6]][[1]], type2_resultsGCM[[5]], type2_resultsGCM[[6]][[2]])
#ks statistic, and p value
sample_AUPC[2*i - 1,] <- type2_resultsKCI[[7]]
sample_AUPC[2*i,] <- type2_resultsGCM[[7]]
}
```
```{r}
#This section is for the purpose of bootstrapping 95% confidence intervals for the AUPC
#AUPC function
my_AUPC <- function(p_values, indices){
p_values <- unique(p_values[indices])
return(AUPC(p_values))
}
#AUPC bootstrapped confidence interval
AUPC_bootstrap <- function(p_values){
bootstrapped_AUPC <- boot(p_values, my_AUPC, R = 1000)
AUPC_CI <- boot.ci(bootstrapped_AUPC, type = "bca")
AUPC_CI <- list(AUPC_CI$bca[[4]], AUPC_CI$bca[[5]])
return(AUPC_CI)
}
```
```{r}
#Computing the AUPC confidence intervals
sample_AUPC_CI <- matrix(0, nrow = 12, ncol = 2)
for (i in 1:12){
print(i)
bootstrap <- AUPC_bootstrap(sample_mat2[i,])
sample_AUPC_CI[i,] <- c(bootstrap[[1]], bootstrap[[2]])
}
```
```{r}
#Rearranging the data in a way that is compatible with ggplot...
sample_size_data2 <- data.frame(Test = as.factor(c(rep("KCI", 6), rep("GCM", 6))),