#!/usr/bin/env python3 """ Literature-Informed Healthcare Worker Workforce & Retention Dataset ==================================================================== Generates realistic synthetic records of healthcare workers in sub-Saharan Africa, including cadre, workload, burnout, retention, migration intentions, and facility context. References (web-searched): ----------- [1] McKinsey 2024. SSA HCW shortage could be 93% larger. Disease burden, productivity, resources. [2] ScienceDirect 2025. HCW migration mitigation Africa. Brain drain, push/pull factors. [3] PMC 2025. Concurrent skilled HCW shortage and surplus. WHO Health Workforce Support List 2021. [4] BMJ GH 2022. Projected HWF requirements 2023-2030. Weak planning, constrained fiscal space. [5] PMC 2025. HWF shortage addressing health needs SSA. Attrition, retirement, training gaps. """ import numpy as np import pandas as pd import argparse import os SCENARIOS = { 'urban_tertiary': { 'description': 'Urban tertiary hospital with specialist ' 'doctors, nurses, pharmacists, lab techs ' '(e.g., Kenyatta, Muhimbili, LUTH)', 'doctor_ratio': 0.15, 'nurse_ratio': 0.45, 'specialist_available': True, 'burnout_mod': 1.0, 'migration_mod': 1.0, 'vacancy_rate': 0.25, }, 'district_hospital': { 'description': 'District hospital with clinical officers, ' 'nurses, limited doctors, task shifting ' '(e.g., district hospitals Malawi, Uganda)', 'doctor_ratio': 0.05, 'nurse_ratio': 0.50, 'specialist_available': False, 'burnout_mod': 1.3, 'migration_mod': 0.8, 'vacancy_rate': 0.40, }, 'rural_health_centre': { 'description': 'Rural health centre with nurses, CHWs, ' 'no doctors, severe understaffing ' '(e.g., rural DRC, Niger, South Sudan)', 'doctor_ratio': 0.01, 'nurse_ratio': 0.35, 'specialist_available': False, 'burnout_mod': 1.5, 'migration_mod': 0.5, 'vacancy_rate': 0.60, }, } def generate_dataset(n=10000, seed=42, scenario='district_hospital'): rng = np.random.default_rng(seed) sc = SCENARIOS[scenario] records = [] for idx in range(n): rec = {'id': idx + 1} # ── 1. Demographics ── rec['age'] = max(20, min(65, int(rng.normal(35, 9)))) rec['sex'] = rng.choice(['M', 'F'], p=[0.35, 0.65]) rec['marital_status'] = rng.choice( ['married', 'single', 'divorced_widowed'], p=[0.55, 0.35, 0.10]) rec['children'] = max(0, min(8, int(rng.exponential(1.5)))) # ── 2. Professional profile ── cadre_probs = np.array([sc['doctor_ratio'], sc['nurse_ratio'], 0.10, 0.08, 0.05, 0.05, 1.0 - sc['doctor_ratio'] - sc['nurse_ratio'] - 0.28]) cadre_probs = np.maximum(cadre_probs, 0.01) cadre_probs /= cadre_probs.sum() rec['cadre'] = rng.choice( ['doctor', 'nurse_midwife', 'clinical_officer', 'lab_technician', 'pharmacist', 'chw', 'other'], p=cadre_probs) rec['qualification'] = 'certificate' if rec['cadre'] == 'doctor': rec['qualification'] = rng.choice(['mbbs', 'specialist'], p=[0.70, 0.30]) elif rec['cadre'] == 'nurse_midwife': rec['qualification'] = rng.choice( ['certificate', 'diploma', 'degree'], p=[0.30, 0.45, 0.25]) elif rec['cadre'] == 'clinical_officer': rec['qualification'] = rng.choice(['diploma', 'degree'], p=[0.70, 0.30]) rec['years_experience'] = max(0, min(40, int(rng.exponential(7)))) rec['years_at_current_facility'] = max(0, min(rec['years_experience'], int(rng.exponential(4)))) rec['task_shifting'] = 0 if rec['cadre'] in ('nurse_midwife', 'clinical_officer', 'chw'): rec['task_shifting'] = 1 if rng.random() < 0.45 else 0 rec['continuing_education_past_year'] = 1 if rng.random() < 0.30 else 0 # ── 3. Workload ── rec['patients_per_day'] = max(5, min(200, int(rng.exponential(25) + 10))) rec['hours_per_week'] = max(20, min(80, int(rng.normal(50, 12)))) rec['overtime_hours_week'] = max(0, rec['hours_per_week'] - 40) rec['night_shifts_per_month'] = max(0, min(20, int(rng.exponential(4)))) rec['staffing_adequate'] = 1 if rng.random() < (1 - sc['vacancy_rate']) else 0 rec['essential_medicines_available'] = 1 if rng.random() < 0.50 else 0 rec['equipment_functional'] = 1 if rng.random() < 0.40 else 0 # ── 4. Burnout & wellbeing [1] ── burnout_score = rng.normal(50 * sc['burnout_mod'], 15) if rec['overtime_hours_week'] > 10: burnout_score += 10 if not rec['staffing_adequate']: burnout_score += 10 rec['burnout_score'] = max(0, min(100, int(burnout_score))) rec['burnout_level'] = 'low' if rec['burnout_score'] >= 70: rec['burnout_level'] = 'high' elif rec['burnout_score'] >= 50: rec['burnout_level'] = 'moderate' rec['emotional_exhaustion'] = 1 if rec['burnout_score'] >= 60 else 0 rec['depersonalisation'] = 1 if rec['burnout_score'] >= 70 and rng.random() < 0.50 else 0 rec['personal_accomplishment_low'] = 1 if rec['burnout_score'] >= 55 and rng.random() < 0.40 else 0 rec['depression_symptoms'] = 1 if rng.random() < 0.25 else 0 rec['anxiety_symptoms'] = 1 if rng.random() < 0.30 else 0 # ── 5. Satisfaction & retention [2][3] ── rec['salary_satisfaction'] = rng.choice( ['satisfied', 'neutral', 'dissatisfied'], p=[0.15, 0.30, 0.55]) rec['monthly_salary_usd'] = max(50, min(5000, int(rng.exponential(300) + 100))) rec['salary_paid_on_time'] = 1 if rng.random() < 0.60 else 0 rec['housing_provided'] = 1 if rng.random() < 0.25 else 0 rec['career_progression_opportunities'] = 1 if rng.random() < 0.20 else 0 rec['supervision_received'] = 1 if rng.random() < 0.35 else 0 rec['job_satisfaction'] = rng.choice( ['high', 'moderate', 'low'], p=[0.20, 0.35, 0.45]) rec['intention_to_leave'] = 0 leave_prob = 0.25 if rec['burnout_level'] == 'high': leave_prob += 0.20 if rec['salary_satisfaction'] == 'dissatisfied': leave_prob += 0.15 if not rec['career_progression_opportunities']: leave_prob += 0.05 rec['intention_to_leave'] = 1 if rng.random() < min(leave_prob, 0.70) else 0 rec['intention_to_migrate'] = 0 mig_prob = 0.15 * sc['migration_mod'] if rec['cadre'] == 'doctor': mig_prob *= 2 if rec['qualification'] in ('degree', 'specialist', 'mbbs'): mig_prob *= 1.5 if rec['age'] < 35: mig_prob *= 1.3 rec['intention_to_migrate'] = 1 if rng.random() < min(mig_prob, 0.50) else 0 rec['migration_destination'] = 'none' if rec['intention_to_migrate']: rec['migration_destination'] = rng.choice( ['uk', 'usa', 'middle_east', 'south_africa', 'other_africa', 'europe'], p=[0.25, 0.15, 0.20, 0.15, 0.10, 0.15]) rec['reason_to_leave'] = 'none' if rec['intention_to_leave']: rec['reason_to_leave'] = rng.choice( ['low_salary', 'burnout', 'poor_conditions', 'career', 'family', 'insecurity', 'other'], p=[0.30, 0.20, 0.15, 0.10, 0.10, 0.08, 0.07]) # ── 6. Safety & violence ── rec['workplace_violence_past_year'] = 1 if rng.random() < 0.20 else 0 rec['needlestick_injury_past_year'] = 1 if rng.random() < 0.15 else 0 rec['covid_infected'] = 1 if rng.random() < 0.10 else 0 rec['ppe_adequate'] = 1 if rng.random() < 0.35 else 0 # ── 7. Absenteeism ── rec['sick_days_past_year'] = max(0, min(60, int(rng.exponential(5)))) rec['absenteeism_rate'] = round(rec['sick_days_past_year'] / 260, 3) records.append(rec) df = pd.DataFrame(records) print(f"\n{'='*65}") print(f"HCW Workforce — {scenario} (n={n}, seed={seed})") print(f"{'='*65}") print(f"\n Burnout high: {(df['burnout_level']=='high').mean()*100:.1f}%") print(f" Intention to leave: {df['intention_to_leave'].mean()*100:.1f}%") print(f" Intention to migrate: {df['intention_to_migrate'].mean()*100:.1f}%") print(f" Vacancy rate: {(1-df['staffing_adequate'].mean())*100:.1f}%") print(f" Salary dissatisfied: {(df['salary_satisfaction']=='dissatisfied').mean()*100:.1f}%") return df if __name__ == '__main__': parser = argparse.ArgumentParser( description='Generate HCW workforce dataset') parser.add_argument('--scenario', type=str, default='district_hospital', choices=list(SCENARIOS.keys())) parser.add_argument('--n', type=int, default=10000) parser.add_argument('--seed', type=int, default=42) parser.add_argument('--output', type=str, default=None) parser.add_argument('--all-scenarios', action='store_true') args = parser.parse_args() os.makedirs('data', exist_ok=True) if args.all_scenarios: for sc_name in SCENARIOS: df = generate_dataset(n=args.n, seed=args.seed, scenario=sc_name) out = os.path.join('data', f'hcw_{sc_name}.csv') df.to_csv(out, index=False) print(f" -> Saved to {out}\n") else: df = generate_dataset(n=args.n, seed=args.seed, scenario=args.scenario) out = args.output or os.path.join('data', f'hcw_{args.scenario}.csv') df.to_csv(out, index=False) print(f" -> Saved to {out}")