/
nplcm.R
3252 lines (2916 loc) Β· 148 KB
/
nplcm.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
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
if(getRversion() >= "2.15.1") utils::globalVariables(c("set_prior_tpr","set_prior_eti"))
#' Fit nested partially-latent class models (highest-level wrapper function)
#'
#' Uses `JAGS` (OSX or Windows) operating system for Bayesian posterior inference
#' (see `README` file for an instruction to install `JAGS`). If running `JAGS` on windows,
#' please go to control panel to add the directory to `JAGS` into ENVIRONMENTAL VARIABLE.
#'
#' @param data_nplcm Cases are on top of controls in the rows of diagnostic
#' test results and the covariate matrix. This is assumed by `baker` to automatically
#' write model files (`.bug`).
#' \itemize{
#' \item `Mobs` A list of measurements of distinct qualities (Bronze-, Silver, and Gold-Standard:
#' `MBS`,`MSS`,`MGS`). The elements of the list
#' should include `MBS`, `MSS`, and `MGS`. If any of the component
#' is not available, please specify it as, e.g., `MGS=NULL`
#' (effectively deleting `MGS` from `Mobs`).
#' \itemize{
#' \item `MBS` a list of data frame of bronze-standard (BrS) measurements.
#' For each data frame (referred to as a 'slice'),
#' rows are subjects, columns are causative agents (e.g., pathogen species).
#' We use `list` here to accommodate the possibility of multiple sets of BrS data.
#' They have imperfect sensitivity/specificity (e.g. nasopharyngeal polymerase chain
#' reaction - NPPCR).
#' \item `MSS` a list of data frame of silver-standard (SS) measurements.
#' Rows are subjects, columns are causative agents measured in specimen (e.g. blood culture).
#' These measurements have perfect specificity but imperfect sensitivity.
#' \item `MGS` a list of data frame of gold-standard (GS) measurements.
#' Rows are subject, columns are measured causative agents
#' These measurements have perfect sensitivity and specificity.
#' }
#'
#' \item `Y` Vector of disease status: `1` for case, `0` for control.
#' \item `X` Covariate matrix. A subset of columns are primary covariates in cause-specific-
#' case-fraction (CSCF) functions and hence must be available for cases, and another subset
#' are covariates that are available in the cases and the controls.
#' The two sets of covariates may be identical, overlapping or completely different.
#' In general, this is not the design matrix for regression models,
#' because for enrollment date in a study which may have non-linear effect,
#' basis expansion is often needed for approximation.
#' }
#'
#' @param model_options A list of model options: likelihood and prior.
#' \describe{
#' \item{`use_measurements`}{
#' A vector of characters strings; can be one or more from `"BrS"`, `"SS"`, `"GS"`.
#' }
#' \item{`likelihood`}{
#' \describe{
#' \item{cause_list}{ The vector of causes (NB: specify);}
#' \item{k_subclass}{ The number of nested subclasses in each
#' disease class (one of case classes or the control class; the same `k_subclass`
#' is assumed for each class) and each slice of BrS measurements.
#' `1` for conditional independence; larger than `1` for conditional dependence.
#' It is only available for BrS measurements. It is a vector of length equal to
#' the number of slices of BrS measurements;}
#' \item{Eti_formula}{ Formula for etiology regressions. You can use
#' [s_date_Eti()] to specify the design matrix for `R` format enrollment date;
#' it will produce natural cubic spline basis. Specify `~ 1` if no regression is intended.}
#' \item{FPR_formula}{formula for false positive rates (FPR) regressions; see [formula()].
#' You can use [s_date_FPR()] to specify part of the design matrix for `R`
#' format enrollment date; it will produce penalized-spline basis (based on B-splines).
#' Specify `~ 1` if no regression is intended. (NB: If `effect="fixed"`, [dm_Rdate_FPR()]
#' will just specify a design matrix with appropriately standardized dates.)}
#' }
#' }
#'
#' \item{`prior`}{
#' \describe{
#' \item{Eti_prior}{Description of etiology prior (e.g., `overall_uniform` -
#' all hyperparameters are `1`; or `0_1` - all hyperparameters are `0.1`);}
#' \item{TPR_prior}{Description of priors for the measurements
#' (e.g., informative vs non-informative). Its length should be the
#' same as `use_measurements` above. Please see examples for how to specify.
#' The package can also handle multiple slices of BrS, SS data, so separate
#' specification of the TPR priors are needed.
#' }
#' }
#' }
#' }
#' @param mcmc_options A list of Markov chain Monte Carlo (MCMC) options.
#' \itemize{
#' \item `debugstatus` Logical - whether to pause WinBUGS after it finishes
#' model fitting; (NB: is this obsolete? Test.)
#' \item `n.chains` Number of MCMC chains;
#' \item `n.burnin` Number of burn-in iterations;
#' \item `n.thin` To keep every other `n.thin` samples after burn-in period;
#' \item `individual.pred` `TRUE` to perform individual prediction (`Icat`
#' variables in the `.bug` file); `FALSE` otherwise;
#' \item `ppd` `TRUE` to simulate new data (`XXX.new`
#' variables in the `.bug` file) from the posterior predictive distribution (ppd);
#' `FALSE` otherwise;
#' \item `get.pEti` `TRUE` for getting posterior samples of individual etiologic fractions;
#' `FALSE` otherwise. For non-regression, or regression models with all discrete predictors,
#' by default this is `TRUE`, so no need to specify this entry. It is only relevant for regression models
#' with non-discrete covariates. Because individuals have distinct CSCFs at their specific covariate values,
#' it's easier to just store the posterior samples of the regression coefficients and reconstruct the pies afterwards,
#' rather than storing them through `JAGS`.
#' \item `result.folder` Path to folder storing the results;
#' \item `bugsmodel.dir` Path to `.bug` model files;
#' \item `jags.dir` Path to where JAGS is installed; if `NULL`, this will be set
#' to `jags.dir=""`.
#' }
#' @return A `JAGS` output result, fitted by function [R2jags::jags2()] from `R2jags`.
#' It is an object of class `nplcm` and `bugs`.
#' Current implemented models follow the hierarchy below:
#' \itemize{
#' \item no regression: Fitted by at low level by [nplcm_fit_NoReg]
#'
#' \item regression:
#' Given disease class (control or a class of cases with the same subset of causative agents):
#' \itemize{
#' \item local independence model for BrS measures:
#' Fitted at lower level by
#' \itemize{
#' \item [nplcm_fit_Reg_NoNest] deals with the setting with two sets of covariates,
#' one for CSCF regression and the other for FPR regression. The two sets of
#' covariates may be identical, overlapping or non-overlapping.
#' This function is called when there exists one or more than one discrete covariate among
#' the union of the two covariate sets. The method implemented by this function
#' directly lets FPR depend upon covariates.
#' This is different from Wu and Chen (2021), which let the subclass
#' weights depend upon covariates. We implemented this function for methods comparison.
#' \item [nplcm_fit_Reg_discrete_predictor_NoNest] deals with the setting
#' with all discrete covariates for FPRs and CSCFs. The strata defined by the two sets of
#' covariates need not be identical, e.g., as a result of distinct sets of covariates. Again,
#' this is directly to let FPR be stratified by covariates, hence different from Wu and Chen (2020+)
#' We implemented this function for methods comparison.
#' }
#' \item local dependence model for BrS measures:
#' Fitted at lower level by [nplcm_fit_Reg_Nest]: This is the method introduced in
#' Wu and Chen (2021): CSCF regression + case/control subclass weight regression.
#' It does not provide a specialized function for the setting with all discrete covariates.
#' }
#' }
#'
#' @examples
#'
#' \donttest{
#' data(data_nplcm_noreg)
#' cause_list <- LETTERS[1:6]
#' J.BrS <- 6
#' model_options_no_reg <- list(
#' likelihood = list(
#' cause_list = cause_list,
#' k_subclass = 2,
#' Eti_formula = ~-1, # no covariate for the etiology regression
#' FPR_formula = list(
#' MBS1 = ~-1) # no covariate for the subclass weight regression
#' ),
#' use_measurements = c("BrS"),
#' # use bronze-standard data only for model estimation.
#' prior= list(
#' Eti_prior = overall_uniform(1,cause_list),
#' # Dirichlet(1,...,1) prior for the etiology.
#' TPR_prior = list(BrS = list(
#' info = "informative", # informative prior for TPRs
#' input = "match_range",
#' # specify the informative prior for TPRs by specifying a plausible range.
#' val = list(MBS1 = list(up = list(rep(0.99,J.BrS)),
#' # upper ranges: matched to 97.5% quantile of a Beta prior
#' low = list(rep(0.55,J.BrS))))
#' # lower ranges: matched to 2.5% quantile of a Beta prior
#' )
#' )
#' )
#' )
#'
#'
#' set.seed(1)
#' # include stratification information in file name:
#' thedir <- paste0(tempdir(),"_no_reg")
#'
#' # create folders to store the model results
#' dir.create(thedir, showWarnings = FALSE)
#' result_folder_no_reg <- file.path(thedir,paste("results",collapse="_"))
#' thedir <- result_folder_no_reg
#' dir.create(thedir, showWarnings = FALSE)
#'
#' # options for MCMC chains:
#' mcmc_options_no_reg <- list(
#' debugstatus = TRUE,
#' n.chains = 1,
#' n.itermcmc = as.integer(200),
#' n.burnin = as.integer(100),
#' n.thin = 1,
#' individual.pred = TRUE, # <- must set to TRUE! <------- NOTE!
#' ppd = FALSE,
#' result.folder = thedir,
#' bugsmodel.dir = thedir
#' )
#'
#' BrS_object_1 <- make_meas_object(patho = LETTERS[1:6],
#' specimen = "MBS", test = "1",
#' quality = "BrS", cause_list = cause_list)
#' clean_options <- list(BrS_objects = make_list(BrS_object_1))
#' # place the nplcm data and cleaning options into the results folder
#' dput(data_nplcm_noreg,file.path(thedir,"data_nplcm.txt"))
#' dput(clean_options, file.path(thedir, "data_clean_options.txt"))
#'
#' rjags::load.module("glm")
#'
#' nplcm_noreg <- nplcm(data_nplcm_noreg,model_options_no_reg,mcmc_options_no_reg)
#'
#'
#' }
#'
#'
#' @export
nplcm <- function(data_nplcm,model_options,mcmc_options){
mcmc_options$use_jags <- TRUE
Mobs <- data_nplcm$Mobs
Y <- data_nplcm$Y
if (length(rle(Y)[["values"]])>2 | Y[1]!=1) {
stop("==[baker] 'data_nplcm$Y' must have cases on top of controls.
Use 'baker::subset_data_nplcm_by_index()' to shuffle the rows. Then retry.==\n")}
X <- data_nplcm$X
parsed_model <- assign_model(model_options,data_nplcm)
do_reg_vec <- unlist(parsed_model$regression[grep("^do_reg_",names(parsed_model$regression))])
do_discrete_vec <- unlist(parsed_model$regression[grep("^is_discrete_predictor",names(parsed_model$regression))])
do_discrete_and_reg <- do_reg_vec&do_discrete_vec
do_reg <- any(do_reg_vec)
do_discrete <- sum(do_discrete_and_reg)==sum(do_reg_vec)
# only call nplcm_fit_Reg_discrete_predictor_NoNest
# when ALL regressions use discrete predictors!
do_nested <- parsed_model$nested
fitted_type <- NA
if(!do_reg){
res <- nplcm_fit_NoReg(data_nplcm,model_options,mcmc_options)
fitted_type <- "no_reg"
}
if (do_reg & !any(do_nested)){
if (do_discrete){
fitted_type <- "reg_nonest_strat"
# when every regression is a regression upon discrete variables;
# when it is not a regression, the fitting function treats it as a single stratum
# when specifying the model in the .bug file (in the assign_model function, ~1
# is considered not a regression, i.e., covariate independent)
res <- nplcm_fit_Reg_discrete_predictor_NoNest(data_nplcm,model_options,mcmc_options)
} else{
fitted_type <- "reg_nonest"
# a mix of discrete and continuous regressions:
res <- nplcm_fit_Reg_NoNest(data_nplcm,model_options,mcmc_options)
}
}
if (do_reg & any(do_nested)){
if (do_discrete) {fitted_type <- "reg_nest_strat"}else{fitted_type <- "reg_nest"}
res <- nplcm_fit_Reg_Nest(data_nplcm,model_options,mcmc_options)
}
res$DIR_NPLCM <- mcmc_options$result.folder # <--- add info about where results are stored.
res$fitted_type <- fitted_type
class(res) <- c("nplcm","bugs")
return(res)
}
#' Interpret the specified model structure
#'
#' `assign_model` translates options specified by a user (e.g., in
#' `model_options`) into information that can be understood by `baker`.
#'
#' @details `assign_model` will be modified to check if data are conformable
#' to specified model.
#'
#' @param data_nplcm Data. See [nplcm()] function for data structure.
#' @param model_options See [nplcm()] function.
#' @param silent Default is `TRUE` for no messages; `FALSE` otherwise.
#' @return A list of model specifications:
#' \itemize{
#' \item `num_slice` A vector counting the No. of measurement slices for each
#' level of measurement quality (e.g., MBS, MSS, MGS representing
#' Bronze-Standard Measurements - case-control,
#' Silver-Standard Measurements and Gold-Standard
#' Measurements - case-only);
#' \item `nested` Local dependence specification for modeling bronze-standard
#' data. `TRUE` for nested models (conditional dependence given disease class);
#' `FALSE` for non-nested models (conditional independence given disease class).
#' One for each BrS slice.
#' \item `regression`
#' \itemize{
#' \item `do_reg_Eti` `TRUE` for doing etiology regression.
#' It means let the etiology fractions vary with explanatory variables.
#' `FALSE` otherwise;
#' \item `do_reg_FPR` A vector whose names represent the slices
#' of bronze-standard data. For each slice of BrS measurements,
#' `TRUE` does false positive rate regression. It means the false
#' positive rates, estimatable from controls, can vary with
#' covariates; `FALSE` otherwise.
#' \item `is_discrete_predictor` A list of names "Eti", and
#' the names for every slice of bronze-standard data. `TRUE`
#' if all predictors are discrete; `FALSE` otherwise.
#' }
#' }
#'
#' @examples
#' cause_list <- c(LETTERS[1:6])
#' J.BrS <- 6
#' model_options_no_reg <- list(
#' likelihood = list(
#' cause_list = cause_list,
#' k_subclass = 2,
#' Eti_formula = ~-1,
#' # no covariate for the etiology regression
#' FPR_formula = list(
#' MBS1 = ~-1)
#' # no covariate for the subclass weight regression
#' ),
#' use_measurements = c("BrS"),
#' # use bronze-standard data only for model estimation.
#' prior= list(
#' Eti_prior = overall_uniform(1,cause_list),
#' # Dirichlet(1,...,1) prior for the etiology.
#' TPR_prior = list(BrS = list(
#' info = "informative", # informative prior for TPRs
#' input = "match_range",
#' # specify the informative prior for TPRs by specifying a plausible range.
#' val = list(MBS1 = list(up = list(rep(0.99,J.BrS)),
#' # upper ranges: matched to 97.5% quantile of a Beta prior
#' low = list(rep(0.55,J.BrS))))
#' # lower ranges: matched to 2.5% quantile of a Beta prior
#' )
#' )
#' )
#' )
#' data("data_nplcm_noreg")
#'
#' assign_model(model_options_no_reg,data_nplcm_noreg)
#'
#' @family specification checking functions
#' @export
assign_model <- function(model_options,data_nplcm, silent=TRUE){
# load options:
likelihood <- model_options$likelihood
use_measurements <- model_options$use_measurements
prior <- model_options$prior
# load data:
Mobs <- data_nplcm$Mobs
Y <- data_nplcm$Y
X <- data_nplcm$X
nested <- likelihood$k_subclass > 1
# test the match between actual data and model_options:
use_data_sources <- c("MBS","MSS","MGS")[lookup_quality(use_measurements)]
input_data_sources <- names(Mobs)
if (!all(use_data_sources%in%input_data_sources)){
stop("==[baker] Please supply actual datasets as specified by 'use_measurements' in 'model_options'.==\n")
}
# get the length of each measurement quality:
num_slice <- rep(0,3)
names(num_slice) <- c("MBS","MSS","MGS")
for (i in seq_along(use_data_sources)){
num_slice[use_data_sources[i]] <- length(Mobs[[use_data_sources[i]]])
}
# specify regression for FPR: (only available for bronze-standard data. Silver-standard data automatically have FPR==0.)
do_reg_FPR <- is_discrete_FPR <- rep(NA,length(likelihood$FPR_formula)) # <---- a regression for each measurement slice?
names(do_reg_FPR) <- names(is_discrete_FPR) <- names(likelihood$FPR_formula)
for (i in seq_along(Mobs$MBS)){
ind_tmp <-which(names(likelihood$FPR_formula) == names(Mobs$MBS)[i])
form_tmp <- stats::as.formula(likelihood$FPR_formula[[ind_tmp]])
if (!length(ind_tmp)) { # don't do regression if no regression formula is found:
do_reg_FPR[i] <- FALSE
} else{ # do regression if there is matched regression formula:
do_reg_FPR[i] <-
parse_nplcm_reg(form_tmp,data_nplcm,silent=silent)
}
is_discrete_FPR[i] <- FALSE
if (!is.null(X)){
is_discrete_FPR[i] <- (!is_intercept_only(form_tmp) &
!is_intercept_only(form_tmp)&
is_discrete(data.frame(X,Y), form_tmp))
}
}
#
# specify regression for TPR: (every measurement slice has it.)
#
# do_reg_TPR <- list() # <---- a regression for each measurement slice?
# for (i in seq_along(Mobs$MBS)){
# ind_tmp <-
# which(names(likelihood$TPR_formula) == names(Mobs$MBS)[i])
# if (!length(ind_tmp)) { # don't do regression if no regression formula is found:
# do_reg_TPR[[i]] <- FALSE
# } else{ # do regression if there is matched regression formula:
# do_reg_TPR[[i]] <-
# parse_nplcm_reg(stats::as.formula(likelihood$TPR_formula[[ind_tmp]]),data_nplcm,silent=silent)
# }
# }
#
# names(do_reg_TPR) <- names(Mobs$MBS)
#
# # if using silver-standard data:
# if ("MSS"%in% use_data_sources){
# for (i in length(Mobs$MBS)+seq_along(Mobs$MSS)){
# ind_tmp <-
# which(names(likelihood$TPR_formula) == names(Mobs$MSS)[i])
# if (!length(ind_tmp)) { # don't do regression if no regression formula is found:
# do_reg_TPR[[i]] <- FALSE
# } else{ # do regression if there is matched regression formula:
# do_reg_TPR[[i]] <-
# parse_nplcm_reg(stats::as.formula(likelihood$TPR_formula[[ind_tmp]]),data_nplcm,silent=silent)
# }
# }
# names(do_reg_TPR) <- c(names(Mobs$MBS),names(Mobs$MSS))
# }
# specify regression for etiology:
form_tmp <- stats::as.formula(likelihood$Eti_formula)
do_reg_Eti <- parse_nplcm_reg(form_tmp,data_nplcm,silent=silent)
is_discrete_Eti <- FALSE
if (!is.null(X)){ # <--- potential problem if a user input more data than needed. need fixing.
is_discrete_Eti <- (!stats::is.empty.model(form_tmp) &
!is_intercept_only(form_tmp) &
is_discrete(data.frame(X,Y)[Y==1,,drop=FALSE], form_tmp))
}
is_discrete_predictor <- list(is_discrete_Eti, is_discrete_FPR)
names(is_discrete_predictor)[1] <- "Eti"
names(is_discrete_predictor)[2] <- "FPR"
regression <- make_list(do_reg_Eti, do_reg_FPR,is_discrete_predictor)#, do_reg_TPR)
# check BrS group:
BrS_grp <- FALSE
prior_BrS <- model_options$prior$TPR_prior$BrS
GBrS_TPR <- length(unique(prior_BrS$grp))
grp_spec <- (!is.null(prior_BrS$grp) && GBrS_TPR >1 )
if (grp_spec) {
for (s in seq_along(prior_BrS$val)){
if (prior_BrS$input=="match_range" &&
(length(prior_BrS$val[[s]]$up)!=GBrS_TPR |
length(prior_BrS$val[[s]]$low)!=GBrS_TPR) ){
stop(paste0("==[baker] ",names(prior_BrS$val)[s])," needs ", GBrS_TPR,
" sets of sensitivity ranges.==\n")
}
}
BrS_grp <- TRUE
}
# check SS group:
SS_grp <- FALSE
prior_SS <- model_options$prior$TPR_prior$SS
grp_spec <- (!is.null(prior_SS$grp) && length(unique(prior_SS$grp)) >1 )
if (grp_spec) {SS_grp <- TRUE}
## <-------- the following are more strict grp specifications (may cause error when running old folders):
# val_spec <- (num_slice["MSS"]>0 && any(lapply(prior_SS$val,length)>1))
#
# if (grp_spec && val_spec){SS_grp <- TRUE}
# if (grp_spec && !val_spec){stop("==Specified TPR group in 'grp' of 'model_options$prior$TPR_prior$SS',
# but either there is no SS data or the length of 'val' does not match the no. of TPR groups. ==")}
# if (!grp_spec && val_spec){stop("==No 'grp' specified in 'model_options$prior$TPR_prior$SS',
# but we have >1 sets of TPRs. ==")}
# return results:
make_list(num_slice, nested, regression,BrS_grp,SS_grp)
}
#' Fit nested partially-latent class model (low-level)
#'
#' This function prepares data, specifies hyperparameters in priors
#' (true positive rates and etiology fractions), initializes the posterior
#' sampling chain, writes the model file (for JAGS or WinBUGS with slight differences
#' in syntax), and fits the model. Features:
#' \itemize{
#' \item no regression;
#' \item no nested subclasses
#' }
#'
#' @inheritParams nplcm
#' @return BUGS fit results.
#'
#' @seealso [write_model_NoReg] for constructing `.bug` model file; This function
#' then put it in the folder `mcmc_options$bugsmodel.dir`.
#'
#' @family model fitting functions
#'
#'
nplcm_fit_NoReg<-
function(data_nplcm,model_options,mcmc_options){
# Record the settings of current analysis:
cat("==[baker] Results stored in: ==","\n",mcmc_options$result.folder,"\n")
#model_options:
dput(model_options,file.path(mcmc_options$result.folder,"model_options.txt"))
#mcmc_options:
dput(mcmc_options,file.path(mcmc_options$result.folder,"mcmc_options.txt"))
# read in data:
Mobs <- data_nplcm$Mobs
Y <- data_nplcm$Y
# read in options:
likelihood <- model_options$likelihood
use_measurements <- model_options$use_measurements
prior <- model_options$prior
#####################################################################
# 1. prepare data (including hyper-parameters):
#####################################################################
# sample sizes:
Nd <- sum(Y==1)
Nu <- sum(Y==0)
# lengths of vectors:
cause_list <- likelihood$cause_list
Jcause <- length(cause_list)
in_data <- in_init <- out_parameter <- NULL
if ("BrS" %in% use_measurements){
#
# BrS measurement data:
#
JBrS_list <- lapply(Mobs$MBS,ncol)
# mapping template (by `make_template` function):
patho_BrS_list <- lapply(Mobs$MBS,colnames)
template_BrS_list <- lapply(patho_BrS_list,make_template,cause_list)
for (s in seq_along(template_BrS_list)){
if (sum(template_BrS_list[[s]])==0){
warning(paste0("==[baker] Bronze-standard slice ", names(data_nplcm$Mobs$MBS)[s], " has no measurements informative of the causes specified in 'cause_list', except 'NoA'!
Please check if you need this measurement slice columns correspond to causes other than 'NoA'.=="))
}
}
MBS.case_list <- lapply(Mobs$MBS,"[",which(Y==1),TRUE,drop=FALSE)
MBS.ctrl_list <- lapply(Mobs$MBS,"[",which(Y==0),TRUE,drop=FALSE)
MBS_list <- list()
for (i in seq_along(MBS.case_list)){
MBS_list[[i]] <- rbind(MBS.case_list[[i]],MBS.ctrl_list[[i]])
}
names(MBS_list) <- names(MBS.case_list)
single_column_MBS <- which(lapply(MBS_list,ncol)==1)
for(i in seq_along(JBrS_list)){
assign(paste("JBrS", i, sep = "_"), JBrS_list[[i]])
xx <- as.matrix_or_vec(MBS_list[[i]]); attr(xx,"dimnames") <- NULL # remove dimnames.
assign(paste("MBS", i, sep = "_"), xx)
assign(paste("templateBS", i, sep = "_"), as.matrix_or_vec(template_BrS_list[[i]]))
}
# setup groupwise TPR for BrS:
BrS_TPR_strat <- FALSE
prior_BrS <- model_options$prior$TPR_prior$BrS
parsed_model <- assign_model(model_options,data_nplcm)
if (parsed_model$BrS_grp){
BrS_TPR_strat <- TRUE
for(i in seq_along(JBrS_list)){
assign(paste("GBrS_TPR", i, sep = "_"), length(unique(prior_BrS$grp)))
assign(paste("BrS_TPR_grp", i, sep = "_"), prior_BrS$grp)
}
}
# add GBrS_TPR_1, or 2 if we want to index by slices:
for (i in seq_along(JBrS_list)){
if (!is.null(prior_BrS$grp)){ # <--- need to change to list if we have multiple slices.
assign(paste("GBrS_TPR", i, sep = "_"), length(unique(prior_BrS$grp))) # <--- need to change to depending on i if grp change wrt specimen.
}
if (is.null(prior_BrS$grp)){ # <--- need to change to list if we have multiple slices.
assign(paste("GBrS_TPR", i, sep = "_"), 1)
}
}
# set BrS measurement priors:
# hyper-parameters for sensitivity:
alpha_mat <- list() # dimension for slices.
beta_mat <- list()
for(i in seq_along(JBrS_list)){
if (likelihood$k_subclass[i] == 1){BrS_tpr_prior <- set_prior_tpr_BrS_NoNest(i,model_options,data_nplcm)}
if (likelihood$k_subclass[i] > 1) {BrS_tpr_prior <- set_prior_tpr_BrS_NoNest(i,model_options,data_nplcm)}
GBrS_TPR_curr <- eval(parse(text = paste0("GBrS_TPR_",i)))
alpha_mat[[i]] <- matrix(NA, nrow=GBrS_TPR_curr,ncol=JBrS_list[[i]])
beta_mat[[i]] <- matrix(NA, nrow=GBrS_TPR_curr,ncol=JBrS_list[[i]])
colnames(alpha_mat[[i]]) <- patho_BrS_list[[i]]
colnames(beta_mat[[i]]) <- patho_BrS_list[[i]]
for (g in 1:GBrS_TPR_curr){
alpha_mat[[i]][g,] <- unlist(BrS_tpr_prior[[1]][[g]]$alpha)
beta_mat[[i]][g,] <- unlist(BrS_tpr_prior[[1]][[g]]$beta)
}
if (GBrS_TPR_curr>1){
assign(paste("alphaB", i, sep = "_"), alpha_mat[[i]]) # <---- input BrS TPR prior here.
assign(paste("betaB", i, sep = "_"), beta_mat[[i]])
}else{
assign(paste("alphaB", i, sep = "_"), c(alpha_mat[[i]])) # <---- input BrS TPR prior here.
assign(paste("betaB", i, sep = "_"), c(beta_mat[[i]]))
}
}
names(alpha_mat) <- names(beta_mat)<- names(Mobs$MBS)
if (!BrS_TPR_strat){
# summarize into one name (for all measurements):
if (length(single_column_MBS)==0){
# if all slices have >2 columns:
in_data <- c(in_data,"Nd","Nu","Jcause","alphaEti",
paste("JBrS",1:length(JBrS_list),sep="_"),
paste("MBS",1:length(JBrS_list),sep="_"),
paste("templateBS",1:length(JBrS_list),sep="_")
# paste("alphaB",1:length(JBrS_list),sep="_"),
# paste("betaB",1:length(JBrS_list),sep="_")
)
} else {
# if there exist slices with 1 column:
in_data <- c(in_data,"Nd","Nu","Jcause","alphaEti",
paste("JBrS",1:length(JBrS_list),sep="_")[-single_column_MBS], # <---- no need to iterate in .bug file for a slice with one column.
paste("MBS",1:length(JBrS_list),sep="_"),
paste("templateBS",1:length(JBrS_list),sep="_")
# paste("alphaB",1:length(JBrS_list),sep="_"),
# paste("betaB",1:length(JBrS_list),sep="_")
)
}
} else{
# summarize into one name (for all measurements):
if (length(single_column_MBS)==0){
# if all slices have >2 columns:
in_data <- c(in_data,"Nd","Nu","Jcause","alphaEti",
paste("JBrS",1:length(JBrS_list),sep="_"),
paste("GBrS_TPR",1:length(JBrS_list),sep="_"), # <-- added for TPR strata.
paste("BrS_TPR_grp",1:length(JBrS_list),sep="_"),# <-- added for TPR strata.
paste("MBS",1:length(JBrS_list),sep="_"),
paste("templateBS",1:length(JBrS_list),sep="_")
# paste("alphaB",1:length(JBrS_list),sep="_"),
# paste("betaB",1:length(JBrS_list),sep="_")
)
} else {
# if there exist slices with 1 column:
in_data <- c(in_data,"Nd","Nu","Jcause","alphaEti",
paste("JBrS",1:length(JBrS_list),sep="_")[-single_column_MBS], # <---- no need to iterate in .bug file for a slice with one column.
paste("GBrS_TPR",1:length(JBrS_list),sep="_"), # <-- added for TPR strata.
paste("BrS_TPR_grp",1:length(JBrS_list),sep="_"),# <-- added for TPR strata.
paste("MBS",1:length(JBrS_list),sep="_"),
paste("templateBS",1:length(JBrS_list),sep="_")
# paste("alphaB",1:length(JBrS_list),sep="_"),
# paste("betaB",1:length(JBrS_list),sep="_")
)
}
}
#
# hyper-parameters:
#
# Set BrS measurement priors:
# hyperparameter for sensitivity (can add for specificity if necessary):
for (s in seq_along(Mobs$MBS)){
#if (likelihood$k_subclass[s] == 1){BrS_tpr_prior <- set_prior_tpr_BrS_NoNest(s,model_options,data_nplcm)}
#if (likelihood$k_subclass[s] > 1){BrS_tpr_prior <- set_prior_tpr_BrS_NoNest(s,model_options,data_nplcm)}
#assign(paste("alphaB", s, sep = "_"), BrS_tpr_prior[[1]]$alpha) # <---- input BrS TPR prior here.
#assign(paste("betaB", s, sep = "_"), BrS_tpr_prior[[1]]$beta)
if (likelihood$k_subclass[s]==1){
out_parameter <- c(out_parameter,paste(c("thetaBS","psiBS"), s, sep="_"))
}else{ # <--- TPR stratification for BrS data not implemented for K>1.
assign(paste("K", s, sep = "_"), likelihood$k_subclass[s])
in_data <- unique(c(in_data,paste0("K_",s))) # <---- No. of subclasses for this slice.
out_parameter <- unique(c(out_parameter,
paste(c("ThetaBS","PsiBS","Lambda","Eta","alphadp0","alphadp0_case"),s,sep="_")
))
}
}
#
# collect in_data, out_parameter together:
#
in_data <- c(in_data,
paste("alphaB",1:length(JBrS_list),sep="_"),
paste("betaB",1:length(JBrS_list),sep="_")
)
out_parameter <- c(out_parameter,"pEti")
}
if ("SS" %in% use_measurements){
#
# 2. SS measurement data:
#
JSS_list <- lapply(Mobs$MSS,ncol)
# mapping template (by `make_template` function):
patho_SS_list <- lapply(Mobs$MSS,colnames)
template_SS_list <- lapply(patho_SS_list,make_template,cause_list)
for (s in seq_along(template_SS_list)){
if (nrow(Mobs$MSS[[s]])==0){stop(paste0("==[baker] Silver-standard (SS) slice ", names(data_nplcm$Mobs$MSS)[s],
" has zero rows of data. Check if you accidentally removed all rows in this SS data slice.==\n"))}
if (sum(template_SS_list[[s]])==0){
warning(paste0("==[baker] Silver-standard slice ", names(data_nplcm$Mobs$MSS)[s],
" has no measurements informative of the causes! Please check if measurements' columns correspond to causes.==\n"))
}
}
MSS_list <- lapply(Mobs$MSS,"[",which(Y==1),TRUE,drop=FALSE)
single_column_MSS <- which(lapply(MSS_list,ncol)==1)
for(i in seq_along(JSS_list)){
assign(paste("JSS", i, sep = "_"), JSS_list[[i]])
assign(paste("MSS", i, sep = "_"), as.matrix_or_vec(MSS_list[[i]]))
assign(paste("templateSS", i, sep = "_"), as.matrix_or_vec(template_SS_list[[i]]))
}
# setup groupwise TPR for SS:
SS_TPR_strat <- FALSE
prior_SS <- model_options$prior$TPR_prior$SS
parsed_model <- assign_model(model_options,data_nplcm)
if (parsed_model$SS_grp){
SS_TPR_strat <- TRUE
for(i in seq_along(JSS_list)){
assign(paste("GSS_TPR", i, sep = "_"), length(unique(prior_SS$grp)))
assign(paste("SS_TPR_grp", i, sep = "_"), prior_SS$grp)
}
}
# add GSS_TPR_1, or 2 if we want to index by slices:
for (i in seq_along(JSS_list)){
if (!is.null(prior_SS$grp)){ # <--- need to change to list if we have multiple slices.
assign(paste("GSS_TPR", i, sep = "_"), length(unique(prior_SS$grp))) # <--- need to change to depending on i if grp change wrt specimen.
}
if (is.null(prior_SS$grp)){ # <--- need to change to list if we have multiple slices.
assign(paste("GSS_TPR", i, sep = "_"), 1)
}
}
SS_tpr_prior <- set_prior_tpr_SS(model_options,data_nplcm)
# set SS measurement priors:
# hyper-parameters for sensitivity:
alpha_mat <- list() # dimension for slices.
beta_mat <- list()
for(i in seq_along(JSS_list)){
GSS_TPR_curr <- eval(parse(text = paste0("GSS_TPR_",i)))
alpha_mat[[i]] <- matrix(NA, nrow=GSS_TPR_curr,ncol=JSS_list[[i]])
beta_mat[[i]] <- matrix(NA, nrow=GSS_TPR_curr,ncol=JSS_list[[i]])
colnames(alpha_mat[[i]]) <- patho_SS_list[[i]]
colnames(beta_mat[[i]]) <- patho_SS_list[[i]]
for (g in 1:GSS_TPR_curr){
alpha_mat[[i]][g,] <- unlist(SS_tpr_prior[[i]][[g]]$alpha)
beta_mat[[i]][g,] <- unlist(SS_tpr_prior[[i]][[g]]$beta)
}
if (GSS_TPR_curr>1){
assign(paste("alphaS", i, sep = "_"), alpha_mat[[i]]) # <---- input SS TPR prior here.
assign(paste("betaS", i, sep = "_"), beta_mat[[i]])
}else{
assign(paste("alphaS", i, sep = "_"), c(alpha_mat[[i]])) # <---- input SS TPR prior here.
assign(paste("betaS", i, sep = "_"), c(beta_mat[[i]]))
}
}
names(alpha_mat) <- names(beta_mat)<- names(Mobs$MSS)
if (!SS_TPR_strat){
if (length(single_column_MSS)==0){
# summarize into one name (for all measurements):
in_data <- unique(c(in_data,"Nd","Jcause","alphaEti",
paste("JSS",1:length(JSS_list),sep="_"),
paste("MSS",1:length(JSS_list),sep="_"),
paste("templateSS",1:length(JSS_list),sep="_"),
paste("alphaS",1:length(JSS_list),sep="_"),
paste("betaS",1:length(JSS_list),sep="_")))
} else{
in_data <- unique(c(in_data,"Nd","Nu","Jcause","alphaEti",
paste("JSS",1:length(JSS_list),sep="_")[-single_column_MSS],
paste("MSS",1:length(JSS_list),sep="_"),
paste("templateSS",1:length(JSS_list),sep="_"),
paste("alphaS",1:length(JSS_list),sep="_"),
paste("betaS",1:length(JSS_list),sep="_")))
}
}else {
if (length(single_column_MSS)==0){
# summarize into one name (for all measurements):
in_data <- unique(c(in_data,"Nd","Jcause","alphaEti",
paste("JSS",1:length(JSS_list),sep="_"),
paste("GSS_TPR",1:length(JSS_list),sep="_"), # <-- added for TPR strata.
paste("SS_TPR_grp",1:length(JSS_list),sep="_"), # <-- added for TPR strata.
paste("MSS",1:length(JSS_list),sep="_"),
paste("templateSS",1:length(JSS_list),sep="_"),
paste("alphaS",1:length(JSS_list),sep="_"),
paste("betaS",1:length(JSS_list),sep="_")))
} else{
in_data <- unique(c(in_data,"Nd","Nu","Jcause","alphaEti",
paste("JSS",1:length(JSS_list),sep="_")[-single_column_MSS],
paste("GSS_TPR",1:length(JSS_list),sep="_"), # <-- added for TPR strata.
paste("SS_TPR_grp",1:length(JSS_list),sep="_"),# <-- added for TPR strata.
paste("MSS",1:length(JSS_list),sep="_"),
paste("templateSS",1:length(JSS_list),sep="_"),
paste("alphaS",1:length(JSS_list),sep="_"),
paste("betaS",1:length(JSS_list),sep="_")))
}
}
out_parameter <- unique(c(out_parameter, paste("thetaSS", seq_along(JSS_list), sep = "_"),
"pEti"))
}
#####################################
# set up initialization function:
#####################################
if ("BrS" %in% use_measurements & !("SS" %in% use_measurements)){
in_init <- function(){
res <- list()
for (s in seq_along(Mobs$MBS)){
res_curr <- list()
if (likelihood$k_subclass[s]==1){
#res_curr[[1]] <- stats::rbeta(JBrS_list[[s]],1,1)
GBrS_TPR_curr <- eval(parse(text = paste0("GBrS_TPR_",s)))
if (GBrS_TPR_curr==1){
res_curr[[1]] <- stats::rbeta(JBrS_list[[s]],1,1)
} else{
res_curr[[1]] <- matrix(stats::rbeta(GBrS_TPR_curr*JBrS_list[[s]],1,1),
nrow=GBrS_TPR_curr,ncol=JBrS_list[[s]])
if (JBrS_list[[s]]==1){
res_curr[[1]] <- c(res_curr[[1]])
}
}
res_curr[[2]] <- stats::rbeta(JBrS_list[[s]],1,1)
names(res_curr) <- paste(c("thetaBS","psiBS"),s,sep="_")
res <- c(res,res_curr)
}
if (likelihood$k_subclass[s] > 1){
K_curr <- likelihood$k_subclass[s]
res_curr[[1]] <- c(rep(.5,K_curr-1),NA)
res_curr[[2]] <- c(rep(.5,K_curr-1),NA)
# for different Eta's:
# res_curr[[2]] <- cbind(matrix(rep(.5,Jcause*(K_curr-1)),
# nrow=Jcause,ncol=K_curr-1),
# rep(NA,Jcause))
res_curr[[3]] <- 1
res_curr[[4]] <- 1 #<---- added together with 'alphadp0_case' below.
names(res_curr) <- paste(c("r0","r1","alphadp0","alphadp0_case"),s,sep="_")
res <- c(res,res_curr)
}
}
res
}
}
if (!("BrS" %in% use_measurements) & "SS" %in% use_measurements){
in_init <- function(){
tmp_thetaSS <- list()
#tmp_psiSS <- list()
tmp_Icat_case <- list()
for(i in seq_along(JSS_list)){
GSS_TPR_curr <- eval(parse(text = paste0("GSS_TPR_",i)))
if (GSS_TPR_curr==1){
tmp_thetaSS[[i]] <- stats::rbeta(JSS_list[[i]],1,1)
} else{
tmp_thetaSS[[i]] <- matrix(stats::rbeta(GSS_TPR_curr*JSS_list[[i]],1,1),
nrow=GSS_TPR_curr,ncol=JSS_list[[i]])
}
if (i==1){
#if (length(JSS_list)>1){
# warning("==[baker] Only the first slice of silver-standard data is used to
# initialize 'Icat' in JAGS fitting. Choose wisely!\n ==")
#}
tmp_Icat_case[[i]] <- init_latent_jags_multipleSS(MSS_list,likelihood$cause_list)
}
}
res <- c(tmp_thetaSS,tmp_Icat_case)
names(res) <- c(paste("thetaSS", seq_along(JSS_list), sep = "_"),"Icat")
res
}
}
if ("BrS" %in% use_measurements & "SS" %in% use_measurements){
in_init <- function(){
res <- list()
for (s in seq_along(Mobs$MBS)){
res_curr <- list()
if (likelihood$k_subclass[s]==1){
#res_curr[[1]] <- stats::rbeta(JBrS_list[[s]],1,1)
GBrS_TPR_curr <- eval(parse(text = paste0("GBrS_TPR_",s)))
if (GBrS_TPR_curr==1){
res_curr[[1]] <- stats::rbeta(JBrS_list[[s]],1,1)
} else{
res_curr[[1]] <- matrix(stats::rbeta(GBrS_TPR_curr*JBrS_list[[s]],1,1),
nrow=GBrS_TPR_curr,ncol=JBrS_list[[s]])
if (JBrS_list[[s]]==1){
res_curr[[1]] <- c(res_curr[[1]])
}
}
res_curr[[2]] <- stats::rbeta(JBrS_list[[s]],1,1)
names(res_curr) <- paste(c("thetaBS","psiBS"),s,sep="_")
res <- c(res,res_curr)
}
if (likelihood$k_subclass[s] > 1){
K_curr <- likelihood$k_subclass[s]
res_curr[[1]] <- c(rep(.5,K_curr-1),NA)
res_curr[[2]] <- c(rep(.5,K_curr-1),NA)
# for different Eta's:
# res_curr[[2]] <- cbind(matrix(rep(.5,Jcause*(K_curr-1)),
# nrow=Jcause,ncol=K_curr-1),
# rep(NA,Jcause))
res_curr[[3]] <- 1
res_curr[[4]] <- 1
names(res_curr) <- paste(c("r0","r1","alphadp0","alphadp0_case"),s,sep="_")
res <- c(res,res_curr)
}
}
tmp_thetaSS <- list()
#tmp_psiSS <- list()
tmp_Icat_case <- list()
for(i in seq_along(JSS_list)){
GSS_TPR_curr <- eval(parse(text = paste0("GSS_TPR_",i)))
if (GSS_TPR_curr==1){
tmp_thetaSS[[i]] <- stats::rbeta(JSS_list[[i]],1,1)
} else{
tmp_thetaSS[[i]] <- matrix(stats::rbeta(GSS_TPR_curr*JSS_list[[i]],1,1),nrow=GSS_TPR_curr,ncol=JSS_list[[i]])
}
if (i==1){
#if (length(JSS_list)>1){
# warning("==[baker] Only the first slice of silver-standard data is
# used to initialize 'Icat' in JAGS fitting. Choose wisely!==\n ")
#}
tmp_Icat_case[[i]] <- init_latent_jags_multipleSS(MSS_list,likelihood$cause_list)
}
}
res2 <- c(tmp_thetaSS,tmp_Icat_case)
names(res2) <- c(paste("thetaSS", seq_along(JSS_list), sep = "_"),"Icat")
#print(res2)
c(res,res2)
}
}
# etiology (measurement independent)
alphaEti <- prior$Eti_prior # <-------- input etiology prior here.
#
# fit model :
#
# special operations:
# get individual prediction:
if (!is.null(mcmc_options$individual.pred) && mcmc_options$individual.pred){out_parameter <- c(out_parameter,"Icat")}
# get posterior predictive distribution of BrS measurments:
if (!is.null(mcmc_options$ppd) && mcmc_options$ppd){out_parameter <- c(out_parameter,paste("MBS.new",seq_along(Mobs$MBS),sep = "_"))}
#
# write the .bug files into mcmc_options$bugsmodel.dir;
# could later set it equal to result.folder.
#
use_jags <- (!is.null(mcmc_options$use_jags) && mcmc_options$use_jags)
model_func <- write_model_NoReg(model_options$likelihood$k_subclass,
data_nplcm$Mobs,
model_options$prior,
model_options$likelihood$cause_list,
model_options$use_measurements,
mcmc_options$ppd,
use_jags)
model_bugfile_name <- "model_NoReg.bug"
filename <- file.path(mcmc_options$bugsmodel.dir, model_bugfile_name)
writeLines(model_func, filename)
#
# run the model:
#
here <- environment()
##JAGS
in_data.list <- lapply(as.list(in_data),get, envir= here)
names(in_data.list) <- in_data
#lapply(names(in_data.list), dump, append = TRUE, envir = here,
# file = file.path(mcmc_options$result.folder,"jagsdata.txt"))
#do.call(file.remove, list(list.files(mcmc_options$result.folder, full.names = TRUE)))
curr_data_txt_file <- file.path(mcmc_options$result.folder,"jagsdata.txt")
if(file.exists(curr_data_txt_file)){file.remove(curr_data_txt_file)}