Skip to contents

Set up

You can install the development version of ipacs from GitHub with devtools::install_github("amyheather/ipacs"). You will first need to install devtools, if not already installed. Then, load the ipacs package using library().

Set value for objects that you may wish to change. In this example, system.file() is used to find example data stored in the IPACS package. However, you can input a string with the path to your desired file - e.g. `import_file(“myfile.xlsx”).

# String with file path for excel sheet with model inputs
input_filename <- system.file("extdata", "IPACS_20230214_fix.xlsx", package="ipacs")

# Path to save output files to
visit_filename <- "../inst/extdata/visit_output_using_IPACS_20230214_fix.csv"
visit_stoch_filename <- "../inst/extdata/stochastic_visit_output_using_IPACS_20230214_fix.csv"

# Method for estimation of mu and sigma (for calculate_mu_sigma())
est_method <- 1

# Standard deviation for length of stay
sd_los <- 3

# Number of runs for each simulation
nruns <- 5

# Standard deviation for initial service/visit rate (ISR/IVR) and end service
# rate/final visit rate (ESR/FVR)
sd_isr <- 0.5
sd_esr <- 0.5

# Temporary seed
temp_seed <- 1

# Warmup length
warmup <- 0

Use import_file to import sheets from file containing model parameters. This will produce 5 dataframes: “arrivals_all”, “init_conds”, “capacity”, “losA” and “costs”. As R cannot return multiple objects, we return a list of dataframes and a list of dataframe names, then use assign to create those in our current environment.

files <- import_file(input_filename)
for (x in seq_along(files[[1]])){
  assign(files[[1]][x], files[[2]][[x]])
}

Use calculate_mu_sigma to recalculate mu and sigma in losA dataframe using your chosen method and sd_los (if applicable).

Use create_scenarios to create scenarios and arr_scenarios.

scenarios <- create_scenarios(scenarios=TRUE)
arr_scenarios <- create_scenarios(scenarios=FALSE)

Visit-based simulation (P1)

Set-up: Use sim_setup to create objects for visit-based simulation. These are returned in a list, so use assign to extract and add to environment.

setup_visit <- sim_setup("visit")
for (i in seq_along(setup_visit[[1]])){
  assign(setup_visit[[1]][i], setup_visit[[2]][[i]])
}

Run simulation: Use run_visit_sim to run the visit-based simulation. Extract the results (as returned as list).

visit_sim_result <- run_visit_sim()
visits_based_output <- visit_sim_result[[1]]
visits_based_output_q <- visit_sim_result[[2]]

Save results: Use save_visit() extract results for each date for number in queue, occupancy, wait and costs and then save to csv. Use save_visit_stoch() to correct date formatting and then save the quantiles to csv for optional stochastic report, saving in long format for plotting.

save_visit(visit_filename)
save_visit_stoch(visit_stoch_filename)

Testing

Checks that output from simulations matchs outputs from IPACS_MODEL repository.

test_visit <- read.csv("../inst/extdata/visit_testing_using_IPACS_20230214_fix.csv")
new_visit <- read.csv("../inst/extdata/visit_output_using_IPACS_20230214_fix.csv")
print(diffdf::diffdf(new_visit, test_visit))
#> No issues were found!