-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathm_LCM_p2.R
741 lines (616 loc) · 48.6 KB
/
m_LCM_p2.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
# LCM - Run Model - prior model -------------------------------------------- ----------------------------------------------
# server-side function to run LCM prior-only model via Stan
LCM_run_model_priors_only <- function(id,
stan_model,
stan_model_p_scale_priors,
data,
p_scale_priors_indicator,
priors,
button) {
observeEvent(button(), {
if (is.null(data()$reference.cat) == TRUE) {
shinyalert("Warning",
"To use meta-analysis without a gold standard, please select a dataset with covariates,
and ensure that the reference test data is inputted as a column named 'reference.cat'",
type = "error")
}
else { }
})
moduleServer(
id,
function(input, output, session) {
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
tfile <- tempfile(fileext = ".txt")
r <- reactiveValues( progress_mtime = -1 )
observeEvent(button(), {
# info / guidance on priors [generic info - applies to ALL models]
shinyalert(title = "Note on priors",
text = paste("The default prior distributions ('priors') are weakly informative [see note 1]. Weakly informative priors do not incorporate any known subject-matter knowledge
(e.g. known information on the sensitivity and/or specificity of the test(s) under evaluation), and are designed to be widely applicable and to help stabilize computation -
they will work relatively well for many cases [see note 2].
However, this is not guaranteed - there are no 'default' priors which will work for all situations.
This is more likely to be the case when one is assuming a perfect gold standard, and when there are many studies (e.g., > 10) available.
On the other hand, these default priors are less likely to work well when [see note 3]:
(i) modelling an imperfect gold standard, particularly when modelling conditional dependence [see note 4], and/or when assuming more random effects, and in the
presence of multiple reference tests;
(ii) there are few studies;
(iii) for meta-regression;
(iv) in the presence of high between-study heterogeneity.
In this case, more informative priors may be needed in order to achieve normal model diagnostics - for example, more information on the sensitivity and/or specificity
of the test(s) under evaluation may need to be incorporated into the model.
Moreover, since this is a Bayesian analysis, it generally makes sense to incorporate known information
(i.e., subject-matter knowledge) into the statistical models wherever possible - regardless of whether it is possible to estimate
a model without incorporating any known information.
However, one must ensure that the appropriate uncertainty is encoded into the priors; otherwise, this could result in biased estimates of test accuracy.
For more information and guidance on priors in general, please see the following links:",
tags$a(href="https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3728030/", "#1 (test accuracy example - application to Tuberculous pleuritis - see section 2 and table 1.",
target="_blank"),
tags$a(href="https://discourse.mc-stan.org/t/priors-for-a-novice/9415/6", "#2 (regression example - not test accuracy specific).",
target="_blank"),
" \n
NOTE:
[1] The default priors are weakly informative so they do not incorporate any known subject-matter knowledge and allow the data to dominate. For example,
for the between-study deviation parameters for all three tests, the default priors for all of the models weakly pull the study-specific sensitivities
and specificities towards each other, whilst allowing for very large between-study heterogeneity if the data demands. For example, if $0.8$ is the
value found for the summary sensitivity, and the data suggests a standard deviation equal to 2 (corresponding to a high degree
of between-study heterogeneity), then these priors would allow the study-specific sensitivities and specificities to be in the interval $(0.23, 0.94)$ with 95% probability.
[2] 'work relatively well' means that the default, weakly informative priors will: (i) result in adequate model diagnostics (see 'model diagnostics' tab); and
(ii) be unlikely to bias the estimates of interest (i.e. sensitivity and specificity) due to placing too much prior weight on unreasonable values of test accuracy
in other words, incorrectly assuming - a priori - that values of accuracy which are
not true and/or unlikely to be observed in clinical practice are in fact likely to be observed.
[3] This is not an exhaustive list.
[4] 'Modelling conditional dependence' refers to modelling the correlation between the test results for all individuals within
each of the two disease classes - diseased and non-diseased. This should almost always be attempted wherever possible, due to the fact that tests are unlikely to be
conditionally independent in clinical practice.
"
),
type = "info",
confirmButtonText = "OK",
html = TRUE)
})
X <- reactive({
data()
})
observeEvent(button(), {
req(X()$reference.cat)
p_scale_priors_indicator <- p_scale_priors_indicator$p_scale_priors_indicator
if (p_scale_priors_indicator == TRUE) {
stan_model_p_scale_priors_model <- stan_model_p_scale_priors$getModel()
r$bg_process <<- callr::r_bg(
func = function(stan_model_p_scale_priors,
X,
LCM_prior_sens_ref_lower95,
LCM_prior_sens_ref_upper95,
LCM_prior_spec_ref_lower95,
LCM_prior_spec_ref_upper95,
LCM_prior_SD_sens_ref_sd,
LCM_prior_SD_spec_ref_sd,
LCM_prior_sens_index_lower95,
LCM_prior_sens_index_upper95,
LCM_prior_spec_index_lower95,
LCM_prior_spec_index_upper95,
LCM_prior_SD_sens_index_sd,
LCM_prior_SD_spec_index_sd,
LCM_prior_prev_a,
LCM_prior_prev_b) {
rstan::sampling(
object = stan_model_p_scale_priors,
data = list(num_ref = length(unique(as.numeric(as.factor(X$reference.cat)))),
n_studies = nrow(X),
Ref = as.numeric(as.factor(X$reference.cat)),
# "dynamic" priors for ref test (# of priors is dynamic as depends on # of ref. tests)
LCM_prior_sens_ref_lower95 = LCM_prior_sens_ref_lower95,
LCM_prior_sens_ref_upper95 = LCM_prior_sens_ref_upper95,
LCM_prior_spec_ref_lower95 = LCM_prior_spec_ref_lower95,
LCM_prior_spec_ref_upper95 = LCM_prior_spec_ref_upper95,
# static priors for ref test (between-study SD's, as meta-reg covariate only)
LCM_prior_SD_sens_ref_sd = LCM_prior_SD_sens_ref_sd,
LCM_prior_SD_spec_ref_sd = LCM_prior_SD_spec_ref_sd,
# priors for index test
LCM_prior_sens_index_lower95 = LCM_prior_sens_index_lower95,
LCM_prior_sens_index_upper95 = LCM_prior_sens_index_upper95,
LCM_prior_spec_index_lower95 = LCM_prior_spec_index_lower95,
LCM_prior_spec_index_upper95 = LCM_prior_spec_index_upper95,
LCM_prior_SD_sens_index_sd = LCM_prior_SD_sens_index_sd,
LCM_prior_SD_spec_index_sd = LCM_prior_SD_spec_index_sd,
# prev priors (beta dist)
LCM_prior_prev_a = LCM_prior_prev_a,
LCM_prior_prev_b = LCM_prior_prev_b
),
chains = 4,
iter = 2000,
warmup = 200,
control=list(adapt_delta=0.80,
max_treedepth = 10),
seed= 123
)
}, # end of function
args = list(stan_model_p_scale_priors = stan_model_p_scale_priors_model,
X = X(),
# "dynamic" priors for ref test (# of priors is dynamic as depends on # of ref. tests)
LCM_prior_sens_ref_lower95 = as.array(priors$LCM_prior_sens_ref_lower95$vec),
LCM_prior_sens_ref_upper95 = as.array(priors$LCM_prior_sens_ref_upper95$vec),
LCM_prior_spec_ref_lower95 = as.array(priors$LCM_prior_spec_ref_lower95$vec),
LCM_prior_spec_ref_upper95 = as.array(priors$LCM_prior_spec_ref_upper95$vec),
# static priors for ref test (between-study SD's, as meta-reg covariate only)
LCM_prior_SD_sens_ref_sd = priors$LCM_prior_SD_sens_ref_sd,
LCM_prior_SD_spec_ref_sd = priors$LCM_prior_SD_spec_ref_sd,
# priors for index test
LCM_prior_sens_index_lower95 = priors$LCM_prior_sens_index_lower95,
LCM_prior_sens_index_upper95 = priors$LCM_prior_sens_index_upper95,
LCM_prior_spec_index_lower95 = priors$LCM_prior_spec_index_lower95,
LCM_prior_spec_index_upper95 = priors$LCM_prior_spec_index_upper95,
LCM_prior_SD_sens_index_sd = priors$LCM_prior_SD_sens_index_sd,
LCM_prior_SD_spec_index_sd = priors$LCM_prior_SD_spec_index_sd,
# prev priors (beta dist)
LCM_prior_prev_a = priors$LCM_prior_prev_a,
LCM_prior_prev_b = priors$LCM_prior_prev_b
),
stdout = tfile,
supervise = TRUE
)
}
else { # logit-scale priors
stan_model_model <- stan_model$getModel()
r$bg_process <<- callr::r_bg(
func = function(stan_model,
X,
LCM_prior_mean_sens_ref_mu,
LCM_prior_mean_sens_ref_sd,
LCM_prior_mean_spec_ref_mu,
LCM_prior_mean_spec_ref_sd,
LCM_prior_SD_sens_ref_sd,
LCM_prior_SD_spec_ref_sd,
LCM_prior_mean_sens_index_mu,
LCM_prior_mean_sens_index_sd,
LCM_prior_mean_spec_index_mu,
LCM_prior_mean_spec_index_sd,
LCM_prior_SD_sens_index_sd,
LCM_prior_SD_spec_index_sd,
LCM_prior_prev_a,
LCM_prior_prev_b) {
rstan::sampling(
object = stan_model,
data = list(num_ref = length(unique(as.numeric(as.factor(X$reference.cat)))),
n_studies = nrow(X),
Ref = as.numeric(as.factor(X$reference.cat)),
# "dynamic" priors for ref test (# of priors is dynamic as depends on # of ref. tests)
LCM_prior_mean_sens_ref_mu = LCM_prior_mean_sens_ref_mu,
LCM_prior_mean_sens_ref_sd = LCM_prior_mean_sens_ref_sd,
LCM_prior_mean_spec_ref_mu = LCM_prior_mean_spec_ref_mu,
LCM_prior_mean_spec_ref_sd = LCM_prior_mean_spec_ref_sd,
# static priors for ref test (between-study SD's, as meta-reg covariate only)
LCM_prior_SD_sens_ref_sd = LCM_prior_SD_sens_ref_sd,
LCM_prior_SD_spec_ref_sd = LCM_prior_SD_spec_ref_sd,
# priors for index test
LCM_prior_mean_sens_index_mu = LCM_prior_mean_sens_index_mu,
LCM_prior_mean_sens_index_sd = LCM_prior_mean_sens_index_sd,
LCM_prior_mean_spec_index_mu = LCM_prior_mean_spec_index_mu,
LCM_prior_mean_spec_index_sd = LCM_prior_mean_spec_index_sd,
LCM_prior_SD_sens_index_sd = LCM_prior_SD_sens_index_sd,
LCM_prior_SD_spec_index_sd = LCM_prior_SD_spec_index_sd,
# prev priors (beta dist)
LCM_prior_prev_a = LCM_prior_prev_a,
LCM_prior_prev_b = LCM_prior_prev_b
),
chains = 4,
iter = 2000,
warmup = 200,
control=list(adapt_delta=0.80,
max_treedepth = 10),
seed= 123
)
}, # end of function
args = list(stan_model = stan_model_model,
X = X(),
# "dynamic" priors for ref test (# of priors is dynamic as depends on # of ref. tests)
LCM_prior_mean_sens_ref_mu = as.array(priors$LCM_prior_mean_sens_ref_mu$vec),
LCM_prior_mean_sens_ref_sd = as.array(priors$LCM_prior_mean_sens_ref_sd$vec),
LCM_prior_mean_spec_ref_mu = as.array(priors$LCM_prior_mean_spec_ref_mu$vec),
LCM_prior_mean_spec_ref_sd = as.array(priors$LCM_prior_mean_spec_ref_sd$vec),
# static priors for ref test (between-study SD's, as meta-reg covariate only)
LCM_prior_SD_sens_ref_sd = priors$LCM_prior_SD_sens_ref_sd,
LCM_prior_SD_spec_ref_sd = priors$LCM_prior_SD_spec_ref_sd,
# priors for index test
LCM_prior_mean_sens_index_mu = priors$LCM_prior_mean_sens_index_mu,
LCM_prior_mean_sens_index_sd = priors$LCM_prior_mean_sens_index_sd,
LCM_prior_mean_spec_index_mu = priors$LCM_prior_mean_spec_index_mu,
LCM_prior_mean_spec_index_sd = priors$LCM_prior_mean_spec_index_sd,
LCM_prior_SD_sens_index_sd = priors$LCM_prior_SD_sens_index_sd,
LCM_prior_SD_spec_index_sd = priors$LCM_prior_SD_spec_index_sd,
# prev priors (beta dist)
LCM_prior_prev_a = priors$LCM_prior_prev_a,
LCM_prior_prev_b = priors$LCM_prior_prev_b
),
stdout = tfile,
supervise = TRUE
)
}
r$poll <- TRUE
observe({
req(r$bg_process, r$poll)
show_modal_spinner(spin = "atom", color = "#005398", text = "Running Model")
invalidateLater(millis = 1000, session)
mtime <- file.info(tfile)$mtime
if (mtime > r$progress_mtime) {
r$progress <- readLines(tfile)
r$progress_mtime <- mtime
}
if (!r$bg_process$is_alive()) {
r$draws <- r$bg_process$get_result()
remove_modal_spinner()
r$poll <- FALSE
}
})
## print progress
output$progress_prior_model <- renderText({
req(r$progress)
paste(r$progress,
collapse = "\n")
})
})
my_list <- list(
draws = reactive({ r$draws })
)
# Run the Garabage Collector to Ensure any excess memory used by stan is freed
gc()
return(my_list)
}
)
}
# LCM - Run Model - full model --------------------------------------------- -----------------------------------------
LCM_run_model <- function( id,
dataset,
stan_model,
stan_model_p_scale_priors,
p_scale_priors_indicator,
button,
priors,
sampler_options,
SA_indicator,
SA_indicator_local,
LCM_options_indicators) {
moduleServer(
id,
function(input, output, session) {
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
observeEvent(button(), {
if (is.null(dataset()$reference.cat) == TRUE) {
shinyalert("Warning",
"To use meta-analysis without a gold standard, please select a dataset with covariates,
and ensure that the reference test data is inputted as a column named 'reference.cat'",
type = "error")
}
else {
shinyalert(title = "Note",
text = paste("Please ensure that you carefully check the 'Model diagnostics' tab once the model has finished running.
If any of the results are abnormal, it is imperative that one does not use the outputs for this model,
as the results are likely to be misleading and nonsensical. This can be due to a lack of convergence and/or
parameter identifiability [see note 1] issues.
For guidance on how model diagnostics (e.g., posterior distribution and trace plots) should look like,
please see the following links:",
tags$a(href="https://cran.r-project.org/web/packages/JointAI/vignettes/AfterFitting.html", "#1.", target="_blank"),
tags$a(href="https://m-clark.github.io/bayesian-basics/diagnostics.html#monitoring-convergence", "#2.", target="_blank"),
"\n
NOTE:
[1] identifiability - we must to ensure that the number of parameters being estimated from our model is not greater than what is possible
for the given dataset; otherwise, it may be non-identifiable - which means that the model will give misleading, nonsensical results.
For example, it may estimate the sensitivity for a test to be equal to both both 0.20 and 0.80.
"
),
type = "info",
confirmButtonText = "OK",
html = TRUE)
}
})
tfile <- tempfile( fileext = ".txt" )
r <- reactiveValues( progress_mtime = -1 )
observeEvent(button(), {
req(dataset(), dataset()$reference.cat)
SA_indicator <- SA_indicator$SA_indicator
if ( SA_indicator == TRUE && SA_indicator_local == 1 ) { # note input$SA_indicator will only be readable if using NS
# corresponding to SA (comes from "SA_indicator_UI" function)
X <<- reactive({
# req(dataset()$reference.cat)
SA_data <- dataset() %>%
dplyr::mutate(author2 = as.numeric(as.factor(author))) %>%
dplyr::filter(!(author2 %in% c(input$triallist))) %>%
dplyr::select(-author2) %>%
dplyr::mutate(ref_numeric = as.numeric(as.factor(reference.cat)))
SA_data
})
ref_indexes <<- reactive({
# req(dataset()$reference.cat)
ref_indexes_obj <- c(sort(unique(X()$ref_numeric)))
ref_indexes_obj
})
}
if (SA_indicator_local == 0) { # if not doing SA leave X as full dataset (regardless of SA_indicator)
X <<- reactive({
# req(dataset()$reference.cat)
dataset() %>%
dplyr::mutate(ref_numeric = as.numeric(as.factor(reference.cat)))
})
ref_indexes <<- reactive({
# req(dataset()$reference.cat)
ref_indexes_obj <- c(sort(unique(X()$ref_numeric)))
ref_indexes_obj
})
}
X <- X()
n_studies <- nrow(X)
r1 <- X$TP
r2 <- X$FN
r3 <- X$FP
r4 <- X$TN
study_sizes <- c()
for (i in 1:n_studies) {
study_sizes[i] <- r1[i] + r2[i] + r3[i] + r4[i]
}
tables <- matrix(ncol = 4, nrow = n_studies, c(r1,r2,r3,r4))
p_scale_priors_indicator <- p_scale_priors_indicator$p_scale_priors_indicator
if (p_scale_priors_indicator == TRUE) {
stan_model_p_scale_priors_model <- stan_model_p_scale_priors$getModel() # p-scale priors for Se and Sp ----------------------------------------------------------
r$bg_process <<- callr::r_bg(
func = function(stan_model_p_scale_priors,
X,
study_sizes,
LCM_conditional_independence_indicator,
SeR_fixed,
SpR_fixed,
SeI_fixed,
SpI_fixed,
tables,
LCM_prior_sens_ref_lower95,
LCM_prior_sens_ref_upper95,
LCM_prior_spec_ref_lower95,
LCM_prior_spec_ref_upper95,
LCM_prior_SD_sens_ref_sd,
LCM_prior_SD_spec_ref_sd,
LCM_prior_sens_index_lower95,
LCM_prior_sens_index_upper95,
LCM_prior_spec_index_lower95,
LCM_prior_spec_index_upper95,
LCM_prior_SD_sens_index_sd,
LCM_prior_SD_spec_index_sd,
LCM_prior_prev_a,
LCM_prior_prev_b,
chains,
total_iter,
warmup_iter,
adapt_delta,
max_treedepth,
seed,
inits) {
rstan::sampling(
object = stan_model_p_scale_priors,
data = list(num_ref = length(unique(as.numeric(as.factor(X$reference.cat)))),
n_studies = nrow(X),
n = study_sizes,
r = tables, # 2x2 tables
Ref = as.numeric(as.factor(X$reference.cat)),
ci = LCM_conditional_independence_indicator,
SeR_fixed = SeR_fixed,
SpR_fixed = SpR_fixed,
SeI_fixed = SeI_fixed,
SpI_fixed = SpI_fixed,
roc_points_sp = seq(by = 0.01, from = 0, to = 1),
# dynamic priors for ref test
LCM_prior_sens_ref_lower95 = LCM_prior_sens_ref_lower95,
LCM_prior_sens_ref_upper95 = LCM_prior_sens_ref_upper95,
LCM_prior_spec_ref_lower95 = LCM_prior_spec_ref_lower95,
LCM_prior_spec_ref_upper95 = LCM_prior_spec_ref_upper95,
# static priors for ref test (between-study SD's, as meta-reg covariate only)
LCM_prior_SD_sens_ref_sd = LCM_prior_SD_sens_ref_sd,
LCM_prior_SD_spec_ref_sd = LCM_prior_SD_spec_ref_sd,
# priors for index test
LCM_prior_sens_index_lower95 = LCM_prior_sens_index_lower95,
LCM_prior_sens_index_upper95 = LCM_prior_sens_index_upper95,
LCM_prior_spec_index_lower95 = LCM_prior_spec_index_lower95,
LCM_prior_spec_index_upper95 = LCM_prior_spec_index_upper95,
LCM_prior_SD_sens_index_sd = LCM_prior_SD_sens_index_sd,
LCM_prior_SD_spec_index_sd = LCM_prior_SD_spec_index_sd,
# prev priors (beta dist)
LCM_prior_prev_a = LCM_prior_prev_a,
LCM_prior_prev_b = LCM_prior_prev_b
),
chains = chains,
iter = total_iter,
warmup = warmup_iter,
control=list(adapt_delta= adapt_delta,
max_treedepth = max_treedepth),
seed= seed,
init = inits
)
},
args = list(stan_model_p_scale_priors = stan_model_p_scale_priors_model,
X = X,
study_sizes = study_sizes,
LCM_conditional_independence_indicator = LCM_options_indicators$LCM_conditional_independence_indicator,
SeR_fixed = LCM_options_indicators$LCM_SeR_fixed_indicator,
SpR_fixed = LCM_options_indicators$LCM_SpR_fixed_indicator,
SeI_fixed = LCM_options_indicators$LCM_SeI_fixed_indicator,
SpI_fixed = LCM_options_indicators$LCM_SpI_fixed_indicator,
tables = tables,
# dynamic priors for ref test
LCM_prior_sens_ref_lower95 = as.array(priors$LCM_prior_sens_ref_lower95$vec[ref_indexes()]),
LCM_prior_sens_ref_upper95 = as.array(priors$LCM_prior_sens_ref_upper95$vec[ref_indexes()]),
LCM_prior_spec_ref_lower95 = as.array(priors$LCM_prior_spec_ref_lower95$vec[ref_indexes()]),
LCM_prior_spec_ref_upper95 = as.array(priors$LCM_prior_spec_ref_upper95$vec[ref_indexes()]),
# static priors for ref test (between-study SD's, as meta-reg covariate only)
LCM_prior_SD_sens_ref_sd = priors$LCM_prior_SD_sens_ref_sd,
LCM_prior_SD_spec_ref_sd = priors$LCM_prior_SD_spec_ref_sd,
# priors for index test
LCM_prior_sens_index_lower95 = priors$LCM_prior_sens_index_lower95,
LCM_prior_sens_index_upper95 = priors$LCM_prior_sens_index_upper95,
LCM_prior_spec_index_lower95 = priors$LCM_prior_spec_index_lower95,
LCM_prior_spec_index_upper95 = priors$LCM_prior_spec_index_upper95,
LCM_prior_SD_sens_index_sd = priors$LCM_prior_SD_sens_index_sd,
LCM_prior_SD_spec_index_sd = priors$LCM_prior_SD_spec_index_sd,
# prev priors (beta dist)
LCM_prior_prev_a = priors$LCM_prior_prev_a,
LCM_prior_prev_b = priors$LCM_prior_prev_b,
chains = sampler_options$MA_num_chains,
total_iter = sampler_options$MA_total_iter,
warmup_iter = sampler_options$MA_warmup_iter,
adapt_delta = sampler_options$MA_adapt_delta,
max_treedepth = sampler_options$MA_max_treedepth,
seed= sampler_options$MA_seed,
inits = rep(list(list(index_logit_mu = c(LCM_options_indicators$LCM_index_logit_mu_se,
LCM_options_indicators$LCM_index_logit_mu_sp))),
times = sampler_options$MA_num_chains )
),
stdout = tfile,
supervise = TRUE
)
}
else { # logit-scale priors for Se and Sp ----------------------------------------------------------
stan_model_model <- stan_model$getModel()
r$bg_process <<- callr::r_bg(
func = function(stan_model,
X,
study_sizes,
LCM_conditional_independence_indicator,
SeR_fixed,
SpR_fixed,
SeI_fixed,
SpI_fixed,
tables,
LCM_prior_mean_sens_ref_mu,
LCM_prior_mean_sens_ref_sd,
LCM_prior_mean_spec_ref_mu,
LCM_prior_mean_spec_ref_sd,
LCM_prior_SD_sens_ref_sd,
LCM_prior_SD_spec_ref_sd,
LCM_prior_mean_sens_index_mu,
LCM_prior_mean_sens_index_sd,
LCM_prior_mean_spec_index_mu,
LCM_prior_mean_spec_index_sd,
LCM_prior_SD_sens_index_sd,
LCM_prior_SD_spec_index_sd,
LCM_prior_prev_a,
LCM_prior_prev_b,
chains,
total_iter,
warmup_iter,
adapt_delta,
max_treedepth,
seed,
inits) {
rstan::sampling(
object = stan_model,
data = list(num_ref = length(unique(as.numeric(as.factor(X$reference.cat)))),
n_studies = nrow(X),
n = study_sizes,
r = tables, # 2x2 tables
Ref = as.numeric(as.factor(X$reference.cat)),
ci = LCM_conditional_independence_indicator,
SeR_fixed = SeR_fixed,
SpR_fixed = SpR_fixed,
SeI_fixed = SeI_fixed,
SpI_fixed = SpI_fixed,
roc_points_sp = seq(by = 0.01, from = 0, to = 1),
# dynamic priors for ref test
LCM_prior_mean_sens_ref_mu = LCM_prior_mean_sens_ref_mu,
LCM_prior_mean_sens_ref_sd = LCM_prior_mean_sens_ref_sd,
LCM_prior_mean_spec_ref_mu = LCM_prior_mean_spec_ref_mu,
LCM_prior_mean_spec_ref_sd = LCM_prior_mean_spec_ref_sd,
# static priors for ref test (between-study SD's, as meta-reg covariate only)
LCM_prior_SD_sens_ref_sd = LCM_prior_SD_sens_ref_sd,
LCM_prior_SD_spec_ref_sd = LCM_prior_SD_spec_ref_sd,
# priors for index test
LCM_prior_mean_sens_index_mu = LCM_prior_mean_sens_index_mu,
LCM_prior_mean_sens_index_sd = LCM_prior_mean_sens_index_sd,
LCM_prior_mean_spec_index_mu = LCM_prior_mean_spec_index_mu,
LCM_prior_mean_spec_index_sd = LCM_prior_mean_spec_index_sd,
LCM_prior_SD_sens_index_sd = LCM_prior_SD_sens_index_sd,
LCM_prior_SD_spec_index_sd = LCM_prior_SD_spec_index_sd,
# prev priors (beta dist)
LCM_prior_prev_a = LCM_prior_prev_a,
LCM_prior_prev_b = LCM_prior_prev_b
),
chains = chains,
iter = total_iter,
warmup = warmup_iter,
control=list(adapt_delta= adapt_delta,
max_treedepth = max_treedepth),
seed= seed,
init = inits
)
},
args = list(stan_model = stan_model_model,
X = X,
study_sizes = study_sizes,
LCM_conditional_independence_indicator = LCM_options_indicators$LCM_conditional_independence_indicator,
SeR_fixed = LCM_options_indicators$LCM_SeR_fixed_indicator,
SpR_fixed = LCM_options_indicators$LCM_SpR_fixed_indicator,
SeI_fixed = LCM_options_indicators$LCM_SeI_fixed_indicator,
SpI_fixed = LCM_options_indicators$LCM_SpI_fixed_indicator,
tables = tables,
# dynamic priors for ref test
LCM_prior_mean_sens_ref_mu = as.array(priors$LCM_prior_mean_sens_ref_mu$vec[ref_indexes()]),
LCM_prior_mean_sens_ref_sd = as.array(priors$LCM_prior_mean_sens_ref_sd$vec[ref_indexes()]),
LCM_prior_mean_spec_ref_mu = as.array(priors$LCM_prior_mean_spec_ref_mu$vec[ref_indexes()]),
LCM_prior_mean_spec_ref_sd = as.array(priors$LCM_prior_mean_spec_ref_sd$vec[ref_indexes()]),
# static priors for ref test (between-study SD's, as meta-reg covariate only)
LCM_prior_SD_sens_ref_sd = priors$LCM_prior_SD_sens_ref_sd,
LCM_prior_SD_spec_ref_sd = priors$LCM_prior_SD_spec_ref_sd,
# priors for index test
LCM_prior_mean_sens_index_mu = priors$LCM_prior_mean_sens_index_mu,
LCM_prior_mean_sens_index_sd = priors$LCM_prior_mean_sens_index_sd,
LCM_prior_mean_spec_index_mu = priors$LCM_prior_mean_spec_index_mu,
LCM_prior_mean_spec_index_sd = priors$LCM_prior_mean_spec_index_sd,
LCM_prior_SD_sens_index_sd = priors$LCM_prior_SD_sens_index_sd,
LCM_prior_SD_spec_index_sd = priors$LCM_prior_SD_spec_index_sd,
# prev priors (beta dist)
LCM_prior_prev_a = priors$LCM_prior_prev_a,
LCM_prior_prev_b = priors$LCM_prior_prev_b,
chains = sampler_options$MA_num_chains,
total_iter = sampler_options$MA_total_iter,
warmup_iter = sampler_options$MA_warmup_iter,
adapt_delta = sampler_options$MA_adapt_delta,
max_treedepth = sampler_options$MA_max_treedepth,
seed= sampler_options$MA_seed,
inits = rep(list(list(index_logit_mu = c(LCM_options_indicators$LCM_index_logit_mu_se,
LCM_options_indicators$LCM_index_logit_mu_sp))),
times = sampler_options$MA_num_chains )
),
stdout = tfile,
supervise = TRUE
)
} # end of else
r$poll <- TRUE
observe({
req(r$bg_process, r$poll)
show_modal_spinner(spin = "atom", color = "#005398", text = "Running Model")
invalidateLater(millis = 1000, session)
mtime <- file.info(tfile)$mtime
if (mtime > r$progress_mtime) {
r$progress <- readLines(tfile)
r$progress_mtime <- mtime
}
if (!r$bg_process$is_alive()) {
r$draws <- r$bg_process$get_result()
remove_modal_spinner()
r$poll <- FALSE
}
})
## print progress
output$progress_main_model <- renderText({
req(r$progress)
paste(r$progress,
collapse = "\n")
})
})
# Output list -------------------------------------------------------------
my_list <- list(
draws = reactive({ r$draws })
)
# Run the Garabage Collector to Ensure any excess memory used by stan is freed
gc()
return(my_list)
}
)
}