Skip to content

Commit fa8931b

Browse files
committed
feat(model): in ASU trajectory (create_asu_trajectory), add sampling of ASU LOS (transform_to_lnorm)
1 parent ead2f90 commit fa8931b

File tree

6 files changed

+223
-111
lines changed

6 files changed

+223
-111
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export(create_rehab_routing)
1212
export(create_rehab_trajectory)
1313
export(model)
1414
export(sample_routing)
15+
export(transform_to_lnorm)
1516
importFrom(simmer,add_generator)
1617
importFrom(simmer,get_attribute)
1718
importFrom(simmer,get_mon_arrivals)
@@ -22,3 +23,4 @@ importFrom(simmer,timeout)
2223
importFrom(simmer,trajectory)
2324
importFrom(simmer,wrap)
2425
importFrom(stats,rexp)
26+
importFrom(stats,rlnorm)

R/create_asu_trajectory.R

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,60 @@
1-
#' Create ASU patient trajectory
1+
#' Create acute stroke unit (ASU) patient trajectory.
22
#'
3+
#' Represents patient stay in the ASU - samples their (1) destination after
4+
#' the ASU, and (2) length of stay (LOS) on the ASU.
5+
#'
6+
#' @param env Simmer environment object. The simulation environment where
7+
#' generators will be added.
38
#' @param patient_type Character string specifying patient category. Must be
49
#' one of: "stroke", "tia", "neuro", or "other". Determines which arrival rate
510
#' parameter is used.
611
#' @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`).
12+
#' structure \code{param$asu_routing$<patient_type>} containing the probability
13+
#' of routing to each destination (e.g.
14+
#' \code{param$asu_routing$stroke$rehab = 0.24}).
915
#'
1016
#' @importFrom simmer trajectory
17+
#' @importFrom stats rlnorm
1118
#'
1219
#' @return Simmer trajectory object. Defines patient journey logic through the
1320
#' healthcare system.
1421
#' @export
1522

16-
create_asu_trajectory <- function(patient_type, param) {
23+
create_asu_trajectory <- function(env, patient_type, param) {
24+
25+
# Set up simmer trajectory object...
1726
trajectory(paste0("ASU_", patient_type, "_path")) |>
18-
set_attribute("post_asu_destination", function(env) {
27+
28+
# Sample destination after ASU (we do this immediately on arrival in the
29+
# ASU, as the destination influences the length of stay)
30+
set_attribute("post_asu_destination", function() {
1931
sample_routing(prob_list = param[["asu_routing"]][[patient_type]])
2032
}) |>
21-
timeout(1L)
33+
34+
timeout(function() {
35+
36+
# Retrieve attribute, and use to get post-ASU destination as a string
37+
dest_index <- get_attribute(env, "post_asu_destination")
38+
dest_names <- names(param[["asu_routing"]][[patient_type]])
39+
dest <- dest_names[dest_index]
40+
41+
# Determine which LOS distribution to use
42+
if (patient_type == "stroke") {
43+
los_params <- switch(
44+
dest,
45+
"esd" = param[["asu_los_lnorm"]][["stroke_esd"]],
46+
"rehab" = param[["asu_los_lnorm"]][["stroke_noesd"]],
47+
param[["asu_los_lnorm"]][["stroke_mortality"]] # Default case
48+
)
49+
} else {
50+
los_params <- param[["asu_los_lnorm"]][[patient_type]]
51+
}
52+
53+
# Sample LOS from lognormal
54+
rlnorm(
55+
n = 1L,
56+
meanlog = los_params[["meanlog"]],
57+
sdlog = los_params[["sdlog"]]
58+
)
59+
})
2260
}

R/transform_to_lnorm.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#' Convert LOS mean/sd to lognormal parameters for all patient types.
2+
#'
3+
#' Given a named list of length of stay (LOS) distributions (each with
4+
#' \code{mean} and \code{sd} on the original scale), this function returns a
5+
#' new named list where each entry contains the corresponding \code{meanlog}
6+
#' and \code{sdlog} parameters required by R's \code{rlnorm()} and related
7+
#' functions.
8+
#'
9+
#' @param los_list Named list. Each element should itself be a list with
10+
#' elements \code{mean} and \code{sd} (e.g., as produced by
11+
#' \code{create_asu_los()} or \code{create_rehab_los()}).
12+
#'
13+
#' @return A named list of the same structure, but with elements \code{meanlog}
14+
#' and \code{sdlog} for each patient type.
15+
#' @export
16+
17+
transform_to_lnorm <- function(los_list) {
18+
lapply(los_list, function(x) {
19+
variance <- x$sd^2
20+
sigma_sq <- log(variance / (x$mean^2) + 1)
21+
sdlog <- sqrt(sigma_sq)
22+
meanlog <- log(x$mean) - sigma_sq / 2
23+
list(meanlog = meanlog, sdlog = sdlog)
24+
})
25+
}

man/create_asu_trajectory.Rd

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

man/transform_to_lnorm.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.

0 commit comments

Comments
 (0)