Skip to content

Commit fb02ffe

Browse files
authored
Merge pull request #28 from pythonhealthdatascience/dev
Dev
2 parents 8c96956 + d8165b3 commit fb02ffe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1758
-1264
lines changed

.Rbuildignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
^\.lintr$
1111
^outputs$
1212
^rmarkdown$
13-
^CITATION\.cff$
13+
^CITATION\.cff$
14+
^run_rmarkdown\.sh$

.github/workflows/R-CMD-check.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
33
on:
44
push:
5-
branches: [main, master]
6-
pull_request:
5+
branches: [main]
76
workflow_dispatch:
87

98
name: R-CMD-check.yaml

.github/workflows/lint.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
on:
55
push:
66
branches: [main]
7-
pull_request:
87
workflow_dispatch:
98

109
name: lint

.gitignore

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,49 @@
1-
.Rproj.user
1+
# History files
22
.Rhistory
3+
.Rapp.history
4+
5+
# Session Data files
36
.RData
7+
.RDataTmp
8+
9+
# User-specific files
410
.Ruserdata
11+
12+
# Example code in package build process
13+
*-Ex.R
14+
15+
# Output files from R CMD build
16+
/*.tar.gz
17+
18+
# Output files from R CMD check
19+
/*.Rcheck/
20+
21+
# RStudio files
22+
.Rproj.user/
23+
24+
# produced vignettes
25+
vignettes/*.html
26+
vignettes/*.pdf
27+
28+
# OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3
29+
.httr-oauth
30+
31+
# knitr and R markdown default cache directories
32+
*_cache/
33+
/cache/
34+
35+
# Temporary files created by R markdown
36+
*.utf8.md
37+
*.knit.md
38+
39+
# R Environment Variables
40+
.Renviron
41+
42+
# pkgdown site
43+
docs/
44+
45+
# translation temp files
46+
po/*~
47+
48+
# RStudio Connect folder
49+
rsconnect/

DESCRIPTION

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ Imports:
2323
purrr,
2424
rlang,
2525
tidyr,
26-
R6,
2726
tidyselect,
2827
future,
29-
future.apply
28+
future.apply,
29+
ggplot2
3030
Suggests:
3131
testthat (>= 3.0.0),
32+
patrick,
3233
lintr,
3334
devtools,
34-
ggplot2,
3535
xtable,
3636
data.table
3737
Config/testthat/edition: 3

NAMESPACE

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
11
# Generated by roxygen2: do not edit by hand
22

3-
export(Defaults)
4-
export(defaults)
3+
export(confidence_interval_method)
4+
export(get_run_results)
55
export(model)
6-
export(process_replications)
7-
export(trial)
6+
export(parameters)
7+
export(run_scenarios)
8+
export(runner)
89
export(valid_inputs)
9-
importFrom(R6,R6Class)
10+
importFrom(dplyr,filter)
1011
importFrom(dplyr,full_join)
1112
importFrom(dplyr,group_by)
1213
importFrom(dplyr,lead)
1314
importFrom(dplyr,mutate)
1415
importFrom(dplyr,n_distinct)
16+
importFrom(dplyr,pull)
17+
importFrom(dplyr,select)
18+
importFrom(dplyr,slice_head)
1519
importFrom(dplyr,summarise)
1620
importFrom(future,multisession)
1721
importFrom(future,plan)
1822
importFrom(future,sequential)
1923
importFrom(future.apply,future_lapply)
24+
importFrom(ggplot2,aes)
25+
importFrom(ggplot2,geom_line)
26+
importFrom(ggplot2,geom_ribbon)
27+
importFrom(ggplot2,geom_vline)
28+
importFrom(ggplot2,ggplot)
29+
importFrom(ggplot2,ggsave)
30+
importFrom(ggplot2,labs)
31+
importFrom(ggplot2,theme_minimal)
2032
importFrom(magrittr,"%>%")
2133
importFrom(purrr,reduce)
2234
importFrom(rlang,.data)
2335
importFrom(simmer,add_generator)
2436
importFrom(simmer,add_resource)
2537
importFrom(simmer,get_mon_arrivals)
2638
importFrom(simmer,get_mon_resources)
39+
importFrom(simmer,now)
2740
importFrom(simmer,release)
2841
importFrom(simmer,run)
2942
importFrom(simmer,seize)
@@ -32,6 +45,8 @@ importFrom(simmer,timeout)
3245
importFrom(simmer,trajectory)
3346
importFrom(simmer,wrap)
3447
importFrom(stats,rexp)
48+
importFrom(stats,sd)
49+
importFrom(stats,t.test)
3550
importFrom(tidyr,drop_na)
3651
importFrom(tidyr,pivot_wider)
3752
importFrom(tidyselect,any_of)

R/choose_replications.R

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#' Use the confidence interval method to select the number of replications.
2+
#'
3+
#' @param replications Number of times to run the model.
4+
#' @param desired_precision Desired mean deviation from confidence interval.
5+
#' @param metric Name of performance metric to assess.
6+
#' @param yaxis_title Label for y axis.
7+
#' @param path Path inc. filename to save figure to.
8+
#' @param min_rep A suggested minimum number of replications (default=NULL).
9+
#'
10+
#' @importFrom stats sd t.test
11+
#' @importFrom dplyr filter slice_head select pull
12+
#' @importFrom ggplot2 ggplot aes geom_line geom_ribbon geom_vline labs
13+
#' theme_minimal ggsave
14+
#' @importFrom rlang .data
15+
#'
16+
#' @return Dataframe with results from each replication.
17+
#' @export
18+
19+
confidence_interval_method <- function(replications, desired_precision, metric,
20+
yaxis_title, path, min_rep = NULL) {
21+
# Run model for specified number of replications
22+
param <- parameters(number_of_runs = replications)
23+
raw_results <- runner(param)
24+
results <- get_run_results(raw_results)
25+
26+
# If mean of metric is less than 1, multiply by 100
27+
if (mean(results[[metric]]) < 1L) {
28+
results[[paste0("adj_", metric)]] <- results[[metric]] * 100L
29+
metric <- paste0("adj_", metric)
30+
}
31+
32+
# Initialise list to store the results
33+
cumulative_list <- list()
34+
35+
# For each row in the dataframe, filter to rows up to the i-th replication
36+
# then perform calculations
37+
for (i in 1L:replications) {
38+
39+
# Filter rows up to the i-th replication
40+
subset <- results[[metric]][1L:i]
41+
42+
# Calculate mean
43+
mean <- mean(subset)
44+
45+
# Some calculations require more than 1 observation else will error...
46+
if (i == 1L) {
47+
# When only one observation, set to NA
48+
std_dev <- NA
49+
ci_lower <- NA
50+
ci_upper <- NA
51+
deviation <- NA
52+
} else {
53+
# Else, calculate standard deviation, 95% confidence interval, and
54+
# percentage deviation
55+
std_dev <- sd(subset)
56+
ci <- t.test(subset)[["conf.int"]]
57+
ci_lower <- ci[[1L]]
58+
ci_upper <- ci[[2L]]
59+
deviation <- ((ci_upper - mean) / mean) * 100L
60+
}
61+
62+
# Append to the cumulative list
63+
cumulative_list[[i]] <- data.frame(
64+
replications = i,
65+
cumulative_mean = mean,
66+
cumulative_std = std_dev,
67+
ci_lower = ci_lower,
68+
ci_upper = ci_upper,
69+
perc_deviation = deviation
70+
)
71+
}
72+
73+
# Combine the list into a single data frame
74+
cumulative <- do.call(rbind, cumulative_list)
75+
76+
# Get the minimum number of replications where deviation is less than target
77+
compare <- cumulative %>%
78+
filter(.data[["perc_deviation"]] <= desired_precision * 100L)
79+
if (nrow(compare) > 0L) {
80+
# Get minimum number
81+
n_reps <- compare %>%
82+
slice_head() %>%
83+
dplyr::select(replications) %>%
84+
pull()
85+
print(paste0("Reached desired precision (", desired_precision, ") in ",
86+
n_reps, " replications."))
87+
} else {
88+
warning("Running ", replications, " replications did not reach ",
89+
"desired precision (", desired_precision, ").")
90+
}
91+
92+
# Plot the cumulative mean and confidence interval
93+
p <- ggplot(cumulative, aes(x = .data[["replications"]],
94+
y = .data[["cumulative_mean"]])) +
95+
geom_line() +
96+
geom_ribbon(aes(ymin = ci_lower, ymax = ci_upper), alpha = 0.2)
97+
98+
# If specified, plot the minimum suggested number of replications
99+
if (!is.null(min_rep)) {
100+
p <- p +
101+
geom_vline(xintercept = min_rep, linetype = "dashed", color = "red")
102+
}
103+
104+
# Modify labels and style
105+
p <- p +
106+
labs(x = "Replications", y = yaxis_title) +
107+
theme_minimal()
108+
109+
# Save the plot
110+
ggsave(filename = path, width = 6.5, height = 4L, bg = "white")
111+
112+
return(cumulative)
113+
}

R/defaults.R

Lines changed: 0 additions & 102 deletions
This file was deleted.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#' Process results from each replication.
1+
#' Get results from each replication.
22
#'
33
#' For each replication (there can be one or many), calculate the: (1) number
44
#' of arrivals, (2) mean wait time for each resource, (3) mean activity time
@@ -22,14 +22,14 @@
2222
#' @importFrom dplyr group_by summarise n_distinct mutate lead full_join
2323
#' @importFrom purrr reduce
2424
#' @importFrom rlang .data
25-
#' @importFrom simmer get_mon_resources get_mon_arrivals
25+
#' @importFrom simmer get_mon_resources get_mon_arrivals now
2626
#' @importFrom tidyr pivot_wider drop_na
2727
#' @importFrom tidyselect any_of
2828
#'
2929
#' @return Tibble with results from each replication.
3030
#' @export
3131

32-
process_replications <- function(results) {
32+
get_run_results <- function(results) {
3333

3434
# Remove patients who were still waiting and had not completed
3535
results[["arrivals"]] <- results[["arrivals"]] %>%

0 commit comments

Comments
 (0)