A hands-on tutorial on real-world evidence (RWE) analysis in R and SQL, built on synthetic electronic health record data.
Who is it for? Anyone interested in RWE analysis: programmers and data analysts who want to explore this field, biostatisticians who want to strengthen their programming skills, and epidemiologists who want to understand the data and programming aspects and nuances of observational database studies. It is also a good starting point for students and graduates of life-science and related fields who want to break into clinical data science or the wider health data analytics industry, or who are simply looking for inspiration and a direction to grow in.
The data. The records come from Synthea and were then modified for teaching purposes so that the dataset supports reproducing the analysis of a published study.
The analysis. The tutorial follows a typical industry workflow for RWE database studies, from raw data to publication-ready tables, listings and figures (TLFs): build a cohort, apply inclusion and exclusion criteria, identify exposure, derive patient demographic and clinical characteristics, define an outcome variable, and reproduce publication outputs. The worked example is inspired by Incidence, risk factors, and outcomes of respiratory related hospitalizations in older patients with COPD, asthma, and bronchiectasis (PMC12216934). It is a simplified teaching reconstruction of selected parts of the study's results, not a replication of the study.
The working environment. Analyses are written in SQL and run from R, the way real-world data are typically stored and queried in industry: in a relational database rather than in flat files in memory. You can use either:
- a self-hosted PostgreSQL database on Aiven (free plan), loaded with the provided script, or
- a local DuckDB database that reads the CSV files directly, with no account or setup.
Both paths use the same data and the same cohort logic, so you can start locally and move to a server database later.
All data are synthetic. No real patient is represented, and results must not be interpreted clinically.
rwe-tutorial/
|-- load_to_aiven.R # loads the CSV files into the Aiven PostgreSQL database
|-- raw_data/ # CSV files (download from the Releases page, see below)
|-- instruction/ # setup and loading instructions
|-- analysis_files/ # analysis scripts
| |-- 01_cohort.R # cohort and outcome on Aiven (SQL via ODBC)
| `-- 01_cohort_csv.R # the same analysis on the CSV files (DuckDB)
`-- output/ # reference and user-generated outputs
- Download
rwe-tutorial-v1.0.zipfrom the Releases page and unzip it. It contains the whole project, including the CSV files inraw_data/, so it is ready to use. (If you prefergit clone, also unzip the data intoraw_data/- the CSVs are not in git because some exceed GitHub's 100 MB file limit.) - Follow instruction/AIVEN_TUTORIAL.md to create a free Aiven database and load the data.
- Open
analysis_files/01_cohort.Rin RStudio and run it step by step.
To work without a database, run analysis_files/01_cohort_csv.R. It needs the
DBI, duckdb and glue R packages.
-
Code (R scripts, SQL) is licensed under the PolyForm Noncommercial License 1.0.0.
-
Documentation and the synthetic data are licensed under Creative Commons Attribution-NonCommercial 4.0 International.
-
You may copy, adapt and redistribute this material for noncommercial purposes (e.g. personal learning, teaching, research), provided that you keep the copyright notice and give appropriate credit, for example:
Based on "RWE tutorial" by Tomasz Mikołajczyk, https://github.com/tommi017/rwe-tutorial, licensed under PolyForm Noncommercial 1.0.0 / CC BY-NC 4.0.
-
Commercial use requires a separate license. Please contact me via GitHub.
-
This is a source-available license, not an OSI-approved open source license.
- The synthetic patient records were generated with Synthea (Apache License 2.0) and then curated and adjusted for teaching purposes.
icd10cm_dictionary.csvandatc_dictionary.csv, as well as theatc_code*columns, are based on the ICD-10-CM, WHO ATC and RxNorm vocabularies obtained through OHDSI Athena. These remain subject to the terms of their owners (e.g. WHO Collaborating Centre for Drug Statistics Methodology, NLM RxNorm, CDC/NCHS for ICD-10-CM), and the licenses above do not apply to them.