-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.R
More file actions
1397 lines (1164 loc) · 49 KB
/
server.R
File metadata and controls
1397 lines (1164 loc) · 49 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
################################################################################
### Load Packages
################################################################################
library(shiny)
library(tidyverse)
library(shinyjs)
library(rclipboard)
library(fedmatch)
library(googlesheets4)
# install.packages("remotes")
# remotes::install_github("deepanshu88/shinyDarkmode")
library(shinyDarkmode)
#devtools::install_github("konfound-project/konfound")
library(konfound)
source("calculations-rir-itcv.R")
source("calculations-pse.R")
source("calculations-cop.R")
source("calculations-log.R")
################################################################################
### Set up Shiny Server
################################################################################
jscode <- "shinyjs.refresh_page = function() { history.go(0); }"
if (file.exists("konfound-shiny-service-account-key.json")) {
gs4_auth(path = "konfound-shiny-service-account-key.json")
}
EMAIL_SHEET_ID <- "https://docs.google.com/spreadsheets/d/1sO4ADOz_tBTnFcg0poty_LSUHRypWS3yXPLB-k7f5qA"
server <- function(input, output, session) {
darkmode(buttonColorDark = "#7f9f3d", # Background color of the button while in lightmode
buttonColorLight = "#639dad", # Background color of the button while in darkmode
backgroundColor = "#fff", # Background color of the page while in lightmode
mixColor = "#fff", # Color used to generate darkmode: lighter colors create darker darkmode
label = "<strong>|</strong>", # Text that shows up on the darkmode button
bottom = "32px",
right = "16px",
autoMatchOsTheme = FALSE
)
r <- reactiveValues(print_results1 = "") # Create empty reactive string for printed results (formatted).
r <- reactiveValues(print_results2 = "") # Create empty reactive string for printed results (raw).
observeEvent(input$submit_email_button, {
req(input$user_email) # Stop if empty
new_data <-
data.frame(
Timestamp = Sys.time(),
Email = input$user_email
)
# Save the data
tryCatch({
sheet_append(EMAIL_SHEET_ID, new_data)
output$confirmation <-
renderText("Success! We received your email. Thank you!")
updateTextInput(session, "user_email", value = "")
}, error = function(e) {
output$confirmation <-
renderText("Error: Could not save your email.")
})
})
################################################################################
### GENERATE LINEAR RIR + LINEAR ITCV RESULTS
### Generalized Robustness of Inference to Replacement (RIR)
### Impact Threshold for a Confounding Variable (ITCV)
################################################################################
df_linear <-
eventReactive(input$results_pg_l, get_rir_itcv_results(input))
##############################################################################
### GENERATE PRINTED OUTPUT AND SHOW LINEAR RIR + LINEAR ITCV RESULTS
##############################################################################
observeEvent(input$results_pg_l, {
output$print_results1 <-
renderText({
df_linear()$text # Combine text output lines
})
output$fig_results <-
renderPlot({
df_linear()$plot # Render the plot output
})
output$print_results2 <-
renderText({
paste(df_linear()$raw , collapse = "\n") # Combine text output lines
})
})
##############################################################################
### GENERATE LINEAR RIR + LINEAR ITCV R CODE
##############################################################################
user_est_l <-
eventReactive(input$results_pg_l, {
paste0(
"# install.packages('konfound')", "\n",
"library(konfound) # konfound R package version: ", packageVersion("konfound"), "\n",
"pkonfound(est_eff = ", input$est_effect_rir_ee,
", std_err = ", input$std_error_rir_ee,
", n_obs = ", input$n_obs_rir_ee,
", n_covariates = ", input$n_covariates_rir_ee,
", index = ", "'", input$AnalysisL, "')"
)
})
user_est_l_default <-
eventReactive(input$results_pg_l, {
paste0(
"# install.packages('konfound')", "\n",
"library(konfound) # konfound R package version: ", packageVersion("konfound"), "\n",
"# help(pkonfound) # Check this help page for more details on default arguments", "\n",
"pkonfound(est_eff = ", input$est_effect_rir_ee,
", std_err = ", input$std_error_rir_ee,
", n_obs = ", input$n_obs_rir_ee,
", n_covariates = ", input$n_covariates_rir_ee, ",", "\n\t",
"sdx = NA, sdy = NA, R2 = NA, alpha = 0.05, tails = 2, nu = 0,", "\n\t",
"far_bound = 0, eff_thr = NA,", "\n\t",
"upper_bound = NULL, lower_bound = NULL,",
", to_return = 'print', index = ", "'", input$AnalysisL, "')"
)
})
user_est_l_ci <-
eventReactive(input$results_pg_l, {
paste0(
"# install.packages('konfound')", "\n",
"library(konfound) # konfound R package version: ", packageVersion("konfound"), "\n",
"pkonfound(lower_bound = ", input$lower_bnd_rir_ci,
", upper_bound = ", input$upper_bnd_rir_ci,
", n_obs = ", input$n_obs_rir_ci,
", n_covariates = ", input$n_covariates_rir_ci, ",", "\n\t",
"sdx = NA, sdy = NA, R2 = NA, index = '", input$AnalysisL, "')"
)
})
user_est_l_ci_default <-
eventReactive(input$results_pg_l, {
paste0(
"# install.packages('konfound')", "\n",
"library(konfound) # konfound R package version: ", packageVersion("konfound"), "\n",
"# help(pkonfound) # Check this help page for more details on default arguments", "\n",
"pkonfound(lower_bound = ", input$lower_bnd_rir_ci,
", upper_bound = ", input$upper_bnd_rir_ci,
", n_obs = ", input$n_obs_rir_ci,
", n_covariates = ", input$n_covariates_rir_ci, ",", "\n\t",
"sdx = NA, sdy = NA, R2 = NA, alpha = 0.05, tails = 2, nu = 0,", "\n\t",
"far_bound = 0, eff_thr = NA, to_return = 'print', index = '", input$AnalysisL, "')"
)
})
##############################################################################
### GENERATE LINEAR RIR + LINEAR ITCV STATA CODE
##############################################################################
s_user_est_l <-
eventReactive(input$results_pg_l, {
paste0(
"ssc install konfound", "\n",
"ssc install indeplist", "\n",
"ssc install moss", "\n",
"ssc install matsort", "\n",
"pkonfound ",
input$est_effect_rir_ee, " ",
input$std_error_rir_ee, " ",
input$n_obs_rir_ee, " ",
input$n_covariates_rir_ee,
", model_type(0) indx(", input$AnalysisL, ")"
)
})
s_user_est_l_default <-
eventReactive(input$results_pg_l, {
paste0(
"ssc install konfound", "\n",
"ssc install indeplist", "\n",
"ssc install moss", "\n",
"ssc install matsort", "\n",
"* help pkonfound // Check this help page for more details on default arguments", "\n",
"pkonfound ",
input$est_effect_rir_ee, " ",
input$std_error_rir_ee, " ",
input$n_obs_rir_ee, " ",
input$n_covariates_rir_ee,
", sig(0.05) nu(0) onetail(0) sdx(NA) sdy(NA) rs(NA) far_bound(0) eff_thr(NA) model_type(0) indx(",
input$AnalysisL, ")"
)
})
##############################################################################
### GENERATE PSE RESULTS
### Preserve Standard Error (PSE)
##############################################################################
df_pse <-
eventReactive(input$results_pg_pse, get_pse_results(input))
##############################################################################
### GENERATE PRINTED OUTPUT AND SHOW PSE RESULTS
##############################################################################
observeEvent(input$results_pg_pse, {
output$print_results1 <-
renderText({
df_pse()$text # Combine text output lines
})
# Render a dummy plot with text message
output$fig_results <-
renderPlot({
message <- df_pse()$plot_message
plot.new()
text(0.5, 0.5, message, cex = 1.5, col = "black", font = 1.8)
})
output$print_results2 <-
renderText({
paste(df_pse()$raw, collapse = "\n") # Combine text output lines
})
})
##############################################################################
### GENERATE PSE R CODE
##############################################################################
user_est_pse <-
eventReactive(input$results_pg_pse, {
paste0(
"# install.packages('konfound')", "\n",
"library(konfound) # konfound R package version: ",
packageVersion("konfound"), "\n",
"pkonfound(est_eff = ", input$est_effect_pse_ee,
", std_err = ", input$std_err_pse_ee,
", n_obs = ", input$n_obs_pse_ee,
", n_covariates = ", input$n_covariates_pse_ee, ",", "\n\t",
"eff_thr = ", input$eff_thr_pse_ee,
", sdx = ", input$sdx_pse_ee,
", sdy = ", input$sdy_pse_ee,
", R2 = ", input$R2_pse_ee, ",", "\n\t",
"upper_bound = NULL, lower_bound = NULL,", "\n\t",
"index = 'PSE')"
)
})
user_est_pse_default <-
eventReactive(input$results_pg_pse, {
paste0(
"# install.packages('konfound')", "\n",
"library(konfound) # konfound R package version: ",
packageVersion("konfound"), "\n",
"# help(pkonfound) # Check this help page for more details on default arguments \n",
"pkonfound(est_eff = ", input$est_effect_pse_ee,
", std_err = ", input$std_err_pse_ee,
", n_obs = ", input$n_obs_pse_ee,
", n_covariates = ", input$n_covariates_pse_ee, ",", "\n\t",
"eff_thr = ", input$eff_thr_pse_ee,
", sdx = ", input$sdx_pse_ee,
", sdy = ", input$sdy_pse_ee,
", R2 = ", input$R2_pse_ee, ",", "\n\t",
"upper_bound = NULL, lower_bound = NULL,", "\n\t",
"to_return = 'print', index = 'PSE')"
)
})
user_est_pse_ci <-
eventReactive(input$results_pg_pse, {
paste0(
"# install.packages('konfound')", "\n",
"library(konfound) # konfound R package version: ",
packageVersion("konfound"), "\n",
"pkonfound(lower_bound = ", input$lower_bnd_pse_ci,
", upper_bound = ", input$upper_bnd_pse_ci,
", n_obs = ", input$n_obs_pse_ci,
", n_covariates = ", input$n_covariates_pse_ci, ",", "\n\t",
"eff_thr = ", input$eff_thr_pse_ci,
", sdx = ", input$sdx_pse_ci,
", sdy = ", input$sdy_pse_ci,
", R2 = ", input$R2_pse_ci,
", index = 'PSE')"
)
})
user_est_pse_ci_default <-
eventReactive(input$results_pg_pse, {
paste0(
"# install.packages('konfound')", "\n",
"library(konfound) # konfound R package version: ",
packageVersion("konfound"), "\n",
"# help(pkonfound) # Check this help page for more details on default arguments\n",
"pkonfound(lower_bound = ", input$lower_bnd_pse_ci,
", upper_bound = ", input$upper_bnd_pse_ci,
", n_obs = ", input$n_obs_pse_ci,
", n_covariates = ", input$n_covariates_pse_ci, ",", "\n\t",
"eff_thr = ", input$eff_thr_pse_ci,
", sdx = ", input$sdx_pse_ci,
", sdy = ", input$sdy_pse_ci,
", R2 = ", input$R2_pse_ci, ",", "\n\t",
"to_return = 'print', index = 'PSE')"
)
})
##############################################################################
### GENERATE PSE STATA CODE
##############################################################################
s_user_est_pse <-
eventReactive(input$results_pg_pse, {
paste0(
"ssc install konfound", "\n",
"pkonfound ",
input$est_effect_pse_ee, " ",
input$std_err_pse_ee, " ",
input$n_obs_pse_ee, " ",
input$n_covariates_pse_ee, " ",
input$eff_thr_pse_ee, " ",
input$sdx_pse_ee, " ",
input$sdy_pse_ee, " ",
input$R2_pse_ee, ", eff_thr(",
input$eff_thr_pse_ee, ") indx(PSE)"
)
})
s_user_est_pse_default <-
eventReactive(input$results_pg_pse, {
paste0(
"ssc install konfound", "\n",
"* help pkonfound // Check this help page for more details on default arguments", "\n",
"pkonfound ",
input$est_effect_pse_ee, " ",
input$std_err_pse_ee, " ",
input$n_obs_pse_ee, " ",
input$n_covariates_pse_ee, " ",
input$eff_thr_pse_ee, " ",
input$sdx_pse_ee, " ",
input$sdy_pse_ee, " ",
input$R2_pse_ee, ", eff_thr(",
input$eff_thr_pse_ee, ") indx(PSE)"
)
})
################################################################################
### GENERATE COP RESULTS
### Coefficient of Proportionality (COP)
################################################################################
df_cop <-
eventReactive(input$results_pg_cop, get_cop_results(input))
##############################################################################
### GENERATE PRINTED OUTPUT AND SHOW COP RESULTS
##############################################################################
observeEvent(input$results_pg_cop, {
output$print_results1 <- renderText({
df_cop()$text # Combine text output lines
})
output$fig_results <- renderPlot({
df_cop()$plot # Render the plot output
})
output$print_results2 <- renderText({
paste(df_cop()$raw, collapse = "\n") # Combine text output lines
})
})
##############################################################################
### GENERATE COP R CODE
##############################################################################
user_est_cop <-
eventReactive(input$results_pg_cop, {
paste0(
"# install.packages('konfound')", "\n",
"library(konfound) # konfound R package version: ",
packageVersion("konfound"), "\n",
"pkonfound(est_eff = ", input$est_effect_cop_ee,
", std_err = ", input$std_err_cop_ee,
", n_obs = ", input$n_obs_cop_ee,
", n_covariates = ", input$n_covariates_cop_ee, ",", "\n\t",
"sdx = ", input$sdx_cop_ee,
", sdy = ", input$sdy_cop_ee,
", R2 = ", input$R2_cop_ee,
", eff_thr = ", input$eff_thr_cop_ee,
", FR2max = ", input$FR2max_cop_ee, ",", "\n\t",
"upper_bound = NULL, lower_bound = NULL,", "\n\t",
"index = 'COP')"
)
})
user_est_cop_default <-
eventReactive(input$results_pg_cop, {
paste0(
"# install.packages('konfound')", "\n",
"library(konfound) # konfound R package version: ", packageVersion("konfound"), "\n",
"# help(pkonfound) # Check this help page for more details on default arguments \n",
"pkonfound(est_eff = ", input$est_effect_cop_ee,
", std_err = ", input$std_err_cop_ee,
", n_obs = ", input$n_obs_cop_ee,
", n_covariates = ", input$n_covariates_cop_ee, ",", "\n\t",
"sdx = ", input$sdx_cop_ee,
", sdy = ", input$sdy_cop_ee,
", R2 = ", input$R2_cop_ee,
", eff_thr = ", input$eff_thr_cop_ee,
", FR2max = ", input$FR2max_cop_ee, ",", "\n\t",
"alpha = 0.05, tails = 2, ",
"upper_bound = NULL, lower_bound = NULL,", "\n\t",
"index = 'COP')"
)
})
user_est_cop_ci <-
eventReactive(input$results_pg_cop, {
paste0(
"# install.packages('konfound')", "\n",
"library(konfound) # konfound R package version: ", packageVersion("konfound"), "\n",
"pkonfound(lower_bound = ", input$lower_bnd_cop_ci,
", upper_bound = ", input$upper_bnd_cop_ci,
", n_obs = ", input$n_obs_cop_ci,
", n_covariates = ", input$n_covariates_cop_ci, ",", "\n\t",
"sdx = ", input$sdx_cop_ci,
", sdy = ", input$sdy_cop_ci,
", R2 = ", input$R2_cop_ci,
", eff_thr = ", input$eff_thr_cop_ci,
", FR2max = ", input$FR2max_cop_ci,
", index = 'COP')"
)
})
user_est_cop_ci_default <-
eventReactive(input$results_pg_cop, {
paste0(
"# install.packages('konfound')", "\n",
"library(konfound) # konfound R package version: ", packageVersion("konfound"), "\n",
"# help(pkonfound) # Check this help page for more details on default arguments\n",
"pkonfound(lower_bound = ", input$lower_bnd_cop_ci,
", upper_bound = ", input$upper_bnd_cop_ci,
", n_obs = ", input$n_obs_cop_ci,
", n_covariates = ", input$n_covariates_cop_ci, ",", "\n\t",
"sdx= ", input$sdx_cop_ci,
", sdy = ", input$sdy_cop_ci,
", R2 = ", input$R2_cop_ci,
", eff_thr = ", input$eff_thr_cop_ci,
", FR2max = ", input$FR2max_cop_ci, ",", "\n\t",
"alpha = 0.05, tails = 2, index = 'COP')"
)
})
##############################################################################
### GENERATE COP STATA CODE
##############################################################################
s_user_est_cop <-
eventReactive(input$results_pg_cop, {
paste0(
"ssc install konfound", "\n",
"pkonfound ",
input$est_effect_cop_ee, " ",
input$std_err_cop_ee, " ",
input$n_obs_cop_ee, " ",
input$n_covariates_cop_ee, " ",
input$sdx_cop_ee, " ",
input$sdy_cop_ee, " ",
input$R2_cop_ee,
", eff_thr(", input$eff_thr_cop_ee, ") fr2max(", input$FR2max_cop_ee,
") indx(COP)"
)
})
s_user_est_cop_default <-
eventReactive(input$results_pg_cop, {
paste0(
"ssc install konfound", "\n",
"* help pkonfound // Check this help page for more details on default arguments", "\n",
"pkonfound ",
input$est_effect_cop_ee, " ",
input$std_err_cop_ee, " ",
input$n_obs_cop_ee, " ",
input$n_covariates_cop_ee, " ",
input$sdx_cop_ee, " ",
input$sdy_cop_ee, " ",
input$R2_cop_ee,
", eff_thr(", input$eff_thr_cop_ee, ") fr2max(", input$FR2max_cop_ee,
") sig(0.05) onetail(0) indx(COP)"
)
})
################################################################################
### GENERATE LOG RESULTS
### Coefficient of Proportionality (COP)
################################################################################
df_log <-
eventReactive(input$results_pg_di, get_log_results(input))
##############################################################################
### GENERATE PRINTED OUTPUT AND SHOW LOG RESULTS
##############################################################################
observeEvent(input$results_pg_di, {
output$print_results1 <- renderText({
df_log()$text # Combine text output lines
})
# Render a dummy plot with text message
output$fig_results <- renderPlot({
if (!is.null(df_log()$plot)) {
df_log()$plot
} else {
plot.new()
text(0.5, 0.5, df_log()$plot_message, cex = 1.5, col = "black", font = 1.8)
}
})
output$print_results2 <- renderText({
paste(df_log()$raw, collapse = "\n") # Combine text output lines
})
})
##############################################################################
### GENERATE LOG R CODE
##############################################################################
user_est_di <-
eventReactive(input$results_pg_di, {
paste0(
"# install.packages('konfound')", "\n",
"library(konfound) # konfound R package version: ",
packageVersion("konfound"), "\n",
"pkonfound(est_eff = ", input$est_effect_log_ee,
", std_err = ", input$std_error_log_ee,
", n_obs = ", input$n_obs_log_ee,
", n_covariates = ", input$n_covariates_log_ee, ",", "\n\t",
"n_treat = ", input$n_trm_log_ee,
"upper_bound = NULL, lower_bound = NULL,", "\n\t",
"model_type = 'logistic')"
)
})
user_est_di_default <-
eventReactive(input$results_pg_di, {
paste0(
"# install.packages('konfound')", "\n",
"library(konfound) # konfound R package version: ", packageVersion("konfound"), "\n",
"# help(pkonfound) # Check this help page for more details on default arguments \n",
"pkonfound(est_eff = ", input$est_effect_log_ee,
", std_err = ", input$std_error_log_ee,
", n_obs = ", input$n_obs_log_ee,
", n_covariates = ", input$n_covariates_log_ee, ",", "\n\t",
"n_treat = ", input$n_trm_log_ee,
", alpha = 0.05, tails = 2, nu = 0, switch_trm = TRUE, replace = 'control',", "\n\t",
"upper_bound = NULL, lower_bound = NULL,", "\n\t",
"to_return = 'print', model_type = 'logistic')"
)
})
user_est_di_ci <-
eventReactive(input$results_pg_di, {
paste0(
"# install.packages('konfound')", "\n",
"library(konfound) # konfound R package version: ", packageVersion("konfound"), "\n",
"pkonfound(lower_bound = ", input$lower_bnd_log_ci,
", upper_bound = ", input$upper_bnd_log_ci,
", n_obs = ", input$n_obs_log_ci,
", n_covariates = ", input$n_covariates_log_ci, ",", "\n\t",
"n_treat = ", input$n_trm_log_ci,
", model_type = 'logistic')"
)
})
user_est_di_ci_default <-
eventReactive(input$results_pg_di, {
paste0(
"# install.packages('konfound')", "\n",
"library(konfound) # konfound R package version: ", packageVersion("konfound"), "\n",
"# help(pkonfound) # Check this help page for more details on default arguments", "\n",
"pkonfound(lower_bound = ", input$lower_bnd_log_ci,
", upper_bound = ", input$upper_bnd_log_ci,
", n_obs = ", input$n_obs_log_ci,
", n_covariates = ", input$n_covariates_log_ci, ",", "\n\t",
"n_treat = ", input$n_trm_log_ci,
", alpha = 0.05, tails = 2, nu = 0, switch_trm = TRUE, replace = 'control',", "\n\t",
"to_return = 'print', model_type = 'logistic')"
)
})
##############################################################################
### GENERATE LOG STATA CODE
##############################################################################
s_user_est_di <-
eventReactive(input$results_pg_di, {
paste0(
"ssc install konfound", "\n",
"pkonfound ",
input$est_effect_log_ee, " ",
input$std_error_log_ee, " ",
input$n_obs_log_ee, " ",
input$n_covariates_log_ee, " ",
input$n_trm_log_ee,
", model_type(1)"
)
})
s_user_est_di_default <-
eventReactive(input$results_pg_di, {
paste0(
"ssc install konfound", "\n",
"* help pkonfound // Check this help page for more details on default arguments", "\n",
"pkonfound ",
input$est_effect_log_ee, " ",
input$std_error_log_ee, " ",
input$n_obs_log_ee, " ",
input$n_covariates_log_ee, " ",
input$n_trm_log_ee,
", sig(0.05) onetail(0) switch_trm(1) replace(1) model_type(1)"
)
})
################################################################################
### GENERATE 2x2 RESULTS
################################################################################
# If user presses the results button for 2x2 tables, show the 2x2 tables
df_twobytwo <-
eventReactive(input$results_pg_2x2, {
validate(
need(is.numeric(input$ctrl_fail) &
is.numeric(input$ctrl_success) &
is.numeric(input$treat_fail) &
is.numeric(input$treat_success),
"Did not run! Did you enter numbers for the input values? Please change any of these that are not to a number."),
need(input$ctrl_fail > 0, "Did not run! Control Condition: Result Failure needs to be greater than zero"),
need(input$ctrl_success > 0, "Did not run! Control Condition: Result Success needs to be greater than zero"),
need(input$treat_fail > 0, "Did not run! Treatment Condition: Result Failure needs to be greater than zero"),
need(input$treat_success > 0, "Did not run! Treatment Condition: Result Success needs to be greater than zero")
)
# Capture the full output of the pkonfound function as a single string with proper line breaks
twobytwo_output_raw <-
capture.output(
pkonfound(a = input$ctrl_fail,
b = input$ctrl_success,
c = input$treat_fail,
d = input$treat_success,
to_return = "print")
)
raw_calc <-
pkonfound(a = input$ctrl_fail,
b = input$ctrl_success,
c = input$treat_fail,
d = input$treat_success,
to_return = "raw_output")
# Extract key input values from user-entered fields
a = input$ctrl_fail
b = input$ctrl_success
c = input$treat_fail
d = input$treat_success
# Extract key fields from raw_calc
needtworows <- isTRUE(raw_calc$needtworows)
invalidate_ob <- isTRUE(raw_calc$invalidate_ob)
table_start <- raw_calc$starting_table
table_final <- raw_calc$final_table
# RIR / Fragility
frag_primary <- raw_calc$fragility_primary
frag_extra <- raw_calc$fragility_supplemental %||% NA
RIR_primary <- raw_calc$RIR_primary
RIR_extra <- raw_calc$RIR_supplemental %||% NA
total_RIR <- RIR_primary + ifelse(is.na(RIR_extra), 0, RIR_extra)
total_switch <- frag_primary + ifelse(is.na(frag_extra), 0, frag_extra)
RIR_perc <- raw_calc$RIR_perc
RIR <- raw_calc$RIR_primary # only for RIR_pi calculation
# Odds ratio and p-values for user/transfer tables (if stored):
fisher_ob <- raw_calc$fisher_ob %||% NA
fisher_final <- raw_calc$fisher_final %||% NA
p_start <- raw_calc$p_start
p_final <- raw_calc$p_final %||% NA
alpha <- 0.05 # default
p_ob <- raw_calc$p_start
allnotenough <- raw_calc$needtworows
switch_trm = TRUE # default
test = "fisher" # default
replace = "control" # default
# Constructing conditional message based on intermediate values
if (a == 0 || b == 0 || c == 0 || d == 0) {
a_OR <- a + 0.5
b_OR <- b + 0.5
c_OR <- c + 0.5
d_OR <- d + 0.5
} else {
a_OR <- a
b_OR <- b
c_OR <- c
d_OR <- d
}
odds_ratio <- a_OR * d_OR / (b_OR * c_OR)
if (test == "fisher"){
solution <- konfound:::getswitch_fisher(a, b, c, d, odds_ratio, 0.05, switch_trm)
}
dcroddsratio_ob <- solution$dcroddsratio_ob
if (switch_trm && dcroddsratio_ob) {
transferway <- "treatment success to treatment failure"
transferway_start <- "treatment row"
RIRway <- "treatment success"
RIRway_start <- "treatment row"
RIR_pi <- RIR / d * 100
p_destination_control <- a/(a+b)
p_destination <- round((a+c)/(a+b+c+d) * 100, 3) * (replace == "entire") +
round(a/(a+b) * 100, 3) * (1 - (replace == "entire"))
}
if (switch_trm && !dcroddsratio_ob) {
transferway <- "treatment failure to treatment success"
transferway_start <- "treatment row"
RIRway <- "treatment failure"
RIRway_start <- "treatment row"
RIR_pi <- RIR / c * 100
p_destination_control <- b/(a+b)
p_destination <- round((b+d)/(a+b+c+d) * 100, 3) * (replace == "entire") +
round(b/(a+b) * 100, 3) * (1 - (replace == "entire"))
}
if (allnotenough) {
RIR_pi <- NA
if (switch_trm && dcroddsratio_ob) {
transferway_extra <- "control failure to control success"
transferway_extra_start <- "control row"
RIRway_extra <- "control failure"
RIRway_extra_start <- "control row"
p_destination_control_extra <- b/(a+b)
p_destination_extra <- round((b+d)/(a+b+c+d) * 100, 3) * (replace == "entire") +
round(b/(a+b) * 100, 3) * (1 - (replace == "entire"))
}
if (switch_trm && !dcroddsratio_ob) {
transferway_extra <- "control success to control failure"
transferway_extra_start <- "control row"
RIRway_extra <- "control success"
RIRway_extra_start <- "control row"
p_destination_control_extra <- a/(a+b)
p_destination_extra <- round((a+c)/(a+b+c+d) * 100, 3) * (replace == "entire") +
round(a/(a+b) * 100, 3) * (1 - (replace == "entire"))
}
}
### Output language objects
# fragility calculation component (when needtworows == F)
if (p_ob < 0.05) {
if (RIRway == "treatment success") {
prob_indicator = "failure"
} else if (RIRway == "treatment failure") {
prob_indicator = "success"
} else if (RIRway == "control success") {
prob_indicator = "failure"
} else if (RIRway == "control failure") {
prob_indicator = "success"
}
} else {
if (RIRway == "treatment success") {
prob_indicator = "failure"
} else if (RIRway == "treatment failure") {
prob_indicator = "success"
} else if (RIRway == "control success") {
prob_indicator = "failure"
} else if (RIRway == "control failure") {
prob_indicator = "success"
}
}
if (allnotenough) {
# fragility calculation component (when needtworows == T)
if (p_ob < 0.05) {
if (RIRway_extra == "treatment success") {
prob_indicator_extra = "failure"
} else if (RIRway_extra == "treatment failure") {
prob_indicator_extra = "success"
} else if (RIRway_extra == "control success") {
prob_indicator_extra = "failure"
} else if (RIRway_extra == "control failure") {
prob_indicator_extra = "success"
}
} else {
if (RIRway_extra == "treatment success") {
prob_indicator_extra = "failure"
} else if (RIRway_extra == "treatment failure") {
prob_indicator_extra = "success"
} else if (RIRway_extra == "control success") {
prob_indicator_extra = "failure"
} else if (RIRway_extra == "control failure") {
prob_indicator_extra = "success"
}
}
}
if (!exists("p_destination_extra") || is.na(p_destination_extra)) {
p_destination_extra <- NA
}
# Decide if p_start < alpha => “nullify” or else => “sustain”
change_phrase <- if (!is.na(p_start) && p_start < 0.05) {
"To nullify the inference that the effect is different from 0 (alpha = 0.05), one would need to transfer"
} else {
"To sustain an inference that the effect is different from 0 (alpha = 0.05), one would need to transfer"
}
# Special case if RIR percentage > 100
if (!needtworows && RIR_pi > 100) {
conclusion_large_rir <- paste0(
sprintf("\nNote the RIR exceeds 100%%. Generating the transfer of %d data points would", raw_calc$fragility_primary),
" require replacing more data points than are in the ", RIRway, " condition.\n\n")
} else {
conclusion_large_rir <- "" # Empty string if RIR_pi <= 100
}
# RIR calculation note
if (!needtworows) {
RIR_calc <- paste0(
"\n\nNote that RIR = Fragility/P(destination) = ",
raw_calc$fragility_primary, "/", sprintf("%.3f", p_destination/100), " ~ ", total_RIR, ".\n")
} else{
RIR_calc <- paste0(
"\n\nNote that RIR = primary RIR + supplemental RIR = (",
raw_calc$fragility_primary, "/", sprintf("%.3f", p_destination/100), ") + (",
raw_calc$fragility_supplemental, "/", sprintf("%.3f", p_destination_extra/100), ") ~ ", total_RIR, "<br>",
"based on the calculation RIR = Fragility/P(destination).\n"
)
}
single_row_text <- ""
double_row_text <- ""
if (!needtworows) {
## Single-switch scenario
single_row_text <- paste0(
change_phrase, " ", frag_primary, " data points from ",
transferway, " as shown, from the User-entered Table to the Transfer Table (Fragility = ", frag_primary, ").<br>",
"This is equivalent to replacing ", total_RIR, " (", sprintf("%.3f", RIR_perc), "%) ",
RIRway, " data points with data points ",
"for which the probability of ", prob_indicator, " in the control group (", p_destination, "%) applies ",
"(RIR = ", RIR_primary, ").<br>"
)
} else {
## Double-switch scenario
double_row_text <- paste0(
"In terms of Fragility, to ", if (invalidate_ob) "nullify" else "sustain",
" an inference that the effect is different from 0 (alpha = 0.05), ",
"transferring ", frag_primary, " data points from ",
transferway, " is not enough to change the inference.<br>",
"One would also need to transfer ", frag_extra, " data points from ", transferway_extra, " as shown, ",
"from the User-Entered Table to the Transfer Table.<br><br>",
"In terms of RIR, generating the ", frag_primary, " switches from ", transferway, " ",
"is equivalent to replacing ", RIR_primary, " ", RIRway, " data points with data points for which",
"the probability of ", prob_indicator, " in the control group (", p_destination, "%) applies.<br><br>",
"In addition, generating the ", frag_extra, " switches from ", transferway_extra, " is ",
"equivalent to replacing ", RIR_extra, " ", RIRway_extra, " data points with data points for which ",
"the probability of ", prob_indicator_extra, " in the control group (", p_destination_extra, "%) applies.<br>"
)
}
# construct final HTML
twobytwo_output <- HTML(
paste0(
"<strong>Robustness of Inference to Replacement (RIR):</strong><br>",
# RIR
"RIR = ",
if (!needtworows) {
RIR_primary
} else {
paste0(RIR_primary, " + ", RIR_extra, " = ", total_RIR)
},
"<br>",
# Optional total RIR line for needtworows == T
if (needtworows) {
paste0(
"Total RIR = primary RIR in ", RIRway_start, " + supplemental RIR in ", RIRway_extra_start, "<br><br>"
)
} else {
""
},
# Fragility
"Fragility = ", if (!needtworows) {
frag_primary
} else {
paste0(frag_primary, " + ", frag_extra, " = ", total_switch)
},
"<br>",
# Optional total Fragility line for needtworows == T
if (needtworows) {
paste0(
"Total Fragility = primary Fragility in ", transferway_start, " + supplemental Fragility in ", transferway_extra_start, "<br>"
)
} else {
""
},
"<br>This function calculates the number of data points that would have to be replaced with zero-effect data points (RIR) ",
"to nullify or sustain the inference made about the association between the rows and columns in a 2x2 table.<br>",
"One can also interpret this as switches (Fragility) from one cell to another, such as from the treatment success cell ",
"to the treatment failure cell.<br><br>",
single_row_text,
double_row_text,
"<br>",
RIR_calc, "<br><br>",
if (!needtworows && RIR_pi > 100) {
paste0(conclusion_large_rir, "<br><br>")
},
# show user odds ratio or p-value from the user table
if (!is.na(fisher_ob)) paste0("For the User-entered Table, the estimated odds ratio is ",
sprintf("%.3f", fisher_ob),
", with p-value of ", sprintf("%.3f", p_start),
":<br><br>") else "",
"<strong><u>Implied Table:</u></strong><br>",
knitr::kable(raw_calc$starting_table, format = "html", align = "c",