Skip to content

Commit 438cce7

Browse files
committed
feat(model): set up rehab sampling destination + LOS (like ASU) + linting
1 parent fa8931b commit 438cce7

File tree

5 files changed

+59
-16
lines changed

5 files changed

+59
-16
lines changed

R/create_asu_trajectory.R

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ create_asu_trajectory <- function(env, patient_type, param) {
2525
# Set up simmer trajectory object...
2626
trajectory(paste0("ASU_", patient_type, "_path")) |>
2727

28-
# Sample destination after ASU (we do this immediately on arrival in the
29-
# ASU, as the destination influences the length of stay)
28+
# Sample destination after ASU (as destination influences length of stay)
3029
set_attribute("post_asu_destination", function() {
3130
sample_routing(prob_list = param[["asu_routing"]][[patient_type]])
3231
}) |>
@@ -42,9 +41,11 @@ create_asu_trajectory <- function(env, patient_type, param) {
4241
if (patient_type == "stroke") {
4342
los_params <- switch(
4443
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
44+
esd = param[["asu_los_lnorm"]][["stroke_esd"]],
45+
rehab = param[["asu_los_lnorm"]][["stroke_noesd"]],
46+
other = param[["asu_los_lnorm"]][["stroke_mortality"]],
47+
stop("Stroke post-asu destination '", dest, "' invalid",
48+
call. = FALSE)
4849
)
4950
} else {
5051
los_params <- param[["asu_los_lnorm"]][[patient_type]]

R/create_rehab_trajectory.R

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#' Create rehab patient trajectory.
22
#'
3+
#' Represents patient stay on the rehabilitation unit - samples their (1)
4+
#' destination after rehab, and (2) length of stay (LOS) on the unit.
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.
@@ -9,12 +14,47 @@
914
#' \code{param$rehab_routing$stroke$esd = 0.40}).
1015
#'
1116
#' @importFrom simmer trajectory
17+
#' @importFrom stats rlnorm
1218
#'
1319
#' @return Simmer trajectory object. Defines patient journey logic through the
1420
#' healthcare system.
1521
#' @export
1622

17-
create_rehab_trajectory <- function(patient_type, param) {
23+
create_rehab_trajectory <- function(env, patient_type, param) {
24+
25+
# Set up simmer trajectory object...
1826
trajectory(paste0("rehab_", patient_type, "_path")) |>
19-
timeout(1L)
27+
28+
# Sample destination after rehab (as destination influences length of stay)
29+
set_attribute("post_rehab_destination", function() {
30+
sample_routing(prob_list = param[["rehab_routing"]][[patient_type]])
31+
}) |>
32+
33+
timeout(function() {
34+
35+
# Retrieve attribute, and use to get post-rehab destination as a string
36+
dest_index <- get_attribute(env, "post_rehab_destination")
37+
dest_names <- names(param[["rehab_routing"]][[patient_type]])
38+
dest <- dest_names[dest_index]
39+
40+
# Determine which LOS distribution to use
41+
if (patient_type == "stroke") {
42+
los_params <- switch(
43+
dest,
44+
esd = param[["rehab_los_lnorm"]][["stroke_esd"]],
45+
other = param[["rehab_los_lnorm"]][["stroke_noesd"]],
46+
stop("Stroke post-rehab destination '", dest, "' invalid",
47+
call. = FALSE)
48+
)
49+
} else {
50+
los_params <- param[["rehab_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+
})
2060
}

R/model.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@ model <- function(run_number, param, set_seed = TRUE) {
3232

3333
# Create patient trajectory
3434
traj <- if (unit == "asu") {
35-
create_asu_trajectory(
36-
env = env, patient_type = patient_type, param = param
37-
)
35+
create_asu_trajectory(env, patient_type, param)
3836
} else {
39-
create_rehab_trajectory(patient_type, param)
37+
create_rehab_trajectory(env, patient_type, param)
4038
}
4139

4240
# Add patient generator using the created trajectory

R/transform_to_lnorm.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
transform_to_lnorm <- function(los_list) {
1818
lapply(los_list, function(x) {
19-
variance <- x$sd^2
20-
sigma_sq <- log(variance / (x$mean^2) + 1)
19+
variance <- x$sd^2L
20+
sigma_sq <- log(variance / (x$mean^2L) + 1L)
2121
sdlog <- sqrt(sigma_sq)
22-
meanlog <- log(x$mean) - sigma_sq / 2
22+
meanlog <- log(x$mean) - sigma_sq / 2L
2323
list(meanlog = meanlog, sdlog = sdlog)
2424
})
2525
}

man/create_rehab_trajectory.Rd

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

0 commit comments

Comments
 (0)