simulate_ehr_data.R

simulate_ehr_data.R generates synthetic EHR BMI measurements and demographic records that can be used to practice the 8-step GenAI coding workflow.

Purpose

The script creates tab-delimited synthetic data with realistic issues such as missing values, implausible measurements, and separate data dictionaries. This gives readers a safe example dataset for prompt engineering, code review, data cleaning, and documentation exercises.

Setup

Install R and prepare its dependencies before running the script. For a plain R project, install optparse explicitly:

Rscript -e 'install.packages("optparse", repos = "https://cloud.r-project.org")'
Rscript scripts/simulate_ehr_data.R --help

If you are using an existing project with renv.lock, restore that project’s environment with renv::restore() from its root instead. The simulator checks for missing packages and stops with setup instructions; it does not install packages during execution. See Optional R Setup.

Example Usage

Rscript scripts/simulate_ehr_data.R \
  --output_ehr "./data/raw/ehr_bmi_simulated_data.tsv" \
  --output_ehr_dict "./data/raw/data_dictionary.txt" \
  --output_demo "./data/raw/demographics_simulated_data.tsv" \
  --output_demo_dict "./data/raw/demographics_data_dictionary.txt" \
  --seed 123 \
  --n_individuals 1000

Outputs

  • Synthetic EHR BMI data in TSV format.
  • An EHR data dictionary.
  • Synthetic demographic data in TSV format.
  • A demographic data dictionary.

The demographic age is a snapshot as of December 31, 2019. Measurements span earlier dates and can include people younger than 20 at measurement. The advanced BMI cleaning plan therefore restricts its exercise to age 20 or older at measurement, calculated from date_of_birth and measurement_date; filtering on the demographic snapshot age alone is insufficient.

erDiagram
    accTitle: Synthetic EHR output relationships
    accDescr: An entity relationship diagram showing that each demographic record can have many synthetic EHR measurement records linked by person_id.
    DEMOGRAPHICS ||--o{ EHR_MEASUREMENT : has
    DEMOGRAPHICS {
        string person_id PK
        date date_of_birth
        int age
        string age_bin
        string race_ethnicity_harmonized
        string sex_gender
        string zip3
    }
    EHR_MEASUREMENT {
        string encounter_id PK
        string person_id FK
        float bmi
        float height_cm
        float weight_kg
        datetime measurement_date
    }