-
Notifications
You must be signed in to change notification settings - Fork 0
/
fn_write.R
1794 lines (1794 loc) · 117 KB
/
fn_write.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
#' Write analyses
#' @description write_analyses() is a Write function that writes a file to a specified local directory. Specifically, this function implements an algorithm to write analyses. The function is called for its side effects and does not return a value. WARNING: This function writes R scripts to your local environment. Make sure to only use if you want this behaviour
#' @param input_params_ls Input parameters (a list)
#' @param abstract_args_ls Abstract arguments (a list), Default: NULL
#' @param combinations_1L_lgl Combinations (a logical vector of length one), Default: F
#' @param consent_1L_chr Consent (a character vector of length one), Default: ''
#' @param consent_indcs_int Consent indices (an integer vector), Default: 1
#' @param cores_1L_int Cores (an integer vector of length one), Default: 1
#' @param depnt_var_min_val_1L_dbl Dependent variable minimum value (a double vector of length one), Default: numeric(0)
#' @param existing_predrs_ls Existing predictors (a list), Default: NULL
#' @param max_nbr_of_covars_1L_int Maximum number of covariates (an integer vector of length one), Default: integer(0)
#' @param options_chr Options (a character vector), Default: c("Y", "N")
#' @param start_at_int Start at (an integer vector), Default: c(2, 1)
#' @return NULL
#' @rdname write_analyses
#' @export
#' @importFrom ready4show write_report
write_analyses <- function (input_params_ls, abstract_args_ls = NULL, combinations_1L_lgl = F,
consent_1L_chr = "", consent_indcs_int = 1L, cores_1L_int = 1L,
depnt_var_min_val_1L_dbl = numeric(0), existing_predrs_ls = NULL,
max_nbr_of_covars_1L_int = integer(0), options_chr = c("Y",
"N"), start_at_int = c(2, 1))
{
ready4show::write_report(params_ls = input_params_ls$params_ls,
paths_ls = input_params_ls$path_params_ls$paths_ls, rprt_nm_1L_chr = "AAA_PMRY_ANLYS_MTH",
abstract_args_ls = abstract_args_ls, consent_1L_chr = consent_1L_chr,
consent_indcs_int = consent_indcs_int, header_yaml_args_ls = input_params_ls$header_yaml_args_ls,
options_chr = options_chr)
if (!is.null(input_params_ls$scndry_anlys_params_ls)) {
write_secondary_analyses(input_params_ls, combinations_1L_lgl = combinations_1L_lgl,
consent_1L_chr = consent_1L_chr, consent_indcs_int = consent_indcs_int,
cores_1L_int = cores_1L_int, depnt_var_min_val_1L_dbl = depnt_var_min_val_1L_dbl,
existing_predrs_ls = existing_predrs_ls, max_nbr_of_covars_1L_int = max_nbr_of_covars_1L_int,
options_chr = options_chr)
}
}
#' Write box cox transformation
#' @description write_box_cox_tfmn() is a Write function that writes a file to a specified local directory. Specifically, this function implements an algorithm to write box cox transformation. The function returns Path to plot (a character vector of length one).
#' @param data_tb Data (a tibble)
#' @param predr_var_nm_1L_chr Predictor variable name (a character vector of length one)
#' @param path_to_write_to_1L_chr Path to write to (a character vector of length one)
#' @param consent_1L_chr Consent (a character vector of length one), Default: ''
#' @param consent_indcs_int Consent indices (an integer vector), Default: 1
#' @param depnt_var_nm_1L_chr Dependent variable name (a character vector of length one), Default: 'utl_total_w'
#' @param covar_var_nms_chr Covariate variable names (a character vector), Default: 'NA'
#' @param fl_nm_pfx_1L_chr File name prefix (a character vector of length one), Default: 'A_RT'
#' @param height_1L_dbl Height (a double vector of length one), Default: 6
#' @param mdl_types_lup Model types (a lookup table), Default: NULL
#' @param options_chr Options (a character vector), Default: c("Y", "N")
#' @param start_1L_chr Start (a character vector of length one), Default: NULL
#' @param width_1L_dbl Width (a double vector of length one), Default: 6
#' @return Path to plot (a character vector of length one)
#' @rdname write_box_cox_tfmn
#' @export
#' @importFrom utils data
#' @importFrom ready4show write_mdl_plt_fl
#' @importFrom MASS boxcox
#' @keywords internal
write_box_cox_tfmn <- function (data_tb, predr_var_nm_1L_chr, path_to_write_to_1L_chr,
consent_1L_chr = "", consent_indcs_int = 1L, depnt_var_nm_1L_chr = "utl_total_w",
covar_var_nms_chr = NA_character_, fl_nm_pfx_1L_chr = "A_RT",
height_1L_dbl = 6, mdl_types_lup = NULL, options_chr = c("Y",
"N"), start_1L_chr = NULL, width_1L_dbl = 6)
{
if (is.null(mdl_types_lup))
utils::data("mdl_types_lup", envir = environment())
mdl <- make_mdl(data_tb, depnt_var_nm_1L_chr = depnt_var_nm_1L_chr,
predr_var_nm_1L_chr = predr_var_nm_1L_chr, covar_var_nms_chr = covar_var_nms_chr,
mdl_type_1L_chr = "OLS_NTF", mdl_types_lup = mdl_types_lup,
start_1L_chr = start_1L_chr)
path_to_plot_1L_chr <- ready4show::write_mdl_plt_fl(plt_fn = MASS::boxcox,
consent_1L_chr = consent_1L_chr, consent_indcs_int = consent_indcs_int,
fn_args_ls = list(mdl, plotit = T), height_1L_dbl = height_1L_dbl,
options_chr = options_chr, path_to_write_to_1L_chr = path_to_write_to_1L_chr,
plt_nm_1L_chr = paste0(fl_nm_pfx_1L_chr, "_", predr_var_nm_1L_chr,
"_", "BOXCOX"), width_1L_dbl = width_1L_dbl)
return(path_to_plot_1L_chr)
}
#' Write model comparison
#' @description write_mdl_cmprsn() is a Write function that writes a file to a specified local directory. Specifically, this function implements an algorithm to write model comparison. The function returns Model comparison (a list).
#' @param scored_data_tb Scored data (a tibble)
#' @param ds_smry_ls Dataset summary (a list)
#' @param mdl_smry_ls Model summary (a list)
#' @param output_data_dir_1L_chr Output data directory (a character vector of length one)
#' @param consent_1L_chr Consent (a character vector of length one), Default: ''
#' @param consent_indcs_int Consent indices (an integer vector), Default: 1
#' @param depnt_var_max_val_1L_dbl Dependent variable maximum value (a double vector of length one), Default: 0.99999
#' @param depnt_var_min_val_1L_dbl Dependent variable minimum value (a double vector of length one), Default: 1e-05
#' @param options_chr Options (a character vector), Default: c("Y", "N")
#' @param seed_1L_int Seed (an integer vector of length one), Default: 1234
#' @return Model comparison (a list)
#' @rdname write_mdl_cmprsn
#' @export
#' @importFrom youthvars transform_ds_for_tstng
write_mdl_cmprsn <- function (scored_data_tb, ds_smry_ls, mdl_smry_ls, output_data_dir_1L_chr,
consent_1L_chr = "", consent_indcs_int = 1L, depnt_var_max_val_1L_dbl = 0.99999,
depnt_var_min_val_1L_dbl = 1e-05, options_chr = c("Y", "N"),
seed_1L_int = 1234)
{
bl_tb <- youthvars::transform_ds_for_tstng(scored_data_tb,
depnt_var_nm_1L_chr = ds_smry_ls$depnt_var_nm_1L_chr,
candidate_predrs_chr = ds_smry_ls$candidate_predrs_chr,
depnt_var_max_val_1L_dbl = depnt_var_max_val_1L_dbl,
round_var_nm_1L_chr = if (identical(ds_smry_ls$round_var_nm_1L_chr,
character(0))) {
NA_character_
}
else {
ds_smry_ls$round_var_nm_1L_chr
}, round_val_1L_chr = if (identical(ds_smry_ls$round_var_nm_1L_chr,
character(0))) {
NA_character_
}
else {
ds_smry_ls$round_bl_val_1L_chr
})
ds_smry_ls$candidate_predrs_chr <- reorder_cndt_predrs_chr(ds_smry_ls$candidate_predrs_chr,
data_tb = bl_tb, depnt_var_nm_1L_chr = ds_smry_ls$depnt_var_nm_1L_chr)
mdl_smry_ls <- add_prefd_predr_var_to_mdl_smry_ls(mdl_smry_ls,
ds_smry_ls = ds_smry_ls)
mdl_smry_ls$smry_of_sngl_predr_mdls_tb <- write_sngl_predr_multi_mdls_outps(data_tb = bl_tb,
consent_1L_chr = consent_1L_chr, consent_indcs_int = consent_indcs_int,
dictionary_tb = ds_smry_ls$dictionary_tb, folds_1L_int = mdl_smry_ls$folds_1L_int,
mdl_types_chr = mdl_smry_ls$mdl_types_chr, depnt_var_nm_1L_chr = ds_smry_ls$depnt_var_nm_1L_chr,
depnt_var_min_val_1L_dbl = depnt_var_min_val_1L_dbl,
mdl_types_lup = mdl_smry_ls$mdl_types_lup, new_dir_nm_1L_chr = "A_Candidate_Mdls_Cmprsn",
options_chr = options_chr, predr_var_nm_1L_chr = mdl_smry_ls$predr_var_nm_1L_chr,
predr_var_desc_1L_chr = mdl_smry_ls$predr_var_desc_1L_chr,
predr_vals_dbl = mdl_smry_ls$predr_vals_dbl, path_to_write_to_1L_chr = output_data_dir_1L_chr)
mdl_smry_ls$prefd_mdl_types_chr <- make_prefd_mdls_vec(mdl_smry_ls$smry_of_sngl_predr_mdls_tb,
choose_from_pfx_chr = mdl_smry_ls$choose_from_pfx_chr,
mdl_types_lup = mdl_smry_ls$mdl_types_lup)
mdl_cmprsn_ls <- list(bl_tb = bl_tb, ds_smry_ls = ds_smry_ls,
mdl_smry_ls = mdl_smry_ls)
return(mdl_cmprsn_ls)
}
#' Write model plots
#' @description write_mdl_plts() is a Write function that writes a file to a specified local directory. Specifically, this function implements an algorithm to write model plots. The function is called for its side effects and does not return a value. WARNING: This function writes R scripts to your local environment. Make sure to only use if you want this behaviour
#' @param data_tb Data (a tibble)
#' @param model_mdl Model (a model)
#' @param predr_var_nm_1L_chr Predictor variable name (a character vector of length one)
#' @param predr_var_desc_1L_chr Predictor variable description (a character vector of length one)
#' @param predr_vals_dbl Predictor values (a double vector)
#' @param consent_1L_chr Consent (a character vector of length one), Default: ''
#' @param consent_indcs_int Consent indices (an integer vector), Default: 1
#' @param covar_var_nms_chr Covariate variable names (a character vector), Default: 'NA'
#' @param depnt_var_min_val_1L_dbl Dependent variable minimum value (a double vector of length one), Default: numeric(0)
#' @param depnt_var_nm_1L_chr Dependent variable name (a character vector of length one), Default: 'utl_total_w'
#' @param depnt_var_desc_1L_chr Dependent variable description (a character vector of length one), Default: 'Utility score'
#' @param family_1L_chr Family (a character vector of length one), Default: 'NA'
#' @param mdl_fl_nm_1L_chr Model file name (a character vector of length one), Default: 'OLS_NTF'
#' @param options_chr Options (a character vector), Default: c("Y", "N")
#' @param path_to_write_to_1L_chr Path to write to (a character vector of length one)
#' @param plt_indcs_int Plot indices (an integer vector), Default: 1:5
#' @param predn_type_1L_chr Prediction type (a character vector of length one), Default: NULL
#' @param tfmn_1L_chr Transformation (a character vector of length one), Default: 'NTF'
#' @param tfmn_for_bnml_1L_lgl Transformation for binomial (a logical vector of length one), Default: F
#' @return NULL
#' @rdname write_mdl_plts
#' @export
#' @importFrom purrr pwalk
#' @importFrom ready4show write_mdl_plt_fl
#' @keywords internal
write_mdl_plts <- function (data_tb, model_mdl, predr_var_nm_1L_chr, predr_var_desc_1L_chr,
predr_vals_dbl, consent_1L_chr = "", consent_indcs_int = 1L,
covar_var_nms_chr = NA_character_, depnt_var_min_val_1L_dbl = numeric(0),
depnt_var_nm_1L_chr = "utl_total_w", depnt_var_desc_1L_chr = "Utility score",
family_1L_chr = NA_character_, mdl_fl_nm_1L_chr = "OLS_NTF",
options_chr = c("Y", "N"), path_to_write_to_1L_chr, plt_indcs_int = 1:5,
predn_type_1L_chr = NULL, tfmn_1L_chr = "NTF", tfmn_for_bnml_1L_lgl = F)
{
data_tb <- transform_ds_for_mdlng(data_tb, depnt_var_nm_1L_chr = depnt_var_nm_1L_chr,
depnt_var_min_val_1L_dbl = depnt_var_min_val_1L_dbl,
predr_var_nm_1L_chr = predr_var_nm_1L_chr, covar_var_nms_chr = covar_var_nms_chr)
tfd_data_tb <- transform_data_tb_for_cmprsn(data_tb, model_mdl = model_mdl,
depnt_var_nm_1L_chr = depnt_var_nm_1L_chr, predn_type_1L_chr = predn_type_1L_chr,
tfmn_for_bnml_1L_lgl = tfmn_for_bnml_1L_lgl, family_1L_chr = family_1L_chr,
tfmn_1L_chr = tfmn_1L_chr)
if (1 %in% plt_indcs_int) {
predn_ds_tb <- make_predn_ds_with_one_predr(model_mdl,
depnt_var_nm_1L_chr = depnt_var_nm_1L_chr, tfmn_1L_chr = tfmn_1L_chr,
predr_var_nm_1L_chr = predr_var_nm_1L_chr, predr_vals_dbl = predr_vals_dbl,
predn_type_1L_chr = predn_type_1L_chr)
}
else {
predn_ds_tb <- NULL
}
purrr::pwalk(list(plt_fn_ls = list(plot_lnr_cmprsn, plot_auto_lm,
plot_obsd_predd_dnst, plot_obsd_predd_dnst, plot_sctr_plt_cmprsn)[plt_indcs_int],
fn_args_ls_ls = list(list(data_tb = data_tb, predn_ds_tb = predn_ds_tb,
predr_var_nm_1L_chr = predr_var_nm_1L_chr, predr_var_desc_1L_chr = predr_var_desc_1L_chr,
depnt_var_nm_1L_chr = depnt_var_nm_1L_chr, depnt_var_desc_1L_chr = depnt_var_desc_1L_chr),
list(model_mdl, which_dbl = 1:6, ncol_1L_int = 3L,
label_size_1L_int = 3), list(tfd_data_tb = tfd_data_tb,
depnt_var_nm_1L_chr = depnt_var_nm_1L_chr, depnt_var_desc_1L_chr = depnt_var_desc_1L_chr),
list(tfd_data_tb = transform_data_tb_for_cmprsn(data_tb,
model_mdl = model_mdl, depnt_var_nm_1L_chr = depnt_var_nm_1L_chr,
new_data_is_1L_chr = ifelse(!4 %in% plt_indcs_int,
"Predicted", "Simulated"), predn_type_1L_chr = NULL,
tfmn_for_bnml_1L_lgl = tfmn_for_bnml_1L_lgl,
family_1L_chr = family_1L_chr, tfmn_1L_chr = tfmn_1L_chr),
depnt_var_nm_1L_chr = depnt_var_nm_1L_chr, depnt_var_desc_1L_chr = depnt_var_desc_1L_chr,
predd_val_var_nm_1L_chr = "Simulated"), list(tfd_data_tb = tfd_data_tb,
depnt_var_nm_1L_chr = depnt_var_nm_1L_chr))[plt_indcs_int],
plt_nm_sfx_chr = c("_LNR_CMPRSN", "_AUTOPLT", "_PRED_DNSTY",
"_SIM_DNSTY", "_PRED_SCTR")[plt_indcs_int], size_ls = list(c(6,
6), c(4, 7), c(6, 6), c(6, 6), c(6, 6))[plt_indcs_int]),
~ready4show::write_mdl_plt_fl(plt_fn = ..1, consent_1L_chr = consent_1L_chr,
consent_indcs_int = consent_indcs_int, fn_args_ls = ..2,
options_chr = options_chr, path_to_write_to_1L_chr = path_to_write_to_1L_chr,
plt_nm_1L_chr = paste0(mdl_fl_nm_1L_chr, ifelse(!is.na(covar_var_nms_chr[1]),
paste("_", paste0(covar_var_nms_chr[1:min(length(covar_var_nms_chr),
3)], collapse = "")), ""), ..3), height_1L_dbl = ..4[1],
width_1L_dbl = ..4[2]))
}
#' Write model summary report
#' @description write_mdl_smry_rprt() is a Write function that writes a file to a specified local directory. Specifically, this function implements an algorithm to write model summary report. The function returns Input parameters (a list).
#' @param input_params_ls Input parameters (a list), Default: NULL
#' @param abstract_args_ls Abstract arguments (a list), Default: NULL
#' @param consent_1L_chr Consent (a character vector of length one), Default: ''
#' @param consent_indcs_int Consent indices (an integer vector), Default: 1
#' @param dv_ds_nm_and_url_chr Dataverse dataset name and url (a character vector), Default: NULL
#' @param options_chr Options (a character vector), Default: c("Y", "N")
#' @param rprt_lup Report (a lookup table), Default: NULL
#' @param rcrd_nm_1L_chr Record name (a character vector of length one), Default: 'AAA_RPRT_WRTNG_MTH'
#' @param rprt_nm_1L_chr Report name (a character vector of length one), Default: 'AAA_TTU_MDL_CTG'
#' @param start_at_int Start at (an integer vector), Default: c(2, 1)
#' @param use_shareable_mdls_1L_lgl Use shareable models (a logical vector of length one), Default: F
#' @return Input parameters (a list)
#' @rdname write_mdl_smry_rprt
#' @export
#' @importFrom purrr map pluck map_lgl map_chr reduce
#' @importFrom here here
#' @importFrom ready4show write_rprt_with_rcrd
#' @importFrom ready4 write_to_dv_with_wait write_with_consent
#' @importFrom tibble tibble
#' @importFrom dplyr filter pull bind_rows distinct mutate
#' @importFrom stats setNames
#' @keywords internal
write_mdl_smry_rprt <- function (input_params_ls = NULL, abstract_args_ls = NULL, consent_1L_chr = "",
consent_indcs_int = 1L, dv_ds_nm_and_url_chr = NULL, options_chr = c("Y",
"N"), rprt_lup = NULL, rcrd_nm_1L_chr = "AAA_RPRT_WRTNG_MTH",
rprt_nm_1L_chr = "AAA_TTU_MDL_CTG", start_at_int = c(2, 1),
use_shareable_mdls_1L_lgl = F)
{
header_yaml_args_ls <- input_params_ls$header_yaml_args_ls
path_params_ls <- input_params_ls$path_params_ls
output_format_ls <- input_params_ls$output_format_ls
use_fake_data_1L_lgl <- input_params_ls$params_ls$use_fake_data_1L_lgl
reference_int <- 0:(ifelse(is.null(input_params_ls$scndry_anlys_params_ls),
0, length(input_params_ls$scndry_anlys_params_ls)))
paths_ls <- path_params_ls$paths_ls
if (is.null(rprt_lup))
data("rprt_lup", package = "specific", envir = environment())
rprt_lups_ls <- purrr::map(reference_int, ~{
if (.x == 0) {
reference_1L_int <- NULL
}
else {
reference_1L_int <- .x
}
rprt_lup <- rprt_lup %>% transform_rprt_lup(add_suplry_rprt_1L_lgl = !is.null(reference_1L_int),
add_sharing_rprt_1L_lgl = T, start_at_int = start_at_int,
reference_1L_int = reference_1L_int)
if (is.null(reference_1L_int)) {
path_to_outp_fl_1L_chr <- paste0(paths_ls$output_data_dir_1L_chr,
"/I_ALL_OUTPUT_.RDS")
if (use_shareable_mdls_1L_lgl) {
main_rprt_append_ls <- list(rltv_path_to_data_dir_1L_chr = "../Output/G_Shareable/Models")
}
else {
main_rprt_append_ls <- NULL
}
rcrd_rprt_append_ls <- path_params_ls[1:2]
}
else {
path_to_outp_fl_1L_chr <- here::here(paths_ls$path_from_top_level_1L_chr,
paths_ls$write_to_dir_nm_1L_chr, paste0("secondary_",
reference_1L_int), "Output", "I_ALL_OUTPUT_.RDS")
main_rprt_append_ls <- list(existing_predrs_ls = readRDS(paste0(paths_ls$output_data_dir_1L_chr,
"/I_ALL_OUTPUT_.RDS")) %>% purrr::pluck("predr_vars_nms_ls"))
if (use_shareable_mdls_1L_lgl) {
main_rprt_append_ls$rltv_path_to_data_dir_1L_chr <- "../Output/G_Shareable/Models"
}
else {
main_rprt_append_ls$rltv_path_to_data_dir_1L_chr <- NULL
}
paths_ls <- transform_paths_ls_for_scndry(paths_ls,
reference_1L_int = reference_1L_int, remove_prmry_1L_lgl = T)
rcrd_rprt_append_ls <- list(transform_paths_ls = list(fn = transform_paths_ls_for_scndry,
args_ls = list(reference_1L_int = reference_1L_int,
remove_prmry_1L_lgl = T))) %>% append(path_params_ls[1:2])
}
ready4show::write_rprt_with_rcrd(path_to_outp_fl_1L_chr = path_to_outp_fl_1L_chr,
abstract_args_ls = abstract_args_ls, consent_1L_chr = consent_1L_chr,
consent_indcs_int = consent_indcs_int, header_yaml_args_ls = header_yaml_args_ls,
main_rprt_append_ls = main_rprt_append_ls, nbr_of_digits_1L_int = output_format_ls$supplementary_digits_1L_int,
options_chr = options_chr, output_type_1L_chr = output_format_ls$supplementary_outp_1L_chr,
paths_ls = paths_ls, rcrd_nm_1L_chr = rcrd_nm_1L_chr,
rcrd_rprt_append_ls = rcrd_rprt_append_ls, reference_1L_int = reference_1L_int,
rprt_lup = rprt_lup, rprt_nm_1L_chr = rprt_lup$rprt_nms_chr[purrr::map_lgl(rprt_lup$rprt_nms_chr,
~startsWith(.x, rprt_nm_1L_chr))], rprt_output_type_1L_chr = output_format_ls$supplementary_outp_1L_chr,
start_at_int = start_at_int, use_fake_data_1L_lgl = use_fake_data_1L_lgl)
if (!is.null(dv_ds_nm_and_url_chr)) {
ready4::write_to_dv_with_wait(consent_1L_chr = consent_1L_chr,
consent_indcs_int = consent_indcs_int, dss_tb = tibble::tibble(ds_obj_nm_chr = c(rprt_nm_1L_chr,
rcrd_nm_1L_chr), title_chr = rprt_lup %>% dplyr::filter(rprt_nms_chr %in%
c(rprt_nm_1L_chr, rcrd_nm_1L_chr)) %>% dplyr::pull(title_chr)),
dv_nm_1L_chr = dv_ds_nm_and_url_chr[1], ds_url_1L_chr = dv_ds_nm_and_url_chr[2],
options_chr = options_chr, parent_dv_dir_1L_chr = paths_ls$dv_dir_1L_chr,
paths_to_dirs_chr = paths_ls$reports_dir_1L_chr,
inc_fl_types_chr = ".pdf", paths_are_rltv_1L_lgl = F)
}
rprt_lup
}) %>% stats::setNames(reference_int %>% purrr::map_chr(~ifelse(.x ==
0, "Primary", paste0("secondary_", .x))))
consolidated_mdl_ings_ls <- reference_int %>% purrr::reduce(.init = paste0(paths_ls$output_data_dir_1L_chr,
"/G_Shareable/Ingredients/mdl_ingredients.RDS") %>% readRDS(),
~if (.y > 0) {
ingredients_ls <- here::here(paths_ls$path_from_top_level_1L_chr,
paths_ls$write_to_dir_nm_1L_chr, paste0("secondary_",
.y), "Output", "G_Shareable", "Ingredients",
"mdl_ingredients.RDS") %>% readRDS()
.x <- append(.x, list(ingredients_ls) %>% setNames(paste0("secondary_",
.y)))
.x$dictionary_tb <- dplyr::bind_rows(.x$dictionary_tb,
ingredients_ls$dictionary_tb) %>% dplyr::distinct()
.x$mdls_lup <- dplyr::bind_rows(.x$mdls_lup, ingredients_ls$mdls_lup %>%
dplyr::mutate(source_chr = paste0("Secondary Analysis ",
LETTERS[.y]))) %>% dplyr::distinct()
.x$mdls_smry_tb <- dplyr::bind_rows(.x$mdls_smry_tb,
ingredients_ls$mdls_smry_tb) %>% dplyr::distinct()
.x$predictors_lup <- dplyr::bind_rows(.x$predictors_lup,
ingredients_ls$predictors_lup) %>% dplyr::distinct()
.x
}
else {
.x$Primary <- .x
.x$mdls_lup <- .x$mdls_lup %>% dplyr::mutate(source_chr = "Primary Analysis")
.x
})
ready4::write_with_consent(consented_fn = saveRDS, prompt_1L_chr = paste0("Do you confirm that you want to write the file ",
"mdl_ingredients.RDS", " to ", paste0(paths_ls$output_data_dir_1L_chr,
"/G_Shareable/Ingredients"), "?"), consent_1L_chr = consent_1L_chr,
consent_indcs_int = consent_indcs_int, consented_args_ls = list(object = consolidated_mdl_ings_ls,
file = paste0(paths_ls$output_data_dir_1L_chr, "/G_Shareable/Ingredients/mdl_ingredients.RDS")),
consented_msg_1L_chr = paste0("File ", "mdl_ingredients.RDS",
" has been written to ", paste0(paths_ls$output_data_dir_1L_chr,
"/G_Shareable/Ingredients"), "."), declined_msg_1L_chr = "Write request cancelled - no new files have been written.",
options_chr = options_chr)
if (!is.null(input_params_ls)) {
input_params_ls$rprt_lups_ls <- rprt_lups_ls
}
else {
input_params_ls <- rprt_lups_ls
}
return(input_params_ls)
}
#' Write model type covariates models
#' @description write_mdl_type_covars_mdls() is a Write function that writes a file to a specified local directory. Specifically, this function implements an algorithm to write model type covariates models. The function returns Summary of models with covariates (a tibble).
#' @param data_tb Data (a tibble)
#' @param consent_1L_chr Consent (a character vector of length one), Default: ''
#' @param consent_indcs_int Consent indices (an integer vector), Default: 1
#' @param depnt_var_min_val_1L_dbl Dependent variable minimum value (a double vector of length one), Default: numeric(0)
#' @param depnt_var_nm_1L_chr Dependent variable name (a character vector of length one), Default: 'utl_total_w'
#' @param options_chr Options (a character vector), Default: c("Y", "N")
#' @param predrs_var_nms_chr Predictors variable names (a character vector)
#' @param covar_var_nms_chr Covariate variable names (a character vector)
#' @param mdl_type_1L_chr Model type (a character vector of length one)
#' @param path_to_write_to_1L_chr Path to write to (a character vector of length one)
#' @param new_dir_nm_1L_chr New directory name (a character vector of length one), Default: 'D_Covars_Selection'
#' @param fl_nm_pfx_1L_chr File name prefix (a character vector of length one), Default: 'D_CT'
#' @param mdl_types_lup Model types (a lookup table), Default: NULL
#' @param start_1L_chr Start (a character vector of length one), Default: 'NA'
#' @return Summary of models with covariates (a tibble)
#' @rdname write_mdl_type_covars_mdls
#' @export
#' @importFrom utils data
#' @importFrom purrr map_chr map_dfr
#' @importFrom ready4 get_from_lup_obj write_with_consent
#' @importFrom tibble tibble
#' @importFrom caret R2
#' @importFrom stats predict AIC BIC
#' @importFrom dplyr pull arrange desc
#' @importFrom rlang sym
write_mdl_type_covars_mdls <- function (data_tb, consent_1L_chr = "", consent_indcs_int = 1L,
depnt_var_min_val_1L_dbl = numeric(0), depnt_var_nm_1L_chr = "utl_total_w",
options_chr = c("Y", "N"), predrs_var_nms_chr, covar_var_nms_chr,
mdl_type_1L_chr, path_to_write_to_1L_chr, new_dir_nm_1L_chr = "D_Covars_Selection",
fl_nm_pfx_1L_chr = "D_CT", mdl_types_lup = NULL, start_1L_chr = NA_character_)
{
if (is.null(mdl_types_lup))
utils::data("mdl_types_lup", envir = environment())
arg_vals_chr <- c("control_chr", "predn_type_chr", "tfmn_chr") %>%
purrr::map_chr(~ready4::get_from_lup_obj(mdl_types_lup,
match_var_nm_1L_chr = "short_name_chr", match_value_xx = mdl_type_1L_chr,
target_var_nm_1L_chr = .x, evaluate_1L_lgl = F))
control_1L_chr <- arg_vals_chr[1]
predn_type_1L_chr <- arg_vals_chr[2]
tfmn_1L_chr <- arg_vals_chr[3]
if (is.na(predn_type_1L_chr))
predn_type_1L_chr <- NULL
data_tb <- data_tb %>% add_tfd_var_to_ds(depnt_var_nm_1L_chr = depnt_var_nm_1L_chr,
tfmn_1L_chr = tfmn_1L_chr)
output_dir_1L_chr <- output_dir_1L_chr <- write_new_outp_dir(path_to_write_to_1L_chr,
consent_1L_chr = consent_1L_chr, consent_indcs_int = consent_indcs_int,
new_dir_nm_1L_chr = new_dir_nm_1L_chr, options_chr = options_chr)
smry_of_mdls_with_covars_tb <- purrr::map_dfr(predrs_var_nms_chr,
~{
model_mdl <- make_mdl(data_tb, depnt_var_min_val_1L_dbl = depnt_var_min_val_1L_dbl,
depnt_var_nm_1L_chr = depnt_var_nm_1L_chr, predr_var_nm_1L_chr = .x,
covar_var_nms_chr = covar_var_nms_chr, tfmn_1L_chr = tfmn_1L_chr,
mdl_type_1L_chr = mdl_type_1L_chr, control_1L_chr = control_1L_chr,
mdl_types_lup = mdl_types_lup, start_1L_chr = start_1L_chr)
mdl_fl_nm_1L_chr <- paste0(fl_nm_pfx_1L_chr, "_",
.x, "_", mdl_type_1L_chr)
ready4::write_with_consent(consented_fn = saveRDS,
prompt_1L_chr = paste0("Do you confirm that you want to write the file ",
paste0(mdl_fl_nm_1L_chr, ".RDS"), " to ", output_dir_1L_chr,
"?"), consent_1L_chr = consent_1L_chr, consent_indcs_int = consent_indcs_int,
consented_args_ls = list(object = model_mdl,
file = paste0(output_dir_1L_chr, "/", mdl_fl_nm_1L_chr,
".RDS")), consented_msg_1L_chr = paste0("File ",
paste0(mdl_fl_nm_1L_chr, ".RDS"), " has been written to ",
output_dir_1L_chr, "."), declined_msg_1L_chr = "Write request cancelled - no new files have been written.",
options_chr = options_chr)
if ("summary.betareg" %in% class(summary(model_mdl))) {
coefficients_mat <- summary(model_mdl)$coefficients$mean
}
else {
coefficients_mat <- summary(model_mdl)$coefficients
}
tibble::tibble(variable = .x, Rsquare = caret::R2(stats::predict(model_mdl,
type = predn_type_1L_chr) %>% calculate_depnt_var_tfmn(tfmn_1L_chr = tfmn_1L_chr,
tfmn_is_outp_1L_lgl = T), data_tb %>% dplyr::pull(!!rlang::sym(depnt_var_nm_1L_chr)),
form = "traditional"), AIC = stats::AIC(model_mdl),
BIC = stats::BIC(model_mdl), Significant = paste(names(which(coefficients_mat[,
4] < 0.01)), collapse = " "))
})
smry_of_mdls_with_covars_tb <- smry_of_mdls_with_covars_tb %>%
dplyr::arrange(dplyr::desc(AIC))
saveRDS(smry_of_mdls_with_covars_tb, paste0(output_dir_1L_chr,
"/", paste0(fl_nm_pfx_1L_chr, "_", "SMRY", "_", mdl_type_1L_chr),
".RDS"))
return(smry_of_mdls_with_covars_tb)
}
#' Write model type multi outputs
#' @description write_mdl_type_multi_outps() is a Write function that writes a file to a specified local directory. Specifically, this function implements an algorithm to write model type multi outputs. The function returns Summary of model single predictors (a tibble).
#' @param data_tb Data (a tibble)
#' @param mdl_type_1L_chr Model type (a character vector of length one)
#' @param new_dir_nm_1L_chr New directory name (a character vector of length one)
#' @param path_to_write_to_1L_chr Path to write to (a character vector of length one)
#' @param predrs_var_nms_chr Predictors variable names (a character vector)
#' @param consent_1L_chr Consent (a character vector of length one), Default: ''
#' @param consent_indcs_int Consent indices (an integer vector), Default: 1
#' @param covar_var_nms_chr Covariate variable names (a character vector), Default: 'NA'
#' @param depnt_var_min_val_1L_dbl Dependent variable minimum value (a double vector of length one), Default: numeric(0)
#' @param depnt_var_nm_1L_chr Dependent variable name (a character vector of length one), Default: 'utl_total_w'
#' @param fl_nm_pfx_1L_chr File name prefix (a character vector of length one), Default: 'C_PREDR'
#' @param folds_1L_int Folds (an integer vector of length one), Default: 10
#' @param mdl_types_lup Model types (a lookup table), Default: NULL
#' @param options_chr Options (a character vector), Default: c("Y", "N")
#' @param plt_indcs_int Plot indices (an integer vector), Default: c(3, 5)
#' @param start_1L_chr Start (a character vector of length one), Default: NULL
#' @return Summary of model single predictors (a tibble)
#' @rdname write_mdl_type_multi_outps
#' @export
#' @importFrom utils data
#' @importFrom purrr map_dfr
#' @importFrom ready4 get_from_lup_obj
#' @importFrom dplyr select mutate everything arrange desc
write_mdl_type_multi_outps <- function (data_tb, mdl_type_1L_chr, new_dir_nm_1L_chr, path_to_write_to_1L_chr,
predrs_var_nms_chr, consent_1L_chr = "", consent_indcs_int = 1L,
covar_var_nms_chr = NA_character_, depnt_var_min_val_1L_dbl = numeric(0),
depnt_var_nm_1L_chr = "utl_total_w", fl_nm_pfx_1L_chr = "C_PREDR",
folds_1L_int = 10, mdl_types_lup = NULL, options_chr = c("Y",
"N"), plt_indcs_int = c(3, 5), start_1L_chr = NULL)
{
if (is.null(mdl_types_lup))
utils::data("mdl_types_lup", envir = environment())
output_dir_1L_chr <- write_new_outp_dir(path_to_write_to_1L_chr,
consent_1L_chr = consent_1L_chr, consent_indcs_int = consent_indcs_int,
new_dir_nm_1L_chr = new_dir_nm_1L_chr, options_chr = options_chr)
smry_of_mdl_sngl_predrs_tb <- purrr::map_dfr(predrs_var_nms_chr,
~{
tfmn_1L_chr <- ready4::get_from_lup_obj(mdl_types_lup,
match_var_nm_1L_chr = "short_name_chr", match_value_xx = mdl_type_1L_chr,
target_var_nm_1L_chr = "tfmn_chr", evaluate_1L_lgl = F)
mdl_smry_tb <- write_mdl_type_sngl_outps(data_tb,
consent_1L_chr = consent_1L_chr, consent_indcs_int = consent_indcs_int,
covar_var_nms_chr = covar_var_nms_chr, depnt_var_min_val_1L_dbl = depnt_var_min_val_1L_dbl,
depnt_var_nm_1L_chr = depnt_var_nm_1L_chr, folds_1L_int = folds_1L_int,
mdl_fl_nm_1L_chr = paste0(fl_nm_pfx_1L_chr, "_",
.x, "_", mdl_type_1L_chr), mdl_type_1L_chr = mdl_type_1L_chr,
mdl_types_lup = mdl_types_lup, options_chr = options_chr,
path_to_write_to_1L_chr = output_dir_1L_chr,
plt_indcs_int = plt_indcs_int, predr_vals_dbl = NA_real_,
predr_var_desc_1L_chr = NA_character_, predr_var_nm_1L_chr = .x,
start_1L_chr = start_1L_chr, tfmn_1L_chr = tfmn_1L_chr)
if (!is.null(folds_1L_int)) {
mdl_smry_tb <- mdl_smry_tb %>% dplyr::select((-Model)) %>%
dplyr::mutate(Predictor = .x) %>% dplyr::select(Predictor,
dplyr::everything())
}
mdl_smry_tb
})
if (!is.null(folds_1L_int)) {
smry_of_mdl_sngl_predrs_tb <- smry_of_mdl_sngl_predrs_tb %>%
dplyr::arrange(dplyr::desc(RsquaredP))
}
return(smry_of_mdl_sngl_predrs_tb)
}
#' Write model type single outputs
#' @description write_mdl_type_sngl_outps() is a Write function that writes a file to a specified local directory. Specifically, this function implements an algorithm to write model type single outputs. The function returns Summary of one predictor model (a tibble).
#' @param data_tb Data (a tibble)
#' @param mdl_fl_nm_1L_chr Model file name (a character vector of length one)
#' @param path_to_write_to_1L_chr Path to write to (a character vector of length one)
#' @param predr_vals_dbl Predictor values (a double vector)
#' @param predr_var_desc_1L_chr Predictor variable description (a character vector of length one)
#' @param predr_var_nm_1L_chr Predictor variable name (a character vector of length one)
#' @param consent_1L_chr Consent (a character vector of length one), Default: ''
#' @param consent_indcs_int Consent indices (an integer vector), Default: 1
#' @param covar_var_nms_chr Covariate variable names (a character vector), Default: 'NA'
#' @param depnt_var_nm_1L_chr Dependent variable name (a character vector of length one), Default: 'utl_total_w'
#' @param depnt_var_min_val_1L_dbl Dependent variable minimum value (a double vector of length one), Default: numeric(0)
#' @param folds_1L_int Folds (an integer vector of length one), Default: 10
#' @param mdl_type_1L_chr Model type (a character vector of length one), Default: 'OLS_NTF'
#' @param mdl_types_lup Model types (a lookup table), Default: NULL
#' @param options_chr Options (a character vector), Default: c("Y", "N")
#' @param start_1L_chr Start (a character vector of length one), Default: NULL
#' @param plt_indcs_int Plot indices (an integer vector), Default: NA
#' @param tfmn_1L_chr Transformation (a character vector of length one), Default: 'NTF'
#' @return Summary of one predictor model (a tibble)
#' @rdname write_mdl_type_sngl_outps
#' @export
#' @importFrom utils data
#' @importFrom purrr map_chr
#' @importFrom ready4 get_from_lup_obj write_with_consent
#' @importFrom tibble tibble
#' @keywords internal
write_mdl_type_sngl_outps <- function (data_tb, mdl_fl_nm_1L_chr, path_to_write_to_1L_chr,
predr_vals_dbl, predr_var_desc_1L_chr, predr_var_nm_1L_chr,
consent_1L_chr = "", consent_indcs_int = 1L, covar_var_nms_chr = NA_character_,
depnt_var_nm_1L_chr = "utl_total_w", depnt_var_min_val_1L_dbl = numeric(0),
folds_1L_int = 10, mdl_type_1L_chr = "OLS_NTF", mdl_types_lup = NULL,
options_chr = c("Y", "N"), start_1L_chr = NULL, plt_indcs_int = NA_integer_,
tfmn_1L_chr = "NTF")
{
if (is.null(mdl_types_lup))
utils::data("mdl_types_lup", envir = environment())
arg_vals_chr <- c("control_chr", "family_chr", "predn_type_chr") %>%
purrr::map_chr(~ready4::get_from_lup_obj(mdl_types_lup,
match_var_nm_1L_chr = "short_name_chr", match_value_xx = mdl_type_1L_chr,
target_var_nm_1L_chr = .x, evaluate_1L_lgl = F))
control_1L_chr <- arg_vals_chr[1]
family_1L_chr <- arg_vals_chr[2]
predn_type_1L_chr <- arg_vals_chr[3]
if (is.na(predn_type_1L_chr))
predn_type_1L_chr <- NULL
if (is.na(plt_indcs_int[1])) {
plt_indcs_int <- 1:5
if (!is.na(control_1L_chr)) {
if (control_1L_chr %>% startsWith("betareg"))
plt_indcs_int <- c(1, 3, 4, 5)
}
}
tfmn_for_bnml_1L_lgl <- ready4::get_from_lup_obj(mdl_types_lup,
match_var_nm_1L_chr = "short_name_chr", match_value_xx = mdl_type_1L_chr,
target_var_nm_1L_chr = "tfmn_for_bnml_lgl", evaluate_1L_lgl = F)
data_tb <- data_tb %>% add_tfd_var_to_ds(depnt_var_nm_1L_chr = depnt_var_nm_1L_chr,
tfmn_1L_chr = tfmn_1L_chr)
model_mdl <- make_mdl(data_tb, depnt_var_nm_1L_chr = depnt_var_nm_1L_chr,
depnt_var_min_val_1L_dbl = depnt_var_min_val_1L_dbl,
tfmn_1L_chr = tfmn_1L_chr, predr_var_nm_1L_chr = predr_var_nm_1L_chr,
covar_var_nms_chr = covar_var_nms_chr, mdl_type_1L_chr = mdl_type_1L_chr,
mdl_types_lup = mdl_types_lup, control_1L_chr = control_1L_chr,
start_1L_chr = start_1L_chr)
write_mdl_plts(data_tb, consent_1L_chr = consent_1L_chr,
consent_indcs_int = consent_indcs_int, covar_var_nms_chr = covar_var_nms_chr,
depnt_var_nm_1L_chr = depnt_var_nm_1L_chr, family_1L_chr = family_1L_chr,
mdl_fl_nm_1L_chr = mdl_fl_nm_1L_chr, model_mdl = model_mdl,
options_chr = options_chr, path_to_write_to_1L_chr = path_to_write_to_1L_chr,
plt_indcs_int = plt_indcs_int, predn_type_1L_chr = predn_type_1L_chr,
predr_vals_dbl = predr_vals_dbl, predr_var_desc_1L_chr = predr_var_desc_1L_chr,
predr_var_nm_1L_chr = predr_var_nm_1L_chr, tfmn_1L_chr = tfmn_1L_chr,
tfmn_for_bnml_1L_lgl = tfmn_for_bnml_1L_lgl)
if (!is.null(folds_1L_int)) {
smry_of_one_predr_mdl_tb <- make_smry_of_mdl_outp(data_tb,
folds_1L_int = folds_1L_int, depnt_var_min_val_1L_dbl = depnt_var_min_val_1L_dbl,
depnt_var_nm_1L_chr = depnt_var_nm_1L_chr, tfmn_1L_chr = tfmn_1L_chr,
predr_var_nm_1L_chr = predr_var_nm_1L_chr, covar_var_nms_chr = covar_var_nms_chr,
mdl_type_1L_chr = mdl_type_1L_chr, mdl_types_lup = mdl_types_lup,
start_1L_chr = start_1L_chr, predn_type_1L_chr = predn_type_1L_chr)
}
else {
smry_of_one_predr_mdl_tb <- tibble::tibble()
}
ready4::write_with_consent(consented_fn = saveRDS, prompt_1L_chr = paste0("Do you confirm that you want to write the file ",
paste0(mdl_fl_nm_1L_chr, ".RDS"), " to ", path_to_write_to_1L_chr,
"?"), consent_1L_chr = consent_1L_chr, consent_indcs_int = consent_indcs_int,
consented_args_ls = list(object = model_mdl, file = paste0(path_to_write_to_1L_chr,
"/", mdl_fl_nm_1L_chr, ".RDS")), consented_msg_1L_chr = paste0("File ",
paste0(mdl_fl_nm_1L_chr, ".RDS"), " has been written to ",
path_to_write_to_1L_chr, "."), declined_msg_1L_chr = "Write request cancelled - no new files have been written.",
options_chr = options_chr)
return(smry_of_one_predr_mdl_tb)
}
#' Write models to dataverse
#' @description write_mdls_to_dv() is a Write function that writes a file to a specified local directory. Specifically, this function implements an algorithm to write models to dataverse. The function returns Output summary (a list).
#' @param outp_smry_ls Output summary (a list)
#' @param consent_1L_chr Consent (a character vector of length one), Default: ''
#' @param consent_indcs_int Consent indices (an integer vector), Default: 1
#' @param new_dir_nm_1L_chr New directory name (a character vector of length one), Default: 'G_Shareable'
#' @param options_chr Options (a character vector), Default: c("Y", "N")
#' @param shareable_title_detail_1L_chr Shareable title detail (a character vector of length one), Default: ''
#' @param output_dir_chr Output directory (a character vector), Default: 'NA'
#' @return Output summary (a list)
#' @rdname write_mdls_to_dv
#' @export
#' @keywords internal
write_mdls_to_dv <- function (outp_smry_ls, consent_1L_chr = "", consent_indcs_int = 1L,
new_dir_nm_1L_chr = "G_Shareable", options_chr = c("Y", "N"),
shareable_title_detail_1L_chr = "", output_dir_chr = NA_character_)
{
if (is.na(output_dir_chr[1]))
output_dir_chr <- write_shareable_dir(outp_smry_ls = outp_smry_ls,
consent_1L_chr = consent_1L_chr, consent_indcs_int = consent_indcs_int,
new_dir_nm_1L_chr = new_dir_nm_1L_chr, options_chr = options_chr)
if (!is.null(outp_smry_ls$dv_ls)) {
write_shareable_mdls_to_dv(outp_smry_ls, consent_1L_chr = consent_1L_chr,
consent_indcs_int = consent_indcs_int, new_dir_nm_1L_chr = new_dir_nm_1L_chr,
options_chr = options_chr, output_dir_chr = output_dir_chr,
share_ingredients_1L_lgl = T)
outp_smry_ls$shareable_mdls_tb <- write_shareable_mdls_to_dv(outp_smry_ls,
consent_1L_chr = consent_1L_chr, consent_indcs_int = consent_indcs_int,
new_dir_nm_1L_chr = new_dir_nm_1L_chr, options_chr = options_chr,
output_dir_chr = output_dir_chr, share_ingredients_1L_lgl = F,
shareable_title_detail_1L_chr = shareable_title_detail_1L_chr)
}
return(outp_smry_ls)
}
#' Write models with covariates comparison
#' @description write_mdls_with_covars_cmprsn() is a Write function that writes a file to a specified local directory. Specifically, this function implements an algorithm to write models with covariates comparison. The function returns Output summary (a list).
#' @param scored_data_tb Scored data (a tibble)
#' @param bl_tb Baseline (a tibble)
#' @param combinations_1L_lgl Combinations (a logical vector of length one), Default: F
#' @param consent_1L_chr Consent (a character vector of length one), Default: ''
#' @param consent_indcs_int Consent indices (an integer vector), Default: 1
#' @param depnt_var_min_val_1L_dbl Dependent variable minimum value (a double vector of length one), Default: numeric(0)
#' @param ds_smry_ls Dataset summary (a list)
#' @param existing_predrs_ls Existing predictors (a list), Default: NULL
#' @param max_nbr_of_covars_1L_int Maximum number of covariates (an integer vector of length one), Default: integer(0)
#' @param mdl_smry_ls Model summary (a list)
#' @param options_chr Options (a character vector), Default: c("Y", "N")
#' @param output_data_dir_1L_chr Output data directory (a character vector of length one)
#' @param seed_1L_int Seed (an integer vector of length one), Default: 1234
#' @param session_data_ls Session data (a list), Default: NULL
#' @return Output summary (a list)
#' @rdname write_mdls_with_covars_cmprsn
#' @export
#' @importFrom ready4 write_with_consent
write_mdls_with_covars_cmprsn <- function (scored_data_tb, bl_tb, combinations_1L_lgl = F, consent_1L_chr = "",
consent_indcs_int = 1L, depnt_var_min_val_1L_dbl = numeric(0),
ds_smry_ls, existing_predrs_ls = NULL, max_nbr_of_covars_1L_int = integer(0),
mdl_smry_ls, options_chr = c("Y", "N"), output_data_dir_1L_chr,
seed_1L_int = 1234, session_data_ls = NULL)
{
empty_tb <- write_mdl_type_multi_outps(data_tb = bl_tb, consent_1L_chr = consent_1L_chr,
consent_indcs_int = consent_indcs_int, covar_var_nms_chr = mdl_smry_ls$prefd_covars_chr,
depnt_var_min_val_1L_dbl = depnt_var_min_val_1L_dbl,
depnt_var_nm_1L_chr = ds_smry_ls$depnt_var_nm_1L_chr,
fl_nm_pfx_1L_chr = "E_CK_CV", folds_1L_int = NULL, mdl_type_1L_chr = mdl_smry_ls$prefd_mdl_types_chr[1],
mdl_types_lup = mdl_smry_ls$mdl_types_lup, new_dir_nm_1L_chr = "E_Predrs_W_Covars_Sngl_Mdl_Cmprsn",
options_chr = options_chr, path_to_write_to_1L_chr = output_data_dir_1L_chr,
predrs_var_nms_chr = mdl_smry_ls$predr_cmprsn_tb$predr_chr,
start_1L_chr = NA_character_)
mdl_smry_ls$predr_vars_nms_ls <- make_predr_vars_nms_ls(main_predrs_chr = mdl_smry_ls$predr_cmprsn_tb$predr_chr,
covars_ls = list(mdl_smry_ls$prefd_covars_chr), combinations_1L_lgl = combinations_1L_lgl,
existing_predrs_ls = existing_predrs_ls, max_nbr_of_covars_1L_int = max_nbr_of_covars_1L_int)
mdl_smry_ls$mdl_nms_ls <- make_mdl_nms_ls(mdl_smry_ls$predr_vars_nms_ls,
mdl_types_chr = mdl_smry_ls$prefd_mdl_types_chr)
outp_smry_ls <- list(scored_data_tb = scored_data_tb, dictionary_tb = ds_smry_ls$dictionary_tb,
predictors_lup = ds_smry_ls$predictors_lup, smry_of_sngl_predr_mdls_tb = mdl_smry_ls$smry_of_sngl_predr_mdls_tb,
prefd_mdl_types_chr = mdl_smry_ls$prefd_mdl_types_chr,
predr_cmprsn_tb = mdl_smry_ls$predr_cmprsn_tb, smry_of_mdl_sngl_predrs_tb = mdl_smry_ls$smry_of_mdl_sngl_predrs_tb,
mdls_with_covars_smry_tb = mdl_smry_ls$mdls_with_covars_smry_tb,
signt_covars_chr = mdl_smry_ls$signt_covars_chr, prefd_covars_chr = mdl_smry_ls$prefd_covars_chr,
depnt_var_nm_1L_chr = ds_smry_ls$depnt_var_nm_1L_chr,
predr_vars_nms_ls = mdl_smry_ls$predr_vars_nms_ls, mdl_nms_ls = mdl_smry_ls$mdl_nms_ls,
id_var_nm_1L_chr = ds_smry_ls$id_var_nm_1L_chr, round_var_nm_1L_chr = ds_smry_ls$round_var_nm_1L_chr,
round_bl_val_1L_chr = ds_smry_ls$round_bl_val_1L_chr,
path_to_write_to_1L_chr = output_data_dir_1L_chr, seed_1L_int = seed_1L_int,
folds_1L_int = mdl_smry_ls$folds_1L_int, max_nbr_of_boruta_mdl_runs_int = mdl_smry_ls$max_nbr_of_boruta_mdl_runs_int,
mdl_types_lup = mdl_smry_ls$mdl_types_lup, file_paths_chr = list.files(output_data_dir_1L_chr,
recursive = T), session_data_ls = session_data_ls)
ready4::write_with_consent(consented_fn = saveRDS, prompt_1L_chr = paste0("Do you confirm that you want to write the file ",
"I_ALL_OUTPUT_.RDS", " to ", outp_smry_ls$path_to_write_to_1L_chr,
"?"), consent_1L_chr = consent_1L_chr, consent_indcs_int = consent_indcs_int,
consented_args_ls = list(object = outp_smry_ls, file = paste0(outp_smry_ls$path_to_write_to_1L_chr,
"/I_ALL_OUTPUT_.RDS")), consented_msg_1L_chr = paste0("File ",
"I_ALL_OUTPUT_.RDS", " has been written to ", outp_smry_ls$path_to_write_to_1L_chr,
"."), declined_msg_1L_chr = "Write request cancelled - no new files have been written.",
options_chr = options_chr)
return(outp_smry_ls)
}
#' Write new output directory
#' @description write_new_outp_dir() is a Write function that writes a file to a specified local directory. Specifically, this function implements an algorithm to write new output directory. The function returns Output directory (a character vector of length one).
#' @param path_to_write_to_1L_chr Path to write to (a character vector of length one)
#' @param new_dir_nm_1L_chr New directory name (a character vector of length one)
#' @param consent_1L_chr Consent (a character vector of length one), Default: ''
#' @param consent_indcs_int Consent indices (an integer vector), Default: 1
#' @param options_chr Options (a character vector), Default: c("Y", "N")
#' @return Output directory (a character vector of length one)
#' @rdname write_new_outp_dir
#' @export
#' @importFrom ready4 write_new_dirs
#' @keywords internal
write_new_outp_dir <- function (path_to_write_to_1L_chr, new_dir_nm_1L_chr, consent_1L_chr = "",
consent_indcs_int = 1L, options_chr = c("Y", "N"))
{
output_dir_1L_chr <- paste0(path_to_write_to_1L_chr, "/",
new_dir_nm_1L_chr)
ready4::write_new_dirs(output_dir_1L_chr, consent_1L_chr = consent_1L_chr,
consent_indcs_int = consent_indcs_int, options_chr = options_chr)
return(output_dir_1L_chr)
}
#' Write predictor and covariates comparison
#' @description write_predr_and_covars_cmprsn() is a Write function that writes a file to a specified local directory. Specifically, this function implements an algorithm to write predictor and covariates comparison. The function returns Predictor and covariates comparison (a list).
#' @param scored_data_tb Scored data (a tibble)
#' @param bl_tb Baseline (a tibble)
#' @param consent_1L_chr Consent (a character vector of length one), Default: ''
#' @param consent_indcs_int Consent indices (an integer vector), Default: 1
#' @param depnt_var_min_val_1L_dbl Dependent variable minimum value (a double vector of length one), Default: numeric(0)
#' @param ds_smry_ls Dataset summary (a list)
#' @param mdl_smry_ls Model summary (a list)
#' @param options_chr Options (a character vector), Default: c("Y", "N")
#' @param output_data_dir_1L_chr Output data directory (a character vector of length one)
#' @param seed_1L_int Seed (an integer vector of length one), Default: 1234
#' @param signft_covars_cdn_1L_chr Significant covariates condition (a character vector of length one), Default: 'any'
#' @return Predictor and covariates comparison (a list)
#' @rdname write_predr_and_covars_cmprsn
#' @export
#' @importFrom youthvars transform_ds_for_tstng
#' @importFrom ready4use Ready4useDyad
write_predr_and_covars_cmprsn <- function (scored_data_tb, bl_tb, consent_1L_chr = "", consent_indcs_int = 1L,
depnt_var_min_val_1L_dbl = numeric(0), ds_smry_ls, mdl_smry_ls,
options_chr = c("Y", "N"), output_data_dir_1L_chr, seed_1L_int = 1234,
signft_covars_cdn_1L_chr = "any")
{
mdl_smry_ls$predr_cmprsn_tb <- write_predr_cmprsn_outps(data_tb = bl_tb,
candidate_predrs_chr = ds_smry_ls$candidate_predrs_chr,
consent_1L_chr = consent_1L_chr, consent_indcs_int = consent_indcs_int,
depnt_var_min_val_1L_dbl = depnt_var_min_val_1L_dbl,
depnt_var_nm_1L_chr = ds_smry_ls$depnt_var_nm_1L_chr,
max_nbr_of_boruta_mdl_runs_int = mdl_smry_ls$max_nbr_of_boruta_mdl_runs_int,
new_dir_nm_1L_chr = "B_Candidate_Predrs_Cmprsn", options_chr = options_chr,
path_to_write_to_1L_chr = output_data_dir_1L_chr)
if (identical(mdl_smry_ls$predr_cmprsn_tb$predr_chr, character(0))) {
stop("No important predictors identified - execution aborted. Try specifying other predictors.")
}
mdl_smry_ls$smry_of_mdl_sngl_predrs_tb <- write_mdl_type_multi_outps(data_tb = bl_tb,
consent_1L_chr = consent_1L_chr, consent_indcs_int = consent_indcs_int,
depnt_var_min_val_1L_dbl = depnt_var_min_val_1L_dbl,
depnt_var_nm_1L_chr = ds_smry_ls$depnt_var_nm_1L_chr,
fl_nm_pfx_1L_chr = "C_PREDR", folds_1L_int = mdl_smry_ls$folds_1L_int,
mdl_type_1L_chr = mdl_smry_ls$prefd_mdl_types_chr[1],
mdl_types_lup = mdl_smry_ls$mdl_types_lup, new_dir_nm_1L_chr = "C_Predrs_Sngl_Mdl_Cmprsn",
options_chr = options_chr, path_to_write_to_1L_chr = output_data_dir_1L_chr,
predrs_var_nms_chr = mdl_smry_ls$predr_cmprsn_tb$predr_chr,
start_1L_chr = NA_character_)
bl_tb <- scored_data_tb %>% youthvars::transform_ds_for_tstng(depnt_var_nm_1L_chr = ds_smry_ls$depnt_var_nm_1L_chr,
candidate_predrs_chr = ds_smry_ls$candidate_predrs_chr,
covar_var_nms_chr = ds_smry_ls$candidate_covar_nms_chr,
remove_all_msng_1L_lgl = T, round_var_nm_1L_chr = ds_smry_ls$round_var_nm_1L_chr,
round_val_1L_chr = ds_smry_ls$round_bl_val_1L_chr)
mdl_smry_ls$mdls_with_covars_smry_tb <- write_mdl_type_covars_mdls(bl_tb,
consent_1L_chr = consent_1L_chr, consent_indcs_int = consent_indcs_int,
covar_var_nms_chr = ds_smry_ls$candidate_covar_nms_chr,
depnt_var_min_val_1L_dbl = depnt_var_min_val_1L_dbl,
depnt_var_nm_1L_chr = ds_smry_ls$depnt_var_nm_1L_chr,
fl_nm_pfx_1L_chr = "D_CT", mdl_type_1L_chr = mdl_smry_ls$prefd_mdl_types_chr[1],
new_dir_nm_1L_chr = "D_Predr_Covars_Cmprsn", mdl_types_lup = mdl_smry_ls$mdl_types_lup,
options_chr = options_chr, path_to_write_to_1L_chr = output_data_dir_1L_chr,
predrs_var_nms_chr = ds_smry_ls$candidate_predrs_chr)
mdl_smry_ls$signt_covars_chr <- get_signft_covars(mdls_with_covars_smry_tb = mdl_smry_ls$mdls_with_covars_smry_tb,
covar_var_nms_chr = ds_smry_ls$candidate_covar_nms_chr,
X_Ready4useDyad = ready4use::Ready4useDyad(ds_tb = scored_data_tb,
dictionary_r3 = ds_smry_ls$dictionary_tb), what_1L_chr = signft_covars_cdn_1L_chr)
predr_and_covars_cmprsn_ls <- list(bl_tb = bl_tb, ds_smry_ls = ds_smry_ls,
mdl_smry_ls = mdl_smry_ls)
return(predr_and_covars_cmprsn_ls)
}
#' Write predictor and model testing results
#' @description write_predr_and_mdl_tstng_results() is a Write function that writes a file to a specified local directory. Specifically, this function implements an algorithm to write predictor and model testing results. The function returns Output summary (a list).
#' @param scored_data_tb Scored data (a tibble)
#' @param combinations_1L_lgl Combinations (a logical vector of length one), Default: F
#' @param consent_1L_chr Consent (a character vector of length one), Default: ''
#' @param consent_indcs_int Consent indices (an integer vector), Default: 1
#' @param depnt_var_max_val_1L_dbl Dependent variable maximum value (a double vector of length one), Default: 0.99999
#' @param depnt_var_min_val_1L_dbl Dependent variable minimum value (a double vector of length one), Default: 1e-05
#' @param ds_smry_ls Dataset summary (a list)
#' @param existing_predrs_ls Existing predictors (a list), Default: NULL
#' @param max_nbr_of_covars_1L_int Maximum number of covariates (an integer vector of length one), Default: integer(0)
#' @param mdl_smry_ls Model summary (a list)
#' @param session_data_ls Session data (a list)
#' @param options_chr Options (a character vector), Default: c("Y", "N")
#' @param output_data_dir_1L_chr Output data directory (a character vector of length one)
#' @param seed_1L_int Seed (an integer vector of length one), Default: 1234
#' @param signft_covars_cdn_1L_chr Significant covariates condition (a character vector of length one), Default: 'any'
#' @return Output summary (a list)
#' @rdname write_predr_and_mdl_tstng_results
#' @export
#' @keywords internal
write_predr_and_mdl_tstng_results <- function (scored_data_tb, combinations_1L_lgl = F, consent_1L_chr = "",
consent_indcs_int = 1L, depnt_var_max_val_1L_dbl = 0.99999,
depnt_var_min_val_1L_dbl = 1e-05, ds_smry_ls, existing_predrs_ls = NULL,
max_nbr_of_covars_1L_int = integer(0), mdl_smry_ls, session_data_ls,
options_chr = c("Y", "N"), output_data_dir_1L_chr, seed_1L_int = 1234,
signft_covars_cdn_1L_chr = "any")
{
cmprsn_ls <- write_mdl_cmprsn(scored_data_tb = scored_data_tb,
consent_1L_chr = consent_1L_chr, consent_indcs_int = consent_indcs_int,
depnt_var_min_val_1L_dbl = depnt_var_min_val_1L_dbl,
depnt_var_max_val_1L_dbl = depnt_var_max_val_1L_dbl,
ds_smry_ls = ds_smry_ls, mdl_smry_ls = mdl_smry_ls, options_chr = options_chr,
output_data_dir_1L_chr = output_data_dir_1L_chr, seed_1L_int = seed_1L_int)
cmprsn_ls <- write_predr_and_covars_cmprsn(scored_data_tb = scored_data_tb,
bl_tb = cmprsn_ls$bl_tb, consent_1L_chr = consent_1L_chr,
consent_indcs_int = consent_indcs_int, depnt_var_min_val_1L_dbl = depnt_var_min_val_1L_dbl,
ds_smry_ls = cmprsn_ls$ds_smry_ls, mdl_smry_ls = cmprsn_ls$mdl_smry_ls,
options_chr = options_chr, output_data_dir_1L_chr = output_data_dir_1L_chr,
seed_1L_int = seed_1L_int, signft_covars_cdn_1L_chr = signft_covars_cdn_1L_chr)
if (ifelse(is.null(cmprsn_ls$mdl_smry_ls$prefd_covars_chr),
T, is.na(cmprsn_ls$mdl_smry_ls$prefd_covars_chr[1]))) {
cmprsn_ls$mdl_smry_ls$prefd_covars_chr <- cmprsn_ls$mdl_smry_ls$signt_covars_chr
}
outp_smry_ls <- write_mdls_with_covars_cmprsn(scored_data_tb = scored_data_tb,
bl_tb = cmprsn_ls$bl_tb, combinations_1L_lgl = combinations_1L_lgl,
consent_1L_chr = consent_1L_chr, consent_indcs_int = consent_indcs_int,
depnt_var_min_val_1L_dbl = depnt_var_min_val_1L_dbl,
ds_smry_ls = cmprsn_ls$ds_smry_ls, existing_predrs_ls = existing_predrs_ls,
max_nbr_of_covars_1L_int = max_nbr_of_covars_1L_int,
mdl_smry_ls = cmprsn_ls$mdl_smry_ls, options_chr = options_chr,
output_data_dir_1L_chr = output_data_dir_1L_chr, seed_1L_int = seed_1L_int,
session_data_ls = session_data_ls)
return(outp_smry_ls)
}
#' Write predictor comparison outputs
#' @description write_predr_cmprsn_outps() is a Write function that writes a file to a specified local directory. Specifically, this function implements an algorithm to write predictor comparison outputs. The function returns Confirmed predictors (a tibble).
#' @param data_tb Data (a tibble)
#' @param path_to_write_to_1L_chr Path to write to (a character vector of length one)
#' @param new_dir_nm_1L_chr New directory name (a character vector of length one), Default: 'B_Candidate_Predrs_Cmprsn'
#' @param consent_1L_chr Consent (a character vector of length one), Default: ''
#' @param consent_indcs_int Consent indices (an integer vector), Default: 1
#' @param depnt_var_min_val_1L_dbl Dependent variable minimum value (a double vector of length one), Default: numeric(0)
#' @param depnt_var_nm_1L_chr Dependent variable name (a character vector of length one), Default: 'utl_total_w'
#' @param candidate_predrs_chr Candidate predictors (a character vector)
#' @param max_nbr_of_boruta_mdl_runs_int Maximum number of boruta model runs (an integer vector), Default: 300
#' @param options_chr Options (a character vector), Default: c("Y", "N")
#' @return Confirmed predictors (a tibble)
#' @rdname write_predr_cmprsn_outps
#' @export
#' @importFrom randomForest randomForest varImpPlot
#' @importFrom stats as.formula
#' @importFrom Boruta Boruta
#' @importFrom purrr pwalk
#' @importFrom ready4show write_mdl_plt_fl
#' @importFrom tibble as_tibble
#' @importFrom dplyr arrange desc filter
write_predr_cmprsn_outps <- function (data_tb, path_to_write_to_1L_chr, new_dir_nm_1L_chr = "B_Candidate_Predrs_Cmprsn",
consent_1L_chr = "", consent_indcs_int = 1L, depnt_var_min_val_1L_dbl = numeric(0),
depnt_var_nm_1L_chr = "utl_total_w", candidate_predrs_chr,
max_nbr_of_boruta_mdl_runs_int = 300L, options_chr = c("Y",
"N"))
{
if (length(candidate_predrs_chr) > 1) {
covar_var_nms_chr <- candidate_predrs_chr[2:length(candidate_predrs_chr)]
}
else {
covar_var_nms_chr <- NA_character_
}
data_tb <- transform_ds_for_mdlng(data_tb, depnt_var_min_val_1L_dbl = depnt_var_min_val_1L_dbl,
depnt_var_nm_1L_chr = depnt_var_nm_1L_chr, predr_var_nm_1L_chr = candidate_predrs_chr[1],
covar_var_nms_chr = covar_var_nms_chr)
rf_mdl <- randomForest::randomForest(stats::as.formula(paste0(depnt_var_nm_1L_chr,
" ~ .")), data = data_tb, importance = TRUE)
boruta_mdl <- Boruta::Boruta(stats::as.formula(paste0(depnt_var_nm_1L_chr,
" ~ .")), data = data_tb, maxRuns = max_nbr_of_boruta_mdl_runs_int)
output_dir_1L_chr <- write_new_outp_dir(path_to_write_to_1L_chr,
consent_1L_chr = consent_1L_chr, consent_indcs_int = consent_indcs_int,
new_dir_nm_1L_chr = new_dir_nm_1L_chr, options_chr = options_chr)
purrr::pwalk(list(fn_ls = list(randomForest::varImpPlot,
plot), fn_args_ls_ls = list(list(rf_mdl, main = ""),
list(boruta_mdl, cex = 1.5, cex.axis = 0.8, las = 2,
xlab = "", main = "")), plt_nm_sfx_chr = c("_RF_VAR_IMP",
"_BORUTA_VAR_IMP"), size_ls = list(c(6, 6), c(4, 6))),
~ready4show::write_mdl_plt_fl(plt_fn = ..1, consent_1L_chr = consent_1L_chr,
consent_indcs_int = consent_indcs_int, fn_args_ls = ..2,
options_chr = options_chr, path_to_write_to_1L_chr = output_dir_1L_chr,
plt_nm_1L_chr = paste0("B_PRED_CMPRSN", ..3), height_1L_dbl = ..4[1],
width_1L_dbl = ..4[2]))
confirmed_predrs_chr <- names(boruta_mdl$finalDecision)[boruta_mdl$finalDecision ==
"Confirmed"]
confirmed_predrs_tb <- rf_mdl$importance %>% tibble::as_tibble(rownames = "predr_chr") %>%
dplyr::arrange(dplyr::desc(`%IncMSE`)) %>% dplyr::filter(predr_chr %in%
confirmed_predrs_chr)
return(confirmed_predrs_tb)
}
#' Write secondary analysis
#' @description write_scndry_analysis() is a Write function that writes a file to a specified local directory. Specifically, this function implements an algorithm to write secondary analysis. The function returns Output summary (a list).
#' @param valid_params_ls_ls Valid parameters (a list of lists)
#' @param candidate_covar_nms_chr Candidate covariate names (a character vector)
#' @param path_params_ls Path parameters (a list)
#' @param reference_1L_int Reference (an integer vector of length one)
#' @param backend_1L_chr Backend (a character vector of length one), Default: 'cmdstanr'
#' @param candidate_predrs_chr Candidate predictors (a character vector), Default: NULL
#' @param combinations_1L_lgl Combinations (a logical vector of length one), Default: F
#' @param consent_1L_chr Consent (a character vector of length one), Default: ''
#' @param consent_indcs_int Consent indices (an integer vector), Default: 1
#' @param cores_1L_int Cores (an integer vector of length one), Default: 1
#' @param depnt_var_max_val_1L_dbl Dependent variable maximum value (a double vector of length one), Default: 0.99999
#' @param depnt_var_min_val_1L_dbl Dependent variable minimum value (a double vector of length one), Default: 1e-05
#' @param existing_predrs_ls Existing predictors (a list), Default: NULL
#' @param max_nbr_of_covars_1L_int Maximum number of covariates (an integer vector of length one), Default: integer(0)
#' @param new_dir_nm_1L_chr New directory name (a character vector of length one), Default: 'F_TS_Mdls'
#' @param options_chr Options (a character vector), Default: c("Y", "N")
#' @param predictors_lup Predictors (a lookup table), Default: NULL
#' @param prefd_covars_chr Preferred covariates (a character vector), Default: 'NA'
#' @param signft_covars_cdn_1L_chr Significant covariates condition (a character vector of length one), Default: 'any'
#' @return Output summary (a list)
#' @rdname write_scndry_analysis
#' @export
#' @importFrom purrr map_chr pluck flatten_chr map compact map_lgl
#' @importFrom ready4 get_from_lup_obj
#' @keywords internal
write_scndry_analysis <- function (valid_params_ls_ls, candidate_covar_nms_chr, path_params_ls,
reference_1L_int, backend_1L_chr = "cmdstanr", candidate_predrs_chr = NULL,
combinations_1L_lgl = F, consent_1L_chr = "", consent_indcs_int = 1L,
cores_1L_int = 1L, depnt_var_max_val_1L_dbl = 0.99999, depnt_var_min_val_1L_dbl = 1e-05,
existing_predrs_ls = NULL, max_nbr_of_covars_1L_int = integer(0),
new_dir_nm_1L_chr = "F_TS_Mdls", options_chr = c("Y", "N"),
predictors_lup = NULL, prefd_covars_chr = NA_character_,
signft_covars_cdn_1L_chr = "any")
{
analysis_params_ls <- valid_params_ls_ls$params_ls %>% append(path_params_ls[1:2])
rename_lup <- valid_params_ls_ls$rename_lup
if (!is.null(predictors_lup)) {
predictors_lup$short_name_chr <- predictors_lup$short_name_chr %>%
purrr::map_chr(~ifelse(!.x %in% rename_lup$old_nms_chr,
.x, ready4::get_from_lup_obj(rename_lup, match_value_xx = .x,
match_var_nm_1L_chr = "old_nms_chr", target_var_nm_1L_chr = "new_nms_chr",
evaluate_1L_lgl = F)))
analysis_params_ls$predictors_lup <- predictors_lup
}
if (!is.null(candidate_predrs_chr)) {
candidate_predrs_chr <- candidate_predrs_chr %>% purrr::map_chr(~ifelse(!.x %in%
rename_lup$old_nms_chr, .x, ready4::get_from_lup_obj(rename_lup,
match_value_xx = .x, match_var_nm_1L_chr = "old_nms_chr",
target_var_nm_1L_chr = "new_nms_chr", evaluate_1L_lgl = F)))
analysis_params_ls$ds_descvs_ls$candidate_predrs_chr <- candidate_predrs_chr
}
if (!is.null(candidate_covar_nms_chr)) {
candidate_covar_nms_chr <- candidate_covar_nms_chr %>%
purrr::map_chr(~ifelse(!.x %in% rename_lup$old_nms_chr,
.x, ready4::get_from_lup_obj(rename_lup, match_value_xx = .x,
match_var_nm_1L_chr = "old_nms_chr", target_var_nm_1L_chr = "new_nms_chr",
evaluate_1L_lgl = F)))
}
if (ifelse(is.null(prefd_covars_chr), F, !is.na(prefd_covars_chr))) {
prefd_covars_chr <- prefd_covars_chr %>% purrr::map_chr(~ifelse(!.x %in%
rename_lup$old_nms_chr, .x, ready4::get_from_lup_obj(rename_lup,
match_value_xx = .x, match_var_nm_1L_chr = "old_nms_chr",
target_var_nm_1L_chr = "new_nms_chr", evaluate_1L_lgl = F)))
}
analysis_params_ls$prefd_covars_chr <- prefd_covars_chr
analysis_params_ls$candidate_covar_nms_chr <- candidate_covar_nms_chr
path_params_ls$paths_ls <- write_scndry_analysis_dir(path_params_ls$paths_ls,
consent_1L_chr = consent_1L_chr, consent_indcs_int = consent_indcs_int,
options_chr = options_chr, reference_1L_int = reference_1L_int)
params_ls <- list(candidate_predrs_chr = candidate_predrs_chr,
transform_paths_ls = list(fn = transform_paths_ls_for_scndry,
args_ls = list(reference_1L_int = reference_1L_int))) %>%
append(analysis_params_ls)
params_ls$utl_class_fn_1L_chr <- params_ls$raw_ds_tfmn_fn <- NULL
params_ls_ls <- transform_params_ls_to_valid(params_ls)
params_ls <- params_ls_ls %>% purrr::pluck("params_ls") %>%
append(list(rename_lup = params_ls_ls$rename_lup))
outp_smry_ls <- valid_params_ls_ls$outp_smry_ls
mdl_smry_ls <- params_ls$mdl_smry_ls
data_tb <- outp_smry_ls$scored_data_tb
ds_smry_ls <- params_ls$ds_descvs_ls %>% make_analysis_ds_smry_ls(candidate_covar_nms_chr = params_ls$candidate_covar_nms_chr,
predictors_lup = params_ls$predictors_lup)
ds_smry_ls$candidate_predrs_chr <- params_ls$candidate_predrs_chr
existing_mdls_chr <- outp_smry_ls[["mdl_nms_ls"]] %>% purrr::flatten_chr()
existing_predrs_ls <- outp_smry_ls$predr_vars_nms_ls
cmprsns_ls <- write_mdl_cmprsn(scored_data_tb = data_tb,
consent_1L_chr = consent_1L_chr, consent_indcs_int = consent_indcs_int,
depnt_var_min_val_1L_dbl = depnt_var_min_val_1L_dbl,
depnt_var_max_val_1L_dbl = depnt_var_max_val_1L_dbl,
ds_smry_ls = ds_smry_ls, mdl_smry_ls = mdl_smry_ls, options_chr = options_chr,