Learn R Programming

epicR

R package for Evaluation Platform in COPD (EPIC).

Overview

epicR provides an interface to interact with the Evaluation Platform in COPD (EPIC), a discrete-event-simulation (DES) whole-disease model of Chronic Obstructive Pulmonary Disease.

Installation

Install from CRAN (Recommended)

The easiest way to install epicR is from CRAN:

install.packages("epicR")

This works on all platforms (Windows, macOS, Linux) and handles dependencies automatically.

Install Development Version from GitHub

To install the latest development version with new features and bug fixes:

install.packages("pak")
pak::pkg_install("resplab/epicR")

Note: Installing from GitHub requires compilation tools:

  • Windows: Install Rtools
  • macOS: Install Xcode Command Line Tools (xcode-select --install) and gfortran
  • Linux: Install r-base-dev and build essentials (sudo apt-get install r-base-dev on Ubuntu/Debian)

Quick Guide

Simple Usage (Recommended)

The easiest way to run EPIC is with the simulate() function, which handles all session management automatically and provides progress information including a real-time progress bar:

library(epicR)

# Run with defaults (Canada, 20 year horizon, 60,000 agents)
results <- simulate()
print(results$basic)
# Shows configuration, progress bar (10% 20% ... 100%), and elapsed time

# Run for US with custom parameters
results <- simulate(jurisdiction = "us", time_horizon = 10, n_agents = 100000)

# Quick test with fewer agents (faster)
results <- simulate(n_agents = 10000)

# By default, you get both basic and extended results
results <- simulate()
print(results$basic)
print(results$extended)  # Included by default

# Get basic output only (faster, less memory)
results <- simulate(extended_results = FALSE)
print(results$basic)

# Get event history (automatically enables event recording)
results <- simulate(return_events = TRUE)
print(head(results$events))

# Get everything with custom settings
results <- simulate(
  jurisdiction = "us",
  time_horizon = 15,
  n_agents = 50000,
  extended_results = TRUE,  # TRUE by default
  return_events = TRUE
)
# Returns: results$basic, results$extended, results$events

Setting Seeds for Reproducibility

EPIC uses a custom seed mechanism (different from R's native set.seed()) to ensure reproducible results. Use the seed parameter in the simulate() function:

# Run with a specific seed for reproducible results
results1 <- simulate(seed = 123)
results2 <- simulate(seed = 123)
# Results will be identical!

# Different seeds give different results
results_a <- simulate(seed = 42)
results_b <- simulate(seed = 99)
# These will differ due to different random sequences

# Combine seed with other parameters
results <- simulate(
  jurisdiction = "us",
  time_horizon = 10,
  n_agents = 50000,
  seed = 12345
)

# Use seeds for comparing interventions
baseline <- simulate(seed = 42)

input <- get_input()$values
# Modify input parameters here...
intervention <- simulate(input = input, seed = 42)
# Compare with identical random variation removed

Important: Do NOT use R's native set.seed() function before calling simulate(). The seed parameter handles reproducibility correctly within EPIC's simulation framework.

Advanced Usage

For advanced customization of input parameters:

library(epicR)

# Get and modify inputs
input <- get_input()
input$values$global_parameters$time_horizon <- 5

# Run with custom inputs
results <- simulate(input = input$values)

Jurisdiction-Specific Configuration

EPIC now supports jurisdiction-specific parameter sets to enable modeling for different countries/regions. Currently, Canadian parameters are fully configured, with US parameters available as a template.

Using Canadian Parameters (Default)

# Canadian parameters are used by default
input <- get_input()
# or explicitly specify:
input <- get_input(jurisdiction = "canada")

Using US Parameters

# Note: US parameters must be configured first (see Configuration section)
input <- get_input(jurisdiction = "us")

Parameter Override

Function parameters still override jurisdiction defaults:

# Use Canadian defaults but change time horizon
input <- get_input(jurisdiction = "canada", time_horizon = 10)

Configuration

User-Customizable Configuration Files

epicR now supports user-customizable configuration files. When you load the package for the first time, configuration files are automatically copied to your home directory at ~/.epicR/config/, allowing you to modify model parameters without changing the package code.

Key Features:

  • Automatic Setup: Config files are copied to your home directory on first load
  • Easy Customization: Edit JSON files in ~/.epicR/config/ to adjust parameters
  • Automatic Detection: The package automatically uses your custom configs
  • Simple Reset: Return to defaults anytime with reset_user_configs()

Configuration Management Functions:

# View config directory location
get_user_config_dir()

# Open config directory in file explorer
open_user_config_dir()

# List available jurisdictions
list_available_jurisdictions()

# Validate your configuration
validate_config("canada", user = TRUE)

# Reset to package defaults
reset_user_configs()  # All configs
reset_user_configs("canada")  # Specific jurisdiction

Customizing Parameters:

# 1. Open config directory
open_user_config_dir()

# 2. Edit config_canada.json or config_us.json with your parameters

# 3. Your changes are automatically used
input <- get_input(jurisdiction = "canada")

Package Configuration Files

Default configurations are stored in the package at inst/config/:

  • config_canada.json - Canadian parameter set (fully configured)
  • config_us.json - US parameter template (requires configuration)

Configuring US Parameters

To use EPIC for US populations, replace placeholder values in config_us.json with appropriate US-specific data:

  1. Demographics (age pyramid, mortality tables)
  2. Smoking patterns and prevalence
  3. Healthcare costs and utilization
  4. Disease epidemiology parameters

Example placeholder format:

{
  "agent": {
    "p_female": "PLACEHOLDER_US_P_FEMALE"
  }
}

Replace with actual values:

{
  "agent": {
    "p_female": 0.51
  }
}

Test Values

Configuration files also include jurisdiction-specific test values used for package validation:

{
  "test_values": {
    "population_over_40_2015": 18600000,
    "expected_severe_exacerbations_per_year": 100000,
    "expected_severe_exac_tolerance": 5000
  }
}

These values represent:

  • population_over_40_2015: Population over 40 years of age (used for scaling test results)
  • expected_severe_exacerbations_per_year: Expected number of severe exacerbations per year for validation
  • expected_severe_exac_tolerance: Tolerance level for test assertions

For US parameters, replace with appropriate US values:

{
  "test_values": {
    "population_over_40_2015": 130000000,
    "expected_severe_exacerbations_per_year": 700000,
    "expected_severe_exac_tolerance": 35000
  }
}

For some studies, having access to the entire event history of the simulated population might be beneficial. Capturing event history is possible by using the return_events parameter in simulate():

# Get events along with other results
results <- simulate(
  n_agents = 10000,
  return_events = TRUE
)

# Access the events
events <- results$events
head(events)

Alternatively, you can set record_mode as a setting:

settings <- get_default_settings()
settings$record_mode <- 2
settings$n_base_agents <- 1e4
results <- simulate(settings = settings, return_events = TRUE)
events <- results$events

Note that you might need a large amount of memory available, if you want to collect event history for a large number of patients.

In the events data frame, each type of event has a code corresponding to the table below:

EventNo.
start0
annual1
birthday2
smoking change3
COPD incidence4
Exacerbation5
Exacerbation end6
Death by Exacerbation7
Doctor visit8
Medication change9
Background death13
End14

Closed-cohort analysis

Closed-cohort analysis can be specified by changing the appropriate input parameters.

library(epicR)

# Simple closed-cohort analysis
input <- get_input(closed_cohort = 1)$values
results <- simulate(input = input)

# You can also combine jurisdiction and closed-cohort parameters:
# Canadian closed-cohort analysis
input <- get_input(jurisdiction = "canada", closed_cohort = 1)$values
results <- simulate(input = input)

# US closed-cohort analysis
input <- get_input(jurisdiction = "us", closed_cohort = 1)$values
results <- simulate(input = input)

Publications with epicR

The following publications have used epicR for their analyses:

Yan, K., Duan, K. and Johnson, K.M., 2025. Development and Validation of EPIC-USA: A COPD Policy Model for the United States. American Journal of Respiratory and Critical Care Medicine, 211(Abstracts), pp.A7130-A7130.

Mountain, R., Duan, K.I. and Johnson, K.M., 2024. Benefit–harm analysis of earlier initiation of triple therapy for prevention of acute exacerbation in patients with chronic obstructive pulmonary disease. Annals of the American Thoracic Society, 21(8), pp.1139-1146.

Mountain, R., Kim, D. and Johnson, K.M., 2023. Budget impact analysis of adopting primary care–based case detection of chronic obstructive pulmonary disease in the Canadian general population. Canadian Medical Association Open Access Journal, 11(6), pp.E1048-E1058.

Ahmadian, S., Johnson, K.M., Ho, J.K., Sin, D.D., Lynd, L.D., Harrison, M. and Sadatsafavi, M., 2023. A cost-effectiveness analysis of azithromycin for the prevention of acute exacerbations of chronic obstructive pulmonary disease. Annals of the American Thoracic Society, 20(12), pp.1735-1742.

Johnson KM, Sadatsafavi M, Adibi A, Lynd L, Harrison M, Tavakoli H, Sin DD, Bryan S. Cost effectiveness of case detection strategies for the early detection of COPD. Applied Health Economics and Health Policy. 2021 Mar;19(2):203-15. https://doi.org/10.1007/s40258-020-00616-2

Citation

Please cite:

Sadatsafavi, M., Ghanbarian, S., Adibi, A., Johnson, K., Mark FitzGerald, J., Flanagan, W., Sin, D. (2019). Development and Validation of the Evaluation Platform in COPD (EPIC): A Population-Based Outcomes Model of COPD for Canada. Medical Decision Making. https://doi.org/10.1177/0272989X18824098

Copy Link

Version

Install

install.packages('epicR')

Monthly Downloads

273

Version

1.0.2

License

GPL-3

Maintainer

Kate Johnson

Last Published

September 9th, 2026

Functions in epicR (1.0.2)

get_errors

Returns errors
get_output_ex

Extra outputs from the model
get_runtime_stats

Returns run time stats.
get_smith

Returns agent Smith.
get_sample_output

Returns a sample output for a given year and gender.
get_settings

Returns current settings.
validate_config

Validate a config file
validate_diagnosis

Returns results of validation tests for diagnosis
get_progress

Returns current simulation progress.
validate_gpvisits

Returns results of validation tests for GP visits
validate_exacerbation

Returns results of validation tests for exacerbation rates
get_user_config_dir

Get user config directory path
user_config_exists

Check if user config exists
init_session

Initializes a model. Allocates memory to the C engine.
validate_COPD

Returns results of validation tests for COPD
get_agent_events

Returns all events of an agent.
set_settings_var

Sets model settings.
list_available_jurisdictions

List available config jurisdictions
mvrnormArma

Samples from a multivariate normal
report_exacerbation_by_time

Reports exacerbation-related stats.
report_COPD_by_ctime

Reports COPD related stats.
validate_smoking

Returns results of validation tests for smoking module.
sanity_check

Basic tests of model functionality. Serious issues if the test does not pass.
get_n_events

Returns total number of events.
validate_survival

Returns the Kaplan Meier curve comparing COPD and non-COPD
get_event

Returns the events stack.
reset_model_input

Reset the cached model input
get_events_by_type

Returns all events of a certain type.
get_input

Returns a list of default model input values
reset_user_configs

Reset user configs to package defaults
resume

Resumes running of model.
sanity_COPD

Basic COPD test.
get_output

Main outputs of the current run.
set_input_var

Sets input variables.
validate_symptoms

Returns results of validation tests for Symptoms
validate_treatment

Returns results of validation tests for Treatment
validate_mortality

Returns results of validation tests for mortality rate
validate_overdiagnosis

Returns results of validation tests for overdiagnosis
validate_lung_function

Returns results of validation tests for lung function
terminate_session

Terminates a session and releases allocated memory.
simulate

Convenience function: run simulation and return results
validate_medication

Returns results of validation tests for medication module.
validate_payoffs

Returns results of validation tests for payoffs, costs and QALYs
test_case_detection

Returns results of Case Detection strategies
validate_population

Returns simulated vs. predicted population table and a plot
calibrate_explicit_mortality2

Calibrates explicit mortality by Amin
calibrate_smoking

Calibrates smoking
epicR-config

epicR Configuration Management
calc_event_stack_size

Calculate recommended event_stack_size for a given number of agents
export_figures

Runs the model and exports an excel file with all output data
calibrate_explicit_mortality

Calibrates explicit mortality
calibrate_COPD_inc

Solves stochastically for COPD incidence rate equation.
estimate_memory_required

Estimate memory required for simulation
express_matrix

Express matrix.
copy_configs_to_user

Copy default configs to user directory
get_all_events_matrix

Returns a matrix containing all events
epicR-package

epicR package
get_agent_size_bytes

Returns the size of agent struct in bytes
get_all_events

Returns all events.
get_available_memory

Get available system memory (platform-specific)
get_default_model_input

Get the default model input (lazy initialization)
get_default_settings

Exports default settings
get_inputs

Returns inputs
get_list_elements

Get list elements