Skip to content

Commit db9212c

Browse files
committed
feat(model): add creation of ASU and rehab trajectories (create_asu/rehab_trajectory), with ASU sampling the routing destination (sample_routing)
1 parent f4d0615 commit db9212c

File tree

11 files changed

+267
-76
lines changed

11 files changed

+267
-76
lines changed

NAMESPACE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ export(add_patient_generator)
44
export(create_asu_arrivals)
55
export(create_asu_los)
66
export(create_asu_routing)
7+
export(create_asu_trajectory)
78
export(create_parameters)
89
export(create_rehab_arrivals)
910
export(create_rehab_los)
1011
export(create_rehab_routing)
12+
export(create_rehab_trajectory)
1113
export(model)
14+
export(sample_routing)
1215
importFrom(simmer,add_generator)
16+
importFrom(simmer,get_attribute)
1317
importFrom(simmer,get_mon_arrivals)
1418
importFrom(simmer,get_mon_resources)
19+
importFrom(simmer,set_attribute)
1520
importFrom(simmer,simmer)
1621
importFrom(simmer,timeout)
1722
importFrom(simmer,trajectory)

R/create_asu_trajectory.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#' Create ASU patient trajectory
2+
#'
3+
#' @param patient_type Character string specifying patient category. Must be
4+
#' one of: "stroke", "tia", "neuro", or "other". Determines which arrival rate
5+
#' parameter is used.
6+
#' @param param Nested list containing simulation parameters. Must have
7+
#' structure `param$asu_routing$<patient_type>` containing the probability of
8+
#' routing to each destination (e.g.`param$asu_routing_stroke$rehab = 0.24`).
9+
#'
10+
#' @importFrom simmer trajectory
11+
#'
12+
#' @return Simmer trajectory object. Defines patient journey logic through the
13+
#' healthcare system.
14+
#' @export
15+
16+
create_asu_trajectory <- function(patient_type, param) {
17+
trajectory(paste0("ASU_", patient_type, "_path")) |>
18+
set_attribute("post_asu_destination", function(env) {
19+
sample_routing(prob_list = param[["asu_routing"]][[patient_type]])
20+
}) |>
21+
timeout(1L)
22+
}

R/create_rehab_trajectory.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#' Create rehab patient trajectory
2+
#'
3+
#' @param patient_type Character string specifying patient category. Must be
4+
#' one of: "stroke", "tia", "neuro", or "other". Determines which arrival rate
5+
#' parameter is used.
6+
#' @param param Nested list containing simulation parameters. Must have
7+
#' structure `param$asu_routing$<patient_type>` containing the probability of
8+
#' routing to each destination (e.g.`param$asu_routing_stroke$rehab = 0.24`).
9+
#'
10+
#' @importFrom simmer trajectory
11+
#'
12+
#' @return Simmer trajectory object. Defines patient journey logic through the
13+
#' healthcare system.
14+
#' @export
15+
16+
create_rehab_trajectory <- function(patient_type, param) {
17+
trajectory(paste0("rehab_", patient_type, "_path")) |>
18+
timeout(1L)
19+
}

R/model.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#' may not wish to do if being set elsewhere - such as done in runner()).
77
#' Default is TRUE.
88
#'
9-
#' @importFrom simmer get_mon_arrivals get_mon_resources simmer timeout
10-
#' @importFrom simmer trajectory wrap
9+
#' @importFrom simmer get_attribute get_mon_arrivals get_mon_resources
10+
#' @importFrom simmer set_attribute simmer timeout wrap
1111
#'
1212
#' @return TBC
1313
#' @export
@@ -22,16 +22,16 @@ model <- function(run_number, param, set_seed = TRUE) {
2222
# Create simmer environment
2323
env <- simmer("simulation", verbose = TRUE)
2424

25-
# Define the stroke patient trajectory
26-
patient <- trajectory("patient_path") |>
27-
timeout(1L)
28-
2925
# Add ASU and rehab direct admission patient generators
3026
for (unit in c("asu", "rehab")) {
3127
for (patient_type in names(param[[paste0(unit, "_arrivals")]])) {
3228
env <- add_patient_generator(
3329
env = env,
34-
trajectory = patient,
30+
# Get trajectory given unit and patient type
31+
trajectory = (
32+
if (unit == "asu") create_asu_trajectory(patient_type, param)
33+
else create_rehab_trajectory(patient_type, param)
34+
),
3535
unit = unit,
3636
patient_type = patient_type,
3737
param = param

R/parameters.R

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,15 @@ create_asu_los <- function(
6868
neuro_mean = 4.0, neuro_sd = 5.0,
6969
other_mean = 3.8, other_sd = 5.2
7070
) {
71-
return(as.list(environment()))
71+
list(
72+
stroke_noesd = list(mean = stroke_noesd_mean, sd = stroke_noesd_sd),
73+
stroke_esd = list(mean = stroke_esd_mean, sd = stroke_esd_sd),
74+
stroke_mortality = list(mean = stroke_mortality_mean,
75+
sd = stroke_mortality_sd),
76+
tia = list(mean = tia_mean, sd = tia_sd),
77+
neuro = list(mean = neuro_mean, sd = neuro_sd),
78+
other = list(mean = other_mean, sd = other_sd)
79+
)
7280
}
7381

7482
#' Rehabilitation unit length of stay (LOS) distributions (days).
@@ -96,7 +104,13 @@ create_rehab_los <- function(
96104
neuro_mean = 27.6, neuro_sd = 28.4,
97105
other_mean = 16.1, other_sd = 14.1
98106
) {
99-
return(as.list(environment()))
107+
list(
108+
stroke_noesd = list(mean = stroke_noesd_mean, sd = stroke_noesd_sd),
109+
stroke_esd = list(mean = stroke_esd_mean, sd = stroke_esd_sd),
110+
tia = list(mean = tia_mean, sd = tia_sd),
111+
neuro = list(mean = neuro_mean, sd = neuro_sd),
112+
other = list(mean = other_mean, sd = other_sd)
113+
)
100114
}
101115

102116
#' ASU routing probabilities.
@@ -131,7 +145,12 @@ create_asu_routing <- function(
131145
neuro_rehab = 0.11, neuro_esd = 0.05, neuro_other = 0.84,
132146
other_rehab = 0.05, other_esd = 0.10, other_other = 0.85
133147
) {
134-
return(as.list(environment()))
148+
list(
149+
stroke = list(rehab = stroke_rehab, esd = stroke_esd, other = stroke_other),
150+
tia = list(rehab = tia_rehab, esd = tia_esd, other = tia_other),
151+
neuro = list(rehab = neuro_rehab, esd = neuro_esd, other = neuro_other),
152+
other = list(rehab = other_rehab, esd = other_esd, other = other_other)
153+
)
135154
}
136155

137156
#' Rehabilitation unit routing probabilities.
@@ -161,7 +180,12 @@ create_rehab_routing <- function(
161180
neuro_esd = 0.09, neuro_other = 0.91,
162181
other_esd = 0.13, other_other = 0.88
163182
) {
164-
return(as.list(environment()))
183+
list(
184+
stroke = list(esd = stroke_esd, other = stroke_other),
185+
tia = list(esd = tia_esd, other = tia_other),
186+
neuro = list(esd = neuro_esd, other = neuro_other),
187+
other = list(esd = other_esd, other = other_other)
188+
)
165189
}
166190

167191
#' Generate complete parameter list for simulation.

R/sample_routing.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#' Sample a destination based on probabilities
2+
#'
3+
#' Randomly selects a destination from a list, where each destination has an
4+
#' associated probability.
5+
#'
6+
#' @param prob_list Named list. The names are destination labels (character),
7+
#' and the values are their corresponding probabilities (numeric, non-negative,
8+
#' sum to 1).
9+
#'
10+
#' @return A character string. The name of the selected destination.
11+
#' @export
12+
13+
sample_routing <- function(prob_list) {
14+
sample(seq_along(prob_list), size = 1L, prob = unlist(prob_list))
15+
}

man/create_asu_trajectory.Rd

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/create_rehab_trajectory.Rd

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/sample_routing.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rmarkdown/analysis.Rmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ devtools::load_all()
1616
library(simulation)
1717
```
1818

19+
1920
```{r}
2021
model(run_number = 1L, param = create_parameters(), set_seed = TRUE)
2122
```

0 commit comments

Comments
 (0)