-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim_plots.Rmd
4016 lines (3152 loc) · 232 KB
/
sim_plots.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: "sim_plots"
author: "Raine Detmer"
date: "2023-12-13"
output: html_document
---
README: plot simulation results
```{r, include=FALSE}
library("mgcv")
library("gratia")
library("tidyverse")
#library("ggpubr") # for plotting
#library("quantmod") # for findPeaks function
library("pracma") # for the findpeaks function
#https://search.r-project.org/CRAN/refmans/pracma/html/findpeaks.html
library("data.table") #for between() function
library("beeswarm") # for beeswarm plots in base R
library("tictoc") # for timing
source("load_functions.R") # loads all the functions in the "functions" folder
options(dplyr.summarise.inform = FALSE)# get rid of the "summarize has grouped output by..." warnings
```
# read in results
simulations without any covariate effects:
```{r}
focal_df <- read.csv("simulation outputs/focal_results.csv") # simulations with focal functions and no covariate
#View(focal_df)
focal_df <- focal_df[,-1] # remove index column that got added by read.csv
focal_df <- focal_df %>% mutate(thresh_fraction = thresh_n/ts_length) # fraction of jackknife iterations that detected a threshold
# need to adjust thresh_n_full for the jack_quant significance criteria case: assume that if a threshold wasn't detected in the full data when there was no significance criteria, it wouldn't have been detected if the jack_quant were used as sig criteria either, so change thresh_n_full from NA to 0
focal_df <- focal_df %>% group_by(sim, thresh_method, obs_error, ts_length, thresh_quant, cov_sd, shape) %>% mutate(sum_full = sum(thresh_n_full, na.rm = TRUE)) %>% ungroup() %>% mutate(thresh_n_full=if_else(sig_type=="jack_quant" & sum_full==0, 0, thresh_n_full)) %>% select(-sum_full)
#View(focal_df)
nonfocal_df <- read.csv("simulation outputs/nonfocal_results.csv") # simulations with nonfocal functions
nonfocal_df <- nonfocal_df[,-1]
nonfocal_df <- nonfocal_df %>% mutate(thresh_fraction = thresh_n/ts_length) # fraction of jackknife iterations that detected a threshold
nonfocal_df <- nonfocal_df %>% group_by(sim, thresh_method, obs_error, ts_length, thresh_quant, cov_sd, shape) %>% mutate(sum_full = sum(thresh_n_full, na.rm = TRUE)) %>% ungroup() %>% mutate(thresh_n_full=if_else(sig_type=="jack_quant" & sum_full==0, 0, thresh_n_full)) %>% select(-sum_full)
#View(nonfocal_df)
```
simulatiosn with corvariate effects
```{r}
# cov data
# get the case where cov_sd = 0 from the focal data imported above
cov0_df <- focal_df %>% filter(obs_error==0.0275)
# add column with next best model to this dataframe (to match the with-covariate data below)
cov0_df <- cov0_df %>% mutate(next_mod = if_else(best_mod=="gam", "lm", if_else(best_mod=="lm", "gam", if_else(best_mod=="gam_ns", "lm_ns", "gam_ns")))) %>% relocate(next_mod, .after = best_mod)
## simulations with missing covariate (i.e., not included in estimation models)
lin_covM <- read.csv("simulation outputs/lin_cov.csv") # linear covariate
lin_covM <- lin_covM[,-1]
lin_covM <- lin_covM %>% mutate(next_mod = if_else(best_mod=="gam", "lm", if_else(best_mod=="lm", "gam", if_else(best_mod=="gam_ns", "lm_ns", "gam_ns")))) %>% relocate(next_mod, .after = best_mod)
lin_covM <- lin_covM %>% mutate(thresh_fraction = thresh_n/ts_length)
lin_covM <- lin_covM %>% group_by(sim, thresh_method, obs_error, ts_length, thresh_quant, cov_sd, shape) %>% mutate(sum_full = sum(thresh_n_full, na.rm = TRUE)) %>% ungroup() %>% mutate(thresh_n_full=if_else(sig_type=="jack_quant" & sum_full==0, 0, thresh_n_full)) %>% select(-sum_full)
lin_covM <- rbind(lin_covM, cov0_df)
lin_covM$cov_type <- rep("linear", length(lin_covM$sim)) # linear cov
lin_covM$cov_inc <- rep("N", length(lin_covM$sim)) # cov wan't included in estimation models
lin_covM$cov_val <- rep(NA, length(lin_covM$sim)) # cov wasn't in estimation models so no value was used in generating predictions from these models
#View(lin_covM)
exp_covM <- read.csv("simulation outputs/exp_cov.csv") # exponential covariate
exp_covM <- exp_covM[,-1]
exp_covM <- exp_covM %>% mutate(next_mod = if_else(best_mod=="gam", "lm", if_else(best_mod=="lm", "gam", if_else(best_mod=="gam_ns", "lm_ns", "gam_ns")))) %>% relocate(next_mod, .after = best_mod)
exp_covM <- exp_covM %>% mutate(thresh_fraction = thresh_n/ts_length)
exp_covM <- exp_covM %>% group_by(sim, thresh_method, obs_error, ts_length, thresh_quant, cov_sd, shape) %>% mutate(sum_full = sum(thresh_n_full, na.rm = TRUE)) %>% ungroup() %>% mutate(thresh_n_full=if_else(sig_type=="jack_quant" & sum_full==0, 0, thresh_n_full)) %>% select(-sum_full)
exp_covM <- rbind(exp_covM, cov0_df)
exp_covM$cov_type <- rep("exp", length(exp_covM$sim)) # exponential covariate
exp_covM$cov_inc <- rep("N", length(exp_covM$sim))
exp_covM$cov_val <- rep(NA, length(exp_covM$sim))
## simulations with covariate included in estimation models
lin_covI <- read.csv("simulation outputs/lin_cov0.csv")
lin_covI <- lin_covI[,-1]
lin_covI <- lin_covI %>% mutate(thresh_fraction = thresh_n/ts_length)
lin_covI <- lin_covI %>% group_by(sim, thresh_method, obs_error, ts_length, thresh_quant, cov_sd, shape) %>% mutate(sum_full = sum(thresh_n_full, na.rm = TRUE)) %>% ungroup() %>% mutate(thresh_n_full=if_else(sig_type=="jack_quant" & sum_full==0, 0, thresh_n_full)) %>% select(-sum_full)
lin_covI <- rbind(lin_covI, cov0_df)
lin_covI$cov_type <- rep("linear", length(lin_covI$sim))
lin_covI$cov_inc <- rep("Y", length(lin_covI$sim)) # covariate was included in estimation models
lin_covI$cov_val <- rep(0, length(lin_covI$sim)) # covariate was set to 0 when generating predictions from the estimation models
#View(lin_covI)
exp_covI <- read.csv("simulation outputs/exp_cov0.csv")
exp_covI <- exp_covI[,-1]
exp_covI <- exp_covI %>% mutate(thresh_fraction = thresh_n/ts_length)
exp_covI <- exp_covI %>% group_by(sim, thresh_method, obs_error, ts_length, thresh_quant, cov_sd, shape) %>% mutate(sum_full = sum(thresh_n_full, na.rm = TRUE)) %>% ungroup() %>% mutate(thresh_n_full=if_else(sig_type=="jack_quant" & sum_full==0, 0, thresh_n_full)) %>% select(-sum_full)
exp_covI <- rbind(exp_covI, cov0_df)
exp_covI$cov_type <- rep("exp", length(exp_covI$sim))
exp_covI$cov_inc <- rep("Y", length(exp_covI$sim))
exp_covI$cov_val <- rep(0, length(exp_covI$sim))
# join altogether
linexp_cov <- rbind(lin_covM, exp_covM, lin_covI, exp_covI)
#View(linexp_cov)
```
# plot pars
```{r}
cov_sd_set2 <- c(0, 0.5, 1) # covariate sd values
# colors for the different threshold definitions
threshcols <- c("purple", "forestgreen", "orange", "dodgerblue")
#threshcols2 <- adjustcolor(threshcols, alpha.f = 0.3)
threshcols2 <- adjustcolor(threshcols, alpha.f = 0.4)
threshcols3 <- adjustcolor(threshcols, alpha.f = 0.3)
# driver values for generating predictions
xvals1 <- seq(from = -3, to = 3, by = 0.01) # needs to be the same as used in simulations
# turn these into a data frame
xdt <- data.frame(
driver = xvals1
)
jt_amount <- 0.1 # amount to jitter data points in boxplots
sig_choice <- "none" # significance criteria for which to plot results
sig_thresh <- 0.25 # minimum fraction of jackknife iterations that need to detected a threshold for that simulation to count as having found a threshold
```
# main results
```{r}
# the simulation parameters
obs_set <- c(0.0275, 0.55, 2.75) # sd of observation errors for response
ts_set <- c(15, 25, 35) # time series lengths
quant_set <- c(0.05, 0.25, 0.5) # threshold quantiles
cov_sd_set1 <- c(0) # sd of covariate
```
## estimate bias
plot difference between threshold estimates and true threshold values
### means, all defs
all threshold definitions
```{r}
# 3x3 figure w/ boxplots
boxpos <- c(1:4, 7:10, 13:16)#c(1:4, 7:10) # positions of the boxes
n_adj <- 0.5 # how far from top of box to adjust sample sizes
n_size <- 1 # font size of sample sizes
max_adj <- 0.5 # adjust ymax
xlab_cex <- 0.8 # size to make x axis labels
col_cex <- 1.2 # font for column labels
plot_dt <- focal_df %>% filter(best_mod=="gam") %>% filter(thresh_n_full %in% c(1, NA)) %>% filter(sig_type==sig_choice) %>% filter(thresh_fraction >= sig_thresh)
#View(plot_dt)
ymin <- min(plot_dt$thresh_diff, na.rm = T)
ymax <- max(plot_dt$thresh_diff, na.rm = T)+max_adj
#yaxt_at <- c(-2, -1, 0, 1, 2)# where to put y axis labels
yaxt_at <- round(seq(from = ymin, to = ymax, by = 1), 0)# where to put y axis labels
ylabs <- seq(from = ymin, to = ymax, by = 1)
pdf("figurepdfs/S_FullDiff.pdf", width = 7.5)
par(mfrow = c(4, 3))
layout(matrix(c(1, 4, 7, 10, 2, 5, 8, 10, 3, 6, 9, 10), nrow = 4, ncol = 3), widths = c(1, 1, 1), heights = c(1, 1, 1, 0.3))
#layout.show(10)
#par(mar=c(0, 1.5, 0.5, 0), oma = c(0, 2, 3, 3))
par(mar=c(0, 1.5, 0.5, 0), oma = c(0, 2, 3, 0.5))
# top row: sigmoidal
plot_dt <- focal_df[which(focal_df$shape=="sigmoidal"),] %>% filter(best_mod=="gam") %>% filter(thresh_n_full %in% c(1, NA)) %>% filter(sig_type==sig_choice) %>% filter(thresh_fraction >= sig_thresh)
#View(plot_dt)
# need to make dummy points for cases where results for all simulations were NA in order to keep the placeholder for the boxplot
plot_dt <- plot_dt %>% group_by(thresh_method, obs_error, ts_length, cov_sd, thresh_quant) %>% mutate(na_check = mean(thresh_diff, na.rm = T)) %>% ungroup()
plot_dt$thresh_diff <- ifelse(is.na(plot_dt$na_check)==T, -9999, plot_dt$thresh_diff)
################ effect of ts length ####################
levels <- matrix(NA, nrow = 12, ncol = 2) # levels for jittering points
levels[, 1] <- rep(c("abs_max_d2", "min_d2", "zero_d1", "zero_d2"), 3)
levels[,2] <- rep(c("15", "25", "35"), each = 4)
datasub <- plot_dt %>% filter(obs_error==obs_set[2], thresh_quant == quant_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
boxplot(thresh_diff ~ thresh_method + as.character(ts_length), data = datasub,
at = boxpos, col = adjustcolor(threshcols, alpha.f = 0.3), border = threshcols, outline = F, names = NA, xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n")
axis(side = 2, at = yaxt_at, las = 1)
#axis(side = 1, at = c(2.5, 8.5, 14.5), labels = c("15", "25", "35"), tick = F, line = -1)
mtext(side = 3, "Time series length", line = 0)
#mtext(side = 2, "Mean estimate - true threshold", line = 2)
abline(h = 0, lty = 2)
text(x = 3.4, y = 3.5, "risk-averse side")
text(x = 3.4, y = -2, "risk-prone side")
for(i in 1:nrow(levels)){ # add jittered points
#prop <- length(datasub$sim[which(datasub$thresh_method==levels[i,1])]/nrow(datasub))
yvals <- datasub$thresh_diff[which(datasub$thresh_method==levels[i,1] & datasub$ts_length==as.numeric(levels[i, 2]))]
jtx <- jitter(rep(boxpos[i], length(yvals)), amount = jt_amount)# add jitter to x-axis indices proportional number in each level
points(jtx, yvals, pch=20, col=ifelse(levels[i, 1]=="abs_max_d2", threshcols2[1], ifelse(levels[i, 1]=="min_d2", threshcols2[2], ifelse(levels[i, 1]=="zero_d1", threshcols2[3], threshcols2[4]))))
}
# adding sample sizes
n_group <- datasub %>% group_by(thresh_method, ts_length) %>% summarize(n = length(which(is.na(thresh_diff)==F))) %>% arrange(ts_length, thresh_method)
boxbnds <- boxplot(thresh_diff ~ thresh_method + as.character(ts_length), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
################ effect of obs error ####################
levels <- matrix(NA, nrow = 12, ncol = 2) # levels for jittering points
levels[, 1] <- rep(c("abs_max_d2", "min_d2", "zero_d1", "zero_d2"), 3)
levels[,2] <- rep(as.character(obs_set), each = 4)
datasub <- plot_dt %>% filter(ts_length==ts_set[2], thresh_quant == quant_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
boxplot(thresh_diff ~ thresh_method + as.character(obs_error), data = datasub,
at = boxpos, col = adjustcolor(threshcols, alpha.f = 0.3), border = threshcols, outline = F, names = NA, xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n")
axis(side = 2, at = yaxt_at, labels = NA)
#axis(side = 1, at = c(2.5, 8.5, 14.5), labels = c("cv ~ 0.01", "cv ~ 0.2", "cv ~ 1"), tick = F, line = -1)
mtext(side = 3, "Observation error", line = 0)
#mtext(side = 2, "Mean estimate - true threshold", line = 2)
abline(h = 0, lty = 2)
for(i in 1:nrow(levels)){
#prop <- length(datasub$sim[which(datasub$thresh_method==levels[i,1])]/nrow(datasub))
yvals <- datasub$thresh_diff[which(datasub$thresh_method==levels[i,1] & datasub$obs_error==as.numeric(levels[i, 2]))]
jtx <- jitter(rep(boxpos[i], length(yvals)), amount = jt_amount)# add jitter to x-axis indices proportional number in each level
points(jtx, yvals, pch=20, col=ifelse(levels[i, 1]=="abs_max_d2", threshcols2[1], ifelse(levels[i, 1]=="min_d2", threshcols2[2], ifelse(levels[i, 1]=="zero_d1", threshcols2[3], threshcols2[4]))))
}
# adding sample sizes
n_group <- datasub %>% group_by(thresh_method, obs_error) %>% summarize(n = length(which(is.na(thresh_diff)==F))) %>% arrange(obs_error, thresh_method)
boxbnds <- boxplot(thresh_diff ~ thresh_method + as.character(obs_error), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
################ effect of threshold quantile ####################
levels <- matrix(NA, nrow = 12, ncol = 2) # levels for jittering points
levels[, 1] <- rep(c("abs_max_d2", "min_d2", "zero_d1", "zero_d2"), 3)
levels[,2] <- rep(as.character(quant_set), each = 4)
datasub <- plot_dt %>% filter(ts_length==ts_set[2], obs_error == obs_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
boxplot(thresh_diff ~ thresh_method + as.character(thresh_quant), data = datasub,
at = boxpos, col = adjustcolor(threshcols, alpha.f = 0.3), border = threshcols, outline = F, names = NA, xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n")
axis(side = 2, at = yaxt_at, labels = NA)
#mtext(side = 4, "SG", las = 2)
#axis(side = 1, at = c(2.5, 8.5, 14.5), labels = c("0.05", "0.25", "0.5"), tick = F, line = -1)
mtext(side = 3, "Threshold quantile", line = 0)
#mtext(side = 2, "Mean estimate - true threshold", line = 2)
abline(h = 0, lty = 2)
for(i in 1:nrow(levels)){
#prop <- length(datasub$sim[which(datasub$thresh_method==levels[i,1])]/nrow(datasub))
yvals <- datasub$thresh_diff[which(datasub$thresh_method==levels[i,1] & datasub$thresh_quant==as.numeric(levels[i, 2]))]
jtx <- jitter(rep(boxpos[i], length(yvals)), amount = jt_amount)# add jitter to x-axis indices proportional number in each level
points(jtx, yvals, pch=20, col=ifelse(levels[i, 1]=="abs_max_d2", threshcols2[1], ifelse(levels[i, 1]=="min_d2", threshcols2[2], ifelse(levels[i, 1]=="zero_d1", threshcols2[3], threshcols2[4]))))
}
# adding sample sizes
n_group <- datasub %>% group_by(thresh_method, thresh_quant) %>% summarize(n = length(which(is.na(thresh_diff)==F))) %>% arrange(thresh_quant, thresh_method)
boxbnds <- boxplot(thresh_diff ~ thresh_method + as.character(thresh_quant), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
# add mini plot showing functional form
#mtext(side = 4, "SG", las = 2, padj = 5)
text(x = boxpos[9]-0.85, y = -1.5, "SG", cex = 1.2)
# plot border
rect(xleft = boxpos[length(boxpos)-3]-0.1, ybottom= -2, xright = boxpos[length(boxpos)] -0.6, ytop = -1, border = "black")
# x vals for the mini plot
mini_x <- seq(from = boxpos[length(boxpos)-3]-0.1, to = boxpos[length(boxpos)] -0.6, length.out = 1000)
# x vals for the real-size function: go from the left to the right of the threshold (which is approximately the middle of mini_x, here make it a little less than the middle - divide by 1.9 instead of 2)
big_x <- seq(from = mini_x[length(mini_x)/1.9]-1, to = mini_x[length(mini_x)/1.9] + 1, length.out = 1000)
# y vals for real-size function: make these range from 0 to 1
big_y <- 1/(1 + exp(-6*(big_x-mini_x[length(mini_x)/1.9])))
mini_y <- (-1 + 2)*big_y-2 # now squish the yvalues to fit in the mini plot (which has a yrange of ybottom to ytop in the rect function) and shift them to line up w/ the lower border of the plot
lines(x = mini_x, y = mini_y, type = "l")
# <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <-
# middle row: skew
plot_dt <- focal_df[which(focal_df$shape=="skew"),] %>% filter(best_mod=="gam") %>% filter(thresh_n_full %in% c(1, NA)) %>% filter(sig_type==sig_choice) %>% filter(thresh_fraction >= sig_thresh)
#View(plot_dt)
# need to make dummy points for cases where results for all simulations were NA in order to keep the placeholder for the boxplot
plot_dt <- plot_dt %>% group_by(thresh_method, obs_error, ts_length, cov_sd, thresh_quant) %>% mutate(na_check = mean(thresh_diff, na.rm = T)) %>% ungroup()
plot_dt$thresh_diff <- ifelse(is.na(plot_dt$na_check)==T, -9999, plot_dt$thresh_diff)
################ effect of ts length ####################
levels <- matrix(NA, nrow = 12, ncol = 2) # levels for jittering points
levels[, 1] <- rep(c("abs_max_d2", "min_d2", "zero_d1", "zero_d2"), 3)
levels[,2] <- rep(c("15", "25", "35"), each = 4)
datasub <- plot_dt %>% filter(obs_error==obs_set[2], thresh_quant == quant_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
boxplot(thresh_diff ~ thresh_method + as.character(ts_length), data = datasub,
at = boxpos, col = adjustcolor(threshcols, alpha.f = 0.3), border = threshcols, outline = F, names = NA, xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n")
axis(side = 2, at = yaxt_at, las = 1)
#axis(side = 1, at = c(2.5, 8.5, 14.5), labels = c("15", "25", "35"), tick = F, line = -1)
#mtext(side = 1, "Time series length", line = 1.2)
mtext(side = 2, "Bias (mean estimate - true threshold)", line = 2)
abline(h = 0, lty = 2)
for(i in 1:nrow(levels)){
#prop <- length(datasub$sim[which(datasub$thresh_method==levels[i,1])]/nrow(datasub))
yvals <- datasub$thresh_diff[which(datasub$thresh_method==levels[i,1] & datasub$ts_length==as.numeric(levels[i, 2]))]
jtx <- jitter(rep(boxpos[i], length(yvals)), amount = jt_amount)# add jitter to x-axis indices proportional number in each level
points(jtx, yvals, pch=20, col=ifelse(levels[i, 1]=="abs_max_d2", threshcols2[1], ifelse(levels[i, 1]=="min_d2", threshcols2[2], ifelse(levels[i, 1]=="zero_d1", threshcols2[3], threshcols2[4]))))
}
# adding sample sizes
n_group <- datasub %>% group_by(thresh_method, ts_length) %>% summarize(n = length(which(is.na(thresh_diff)==F))) %>% arrange(ts_length, thresh_method)
boxbnds <- boxplot(thresh_diff ~ thresh_method + as.character(ts_length), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
################ effect of obs error ####################
levels <- matrix(NA, nrow = 12, ncol = 2) # levels for jittering points
levels[, 1] <- rep(c("abs_max_d2", "min_d2", "zero_d1", "zero_d2"), 3)
levels[,2] <- rep(as.character(obs_set), each = 4)
datasub <- plot_dt %>% filter(ts_length==ts_set[2], thresh_quant == quant_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
boxplot(thresh_diff ~ thresh_method + as.character(obs_error), data = datasub,
at = boxpos, col = adjustcolor(threshcols, alpha.f = 0.3), border = threshcols, outline = F, names = NA, xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n")
axis(side = 2, at = yaxt_at, labels = NA)
abline(h = 0, lty = 2)
for(i in 1:nrow(levels)){
#prop <- length(datasub$sim[which(datasub$thresh_method==levels[i,1])]/nrow(datasub))
yvals <- datasub$thresh_diff[which(datasub$thresh_method==levels[i,1] & datasub$obs_error==as.numeric(levels[i, 2]))]
jtx <- jitter(rep(boxpos[i], length(yvals)), amount = jt_amount)# add jitter to x-axis indices proportional number in each level
points(jtx, yvals, pch=20, col=ifelse(levels[i, 1]=="abs_max_d2", threshcols2[1], ifelse(levels[i, 1]=="min_d2", threshcols2[2], ifelse(levels[i, 1]=="zero_d1", threshcols2[3], threshcols2[4]))))
}
# adding sample sizes
n_group <- datasub %>% group_by(thresh_method, obs_error) %>% summarize(n = length(which(is.na(thresh_diff)==F))) %>% arrange(obs_error, thresh_method)
boxbnds <- boxplot(thresh_diff ~ thresh_method + as.character(obs_error), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
################ effect of threshold quantile ####################
levels <- matrix(NA, nrow = 12, ncol = 2) # levels for jittering points
levels[, 1] <- rep(c("abs_max_d2", "min_d2", "zero_d1", "zero_d2"), 3)
levels[,2] <- rep(as.character(quant_set), each = 4)
datasub <- plot_dt %>% filter(ts_length==ts_set[2], obs_error == obs_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
boxplot(thresh_diff ~ thresh_method + as.character(thresh_quant), data = datasub,
at = boxpos, col = adjustcolor(threshcols, alpha.f = 0.3), border = threshcols, outline = F, names = NA, xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n")
axis(side = 2, at = yaxt_at, labels = NA)
abline(h = 0, lty = 2)
for(i in 1:nrow(levels)){
#prop <- length(datasub$sim[which(datasub$thresh_method==levels[i,1])]/nrow(datasub))
yvals <- datasub$thresh_diff[which(datasub$thresh_method==levels[i,1] & datasub$thresh_quant==as.numeric(levels[i, 2]))]
jtx <- jitter(rep(boxpos[i], length(yvals)), amount = jt_amount)# add jitter to x-axis indices proportional number in each level
points(jtx, yvals, pch=20, col=ifelse(levels[i, 1]=="abs_max_d2", threshcols2[1], ifelse(levels[i, 1]=="min_d2", threshcols2[2], ifelse(levels[i, 1]=="zero_d1", threshcols2[3], threshcols2[4]))))
}
# adding sample sizes
n_group <- datasub %>% group_by(thresh_method, thresh_quant) %>% summarize(n = length(which(is.na(thresh_diff)==F))) %>% arrange(thresh_quant, thresh_method)
boxbnds <- boxplot(thresh_diff ~ thresh_method + as.character(thresh_quant), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
# add mini plot showing functional form
#mtext(side = 4, "SK", las = 2, padj = 5)
text(x = boxpos[9]-0.85, y = -1.75, "SK", cex = 1.2)
# plot border
rect(xleft = boxpos[length(boxpos)-3]-0.1, ybottom= -2.25, xright = boxpos[length(boxpos)] -0.6, ytop = -1.25, border = "black")
# x vals for the mini plot
mini_x <- seq(from = boxpos[length(boxpos)-3]-0.1, to = boxpos[length(boxpos)] -0.6, length.out = 1000)
big_x <- seq(from = mini_x[length(mini_x)/2]-5, to = mini_x[length(mini_x)/2] + 5, length.out = 1000)
big_y <- dgamma(big_x, shape = mini_x[length(mini_x)/2] + 1, scale = 1)/max(dgamma(big_x, shape = mini_x[length(mini_x)/2] + 1, scale = 1)) # mode is at (shape -1 )*scale
mini_y <- (-1.26 + 2.25)*big_y-2.25
lines(x = mini_x, y = mini_y, type = "l") # -1.22 to go a little below the border
# <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <-
# bottom row: hockeystick
plot_dt <- focal_df[which(focal_df$shape=="hockeystick"),] %>% filter(best_mod=="gam") %>% filter(thresh_n_full %in% c(1, NA)) %>% filter(sig_type==sig_choice) %>% filter(thresh_fraction >= sig_thresh)
plot_dt <- plot_dt %>% group_by(thresh_method, obs_error, ts_length, cov_sd, thresh_quant) %>% mutate(na_check = mean(thresh_diff, na.rm = T)) %>% ungroup()
plot_dt$thresh_diff <- ifelse(is.na(plot_dt$na_check)==T, -9999, plot_dt$thresh_diff)
################ effect of ts length ####################
levels <- matrix(NA, nrow = 12, ncol = 2) # levels for jittering points
levels[, 1] <- rep(c("abs_max_d2", "min_d2", "zero_d1", "zero_d2"), 3)
levels[,2] <- rep(c("15", "25", "35"), each = 4)
datasub <- plot_dt %>% filter(obs_error==obs_set[2], thresh_quant == quant_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
boxplot(thresh_diff ~ thresh_method + as.character(ts_length), data = datasub,
at = boxpos, col = adjustcolor(threshcols, alpha.f = 0.3), border = threshcols, outline = F, names = NA, xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n")
axis(side = 2, at = yaxt_at, las = 1)
axis(side = 1, at = c(2.5, 8.5, 14.5), labels = c("15", "25", "35"), tick = F, line = -1)
mtext(side = 1, "Time series length", line = 1.2, cex = xlab_cex)
#mtext(side = 2, "Mean estimate - true threshold", line = 2)
abline(h = 0, lty = 2)
for(i in 1:nrow(levels)){
#prop <- length(datasub$sim[which(datasub$thresh_method==levels[i,1])]/nrow(datasub))
yvals <- datasub$thresh_diff[which(datasub$thresh_method==levels[i,1] & datasub$ts_length==as.numeric(levels[i, 2]))]
jtx <- jitter(rep(boxpos[i], length(yvals)), amount = jt_amount)# add jitter to x-axis indices proportional number in each level
points(jtx, yvals, pch=20, col=ifelse(levels[i, 1]=="abs_max_d2", threshcols2[1], ifelse(levels[i, 1]=="min_d2", threshcols2[2], ifelse(levels[i, 1]=="zero_d1", threshcols2[3], threshcols2[4]))))
}
# adding sample sizes
n_group <- datasub %>% group_by(thresh_method, ts_length) %>% summarize(n = length(which(is.na(thresh_diff)==F))) %>% arrange(ts_length, thresh_method)
boxbnds <- boxplot(thresh_diff ~ thresh_method + as.character(ts_length), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
################ effect of obs error ####################
levels <- matrix(NA, nrow = 12, ncol = 2) # levels for jittering points
levels[, 1] <- rep(c("abs_max_d2", "min_d2", "zero_d1", "zero_d2"), 3)
levels[,2] <- rep(as.character(obs_set), each = 4)
datasub <- plot_dt %>% filter(ts_length==ts_set[2], thresh_quant == quant_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
boxplot(thresh_diff ~ thresh_method + as.character(obs_error), data = datasub,
at = boxpos, col = adjustcolor(threshcols, alpha.f = 0.3), border = threshcols, outline = F, names = NA, xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n")
axis(side = 2, at = yaxt_at, labels = NA)
axis(side = 1, at = c(2.5, 8.5, 14.5), labels = c("cv ~ 0.01", "cv ~ 0.2", "cv ~ 1"), tick = F, line = -1)
mtext(side = 1, "Observation error", line = 1.2, cex = xlab_cex)
#mtext(side = 2, "Mean estimate - true threshold", line = 2)
abline(h = 0, lty = 2)
for(i in 1:nrow(levels)){
#prop <- length(datasub$sim[which(datasub$thresh_method==levels[i,1])]/nrow(datasub))
yvals <- datasub$thresh_diff[which(datasub$thresh_method==levels[i,1] & datasub$obs_error==as.numeric(levels[i, 2]))]
jtx <- jitter(rep(boxpos[i], length(yvals)), amount = jt_amount)# add jitter to x-axis indices proportional number in each level
points(jtx, yvals, pch=20, col=ifelse(levels[i, 1]=="abs_max_d2", threshcols2[1], ifelse(levels[i, 1]=="min_d2", threshcols2[2], ifelse(levels[i, 1]=="zero_d1", threshcols2[3], threshcols2[4]))))
}
# adding sample sizes
n_group <- datasub %>% group_by(thresh_method, obs_error) %>% summarize(n = length(which(is.na(thresh_diff)==F))) %>% arrange(obs_error, thresh_method)
boxbnds <- boxplot(thresh_diff ~ thresh_method + as.character(obs_error), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
################ effect of threshold quantile ####################
levels <- matrix(NA, nrow = 12, ncol = 2) # levels for jittering points
levels[, 1] <- rep(c("abs_max_d2", "min_d2", "zero_d1", "zero_d2"), 3)
levels[,2] <- rep(as.character(quant_set), each = 4)
datasub <- plot_dt %>% filter(ts_length==ts_set[2], obs_error == obs_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
boxplot(thresh_diff ~ thresh_method + as.character(thresh_quant), data = datasub,
at = boxpos, col = adjustcolor(threshcols, alpha.f = 0.3), border = threshcols, outline = F, names = NA, xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n")
axis(side = 2, at = yaxt_at, labels = NA)
axis(side = 1, at = c(2.5, 8.5, 14.5), labels = c("0.05", "0.25", "0.5"), tick = F, line = -1)
mtext(side = 1, "Threshold quantile", line = 1.2, cex = xlab_cex)
#mtext(side = 4, "HS", las = 2)
#mtext(side = 2, "Mean estimate - true threshold", line = 2)
abline(h = 0, lty = 2)
# add mini plot showing functional form
text(x = boxpos[9]-0.85, y = -1.75, "HS", cex = 1.2)
# plot border
rect(xleft = boxpos[length(boxpos)-3]-0.1, ybottom= -2.25, xright = boxpos[length(boxpos)] -0.6, ytop = -1.25, border = "black")
# x vals for the mini plot
mini_x <- seq(from = boxpos[length(boxpos)-3]-0.1, to = boxpos[length(boxpos)] -0.6, length.out = 1000)
big_x <- seq(from = mini_x[length(mini_x)/2]-10, to = mini_x[length(mini_x)/2] + 10, length.out = 1000)
big_y <- ifelse(big_x < 0, 0, ifelse(big_x < mini_x[length(mini_x)/2], (0.9-0)/ (mini_x[length(mini_x)/2] - 0) * (big_x - mini_x[length(mini_x)/2]) + 0.9, 0.9))
mini_y <- (-1.25 + 2.25)*big_y-2.25
lines(x = mini_x, y = mini_y, type = "l") # -1.22 to go a little below the border
for(i in 1:nrow(levels)){
#prop <- length(datasub$sim[which(datasub$thresh_method==levels[i,1])]/nrow(datasub))
yvals <- datasub$thresh_diff[which(datasub$thresh_method==levels[i,1] & datasub$thresh_quant==as.numeric(levels[i, 2]))]
jtx <- jitter(rep(boxpos[i], length(yvals)), amount = jt_amount)# add jitter to x-axis indices proportional number in each level
points(jtx, yvals, pch=20, col=ifelse(levels[i, 1]=="abs_max_d2", threshcols2[1], ifelse(levels[i, 1]=="min_d2", threshcols2[2], ifelse(levels[i, 1]=="zero_d1", threshcols2[3], threshcols2[4]))))
}
# adding sample sizes
n_group <- datasub %>% group_by(thresh_method, thresh_quant) %>% summarize(n = length(which(is.na(thresh_diff)==F))) %>% arrange(thresh_quant, thresh_method)
boxbnds <- boxplot(thresh_diff ~ thresh_method + as.character(thresh_quant), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
# plot 10: dummy plot for legend
plot(0,type='n',axes=FALSE, ann = FALSE)
legend(0.8, 0.2, col = threshcols, pch = 16, legend = c("max(|s''(x)|)", "min(s''(x))", "s'(x)=0", "s''(x)=0"), bty = "n", ncol =4, cex = 1.1, xpd = TRUE, inset = c(0,-0.5)) #, title = "Threshold calculation method"
text(0.75, -0.4, "Threshold def.", xpd = TRUE)
dev.off()
```
### means, best method
simplified version of the above plot just showing results for the suggested best threshold definition for each functional form
sigmoidal: min(s''(x))
skew: s'(x) = 0
hockeystick: max(|s''(x)|)
```{r}
# 3x3 figure w/ boxplots
boxpos <- c(1, 2, 3)#c(1:4, 7:10) # positions of the boxes
labpos <- c(1, 2, 3) # position of box labels on x axis
n_adj <- 0.5#0.5 # how far from top of box to adjust sample sizes
n_size <- 1 # font size of sample sizes
max_adj <- 0 # adjust ymax
xlab_cex <- 0.8 # size to make x axis labels
col_cex <- 1.2 # font for column labels
plot_dt <- focal_df %>% filter(best_mod=="gam") %>% filter(thresh_n_full %in% c(1, NA)) %>% filter(sig_type==sig_choice) %>% filter(thresh_fraction >= sig_thresh)
ymin <- min(plot_dt$thresh_diff[which(plot_dt$thresh_method != "zero_d2")], na.rm = T)
ymax <- max(plot_dt$thresh_diff[which(plot_dt$thresh_method != "zero_d2")], na.rm = T)+max_adj
#yaxt_at <- c(-2, -1, 0, 1, 2)# where to put y axis labels
yaxt_at <- round(seq(from = ymin, to = ymax, by = 1), 0)# where to put y axis labels
ylabs <- round(seq(from = ymin, to = ymax, by = 1), 0)
pdf("figurepdfs/Best_Diff.pdf", width = 6, height = 6)
par(mfrow = c(4, 3))
layout(matrix(c(1, 4, 7, 10, 2, 5, 8, 10, 3, 6, 9, 10), nrow = 4, ncol = 3), widths = c(1, 1, 1), heights = c(1, 1, 1, 0.3))
#par(mar=c(0.1, 1.5, 0.5, 0), oma = c(1, 2, 1.2, 3))
par(mar=c(0.1, 1.5, 0.5, 0), oma = c(1, 2, 1.2, 0.5))
# top row: sigmoidal
plot_dt <- focal_df[which(focal_df$shape=="sigmoidal"),] %>% filter(best_mod=="gam") %>% filter(thresh_n_full %in% c(1, NA)) %>% filter(thresh_method=="min_d2") %>% filter(sig_type==sig_choice) %>% filter(thresh_fraction >= sig_thresh)
#View(plot_dt)
# need to make dummy points for cases where results for all simulations were NA in order to keep the placeholder for the boxplot
plot_dt <- plot_dt %>% group_by(obs_error, ts_length, cov_sd, thresh_quant) %>% mutate(na_check = mean(thresh_diff, na.rm = T)) %>% ungroup()
plot_dt$thresh_diff <- ifelse(is.na(plot_dt$na_check)==T, -9999, plot_dt$thresh_diff)
#View(plot_dt)
levels <- matrix(NA, nrow = 3, ncol = 2) # levels for jittering points
levels[, 1] <- rep(NA, 3)
levels[,2] <- rep(c("15", "25", "35"), each = 1)
datasub <- plot_dt %>% filter(obs_error==obs_set[2], thresh_quant == quant_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
boxplot(thresh_diff ~ as.character(ts_length), data = datasub,
at = boxpos, col = adjustcolor(threshcols[2], alpha.f = 0.3), border = threshcols[2], outline = F, names = NA, xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n")
axis(side = 2, at = yaxt_at, las = 1)
#axis(side = 1, at = c(2.5, 8.5, 14.5), labels = c("15", "25", "35"), tick = F, line = -1)
mtext(side = 3, "Time series length", line = 0)
#mtext(side = 2, "Mean estimate - true threshold", line = 2)
abline(h = 0, lty = 2)
#text(x = 2.6, y = 2.8, "risk-averse \nside")
#text(x = 2.6, y = -2, "risk-prone \nside")
text(x = 1.25, y = 2.8, "risk-averse side")
text(x = 1.25, y = -2, "risk-prone side")
for(i in 1:nrow(levels)){
#prop <- length(datasub$sim[which(datasub$thresh_method==levels[i,1])]/nrow(datasub))
yvals <- datasub$thresh_diff[which(datasub$ts_length==as.numeric(levels[i, 2]))]
jtx <- jitter(rep(boxpos[i], length(yvals)), amount = jt_amount)# add jitter to x-axis indices proportional number in each level
points(jtx, yvals, pch=20, col=ifelse(datasub$thresh_method[1]=="abs_max_d2", threshcols2[1], ifelse(datasub$thresh_method[1]=="min_d2", threshcols2[2], ifelse(datasub$thresh_method[1]=="zero_d1", threshcols2[3], threshcols2[4]))))
}
# adding sample sizes
n_group <- datasub %>% group_by(ts_length) %>% summarize(n = length(which(is.na(thresh_diff)==F))) %>% arrange(ts_length)
boxbnds <- boxplot(thresh_diff ~ as.character(ts_length), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
################ effect of obs error ####################
levels <- matrix(NA, nrow = 3, ncol = 2) # levels for jittering points
levels[, 1] <- rep(NA, 3)
levels[,2] <- rep(as.character(obs_set), each = 1)
datasub <- plot_dt %>% filter(ts_length==ts_set[2], thresh_quant == quant_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
boxplot(thresh_diff ~ as.character(obs_error), data = datasub,
at = boxpos, col = adjustcolor(threshcols[2], alpha.f = 0.3), border = threshcols[2], outline = F, names = NA, xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n")
axis(side = 2, at = yaxt_at, labels = NA)
#axis(side = 1, at = c(2.5, 8.5, 14.5), labels = c("cv ~ 0.01", "cv ~ 0.2", "cv ~ 1"), tick = F, line = -1)
mtext(side = 3, "Observation error", line = 0)
#mtext(side = 2, "Mean estimate - true threshold", line = 2)
abline(h = 0, lty = 2)
for(i in 1:nrow(levels)){
#prop <- length(datasub$sim[which(datasub$thresh_method==levels[i,1])]/nrow(datasub))
yvals <- datasub$thresh_diff[which(datasub$obs_error==as.numeric(levels[i, 2]))]
jtx <- jitter(rep(boxpos[i], length(yvals)), amount = jt_amount)# add jitter to x-axis indices proportional number in each level
points(jtx, yvals, pch=20, col=ifelse(datasub$thresh_method[1]=="abs_max_d2", threshcols2[1], ifelse(datasub$thresh_method[1]=="min_d2", threshcols2[2], ifelse(datasub$thresh_method[1]=="zero_d1", threshcols2[3], threshcols2[4]))))
}
# adding sample sizes
n_group <- datasub %>% group_by(obs_error) %>% summarize(n = length(which(is.na(thresh_diff)==F))) %>% arrange(obs_error)
boxbnds <- boxplot(thresh_diff ~ as.character(obs_error), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
################ effect of threshold quantile ####################
levels <- matrix(NA, nrow = 3, ncol = 2) # levels for jittering points
levels[, 1] <- rep(NA, 3)
levels[,2] <- rep(as.character(quant_set), each = 1)
datasub <- plot_dt %>% filter(ts_length==ts_set[2], obs_error == obs_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
boxplot(thresh_diff ~ as.character(thresh_quant), data = datasub,
at = boxpos, col = adjustcolor(threshcols[2], alpha.f = 0.3), border = threshcols[2], outline = F, names = NA, xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n")
axis(side = 2, at = yaxt_at, labels = NA)
#mtext(side = 4, "SG", las = 2)
#axis(side = 1, at = c(2.5, 8.5, 14.5), labels = c("0.05", "0.25", "0.5"), tick = F, line = -1)
mtext(side = 3, "Threshold quantile", line = 0)
#mtext(side = 2, "Mean estimate - true threshold", line = 2)
abline(h = 0, lty = 2)
# add mini plot showing functional form
#mtext(side = 4, "SG", las = 2, padj = 5)
text(x = boxpos[length(boxpos)] - 0.25, y = -1.9, "SG", cex = 1.2)
# plot border
rect(xleft = boxpos[length(boxpos)], ybottom= -2.3, xright = boxpos[length(boxpos)] + 1.3, ytop = -1.2, border = "black")
# x vals for the mini plot
mini_x <- seq(from = boxpos[length(boxpos)], to = boxpos[length(boxpos)] + 0.7, length.out = 1000)
# x vals for the real-size function: go from the left to the right of the threshold (which is approximately the middle of mini_x, here make it a little less than the middle - divide by 1.9 instead of 2)
big_x <- seq(from = mini_x[length(mini_x)/1.9]-1, to = mini_x[length(mini_x)/1.9] + 1, length.out = 1000)
# y vals for real-size function: make these range from 0 to 1
big_y <- 1/(1 + exp(-6*(big_x-mini_x[length(mini_x)/1.9])))
mini_y <- (-1.25 + 2.3)*big_y-2.3 # now squish the yvalues to fit in the mini plot (which has a yrange of ybottom to ytop in the rect function) and shift them to line up w/ the lower border of the plot
lines(x = mini_x, y = mini_y, type = "l")
for(i in 1:nrow(levels)){
#prop <- length(datasub$sim[which(datasub$thresh_method==levels[i,1])]/nrow(datasub))
yvals <- datasub$thresh_diff[which(datasub$thresh_quant==as.numeric(levels[i, 2]))]
jtx <- jitter(rep(boxpos[i], length(yvals)), amount = jt_amount)# add jitter to x-axis indices proportional number in each level
points(jtx, yvals, pch=20, col=ifelse(datasub$thresh_method[1]=="abs_max_d2", threshcols2[1], ifelse(datasub$thresh_method[1]=="min_d2", threshcols2[2], ifelse(datasub$thresh_method[1]=="zero_d1", threshcols2[3], threshcols2[4]))))
}
# adding sample sizes
n_group <- datasub %>% group_by(thresh_quant) %>% summarize(n = length(which(is.na(thresh_diff)==F))) %>% arrange(thresh_quant)
boxbnds <- boxplot(thresh_diff ~ as.character(thresh_quant), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
# <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <-
# middle row: skew
plot_dt <- focal_df[which(focal_df$shape=="skew"),] %>% filter(best_mod=="gam") %>% filter(thresh_n_full %in% c(1, NA)) %>% filter(thresh_method =="zero_d1") %>% filter(sig_type==sig_choice) %>% filter(thresh_fraction >= sig_thresh)
#View(plot_dt)
# need to make dummy points for cases where results for all simulations were NA in order to keep the placeholder for the boxplot
plot_dt <- plot_dt %>% group_by(obs_error, ts_length, cov_sd, thresh_quant) %>% mutate(na_check = mean(thresh_diff, na.rm = T)) %>% ungroup()
plot_dt$thresh_diff <- ifelse(is.na(plot_dt$na_check)==T, -9999, plot_dt$thresh_diff)
################ effect of ts length ####################
levels <- matrix(NA, nrow = 3, ncol = 2) # levels for jittering points
levels[, 1] <- rep(NA, 3)
levels[,2] <- rep(c("15", "25", "35"), each = 1)
datasub <- plot_dt %>% filter(obs_error==obs_set[2], thresh_quant == quant_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
boxplot(thresh_diff ~ as.character(ts_length), data = datasub,
at = boxpos, col = adjustcolor(threshcols[3], alpha.f = 0.3), border = threshcols[3], outline = F, names = NA, xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n")
axis(side = 2, at = yaxt_at, las = 1)
#axis(side = 1, at = c(2.5, 8.5, 14.5), labels = c("15", "25", "35"), tick = F, line = -1)
#mtext(side = 1, "Time series length", line = 1.2)
mtext(side = 2, "Bias (mean estimate - true threshold)", line = 2)
abline(h = 0, lty = 2)
for(i in 1:nrow(levels)){
#prop <- length(datasub$sim[which(datasub$thresh_method==levels[i,1])]/nrow(datasub))
yvals <- datasub$thresh_diff[which(datasub$ts_length==as.numeric(levels[i, 2]))]
jtx <- jitter(rep(boxpos[i], length(yvals)), amount = jt_amount)# add jitter to x-axis indices proportional number in each level
points(jtx, yvals, pch=20, col=ifelse(datasub$thresh_method[1]=="abs_max_d2", threshcols2[1], ifelse(datasub$thresh_method[1]=="min_d2", threshcols2[2], ifelse(datasub$thresh_method[1]=="zero_d1", threshcols2[3], threshcols2[4]))))
}
# adding sample sizes
n_group <- datasub %>% group_by(ts_length) %>% summarize(n = length(which(is.na(thresh_diff)==F))) %>% arrange(ts_length)
boxbnds <- boxplot(thresh_diff ~ as.character(ts_length), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
################ effect of obs error ####################
levels <- matrix(NA, nrow = 3, ncol = 2) # levels for jittering points
levels[, 1] <- rep(NA, 3)
levels[,2] <- rep(as.character(obs_set), each = 1)
datasub <- plot_dt %>% filter(ts_length==ts_set[2], thresh_quant == quant_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
boxplot(thresh_diff ~ as.character(obs_error), data = datasub,
at = boxpos, col = adjustcolor(threshcols[3], alpha.f = 0.3), border = threshcols[3], outline = F, names = NA, xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n")
axis(side = 2, at = yaxt_at, labels = NA)
#axis(side = 1, at = c(2.5, 8.5, 14.5), labels = c("cv ~ 0.01", "cv ~ 0.2", "cv ~ 1"), tick = F, line = -1)
#mtext(side = 1, "Observation error", line = 1.2)
#mtext(side = 2, "Mean estimate - true threshold", line = 2)
abline(h = 0, lty = 2)
for(i in 1:nrow(levels)){
#prop <- length(datasub$sim[which(datasub$thresh_method==levels[i,1])]/nrow(datasub))
yvals <- datasub$thresh_diff[which(datasub$obs_error==as.numeric(levels[i, 2]))]
jtx <- jitter(rep(boxpos[i], length(yvals)), amount = jt_amount)# add jitter to x-axis indices proportional number in each level
points(jtx, yvals, pch=20, col=ifelse(datasub$thresh_method[1]=="abs_max_d2", threshcols2[1], ifelse(datasub$thresh_method[1]=="min_d2", threshcols2[2], ifelse(datasub$thresh_method[1]=="zero_d1", threshcols2[3], threshcols2[4]))))
}
# adding sample sizes
n_group <- datasub %>% group_by(obs_error) %>% summarize(n = length(which(is.na(thresh_diff)==F))) %>% arrange(obs_error)
boxbnds <- boxplot(thresh_diff ~ as.character(obs_error), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
################ effect of threshold quantile ####################
levels <- matrix(NA, nrow = 3, ncol = 2) # levels for jittering points
levels[, 1] <- rep(NA, 3)
levels[,2] <- rep(as.character(quant_set), each = 1)
datasub <- plot_dt %>% filter(ts_length==ts_set[2], obs_error == obs_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
boxplot(thresh_diff ~ as.character(thresh_quant), data = datasub,
at = boxpos, col = adjustcolor(threshcols[3], alpha.f = 0.3), border = threshcols[3], outline = F, names = NA, xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n")
axis(side = 2, at = yaxt_at, labels = NA)
abline(h = 0, lty = 2)
# add mini plot showing functional form
#mtext(side = 4, "SK", las = 2, padj = 5)
text(x = boxpos[length(boxpos)] - 0.23, y = -1.9, "SK", cex = 1.2)
rect(xleft = boxpos[length(boxpos)], ybottom= -2.5, xright = boxpos[length(boxpos)] + 1.3, ytop = -1.2, border = "black")
mini_x <- seq(from = boxpos[length(boxpos)], to = boxpos[length(boxpos)] + 0.7, length.out = 1000)
big_x <- seq(from = mini_x[length(mini_x)/2]-3, to = mini_x[length(mini_x)/2] + 3, length.out = 1000)
big_y <- dgamma(big_x, shape = mini_x[length(mini_x)/2] + 1, scale = 1)/max(dgamma(big_x, shape = mini_x[length(mini_x)/2] + 1, scale = 1)) # mode is at (shape -1 )*scale
mini_y <- (-1.25 + 2.3)*big_y-2.3
lines(x = mini_x, y = mini_y, type = "l") # -1.22 to go a little below the border
for(i in 1:nrow(levels)){
#prop <- length(datasub$sim[which(datasub$thresh_method==levels[i,1])]/nrow(datasub))
yvals <- datasub$thresh_diff[which(datasub$thresh_quant==as.numeric(levels[i, 2]))]
jtx <- jitter(rep(boxpos[i], length(yvals)), amount = jt_amount)# add jitter to x-axis indices proportional number in each level
points(jtx, yvals, pch=20, col=ifelse(datasub$thresh_method[1]=="abs_max_d2", threshcols2[1], ifelse(datasub$thresh_method[1]=="min_d2", threshcols2[2], ifelse(datasub$thresh_method[1]=="zero_d1", threshcols2[3], threshcols2[4]))))
}
# adding sample sizes
n_group <- datasub %>% group_by(thresh_quant) %>% summarize(n = length(which(is.na(thresh_diff)==F))) %>% arrange(thresh_quant)
boxbnds <- boxplot(thresh_diff ~ as.character(thresh_quant), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
# <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <- <-
# bottom row: hockeystick
plot_dt <- focal_df[which(focal_df$shape=="hockeystick"),] %>% filter(best_mod=="gam") %>% filter(thresh_n_full %in% c(1, NA)) %>% filter(thresh_method=="abs_max_d2") %>% filter(sig_type==sig_choice) %>% filter(thresh_fraction >= sig_thresh)
plot_dt <- plot_dt %>% group_by(obs_error, ts_length, cov_sd, thresh_quant) %>% mutate(na_check = mean(thresh_diff, na.rm = T)) %>% ungroup()
plot_dt$thresh_diff <- ifelse(is.na(plot_dt$na_check)==T, -9999, plot_dt$thresh_diff)
#View(plot_dt)
################ effect of ts length ####################
levels <- matrix(NA, nrow = 3, ncol = 2) # levels for jittering points
levels[, 1] <- rep(NA, 3)
levels[,2] <- rep(c("15", "25", "35"), each = 1)
datasub <- plot_dt %>% filter(obs_error==obs_set[2], thresh_quant == quant_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
boxplot(thresh_diff ~ as.character(ts_length), data = datasub,
at = boxpos, col = adjustcolor(threshcols[1], alpha.f = 0.3), border = threshcols[1], outline = F, names = NA, xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n")
axis(side = 2, at = yaxt_at, las = 1)
axis(side = 1, at = labpos, labels = c("15", "25", "35"), tick = F, line = -1)
mtext(side = 1, "Time series length", line = 1.2, cex = xlab_cex)
#mtext(side = 2, "Mean estimate - true threshold", line = 2)
abline(h = 0, lty = 2)
for(i in 1:nrow(levels)){
#prop <- length(datasub$sim[which(datasub$thresh_method==levels[i,1])]/nrow(datasub))
yvals <- datasub$thresh_diff[which(datasub$ts_length==as.numeric(levels[i, 2]))]
jtx <- jitter(rep(boxpos[i], length(yvals)), amount = jt_amount)# add jitter to x-axis indices proportional number in each level
points(jtx, yvals, pch=20, col=ifelse(datasub$thresh_method[1]=="abs_max_d2", threshcols2[1], ifelse(datasub$thresh_method[1]=="min_d2", threshcols2[2], ifelse(datasub$thresh_method[1]=="zero_d1", threshcols2[3], threshcols2[4]))))
}
# adding sample sizes
n_group <- datasub %>% group_by(ts_length) %>% summarize(n = length(which(is.na(thresh_diff)==F))) %>% arrange(ts_length)
boxbnds <- boxplot(thresh_diff ~ as.character(ts_length), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
################ effect of obs error ####################
levels <- matrix(NA, nrow = 3, ncol = 2) # levels for jittering points
levels[, 1] <- rep(NA, 3)
levels[,2] <- rep(as.character(obs_set), each = 1)
datasub <- plot_dt %>% filter(ts_length==ts_set[2], thresh_quant == quant_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
boxplot(thresh_diff ~ as.character(obs_error), data = datasub,
at = boxpos, col = adjustcolor(threshcols[1], alpha.f = 0.3), border = threshcols[1], outline = F, names = NA, xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n")
axis(side = 2, at = yaxt_at, labels = NA)
axis(side = 1, at = labpos, labels = c("cv ~ 0.01", "cv ~ 0.2", "cv ~ 1"), tick = F, line = -1)
mtext(side = 1, "Observation error", line = 1.2, cex = xlab_cex)
#mtext(side = 2, "Mean estimate - true threshold", line = 2)
abline(h = 0, lty = 2)
for(i in 1:nrow(levels)){
#prop <- length(datasub$sim[which(datasub$thresh_method==levels[i,1])]/nrow(datasub))
yvals <- datasub$thresh_diff[which(datasub$obs_error==as.numeric(levels[i, 2]))]
jtx <- jitter(rep(boxpos[i], length(yvals)), amount = jt_amount)# add jitter to x-axis indices proportional number in each level
points(jtx, yvals, pch=20, col=ifelse(datasub$thresh_method[1]=="abs_max_d2", threshcols2[1], ifelse(datasub$thresh_method[1]=="min_d2", threshcols2[2], ifelse(datasub$thresh_method[1]=="zero_d1", threshcols2[3], threshcols2[4]))))
}
# adding sample sizes
n_group <- datasub %>% group_by(obs_error) %>% summarize(n = length(which(is.na(thresh_diff)==F))) %>% arrange(obs_error)
boxbnds <- boxplot(thresh_diff ~ as.character(obs_error), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
################ effect of threshold quantile ####################
levels <- matrix(NA, nrow = 3, ncol = 2) # levels for jittering points
levels[, 1] <- rep(NA, 3)
levels[,2] <- rep(as.character(quant_set), each = 1)
datasub <- plot_dt %>% filter(ts_length==ts_set[2], obs_error == obs_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
boxplot(thresh_diff ~ as.character(thresh_quant), data = datasub,
at = boxpos, col = adjustcolor(threshcols[1], alpha.f = 0.3), border = threshcols[1], outline = F, names = NA, xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n")
axis(side = 2, at = yaxt_at, labels = NA)
axis(side = 1, at = labpos, labels = c("0.05", "0.25", "0.5"), tick = F, line = -1)
mtext(side = 1, "Threshold quantile", line = 1.2, cex = xlab_cex)
#mtext(side = 4, "HS", las = 2)
#mtext(side = 2, "Mean estimate - true threshold", line = 2)
abline(h = 0, lty = 2)
# add mini plot showing functional form
#mtext(side = 4, "HS", las = 2, padj = 5)
text(x = boxpos[length(boxpos)] - 0.23, y = -1.9, "HS", cex = 1.2)
rect(xleft = boxpos[length(boxpos)], ybottom= -2.5, xright = boxpos[length(boxpos)] + 1.3, ytop = -1.2, border = "black")
mini_x <- seq(from = boxpos[length(boxpos)], to = boxpos[length(boxpos)] + 0.7, length.out = 1000)
big_x <- seq(from = mini_x[length(mini_x)/2]-3, to = mini_x[length(mini_x)/2] + 3, length.out = 1000)
big_y <- ifelse(big_x < 0, 0, ifelse(big_x < mini_x[length(mini_x)/2], (0.9-0)/ (mini_x[length(mini_x)/2] - 0) * (big_x - mini_x[length(mini_x)/2]) + 0.9, 0.9))
mini_y <- (-1.25 + 2.3)*big_y-2.3
lines(x = mini_x, y = mini_y, type = "l") # -1.22 to go a little below the border
for(i in 1:nrow(levels)){
#prop <- length(datasub$sim[which(datasub$thresh_method==levels[i,1])]/nrow(datasub))
yvals <- datasub$thresh_diff[which(datasub$thresh_quant==as.numeric(levels[i, 2]))]
jtx <- jitter(rep(boxpos[i], length(yvals)), amount = jt_amount)# add jitter to x-axis indices proportional number in each level
points(jtx, yvals, pch=20, col=ifelse(datasub$thresh_method[1]=="abs_max_d2", threshcols2[1], ifelse(datasub$thresh_method[1]=="min_d2", threshcols2[2], ifelse(datasub$thresh_method[1]=="zero_d1", threshcols2[3], threshcols2[4]))))
}
# adding sample sizes
n_group <- datasub %>% group_by(thresh_quant) %>% summarize(n = length(which(is.na(thresh_diff)==F))) %>% arrange(thresh_quant)
boxbnds <- boxplot(thresh_diff ~ as.character(thresh_quant), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
# plot 10: dummy plot for legend
plot(0,type='n',axes=FALSE, ann = FALSE)
#legend(0.8, 0.2, col = threshcols, pch = 16, legend = c("max(|s''(x)|)", "min(s''(x))", "s'(x)=0", "s''(x)=0"), bty = "n", ncol =4, cex = 1.1, xpd = TRUE, inset = c(0,-0.5)) #, title = "Threshold calculation method"
#text(0.75, -0.4, "Threshold \ncalc. method", xpd = TRUE)
legend(0.8, 0.16, col = threshcols[1:3], pch = 16, legend = c("max(|s''(x)|)", "min(s''(x))", "s'(x)=0"), bty = "n", ncol =4, cex = 1.1, xpd = TRUE, inset = c(0,-0.5)) #, title = "Threshold calculation method"
text(0.75, -0.6, "Threshold def.", xpd = TRUE)
dev.off()
```
## estimate precision
plot width of confidence intervals around threshold estimates
### CIs beeswarm
beeswarm plot in base R:
https://r-charts.com/distribution/beeswarm/
more details on the function:
https://www.rdocumentation.org/packages/beeswarm/versions/0.4.0/topics/beeswarm
```{r}
# 3x3 figure w/ boxplots
boxpos <- c(1, 2, 3)#c(1:4, 7:10) # positions of the boxes
labpos <- c(1, 2, 3) # position of box labels on x axis
n_adj <-0.8 #0.5 # how far from top of box to adjust sample sizes
n_size <- 1 # font size of sample sizes
max_adj <- 0 # adjust ymax
xlab_cex <- 0.8 # size to make x axis labels
col_cex <- 1.2 # font for column labels
# point type by condition: 1st element = CI int entirely in risk prone region, 2nd = CI in entirely in risk averse region, and 3rd = CI int includes true value
pt_types <- c(1, 15, 8)
# colors change depending on thresh_method used
pt_alphas <- c(0.8, 0.4, 0.8)
thresh_cols1 <- c(adjustcolor(threshcols[1], alpha.f = pt_alphas[1]), adjustcolor(threshcols[1], alpha.f = pt_alphas[2]), adjustcolor(threshcols[1], alpha.f = pt_alphas[3]))
thresh_cols2 <- c(adjustcolor(threshcols[2], alpha.f = pt_alphas[1]), adjustcolor(threshcols[2], alpha.f = pt_alphas[2]), adjustcolor(threshcols[2], alpha.f = pt_alphas[3]))
thresh_cols3 <- c(adjustcolor(threshcols[3], alpha.f = pt_alphas[1]), adjustcolor(threshcols[3], alpha.f = pt_alphas[2]), adjustcolor(threshcols[3], alpha.f = pt_alphas[3]))
corral_type <- "wrap" # how to deal with observations that go out of the plot boundaries. "gutter" = pile them all on the ends, "wrap" = wrap them back around each other
plot_dt <- focal_df %>% filter(best_mod=="gam") %>% filter(thresh_n_full %in% c(1, NA)) %>% filter(sig_type==sig_choice) %>% filter(thresh_fraction >= sig_thresh) %>% mutate(ci_int = ci_em_up-ci_em_low) %>% mutate(thresh_true = thresh_mean-thresh_diff) %>% mutate(inc_true = if_else(ci_em_up > thresh_true & ci_em_low < thresh_true, 1, 0)) %>% mutate(averse_frac = if_else(ci_em_low >= thresh_true, 1, if_else(ci_em_up <= thresh_true, 0, (ci_em_up-thresh_true)/(ci_em_up-ci_em_low))))
ymin <- min(plot_dt$ci_int, na.rm = T)
ymax <- 3#max(plot_dt$ci_int, na.rm = T)+max_adj
#ymax <- 1
#yaxt_at <- c(-2, -1, 0, 1, 2)# where to put y axis labels
yaxt_at <- round(seq(from = ymin, to = ymax, by = 1), 0)# where to put y axis labels
ylabs <- seq(from = ymin, to = ymax, by = 1)
pdf("figurepdfs/Best_CI_BSw_new.pdf", width = 7, height = 6) # width = 6.5
par(mfrow = c(4, 3))
layout(matrix(c(1, 4, 7, 10, 2, 5, 8, 10, 3, 6, 9, 10), nrow = 4, ncol = 3), widths = c(1, 1, 1), heights = c(1, 1, 1, 0.3))
#par(mar=c(0.1, 1.5, 0.5, 0), oma = c(1, 2, 1.2, 3))
par(mar=c(0.1, 0.5, 0.5, 0), oma = c(1, 3, 1.2, 0.5))
# top row: sigmoidal
plot_dt <- focal_df[which(focal_df$shape=="sigmoidal"),] %>% filter(best_mod=="gam") %>% filter(thresh_n_full %in% c(1, NA)) %>% filter(sig_type==sig_choice) %>% filter(thresh_fraction >= sig_thresh) %>% filter(thresh_method=="min_d2") %>% mutate(ci_int = ci_em_up-ci_em_low) %>% mutate(thresh_true = thresh_mean-thresh_diff) %>% mutate(inc_true = if_else(ci_em_up > thresh_true & ci_em_low < thresh_true, 1, 0)) %>% mutate(averse_frac = if_else(ci_em_low >= thresh_true, 1, if_else(ci_em_up <= thresh_true, 0, (ci_em_up-thresh_true)/(ci_em_up-ci_em_low)))) %>% arrange(averse_frac)
#View(plot_dt)
# need to make dummy points for cases where results for all simulations were NA in order to keep the placeholder for the boxplot
plot_dt <- plot_dt %>% group_by(obs_error, ts_length, cov_sd, thresh_quant) %>% mutate(na_check = mean(ci_int, na.rm = T)) %>% ungroup()
plot_dt$ci_int <- ifelse(is.na(plot_dt$na_check)==T, -9999, plot_dt$ci_int)
################ effect of ts length ####################
datasub <- plot_dt %>% filter(obs_error==obs_set[2], thresh_quant == quant_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
beeswarm(ci_int ~ as.character(ts_length), data = datasub,
at = boxpos, pwcol = ifelse(datasub$averse_frac==0, thresh_cols2[1], ifelse(datasub$averse_frac==1, thresh_cols2[2], thresh_cols2[3])), pwpch = ifelse(datasub$averse_frac==0, pt_types[1], ifelse(datasub$averse_frac==1, pt_types[2], pt_types[3])), xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n", method = "center", corral = corral_type)
axis(side = 2, at = yaxt_at, las = 1)
#axis(side = 1, at = c(2.5, 8.5, 14.5), labels = c("15", "25", "35"), tick = F, line = -1)
mtext(side = 3, "Time series length", line = 0)
#mtext(side = 2, "Mean estimate - true threshold", line = 2)
abline(h = 0, lty = 2)
#pwpch = ifelse(datasub$averse_frac==0, 0,15)
legend("topleft", legend = c("fully in risk-averse side", "fully in risk-prone side", "includes true value"), pch = c(15, 1, 8), col = c(adjustcolor("gray40", alpha.f = 0.5), adjustcolor("gray40", alpha.f = 0.9), adjustcolor("gray40", alpha.f = 0.9)), bty = "n")
# adding sample sizes
n_group <- datasub %>% group_by(ts_length) %>% summarize(n = length(which(is.na(ci_int)==F))) %>% arrange(ts_length)
boxbnds <- boxplot(ci_int ~ as.character(ts_length), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
################ effect of obs error ####################
datasub <- plot_dt %>% filter(ts_length==ts_set[2], thresh_quant == quant_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
beeswarm(ci_int ~ as.character(obs_error), data = datasub,
at = boxpos, pwcol = ifelse(datasub$averse_frac==0, thresh_cols2[1], ifelse(datasub$averse_frac==1, thresh_cols2[2], thresh_cols2[3])), pwpch = ifelse(datasub$averse_frac==0, pt_types[1], ifelse(datasub$averse_frac==1, pt_types[2], pt_types[3])), xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n", method = "center", corral = corral_type)
axis(side = 2, at = yaxt_at, labels = NA)
#axis(side = 1, at = c(2.5, 8.5, 14.5), labels = c("cv ~ 0.01", "cv ~ 0.2", "cv ~ 1"), tick = F, line = -1)
mtext(side = 3, "Observation error", line = 0)
#mtext(side = 2, "Mean estimate - true threshold", line = 2)
abline(h = 0, lty = 2)
# adding sample sizes
n_group <- datasub %>% group_by(obs_error) %>% summarize(n = length(which(is.na(ci_int)==F))) %>% arrange(obs_error)
boxbnds <- boxplot(ci_int ~ as.character(obs_error), data = datasub,at = boxpos, plot = F)$stats
text(x = boxpos, y = boxbnds[nrow(boxbnds), ] + n_adj, as.character(n_group$n), cex = n_size)
################ effect of threshold quantile ####################
datasub <- plot_dt %>% filter(ts_length==ts_set[2], obs_error == obs_set[2], cov_sd == cov_sd_set2[1]) #%>% distinct()
#View(datasub)
beeswarm(ci_int ~ as.character(thresh_quant), data = datasub,
at = boxpos, pwcol = ifelse(datasub$averse_frac==0, thresh_cols2[1], ifelse(datasub$averse_frac==1, thresh_cols2[2], thresh_cols2[3])), pwpch = ifelse(datasub$averse_frac==0, pt_types[1], ifelse(datasub$averse_frac==1, pt_types[2], pt_types[3])), xaxt = "n", las = 1, xlab = NA, ylab = NA, ylim = c(ymin, ymax), yaxt = "n", method = "center", corral = corral_type)
axis(side = 2, at = yaxt_at, labels = NA)
#mtext(side = 4, "SG", las = 2)
#axis(side = 1, at = c(2.5, 8.5, 14.5), labels = c("0.05", "0.25", "0.5"), tick = F, line = -1)
mtext(side = 3, "Threshold quantile", line = 0)
#mtext(side = 2, "Mean estimate - true threshold", line = 2)
abline(h = 0, lty = 2)