-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCausInferShiny.R
743 lines (601 loc) · 24.9 KB
/
CausInferShiny.R
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
require(shiny)
require(bartCause)
require(foreign)
require(readstata13)
require(openxlsx)
require(tidyverse)
require(shinyBS)
require(png)
require(shinythemes)
require(dbarts)
require(plotly)
require(DT)
########################################
csplotaxis <- c("Tree"= "tree", "PCA"= "pca", "Common Support Statistics"= "css",
"Propensity Score"= "p.score", "Outcome"= "y", "y0", "y1",
"Individual Treatment Effect Estimates" = "indiv.diff",
"Propensity Score Weights" = "p.weights"
#, "Any Predictor Column" = "other"
)
source("texts.r")
source("functions.R")
source("shinyPlotly.r")
######
ui <- fluidPage(theme = shinytheme("cerulean"),
# Navigating Tabs
navbarPage("Causal Inference",
# Tab Panel 1
tabPanel("Introduction",
# Sidebar Layout
sidebarLayout(
# Sidebar Panel
sidebarPanel(
# Text
h3("Introduction"),
p("This RShiny application aims to simplify the process of conducting Causal Inference.")
),
# Main Panel
mainPanel(
# Text
h1("Instructions"),
p("This application allows the users to explore different options through
the interface and get live results."),
hr(),
h4("Step 1. Upload data"),
p("Upload data and identify confounders (X), treatment (Z),
response (Y)"),
h4("Step 2. Estimand selection and advanced options"),
h5("A. Specify the treatment effect to estimate"),
h5("B. Model propensity score ?"),
p("(1). Method for treatment assignment? (bart, bart.xval, glm)"),
p("(2). How to include propensity score? (as weight or as covariate)"),
p("(3). Use", actionLink("link3", "TMLE"), "adjustment?"),
h4("Step 3. Display results"),
p("Show summary of model fit"),
######
#Pop over vs. modal
#bsModal("Modal1", "Treatment Effect", "link1", size = "large",
#text1),
bsPopover("link3", "TMLE", content = text3, "hover"),
######
h4("Step 4. Check for common support"),
p("Common support plot"),
h4("Step 5. Convergence diagnostics"),
p("Trace plots"),
hr(),
h4("R Package Used"),
p(a("BARTCause", href = "https://github.com/vdorie/bartCause")),
p(a("dbarts", href = "https://github.com/vdorie/dbarts")),
hr(),
h1("Example"),
p("Start by clicking on the Upload Data tab. Select data type and
click on Browse for file path"),
imageOutput("img1"),
hr(),
p("Specify X, Y, Z variables for the data uploaded, you can check
if the dataframe is in working condition from the Status bar"),
imageOutput("img2"),
hr(),
# sliderInput('overlap1',
# label = div(style='width:500px;',
# div(style='float:left;', 'No Overlap'),
# div(style='float:right;', 'Complete Overlap')),
# ticks = F, min = 0, max = 4, value = 2, width = '500px'),
sliderInput('overlap1',
label = div(style='width:500px;',
div(style='float:left;', 'No Overlap'),
div(style='float:right;', 'Complete Overlap')),
ticks = F, min = 1, max = 100, value = 1, width = '500px'),
plotlyOutput("overlap_plty")
)
)
),
# Tab Panel 2
tabPanel("Upload Data",
# Sidebar Layout
sidebarLayout(
# Sidebar Panel
sidebarPanel(
# Input: file type
selectInput("filetype", "Select File Type",
choices = c("csv" = "csv",
"dta" = "dta",
"xlsx" = "xlsx",
"txt" = "txt",
"spss" = "spss")),
# Input: file
fileInput("file", "Choose File",
multiple = FALSE,
accept = NULL),
hr(),
# Input: header
checkboxInput("header", "Header", TRUE),
hr(),
# Column Selection for X and Y
selectInput("xcol", "Select Covariates (X) Columns",
choices = NULL, multiple = TRUE),
selectInput("ycol", "Select Response (Y) Column", choices = NULL),
# Column Selection for Z, and identify treatment
selectInput("zcol", "Select Treatment (Z) Column", choices = NULL),
#conditionalPanel(
# condition = "input.zcol",
selectInput("trt.ind", "Select the Value Representing Receiving Treatment",
choices = NULL),
#)
# Grouping Variable
checkboxInput("gvarcheck", "Include Grouping Variable?", FALSE),
conditionalPanel(
condition = "input.gvarcheck",
selectInput("gvar", "Select Grouping Variable", choices = NULL)
)
),
# Main Panel
mainPanel(
# Output: Data file
h4("Status"),
textOutput("uploadconfirm"),
#textOutput("variableconfirm"),
hr(),
h4("Data"),
DT::dataTableOutput("uploads")
)
)
),
# Tab Panel 3
tabPanel("Select Estimand & Advanced Options",
# Sidebar layout
sidebarLayout(
# Sidebar Panel
sidebarPanel(
# Input: Select Estimand
radioButtons("estimand", actionLink("link1", "Select Estimand"),
choices = c("ATE" = "ate",
"ATT" = "att",
"ATC" = "atc"),
selected = "ate"),
#bsPopover("link1", "Estimand", content = text1, "hover"),
bsModal("Modal1", "Estimand", "link1", size = "large",
uiOutput("text1")),
#####
checkboxInput("pscheck", actionLink("link2", " Model propensity
score?"), TRUE),
bsModal("Modal2", "Propensity Score", "link2", size = "large",
uiOutput("text2")),
hr(),
conditionalPanel(
condition = "input.pscheck",
radioButtons("trtmethod", "How to include P-Score?",
choices = c("glm" = "glm",
"bart" = "bart",
"bart.xval" = "bart.xval"),
selected = "glm")
),
hr(),
conditionalPanel(
condition = "input.pscheck",
radioButtons("pscoreas", "Propensity Score as?",
choices = c("Covariate" = "cov",
"Weight" = "wgt"),
selected = "cov")
),
hr(),
conditionalPanel(
condition = "input.pscheck",
checkboxInput("tmleadjust", "TMLE Adjustment?", FALSE),
hr(),
#Input: Add common support rule
radioButtons("csrule", "Select Common Support Rule",
choices = c("none" = "none",
"sd" = "sd",
"chisq" = "chisq"),
selected = "sd")
)
#####
),
# Main Panel
mainPanel(
# Output: plots
h4("Filtered Table"),
DT::dataTableOutput("filteredtable")
)
)
),
# Tab Panel 4
tabPanel("Display Results",
# Sidebar layout
sidebarLayout(
# Sidebar Panel
sidebarPanel(
# Input: Model Propensity Score
actionButton("runButton", "Run Model")
),
# Main Panel
mainPanel(
h5("Summary"),
tableOutput("summary.table"),
hr(),
verbatimTextOutput("summary")
######
)
)
),
# Tab Panel 5
tabPanel("Common Support",
# Sidebar layout
sidebarLayout(
# Sidebar Panel
sidebarPanel(
h4("Check for", actionLink("link4", "overlap and balance")),
bsModal("Modal4", "Overlap and Balance", "link4", size = "large",
uiOutput("text4")),
selectInput("xvalplot", "Check overlap on which confounder?",
choices = NULL),
conditionalPanel(
condition = "output.xcont == 1",
sliderInput("plotlybins", "Select Number of Bins",
min = 6, max = 30, value = 10, step = 1)),
hr(),
h4("Common support plot"),
#conditionalPanel(
# condition = "input.csrule == 'sd'",
# sliderInput("cscutsd", "Select Common Support Cut",
# min = 1, max = 3, value = 1, step = 0.5)),
#conditionalPanel(
# condition = "input.csrule == 'chisq'",
# sliderInput("cscutchisq", "Select Common Support Cut",
# min = 0.05, max = 0.3, value = 0.05, step = 0.05)),
# Input: plot common support
conditionalPanel(
condition = "input.csrule != 'none'",
selectInput("xvar", "X Variable",
choices = csplotaxis)),
conditionalPanel(
condition = "input.csrule != 'none'",
selectInput("yvar", "Y Variable",
choices = csplotaxis))
#conditionalPanel(
# condition = "input.csrule != 'none'",
# sliderInput("")
#)
),
# Main Panel
mainPanel(
h4("Common Support Plot"),
##
plotlyOutput("supplotly", width = 500, height = 800),
##
br(),
plotOutput("supplot"),
downloadButton("supexp", "Download Plot")
)
)
),
# Tab Panel 6
tabPanel("Trace Plots",
# Sidebar layout
sidebarLayout(
# Sidebar Panel
sidebarPanel(
# Input: plots to show (plot_sigma, plot_est)
h4("Trace Plots")
),
# Main Panel
mainPanel(
h4("Trace Plots"),
h5("Sigma Plot"),
plotOutput("sigmaplot"),
h5("Estimation Plot"),
plotOutput("estplot")
)
)
)
)
)
###################################################
server <- function(input, output, session) {
# Text outputs
output$text1 <- renderUI({
tag1
})
output$text2 <- renderUI({
tag2
})
output$text4 <- renderUI({
tag4
})
# Image outputs
output$img1 <- renderImage({
return(list(
src = "www/upload_file.png",
filetype = "image/png",
height = 365,
width = 480
))
}, deleteFile = F)
output$img2 <- renderImage({
return(list(
src = "www/select_variables.png",
filetype = "image/png",
height = 365,
width = 480
))
}, deleteFile = F)
output$overlap_plty <- renderPlotly({
req(input$overlap1)
plotly_overlap(input$overlap1)
})
# Create global data object
my_data <- reactive({
req(input$file, input$filetype)
if (input$filetype == "csv") {
df <- read.csv(input$file$datapath, header = input$header)
}
else if (input$filetype == "dta") {
df <- read.dta13(input$file$datapath)
}
else if (input$filetype == "xlsx") {
df <- read.xlsx(input$file$datapath, colNames = input$header)
}
else if (input$filetype == "txt") {
df <- read.table(input$file$datapath, header = input$header)
}
else if (input$filetype == "spss") {
df <- read.spss(input$file$datapath, to.data.frame = T)
}
df
})
# Upload Data tab outputs
output$uploads <- DT::renderDataTable({
req(input$file)
req(my_data())
return(my_data())
})
output$uploadconfirm <- renderText({
# dataset validation
datacheck <- function(data, confound, trt, resp) {
if (is.null(data)) {
"Please Upload a dataset"
}
else {
if (is.null(confound) || trt == "" || resp == "") {
"Please identify X, Y, Z"
}
else {
if (length(unique(my_data()[, which(names(my_data()) == trt)])) > 2) {
"Please check treatment variable selection, and/or missing values"
}
else {
NULL
}
}
}
}
validate(
datacheck(input$file, input$xcol, input$zcol, input$ycol)
)
# datacheck in functions.R
paste("Upload complete")
})
# Updating column selection
observe({
req(input$file)
vars <- names(my_data())
updateSelectInput(session, "idcol", choices = vars)
updateSelectInput(session, "xcol", choices = vars)
updateSelectInput(session, "zcol", choices = vars)
updateSelectInput(session, "ycol", choices = vars)
updateSelectInput(session, "gvar", choices = vars)
#updateNumericInput(session, "xvar", max = ncol(my_data()))
})
##########
# Intermediate Filtered table object with original coding
filtered_pre <- reactive({
req(input$file, input$ycol, input$zcol, input$xcol)
df_clean <- subset(my_data(), select = c(input$ycol, input$zcol, input$xcol))
df_clean
})
observe({
req(filtered_pre())
trtvalues <- unique(filtered_pre()[, 2])
updateSelectInput(session, "trt.ind", choices = trtvalues)
updateSelectInput(session, "xvalplot", choices = input$xcol)
})
# Filtered data frame object
filtered <- reactive({
req(input$trt.ind)
data0 <- filtered_pre()
data0[which(data0[ ,2] == input$trt.ind), 2] <- 1L
data0[which(data0[ ,2] != input$trt.ind), 2] <- 0L
if (input$gvarcheck) {
data0 <- cbind.data.frame(data0, subset(my_data(), select = input$gvar))
return(data0)
}
else return(data0)
})
# Col number of last confounder
confnum <- reactive({
req(filtered())
if (input$gvarcheck) {
return(ncol(filtered()) - 1)
}
else return(ncol(filtered()))
})
# Filtered table display
output$filteredtable <- DT::renderDataTable({
req(filtered())
return(round(filtered(), 2))
})
# Translating fit input and recode variables
rspmethod <- reactive({
req(filtered())
if (!input$pscheck) {
"bart"
}
else if (input$tmleadjust) {
"tmle"
}
else if (input$pscoreas == "wgt") {
"p.weight"
}
else "bart"
})
# p.scoreAsCovariate argument
psas <- reactive({
req(filtered())
return(input$pscoreas == "cov")
})
# group.by argument
gby <- reactive({
req(filtered())
if (input$gvarcheck) {
return(filtered()[ ,ncol(filtered())])
}
else return(F)
})
# cscut argument
#cscut <- reactive({
# req(filtered())
# if (input$csrule == 'none') {
# return(NA_real_)
# }
# else if (input$csrule == 'sd') {
# return(input$cscutsd)
# }
#
# else return(input$cscutchisq)
#})
# Model fitting
fit <- eventReactive(input$runButton, {
req(filtered())
progress <- Progress$new(session, min = 1, max = 12)
on.exit(progress$close())
progress$set(message = 'Model fitting in progress')
for (i in 1:12) {
progress$set(value = i)
Sys.sleep(0.5)
}
if (input$gvarcheck) {
fit0 <- bartc(response = filtered()[, 1], treatment = filtered()[, 2],
confounders = as.matrix(filtered()[, 3:confnum()]),
estimand = input$estimand, method.rsp = rspmethod(),
method.trt = input$trtmethod, commonSup.rule = input$csrule,
group.by = gby(), keepCall = F,
p.scoreAsCovariate = psas(), use.rbart = input$gvarcheck
# commonSup.cut = input$cscut,
)
}
else {
fit0 <- bartc(response = filtered()[, 1], treatment = filtered()[, 2],
confounders = as.matrix(filtered()[, 3:confnum()]),
estimand = input$estimand, method.rsp = rspmethod(),
method.trt = input$trtmethod, commonSup.rule = input$csrule,
group.by = NULL, keepCall = F,
p.scoreAsCovariate = psas(), use.rbart = F
# commonSup.cut = input$cscut,
)
}
fit0
})
# Summary text output
output$summary.table <- renderTable({
req(fit())
as.data.frame(round(summary(fit())$estimates, 3))
})
# Summary chunk output
output$summary <- renderPrint({
req(fit())
summary(fit())
})
##########
# plot_sigma
output$sigmaplot <- renderPlot({
req(fit())
plot_sigma(fit())
})
# plot_est
output$estplot <- renderPlot({
req(fit())
plot_est(fit())
})
#####
# Define x and y axis
csxvar <- reactive({
req(filtered())
if (input$xvar == "tree") {
"tree.1"
}
else if (input$xvar == "pca") {
"pca.1"
}
else if (input$xvar == "other") {
"y"
}
else input$xvar
})
csyvar <- reactive({
req(filtered())
if (input$yvar == "tree") {
"tree.2"
}
else if (input$yvar == "pca") {
"pca.2"
}
else if (input$yvar == "other") {
"y"
}
else input$yvar
})
# plot_support
supplot1 <- reactive({
req(fit())
#if (input$csrule == "none") {
# NULL
# print("Common Support rule not specified, unable to plot")
#}
#else
plot_support(fit(), xvar = csxvar(), yvar = csyvar())
})
# plot_support output
output$supplot <- renderPlot({
supplot1()
})
# Plotly Output
output$xcont <- reactive({
req(filtered())
req(input$xvalplot)
if (length(unique(filtered()[[input$xvalplot]])) >= 6)
return(1)
else return(0)
})
outputOptions(output, "xcont", suspendWhenHidden = FALSE)
plotlyob <- reactive({
req(input$xvalplot)
req(filtered())
if (length(unique(filtered()[[input$xvalplot]])) >= 6) {
xbins <- compbins(filtered()[[input$xvalplot]], input$plotlybins)
plotly_vis_cont(filtered(), input$xvalplot, nbins = xbins)
}
else
plotly_vis_dis(filtered(), input$xvalplot)
})
output$supplotly <- renderPlotly({
req(plotlyob())
plotlyob()
})
output$supexp <- downloadHandler(
filename = function(){
"sup.png"
},
content = function(file){
ggsave(file, plot = plot_support(fit(), xvar = csxvar(), yvar = csyvar()), device = "png")
}
)
#####
}
# Run the application
shinyApp(ui = ui, server = server)
#testdata <- read.csv("/Users/George/Desktop/A3SR/Others/Causal_Inference_Shiny/experiment/simpletest.csv", header = T)
#fit1 <- bartc(testdata$Outcome, testdata$Treatment, testdata[, 1:2], estimand = "ate")
#cpsdat <- read.csv("/Users/George/Desktop/A3SR/Others/Causal_Inference_Shiny/experiment/cps.csv", header = T)
#fit2 <- bartc(cpsdat$re78t, cpsdat$treat, cpsdat$age + cpsdat$educ + cpsdat$re74t, estimand = "ate", method.trt = "glm", p.scoreAsCovariate = T, method.rsp = "bart", commonSup.rule = "sd")
#fit3 <- bartc(cpsdat$re78t, cpsdat$treat, cpsdat$age + cpsdat$black + cpsdat$re74t, estimand = "ate", method.trt = "glm", p.scoreAsCovariate = T, method.rsp = "bart", commonSup.rule = "sd", group.by = cpsdat$educ)