This repository provides the source code for the paper
"Spatial and socioeconomic inequalities in hospital utilization"
This repository contains a collection of Python scripts for simulating, processing, and analyzing synthetic hospital visit trajectories.
Due to the strict regulations for personal privacy protection, all individual level mobility data cannot be accessed by the public. To make the code more accessible despite data sharing restrictions, we provide an example synthetic trajectory dataset provided by the data vendor (see ./data/trajectories) that replicates the data structure and variable schema of the original de-identified mobile phone data, including anonymized user identifiers, longitude, latitude, and timestamps.
Using this synthetic dataset, users can run a complete, end-to-end demo workflow (Step 1–Step 3, corresponding to Step1.hospital_visits_identification.py through Step3.family_accompany_identification.py), including hospital visit identification and patient filtering, without access to restricted data. On a typical desktop computer, the full demo (Step 1–Step 3) is expected to complete within approximately 3–5 minutes. This runnable demo enables researchers with access to comparable mobile phone datasets to adapt the pipeline to their own data, while allowing others to fully understand the expected input format, intermediate outputs, and analytical logic of the framework.
All datasets included in this repository are fully synthetic and generated solely for demonstration and reproducibility purposes. No real individuals, locations, or mobility trajectories are represented.
Python 3.12 is required for this project. All the required packages are listed in requirements.txt.
Below is a concise and standardized description of each script's purpose.
Processes large-scale synthetic trajectory data to extract potential single-day hospital visits. Key steps include:
- Chunk-based loading of raw Parquet trajectory files
- Coordinate cleaning and bounding-box filtering
- Spatial matching with hospital polygons
- Identification of potential hospital visits based on residence duration
This script is for demonstration only and all data used in this step are synthetic data.
Identifies and removes synthetic “hospital staff” from daily trajectory records based on long-term appearance frequency. Workflow:
- Load multiple
single_day_patients_*.csvfiles - Flag users appearing ≥ N days as staff
- Save staff-ID list
- Output cleaned daily patient files
All data used in this step are also simulated data.
Identifies family companions or accompanying persons among patients through spatiotemporal analysis. Implements a comprehensive multi-stage processing pipeline:
Core Processing Steps:
- Hospital Contact Detection - Identifies patient pairs with contact at hospitals based on spatiotemporal overlap analysis
- Residence Contact Detection - Detects patient pairs with contact at their residences based on geographic distance calculations
- ABID Pair Matching - Matches patient pairs that satisfy both hospital contact and residence contact conditions
- Trajectory Record Extraction - Extracts complete spatiotemporal trajectory records of matched patient pairs
- Journey Contact Detection - Calculates patient contact situations outside hospitals and residential areas
- Companion Ratio Calculation - Builds patient contact network graphs and classifies patient and companion roles
The specific functionality implementation of this step is located in src/accompany_identification_core.
All data used in this step are also simulated data.
Computes statistics and generates the visualizations used in Figure 1 of the study. Includes:
- Preprocessing
- Extra travel ratio calculations
- Visualizing travel distance, extra distance, and bypass rate for cities and city groups
Computes summary tables and visualization for Figure 2. Includes:
- Summary CSVs for NNHI, extra travel distance, and bypass rate
- Subsets for top-tier and high-reputation hospitals
- Scatterplots (with log-fit) and bar charts
Generates Figure 3, including:
- Dual bar plots of bypass rate and NNHI by SES groups
- Lorenz curves
- C-index (CI) calculations for road distance, NNHI, and bypass
Computes:
- Experienced Segregation (ES)
- Income Index (II / ESC in manuscript)
Generates Figure 4:
- Boxplots of ES and income index across bypass groups and hospital accessibility levels
Generates Figure 5, including:
- Error-bar panels for hospital grade, capacity, reputation, and distance coefficients across SES groups
- Line charts of willingness-to-travel (WTT) vs option distance by hospital grade, capacity, and reputation
- Heatmaps showing distance vs hospital grade proportions, stratified by Overall, High-SES, and Low-SES groups
Computes the Nearest N Hospitals Index (NNHI) and associated road-network distances. Includes:
- NNHI calculations
- Road-network distance computation via OSMnx
Written for academic replication; does not require actual road networks or city datasets to run.
Performs sensitivity analysis for hospital bypass behavior and SES disparities. Includes:
- Sensitivity testing using alternative bypass metrics
- Sensitivity testing for patient identification thresholds Outputs:
- CSV tables for robustness checks
- City-level and group-level summary results
Performs mixed logit–based discrete choice modeling to analyze hospital bypass behavior.
Includes:
- Estimation of trade-offs between hospital quality and travel distance
- Evaluation of systematic preference differences across SES groups
- Full workflow and methodological details (see METHODS.md)
Outputs:
- Estimated model parameters
- Overall-sample results (no SES grouping)
- SES-stratified results (High-SES, Middle-SES, Low-SES)
Files:
- step1_process.py – Data preprocessing for constructing patient–hospital choice sets
- step2_mixlogit.py – Mixed Logit model estimation
- MixedLogit_without_interaction_no_group – Results for the overall sample
- MixedLogit_without_interaction_SES_group – Results for SES groups
We also provide an additional documentation file (METHODS.md) that describes the analytical methods used in this project in detail. The document includes:
- The algorithm for computing the Nth Nearest Hospital Index (NNHI)
- The procedure for calculating hospital accessibility using the Enhanced Two-Step Floating Catchment Area (E2SFCA) method
- The methodology for estimating the experienced segregation (ES) index These methodological explanations are intended to support reproducibility, transparency, and academic use of the accompanying scripts.
Install the required dependencies using:
pip install -r requirements.txtThe main dependencies include:
- pandas==2.3.3
- numpy==2.3.4
- geopandas==1.1.1
- haversine==2.9.0
- osmnx==2.0.7
- shapely==2.1.2
- seaborn==0.12.2
- matplotlib==3.9.0
- scipy==1.16.3
- xlogit==0.2.7
- cupy-cuda12x==13.6.0
- polars==1.31.0
- scikit-learn==1.7.2
- networkx==3.6.1
- tqdm==4.67.1