This repository is an example accompanying the DES RAP Book — an open educational resource on reproducible discrete-event simulation (DES) in Python and R. The book demonstrates best practices for building, validating, and sharing DES models within a reproducible analytical pipeline (RAP). The rdesrap_mms model illustrates how these principles can be applied to a simple queueing model.
This repository provides a reproducible analytical pipeline (RAP) for a simple M/M/s queuing model implemented in R using simmer. The model simulates patients arriving, waiting to see a nurse, being served, and leaving. All code is structured as a local R package.
An M/M/s queueing model is a classic mathematical model for systems where:
- Arrivals happen at random, following a Poisson process - and the time between arrivals follows an exponential distribution (the first "M", which stands for "Markovian" as it is memoryless - arrivals are independent).
- Service times are exponential (second "M").
- There are s parallel servers (e.g. nurses) sharing a single queue.
This type of model is widely used for studying waiting lines in healthcare, call centers, and other service systems. It helps answer questions like: How long will people wait? How many servers are needed to keep waits short? The only required inputs are the average arrival rate, average service time, and the number of servers.
Installation
Clone the repository:
git clone https://github.com/pythonhealthdatascience/rdesrap_mms.git
cd rdesrap_mms
Set up the R environment using renv (recommended):
renv::init()
renv::restore()
If you encounter issues restoring the exact environment, you can install dependencies from DESCRIPTION and generate your own lock file:
renv::init()
renv::install()
renv::snapshot()
System dependencies: Some packages (e.g. igraph) may require system libraries. For example, for Ubuntu:
sudo apt install build-essential gfortran
sudo apt install libglpk-dev libxml2-dev
How to run
The simulation code is in the R/ folder as a local package. Example analyses and model runs are in rmarkdown/.
Install the local package:
devtools::install()
library(simulation)
Run a single simulation:
params <- parameters(patient_inter = 2L, mean_n_consult_time = 4L, number_of_nurses = 2L)
result <- model(run_number = 1L, param = params)
Run multiple replications:
params <- parameters(patient_inter = 2L, mean_n_consult_time = 4L, number_of_nurses = 2L, number_of_runs = 10L)
results <- runner(param = params)
Run all example analyses (from command line):
bash run_rmarkdown.sh
Input data
Patient-level data for our system is provided in the file: inst/extdata/NHS_synthetic.csv.
Data dictionary (explaining each field) is available in: inst/extdata/NHS_synthetic_dictionary.csv.
This dataset is synthetic and was generated in the pydesrap_mms repository based on the the structure of some fields from the Emergency Care Data Set (ECDS). The data generation process involved:
- Arrivals: Sampled from a Poisson distribution (average 15 patients per hour).
- Wait times: Sampled from an exponential distribution (average wait time: 5 minutes).
- Service times: Sampled from an exponential distribution (average service time: 10 minutes).
- Time period: Data covers one full year (1st January - 31st December 2025).
This dataset is released under the MIT licence. If you use this data, please cite the original repository: pydesrap_mms.
The code for input modelling is in: rmarkdown/input_modelling.Rmd. Model parameters are determined in this file and then stored in: R/parameters.R. Description for each parameter can be found in the class docstring within this file.
Reproducing results
To generate the figures and tables from the paper (mock_paper.md), execute:
- Figure 1-4:
rmarkdown/analysis.Rmd - Figure A.1-A.2:
rmarkdown/input_modelling.Rmd - Figure B.1:
rmarkdown/choosing_warmup.Rmd - Figure C.1-C.3:
rmarkdown/choosing_replications.Rmd
Run time and machine specification
Run times from our analyses (on Intel Core i7-12700H, 32GB RAM, Ubuntu 24.04.1):
analysis.Rmd: 4m 32schoosing_cores.Rmd: 0m 51schoosing_replications.Rmd: 2m 1schoosing_warmup.Rmd: 16sgenerate_exp_results.Rmd: 11sinput_modelling.Rmd: 8s
There are also two notebooks illustrating logging functionality (logs.Rmd) and about the impact of using set_attributes() and get_attributes (using_set_attributes.Rmd).
If you use this repository, please cite either the GitHub repository or Zenodo:
Heather, A. & Monks, T. (2025). Simple M/M/s queuing model: R DES RAP. GitHub. https://github.com/pythonhealthdatascience/rdesrap_mms.
Heather, A. & Monks, T. (2025). Simple M/M/s queuing model: R DES RAP. Zenodo. https://doi.org/10.5281/zenodo.14980863.
Amy Heather - developed the repository.
Tom Monks - peer review of the repository.
MIT Licence. See LICENSE.md for details.
Curious about contributing? Check out the contributing guidelines to learn how you can help. Every bit of help counts, and your contribution - no matter how minor - is highly valued.
This repository was developed with thanks to a few others sources. These are acknowledged throughout in the relevant scipts, and also summarised here:
| Source | Find out more about how it was used... |
|---|---|
| Ucar I, Smeets B (2023). simmer.plot: Plotting Methods for 'simmer'. https://r-simmer.org. https://github.com/r-simmer/simmer.plot. | R/get_run_results.R |
| Tom Monks (2021) sim-tools: fundamental tools to support the simulation process in python (https://github.com/TomMonks/sim-tools) (MIT Licence). Who themselves cite Hoad, Robinson, & Davies (2010). Automated selection of the number of replications for a discrete-event simulation (https://www.jstor.org/stable/40926090), and Knuth. D "The Art of Computer Programming" Vol 2. 2nd ed. Page 216. |
R/choose_replications.R |
This project was developed as part of the project STARS: Sharing Tools and Artefacts for Reproducible Simulations. It is supported by the Medical Research Council [grant number MR/Z503915/1].
