/
simulater.R
1150 lines (1050 loc) · 37.8 KB
/
simulater.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
#' Convenience function used in "simulater"
#' @param x Character vector to be converted to integer
#' @param dataset Data list
#
#' @return An integer vector
#'
#' @export
.as_int <- function(x, dataset = list()) {
if (is.character(x)) x <- strsplit(x, "/") %>% unlist()
asInt <- function(x) ifelse(length(x) > 1, as.integer(as.integer(x[1]) / as.integer(x[2])), as.integer(x))
ret <- sshhr(asInt(x))
if (is.na(ret)) {
if (x %in% names(dataset)) {
dataset[[x]]
} else if (is.na(x)) {
x
} else {
ret <- try(eval(parse(text = paste0("with(dataset, ", x, ")"))), silent = TRUE)
if (inherits(ret, "try-error")) {
cat(glue('"{x}" not (yet) defined when called. Note that simulation\nvariables of type "Constant" are always evaluated first\n\n\n'))
NA
} else {
ret
}
}
} else {
ret
}
}
#' Convenience function used in "simulater"
#'
#' @param x Character vector to be converted to an numeric value
#' @param dataset Data list
#
#' @return An numeric vector
#'
#' @export
.as_num <- function(x, dataset = list()) {
if (is.character(x)) x <- strsplit(x, "/") %>% unlist()
asNum <- function(x) ifelse(length(x) > 1, as.numeric(x[1]) / as.numeric(x[2]), as.numeric(x))
ret <- sshhr(asNum(x))
if (is.na(ret)) {
if (x %in% names(dataset)) {
dataset[[x]]
} else if (is.na(x)) {
x
} else {
ret <- try(eval(parse(text = paste0("with(dataset, ", x, ")"))), silent = TRUE)
if (inherits(ret, "try-error")) {
cat(glue('"{x}" not (yet) defined when called. Note that simulation\nvariables of type "Constant" are always evaluated first\n\n\n'))
NA
} else {
ret
}
}
} else {
ret
}
}
#' Simulate data for decision analysis
#'
#' @details See \url{https://radiant-rstats.github.io/docs/model/simulater.html} for an example in Radiant
#'
#' @param const A character vector listing the constants to include in the analysis (e.g., c("cost = 3", "size = 4"))
#' @param lnorm A character vector listing the log-normally distributed random variables to include in the analysis (e.g., "demand 2000 1000" where the first number is the log-mean and the second is the log-standard deviation)
#' @param norm A character vector listing the normally distributed random variables to include in the analysis (e.g., "demand 2000 1000" where the first number is the mean and the second is the standard deviation)
#' @param unif A character vector listing the uniformly distributed random variables to include in the analysis (e.g., "demand 0 1" where the first number is the minimum value and the second is the maximum value)
#' @param discrete A character vector listing the random variables with a discrete distribution to include in the analysis (e.g., "price 5 8 .3 .7" where the first set of numbers are the values and the second set the probabilities
#' @param binom A character vector listing the random variables with a binomial distribution to include in the analysis (e.g., "crash 100 .01") where the first number is the number of trials and the second is the probability of success)
#' @param pois A character vector listing the random variables with a poisson distribution to include in the analysis (e.g., "demand 10") where the number is the lambda value (i.e., the average number of events or the event rate)
#' @param sequ A character vector listing the start and end for a sequence to include in the analysis (e.g., "trend 1 100 1"). The number of 'steps' is determined by the number of simulations
#' @param grid A character vector listing the start, end, and step for a set of sequences to include in the analysis (e.g., "trend 1 100 1"). The number of rows in the expanded will over ride the number of simulations
#' @param data Dataset to be used in the calculations
#' @param form A character vector with the formula to evaluate (e.g., "profit = demand * (price - cost)")
#' @param funcs A named list of user defined functions to apply to variables generated as part of the simulation
#' @param seed Optional seed used in simulation
#' @param nexact Logical to indicate if normally distributed random variables should be simulated to the exact specified values
#' @param ncorr A string of correlations used for normally distributed random variables. The number of values should be equal to one or to the number of combinations of variables simulated
#' @param name Deprecated argument
#' @param nr Number of simulations
#' @param dataset Data list from previous simulation. Used by repeater function
#' @param envir Environment to extract data from
#'
#' @importFrom dplyr near
#'
#' @return A data.frame with the simulated data
#'
#' @examples
#' simulater(
#' const = "cost 3",
#' norm = "demand 2000 1000",
#' discrete = "price 5 8 .3 .7",
#' form = "profit = demand * (price - cost)",
#' seed = 1234
#' ) %>% str()
#'
#' @seealso \code{\link{summary.simulater}} to summarize results
#' @seealso \code{\link{plot.simulater}} to plot results
#'
#' @export
simulater <- function(const = "", lnorm = "", norm = "", unif = "", discrete = "",
binom = "", pois = "", sequ = "", grid = "", data = NULL,
form = "", funcs = "", seed = NULL, nexact = FALSE, ncorr = NULL,
name = "", nr = 1000, dataset = NULL, envir = parent.frame()) {
if (!is.empty(seed)) set.seed(as.numeric(seed))
if (is.null(dataset)) {
dataset <- list()
} else {
## needed because number may be NA and missing if grid used in Simulate
nr <- attr(dataset, "radiant_sim_call")$nr
data <- attr(dataset, "radiant_sim_call")$data
}
## needed to be exported functions
if (!exists(".as_num") || !exists(".as_int")) {
.as_num <- radiant.model::.as_num
.as_int <- radiant.model::.as_int
}
grid <- sim_cleaner(grid)
if (grid != "" && length(dataset) == 0) {
s <- grid %>% sim_splitter()
for (i in seq_along(s)) {
si <- s[[i]]
if (is.empty(si[4])) si[4] <- 1
dataset[[si[1]]] <- seq(.as_num(si[2], dataset), .as_num(si[3], dataset), .as_num(si[4], dataset))
}
dataset <- as.list(expand.grid(dataset) %>% as.data.frame(stringsAsFactors = FALSE))
nr <- length(dataset[[1]])
}
if (is.empty(nr)) {
mess <- c("error", paste0("Please specify the number of simulations in '# sims'"))
return(add_class(mess, "simulater"))
}
## fetching data if needed
if (!is.empty(data, "none") && is_string(data)) {
if (exists(data, envir = envir)) {
data <- get_data(data, envir = envir)
} else {
stop(paste0("Data set ", data, " cannot be found", call. = FALSE))
}
}
## adding data to dataset list
if (is.data.frame(data)) {
for (i in colnames(data)) {
dataset[[i]] <- data[[i]]
}
}
## parsing constant
const <- sim_cleaner(const)
if (const != "") {
s <- const %>% sim_splitter()
for (i in seq_along(s)) {
si <- s[[i]]
dataset[[si[1]]] <- .as_num(si[2], dataset)
}
}
## parsing uniform
unif <- sim_cleaner(unif)
if (unif != "") {
s <- unif %>% sim_splitter()
for (i in seq_along(s)) {
si <- s[[i]]
dataset[[si[1]]] <- runif(nr, .as_num(si[2], dataset), .as_num(si[3], dataset))
}
}
## parsing log normal
lnorm <- sim_cleaner(lnorm)
if (lnorm != "") {
s <- lnorm %>% sim_splitter()
for (i in seq_along(s)) {
si <- s[[i]]
sdev <- .as_num(si[3], dataset)
if (is.na(sdev) || !sdev > 0) {
mess <- c("error", paste0("All log-normal variables should have a standard deviation larger than 0.\nPlease review the input carefully"))
return(add_class(mess, "simulater"))
}
dataset[[si[1]]] <- rlnorm(nr, .as_num(si[2], dataset), sdev)
}
}
## parsing normal
norm <- sim_cleaner(norm)
if (norm != "") {
s <- norm %>% sim_splitter()
means <- sds <- nms <- c()
for (i in seq_along(s)) {
si <- s[[i]]
sdev <- .as_num(si[3], dataset)
if (is.na(sdev) || !sdev > 0) {
mess <- c("error", paste0("All normal variables should have a standard deviation larger than 0.\nPlease review the input carefully"))
return(add_class(mess, "simulater"))
}
if (is.empty(ncorr) || length(s) == 1) {
if (nexact) {
dataset[[si[1]]] <- scale(rnorm(nr, 0, 1)) * sdev + .as_num(si[2], dataset)
} else {
dataset[[si[1]]] <- rnorm(nr, .as_num(si[2], dataset), sdev)
}
} else {
nms <- c(nms, si[1])
means <- c(means, .as_num(si[2], dataset))
sds <- c(sds, sdev)
}
}
if (!is.empty(ncorr) && length(nms) > 1) {
ncorr <- gsub(",", " ", ncorr) %>%
strsplit("\\s+") %>%
unlist() %>%
.as_num(dataset)
ncorr_nms <- combn(nms, 2) %>% apply(2, paste, collapse = "-")
if (length(ncorr) == 1 && length(ncorr_nms) > 2) {
ncorr <- rep(ncorr, length(ncorr_nms))
}
if (length(ncorr) != length(ncorr_nms)) {
mess <- c("error", paste0("The number of correlations specified is not equal to\nthe number of pairs of variables to be simulated.\nPlease review the input carefully"))
return(add_class(mess, "simulater"))
}
names(ncorr) <- ncorr_nms
df <- try(sim_cor(nr, ncorr, means, sds, exact = nexact), silent = TRUE)
if (inherits(df, "try-error")) {
mess <- c("error", paste0("Data with the specified correlation structure could not be generated.\nPlease review the input and try again"))
return(add_class(mess, "simulater"))
}
colnames(df) <- nms
for (i in nms) {
dataset[[i]] <- df[[i]]
}
}
}
## parsing binomial
binom <- sim_cleaner(binom)
if (binom != "") {
s <- binom %>% sim_splitter()
for (i in 1:length(s)) {
si <- s[[i]]
dataset[[si[1]]] <- rbinom(nr, .as_int(si[2], dataset), .as_num(si[3], dataset))
}
}
## parsing poisson
pois <- sim_cleaner(pois)
if (pois != "") {
s <- pois %>% sim_splitter()
for (i in seq_along(s)) {
si <- s[[i]]
dataset[[si[1]]] <- rpois(nr, .as_num(si[2], dataset))
}
}
## parsing sequence
sequ <- sim_cleaner(sequ)
if (sequ != "") {
s <- sequ %>% sim_splitter()
for (i in 1:length(s)) {
si <- s[[i]]
dataset[[si[1]]] <- seq(.as_num(si[2], dataset), .as_num(si[3], dataset), length.out = .as_num(nr, dataset))
}
}
## parsing discrete
discrete <- sim_cleaner(discrete)
if (discrete != "") {
s <- discrete %>% sim_splitter()
for (i in seq_along(s)) {
si <- s[[i]]
dpar <- si[-1] %>%
gsub(",", " ", .) %>%
strsplit("\\s+") %>%
unlist() %>%
strsplit("/")
asNum <- function(x) ifelse(length(x) > 1, .as_num(x[1], dataset) / .as_num(x[2], dataset), .as_num(x, dataset))
dpar <- sshhr(try(sapply(dpar, asNum) %>% matrix(ncol = 2), silent = TRUE))
if (inherits(dpar, "try-error") || any(is.na(dpar))) {
mess <- c("error", paste0("Input for discrete variable # ", i, " contains an error. Please review the input carefully"))
return(add_class(mess, "simulater"))
} else if (!near(sum(dpar[, 2]), 1)) {
mess <- c("error", glue("Probabilities for discrete variable # {i} do not sum to 1 ({sum(dpar[, 2])})"))
return(add_class(mess, "simulater"))
}
dataset[[si[1]]] <- sample(dpar[, 1], nr, replace = TRUE, prob = dpar[, 2])
}
}
## convert named list of functions to a string to evaluate
if (is.list(funcs)) {
funcs <- sapply(
names(funcs),
function(f) {
paste0(f, " = ", paste0(deparse(funcs[[f]], control = getOption("dctrl"), width.cutoff = 500L), collapse = "\n"))
}
) %>% paste0(collapse = ";")
}
if (!is.expression(funcs)) {
pfuncs <- parse(text = funcs, keep.source = TRUE)
} else {
pfuncs <- funcs
}
if (!is.empty(form)) {
form <- form %>%
gsub("[ ]{2,}", " ", .) %>%
gsub("<-", "=", .)
form_no_comments <- remove_comments(form)
out <- try(do.call(within, list(dataset, c(pfuncs, parse(text = form_no_comments)))), silent = TRUE)
if (!inherits(out, "try-error")) {
dataset <- out
} else {
mess <- c(
"error", paste0("Formula was not successfully evaluated:\n\n", form) %>%
paste0(collapse = "\n"), "\n\nMessage: ", attr(out, "condition")$message
)
return(add_class(mess, "simulater"))
}
}
## removing data from dataset list
if (is.data.frame(data)) {
dataset[colnames(data)] <- NULL
}
## remove functions
ind <- radiant.data::get_class(dataset) == "function"
dataset[ind] <- NULL
## convert list to a data.frame
dataset <- as.data.frame(dataset, stringsAsFactors = FALSE) %>% na.omit()
## capturing the function call for use in repeat
sc <- formals()
smc <- lapply(match.call()[-1], eval, envir = envir)
smc$envir <- NULL
sc[names(smc)] <- smc
sc$nr <- nr
sc$ncorr <- ncorr
sc$nexact <- nexact
sc$funcs <- pfuncs
if (is.empty(sc$data, "none")) {
attr(dataset, "sim_data_name") <- NULL
} else if (is_string(sc$data)) {
attr(dataset, "sim_data_name") <- sc$data
sc$data <- data
} else {
attr(dataset, "sim_data_name") <- deparse(substitute(data))
}
attr(dataset, "radiant_sim_call") <- sc
if (nrow(dataset) == 0) {
mess <- c("error", paste0("The simulated data set has 0 rows"))
return(add_class(mess, "simulater"))
}
form <- gsub("*", "\\*", form, fixed = TRUE) %>%
gsub("^\\s*?\\#+[^\\#]", "##### # ", .) %>%
gsub("[;\n]\\s*?\\#+[^\\#]", "; ##### # ", .) %>%
gsub(";\\s*", "\n\n", .)
mess <- paste0("\n### Simulated data\n\nFormulas:\n\n", form, "\n\nDate: ", lubridate::now())
add_class(set_attr(dataset, "description", mess), "simulater")
}
#' Summary method for the simulater function
#'
#' @details See \url{https://radiant-rstats.github.io/docs/model/simulater.html} for an example in Radiant
#'
#' @param object Return value from \code{\link{simulater}}
#' @param dec Number of decimals to show
#' @param ... further arguments passed to or from other methods
#'
#' @examples
#' simdat <- simulater(norm = "demand 2000 1000", seed = 1234)
#' summary(simdat)
#'
#' @seealso \code{\link{simulater}} to generate the results
#' @seealso \code{\link{plot.simulater}} to plot results
#'
#' @export
summary.simulater <- function(object, dec = 4, ...) {
if (is.character(object)) {
if (length(object) == 2 && object[1] == "error") {
return(cat(object[2]))
}
stop("To generate summary statistics please provide a simulated dataset as input", call. = FALSE)
}
sc <- attr(object, "radiant_sim_call")
clean <- function(x) {
paste0(x, collapse = ";") %>%
gsub(";", "; ", .) %>%
gsub("\\n", "", .) %>%
paste0(., "\n")
}
cat("Simulation\n")
cat("Simulations:", format_nr(nrow(object), dec = 0), "\n")
cat("Random seed:", sc$seed, "\n")
if (is.empty(sc$name)) {
cat("Sim data :", deparse(substitute(object)), "\n")
} else {
cat("Sim data :", sc$name, "\n")
}
if (!is.empty(sc$binom)) cat("Binomial :", clean(sc$binom))
if (!is.empty(sc$discrete)) cat("Discrete :", clean(sc$discrete))
if (!is.empty(sc$lnorm)) cat("Log normal :", clean(sc$lnorm))
if (!is.empty(sc$norm)) cat("Normal :", clean(ifelse(sc$nexact, paste0(sc$norm, "(exact)"), sc$norm)))
if (!is.empty(sc$unif)) cat("Uniform :", clean(sc$unif))
if (!is.empty(sc$pois)) cat("Poisson :", clean(sc$pois))
if (!is.empty(sc$const)) cat("Constant :", clean(sc$const))
if (is.data.frame(sc$data)) cat("Data :", attr(object, "sim_data_name"), "\n")
if (!is.empty(sc$grid)) cat("Grid search:", clean(sc$grid))
if (!is.empty(sc$sequ)) cat("Sequence :", clean(sc$sequ))
funcs <- attr(object, "radiant_funcs")
if (!is.empty(funcs)) {
funcs <- parse(text = funcs)
lfuncs <- list()
for (i in seq_len(length(funcs))) {
tmp <- strsplit(as.character(funcs[i]), "(\\s*=|\\s*<-)")[[1]][1]
lfuncs[[tmp]] <- as.symbol(tmp)
}
cat("Functions :", paste0(names(lfuncs), collapse = ", "), "\n")
}
if (!is.empty(sc$form)) {
cat(paste0("Formulas :\n\t", paste0(sc$form, collapse = ";") %>% gsub(";", "\n", .) %>% gsub("\n", "\n\t", .), "\n"))
}
cat("\n")
if (!is.empty(sc$ncorr) && is.numeric(sc$ncorr)) {
cat("Correlations:\n")
print(sc$ncorr)
cat("\n")
}
sim_summary(object, dec = ifelse(is.empty(dec), 4, round(dec, 0)))
}
#' Plot method for the simulater function
#'
#' @details See \url{https://radiant-rstats.github.io/docs/model/simulater} for an example in Radiant
#'
#' @param x Return value from \code{\link{simulater}}
#' @param bins Number of bins used for histograms (1 - 50)
#' @param shiny Did the function call originate inside a shiny app
#' @param custom Logical (TRUE, FALSE) to indicate if ggplot object (or list of ggplot objects) should be returned. This option can be used to customize plots (e.g., add a title, change x and y labels, etc.). See examples and \url{https://ggplot2.tidyverse.org} for options.
#' @param ... further arguments passed to or from other methods
#'
#' @examples
#' simdat <- simulater(
#' const = "cost 3",
#' norm = "demand 2000 1000",
#' discrete = "price 5 8 .3 .7",
#' form = "profit = demand * (price - cost)",
#' seed = 1234
#' )
#' plot(simdat, bins = 25)
#'
#' @seealso \code{\link{simulater}} to generate the result
#' @seealso \code{\link{summary.simulater}} to summarize results
#'
#' @export
plot.simulater <- function(x, bins = 20, shiny = FALSE, custom = FALSE, ...) {
if (is.character(x)) {
return(invisible())
}
if (nrow(x) == 0) {
return(invisible())
}
plot_list <- list()
for (i in colnames(x)) {
dat <- select_at(x, .vars = i)
if (!does_vary(x[[i]])) next
plot_list[[i]] <- select_at(x, .vars = i) %>%
visualize(xvar = i, bins = bins, custom = TRUE)
}
if (length(plot_list) > 0) {
if (custom) {
if (length(plot_list) == 1) plot_list[[1]] else plot_list
} else {
patchwork::wrap_plots(plot_list, ncol = min(length(plot_list), 2)) %>%
(function(x) if (shiny) x else print(x))
}
}
}
#' Repeated simulation
#'
#' @param dataset Return value from the simulater function
#' @param nr Number times to repeat the simulation
#' @param vars Variables to use in repeated simulation
#' @param grid Character vector of expressions to use in grid search for constants
#' @param sum_vars (Numeric) variables to summaries
#' @param byvar Variable(s) to group data by before summarizing
#' @param fun Functions to use for summarizing
#' @param form A character vector with the formula to apply to the summarized data
#' @param seed Seed for the repeated simulation
#' @param name Deprecated argument
#' @param envir Environment to extract data from
#'
#' @importFrom shiny getDefaultReactiveDomain
#'
#' @examples
#' simdat <- simulater(
#' const = c("var_cost 5", "fixed_cost 1000"),
#' norm = "E 0 100;",
#' discrete = "price 6 8 .3 .7;",
#' form = c(
#' "demand = 1000 - 50*price + E",
#' "profit = demand*(price-var_cost) - fixed_cost",
#' "profit_small = profit < 100"
#' ),
#' seed = 1234
#' )
#'
#' repdat <- repeater(
#' simdat,
#' nr = 12,
#' vars = c("E", "price"),
#' sum_vars = "profit",
#' byvar = ".sim",
#' form = "profit_365 = profit_sum < 36500",
#' seed = 1234,
#' )
#'
#' head(repdat)
#' summary(repdat)
#' plot(repdat)
#'
#' @seealso \code{\link{summary.repeater}} to summarize results from repeated simulation
#' @seealso \code{\link{plot.repeater}} to plot results from repeated simulation
#'
#' @export
repeater <- function(dataset, nr = 12, vars = "", grid = "", sum_vars = "",
byvar = ".sim", fun = "sum", form = "", seed = NULL,
name = "", envir = parent.frame()) {
if (byvar %in% c(".sim", "sim")) grid <- ""
if (is.empty(nr)) {
if (is.empty(grid)) {
mess <- c("error", paste0("Please specify the number of repetitions in '# reps'"))
return(add_class(mess, "repeater"))
} else {
nr <- 1
}
}
## needed to be exported functions
if (!exists(".as_num") || !exists(".as_int")) {
.as_num <- radiant.model::.as_num
.as_int <- radiant.model::.as_int
}
if (is_string(dataset)) {
sim_df_name <- dataset
dataset <- get_data(dataset, envir = envir)
} else {
sim_df_name <- deparse(substitute(dataset))
}
if (!is.empty(seed)) set.seed(as.numeric(seed))
if (identical(vars, "") && identical(grid, "")) {
mess <- c("error", paste0("Select variables to re-simulate and/or a specify a constant\nto change using 'Grid search' when Group by is set to Repeat"))
return(add_class(mess, "repeater"))
}
if (identical(vars, "")) vars <- character(0)
grid_list <- list()
if (!identical(grid, "")) {
grid <- sim_cleaner(grid)
if (grid != "") {
s <- grid %>% sim_splitter()
for (i in seq_along(s)) {
si <- s[[i]]
if (is.empty(s[[i]][4])) s[[i]][4] <- 1
grid_list[[si[1]]] <- seq(.as_num(si[2], dataset), .as_num(si[3], dataset), .as_num(si[4], dataset))
}
}
## expanding list of variables but removing ""
vars <- c(vars, names(grid_list)) %>% unique()
}
## from http://stackoverflow.com/a/7664655/1974918
## keep those list elements that, e.g., q is in
nr_sim <- nrow(dataset)
sc <- attr(dataset, "radiant_sim_call")
if (is.data.frame(sc$data)) {
data <- sc$data
} else {
data <- NULL
}
## reset dataset to list with vectors of the correct length
dataset <- as.list(dataset)
if ("const" %in% names(sc)) {
s <- sc$const
if (length(s) < 2) {
s <- strsplit(gsub("\n", "", s), ";\\s*")[[1]] %>% strsplit("\\s+")
} else {
s <- strsplit(s, "\\s+")
}
for (const in seq_len(length(s))) {
nm <- s[[const]][1]
dataset[[nm]] <- dataset[[nm]][1]
}
}
## needed if inputs are provided as vectors
sc[1:(which(names(sc) == "seed") - 1)] %<>% lapply(paste, collapse = ";")
sc$name <- sc$seed <- "" ## cleaning up the sim call
## using \\b based on https://stackoverflow.com/a/34074458/1974918
sc_keep <- grep(paste(paste0("\\b", vars, "\\b"), collapse = "|"), sc, value = TRUE)
sc_keep["funcs"] <- sc$funcs
## ensure that only the selected variables of a specific type are resimulated
## e.g., if A, B, and C are normal and A should be re-sim'd, don't also re-sim B and C
for (i in names(sc_keep)) {
if (i %in% c("form", "funcs")) next
sc_check <- sim_cleaner(sc_keep[[i]]) %>%
sim_splitter(";")
if (length(sc_check) < 2) {
next
} else {
sc_keep[[i]] <- grep(paste(paste0("\\b", vars, "\\b"), collapse = "|"), sc_check, value = TRUE) %>%
paste0(collapse = ";\n")
}
}
## needed in case there is no 'form' in simulate
sc[1:(which(names(sc) == "seed") - 1)] <- ""
sc[names(sc_keep)] <- sc_keep
sc$dataset <- dataset
if (!is.empty(sc$data, "none") && is_string(sc$data)) {
if (exists(sc$data, envir = envir)) {
sc$data <- get(sc$data, envir = envir)
} else {
stop(paste0("Data set ", sc$data, " cannot be found", call. = FALSE))
}
}
summarize_sim <- function(object) {
if (is.empty(fun) || any(fun == "none")) {
object <- select_at(object, .vars = c(".rep", ".sim", sum_vars))
} else {
cn <- unlist(sapply(fun, function(f) paste0(sum_vars, "_", f), simplify = FALSE))
first <- function(x, ...) dplyr::first(x)
last <- function(x, ...) dplyr::last(x)
object <- group_by_at(object, byvar) %>%
summarise_at(.vars = sum_vars, .funs = fun, na.rm = TRUE) %>%
set_colnames(c(byvar, cn))
}
object
}
rep_sim <- function(rep_nr, nr, sfun = function(x) x) {
bind_cols(
data.frame(.rep = rep(rep_nr, nr_sim), .sim = 1:nr_sim, stringsAsFactors = FALSE),
do.call(simulater, sc)
) %>%
na.omit() %>%
sfun() %T>%
(function(x) incProgress(rep_nr / nr, detail = paste("\nCompleted run", rep_nr, "out of", nr)))
}
rep_grid_sim <- function(gval, rep_nr, nr, sfun = function(x) x) {
gvars <- names(gval)
## removing form and funcs ...
sc_grid <- grep(paste(gvars, collapse = "|"), sc_keep, value = TRUE) %>%
(function(x) x[which(!names(x) %in% c("form", "funcs"))]) %>%
gsub("[ ]{2,}", " ", .)
for (i in 1:length(gvars)) {
sc_grid %<>% sub(paste0("[;\n]", gvars[i], " [.0-9]+"), paste0("\n", gvars[i], " ", gval[gvars[i]]), .) %>%
sub(paste0("^", gvars[i], " [.0-9]+"), paste0(gvars[i], " ", gval[gvars[i]]), .)
}
sc[names(sc_grid)] <- sc_grid
bind_cols(
data.frame(.rep = rep(paste(gval, collapse = "|"), nr_sim), .sim = 1:nr_sim, stringsAsFactors = FALSE),
do.call(simulater, sc)
) %>%
na.omit() %>%
sfun() %>%
{
incProgress(rep_nr / nr, detail = paste("\nCompleted run", rep_nr, "out of", nr))
.
}
}
if (length(shiny::getDefaultReactiveDomain()) > 0) {
trace <- FALSE
incProgress <- shiny::incProgress
withProgress <- shiny::withProgress
} else {
incProgress <- function(...) {}
withProgress <- function(...) list(...)[["expr"]]
}
withProgress(message = "Running repeated simulation", value = 0, {
if (length(grid_list) == 0) {
if (byvar == ".sim") {
ret <- bind_rows(lapply(1:nr, rep_sim, nr)) %>%
summarize_sim() %>%
add_class("repeater")
} else {
ret <- bind_rows(lapply(1:nr, function(x) rep_sim(x, nr, summarize_sim))) %>%
add_class("repeater")
}
} else {
grid <- expand.grid(grid_list)
nr <- nrow(grid)
if (byvar == ".sim") {
ret <- bind_rows(lapply(1:nr, function(x) rep_grid_sim(grid[x, , drop = FALSE], x, nr))) %>%
summarize_sim() %>%
add_class("repeater")
} else {
ret <- bind_rows(lapply(1:nr, function(x) rep_grid_sim(grid[x, , drop = FALSE], x, nr, summarize_sim))) %>%
add_class("repeater")
}
}
})
if (is.data.frame(data)) {
ret <- as.list(ret)
for (i in colnames(data)) {
ret[[i]] <- data[[i]]
}
sim_data_name <- attr(dataset, "sim_data_name")
} else {
sim_data_name <- NULL
}
if (!is.empty(form)) {
form <- form %>%
gsub("[ ]{2,}", " ", .) %>%
gsub("<-", "=", .)
form_no_comments <- remove_comments(form)
out <- try(do.call(within, list(ret, parse(text = form_no_comments))), silent = TRUE)
if (!inherits(out, "try-error")) {
ret <- out
} else {
mess <- c("error", paste0("Formula was not successfully evaluated:\n\n", form) %>% unlist() %>% paste0(collapse = "\n"), "\n\nMessage: ", attr(out, "condition")$message, "\n\nNote that repeated simulation formulas can only be applied to\n(summarized) 'Output variables'")
if (!is.empty(fun)) {
cn <- unlist(sapply(fun, function(f) paste0(sum_vars, "_", f), simplify = FALSE))
mess[2] <- paste0(mess[2], "\n\nAvailable (summarized) output variables:\n* ", paste0(cn, collapse = "\n* "))
}
return(add_class(mess, "repeater"))
}
}
## removing data from dataset list
if (is.data.frame(data)) {
ret[colnames(data)] <- NULL
}
## tbl_df remove attributes so use as.data.frame for now
ret <- as.data.frame(ret, stringsAsFactors = FALSE)
## capturing the function call for use in summary and plot
rc <- formals()
rmc <- lapply(match.call()[-1], eval, envir = envir)
rmc$envir <- NULL
rc[names(rmc)] <- rmc
rc$sc <- sc[base::setdiff(names(sc), "dat")]
attr(ret, "radiant_rep_call") <- rc
attr(ret, "sim_df_name") <- sim_df_name
attr(ret, "sim_data_name") <- sim_data_name
mess <- paste0(
"\n### Repeated simulation data\n\nFormula:\n\n",
gsub("*", "\\*", sc$form, fixed = TRUE) %>%
gsub("[;\n]\\s*?\\#+[^\\#]", "; ##### # ", .) %>%
gsub(";", "\n\n", .),
"\n\nDate: ",
lubridate::now()
)
add_class(set_attr(ret, "description", mess), "repeater")
}
#' Summarize repeated simulation
#'
#' @param object Return value from \code{\link{repeater}}
#' @param dec Number of decimals to show
#' @param ... further arguments passed to or from other methods
#'
#' @seealso \code{\link{repeater}} to run a repeated simulation
#' @seealso \code{\link{plot.repeater}} to plot results from repeated simulation
#'
#' @export
summary.repeater <- function(object, dec = 4, ...) {
if (is.character(object)) {
if (length(object) == 2 && object[1] == "error") {
return(cat(object[2]))
}
stop("To generate summary statistics please provide a simulated dataset as input", call. = FALSE)
}
## getting the repeater call
rc <- attr(object, "radiant_rep_call")
clean <- function(x) {
paste0(x, collapse = ";") %>%
gsub(";", "; ", .) %>%
gsub("\\n", "", .) %>%
paste0(., "\n")
}
## show results
cat("Repeated simulation\n")
cat("Simulations :", ifelse(is.empty(rc$sc$nr), "", format_nr(rc$sc$nr, dec = 0)), "\n")
cat("Repetitions :", format_nr(ifelse(is.empty(rc$nr), nrow(object), rc$nr), dec = 0), "\n")
if (!is.empty(rc$vars)) {
cat("Re-simulated :", paste0(rc$vars, collapse = ", "), "\n")
}
cat("Group by :", ifelse(rc$byvar == ".rep", "Repeat", "Simulation"), "\n")
cat("Function :", rc$fun, "\n")
cat("Random seed :", rc$seed, "\n")
if (is.data.frame(rc$sim)) {
rc$sim <- attr(rc$sim, "radiant_sim_call")$name
}
cat("Simulated data:", attr(object, "sim_df_name"), "\n")
attr(object, "sim_data_name") %>%
{
if (!is.empty(.)) cat("Data :", ., "\n")
}
if (is.empty(rc$name)) {
cat("Repeat data :", deparse(substitute(object)), "\n")
} else {
cat("Repeat data :", rc$name, "\n")
}
if (isTRUE(rc$byvar == "rep") && !is.empty(rc$grid)) {
cat("Grid search. :", clean(rc$grid))
}
if (!is.empty(rc$form)) {
rc$form %<>% sim_cleaner()
paste0(
"Formulas :\n\t",
paste0(rc$form, collapse = ";") %>%
gsub(";", "\n", .) %>%
gsub("\n", "\n\t", .),
"\n"
) %>% cat()
}
cat("\n")
sim_summary(select(object, -1), fun = rc$fun, dec = ifelse(is.na(dec), 4, dec))
}
#' Plot repeated simulation
#'
#' @param x Return value from \code{\link{repeater}}
#' @param bins Number of bins used for histograms (1 - 50)
#' @param shiny Did the function call originate inside a shiny app
#' @param custom Logical (TRUE, FALSE) to indicate if ggplot object (or list of ggplot objects) should be returned. This option can be used to customize plots (e.g., add a title, change x and y labels, etc.). See examples and \url{https://ggplot2.tidyverse.org} for options.
#' @param ... further arguments passed to or from other methods
#'
#' @seealso \code{\link{repeater}} to run a repeated simulation
#' @seealso \code{\link{summary.repeater}} to summarize results from repeated simulation
#'
#' @export
plot.repeater <- function(x, bins = 20, shiny = FALSE, custom = FALSE, ...) {
if (is.character(x)) {
return(invisible())
}
if (nrow(x) == 0) {
return(invisible())
}
## getting the repeater call
rc <- attr(x, "radiant_rep_call")
plot_list <- list()
for (i in colnames(x)[-1]) {
dat <- select_at(x, .vars = i)
if (!does_vary(x[[i]])) next
plot_list[[i]] <- select_at(x, .vars = i) %>%
visualize(xvar = i, bins = bins, custom = TRUE)
if (i %in% rc$sum_vars && !is.empty(rc$fun, "none")) {
plot_list[[i]] <- plot_list[[i]] + labs(x = paste0(rc$fun, " of ", i))
}
}
if (length(plot_list) > 0) {
if (custom) {
if (length(plot_list) == 1) plot_list[[1]] else plot_list
} else {
patchwork::wrap_plots(plot_list, ncol = min(length(plot_list), 2)) %>%
(function(x) if (shiny) x else print(x))
}
}
}
#' Print simulation summary
#'
#' @param dataset Simulated data
#' @param dc Variable classes
#' @param fun Summary function to apply
#' @param dec Number of decimals to show
#'
#' @seealso \code{\link{simulater}} to run a simulation
#' @seealso \code{\link{repeater}} to run a repeated simulation
#'
#' @examples
#' simulater(
#' const = "cost 3",
#' norm = "demand 2000 1000",
#' discrete = "price 5 8 .3 .7",
#' form = c("profit = demand * (price - cost)", "profit5K = profit > 5000"),
#' seed = 1234
#' ) %>% sim_summary()
#'
#' @export
sim_summary <- function(dataset, dc = get_class(dataset), fun = "", dec = 4) {
isFct <- "factor" == dc
isNum <- dc %in% c("numeric", "integer", "Duration")
isChar <- "character" == dc
isLogic <- "logical" == dc
dec <- ifelse(is.na(dec), 4, as.integer(dec))
if (sum(isNum) > 0) {
isConst <- !sapply(dataset, does_vary) & isNum
if (sum(isConst) > 0) {
cn <- names(dc)[isConst]
cat("Constants:\n")
select(dataset, which(isConst)) %>%
na.omit() %>%
.[1, ] %>%
as.data.frame(stringsAsFactors = FALSE) %>%
round(dec) %>%
mutate_all(~ formatC(., big.mark = ",", digits = dec, format = "f")) %>%
set_rownames("") %>%
set_colnames(cn) %>%
print()
cat("\n")
}
isRnd <- isNum & !isConst
if (sum(isRnd) > 0) {
cn <- names(dc)[isRnd]
cat("Variables:\n")
select(dataset, which(isNum & !isConst)) %>%
gather("variable", "values", !!cn) %>%
group_by_at(.vars = "variable") %>%
summarise_all(
list(
n_obs = n_obs, mean = mean, sd = sd, min = min,
p25 = p25, median = median, p75 = p75, max = max
),
na.rm = TRUE
) %>%
mutate(variable = format(variable, justify = "left")) %>%
data.frame(check.names = FALSE, stringsAsFactors = FALSE) %>%
format_df(dec = dec, mark = ",") %>%
rename(` ` = "variable") %>%
print(row.names = FALSE)
cat("\n")
}
}
if (sum(isLogic) > 0) {
cat("Logicals:\n")
select(dataset, which(isLogic)) %>%
summarise_all(list(sum, mean), na.rm = TRUE) %>%
round(dec) %>%
matrix(ncol = 2) %>%
as.data.frame(stringsAsFactors = FALSE) %>%
set_colnames(c("TRUE (nr) ", "TRUE (prop)")) %>%
set_rownames(names(dataset)[isLogic]) %>%
format(big.mark = ",", scientific = FALSE) %>%
print()
cat("\n")
}
if (sum(isFct) > 0 || sum(isChar) > 0) {