Skip to content

Reproducible analytical pipeline (RAP) for R discrete-event simulation (DES) implementing an M/M/s queueing model.

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md
Notifications You must be signed in to change notification settings

pythonhealthdatascience/rdesrap_mms

Simple M/M/s queuing model: R DES RAP

R 4.4.1 MIT Licence DOI R-CMD-check Lint ORCID Codecov test coverage


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.


Repository overview

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.


Usage and reproduction instructions

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 32s
  • choosing_cores.Rmd: 0m 51s
  • choosing_replications.Rmd: 2m 1s
  • choosing_warmup.Rmd: 16s
  • generate_exp_results.Rmd: 11s
  • input_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).


Project details and credits

How to cite the repository

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.

Contributors

Amy Heather - developed the repository.

  • ORCID
  • GitHub

Tom Monks - peer review of the repository.

  • ORCID
  • GitHub

Licence

MIT Licence. See LICENSE.md for details.

Community

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.

Acknowledgements

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

Funding

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].