AgentStack
SKILL verified MIT-0 Self-run

Rwd Cohort Analysis

skill-awslabs-hcls-agent-skills-rwd-cohort-analysis · by awslabs

Real-world data cohort analysis pipeline for claims and EHR data — cohort identification with ICD-10, NDC, and CPT codes, medication adherence metrics (PDC/MPR), propensity score estimation and balance diagnostics, Kaplan-Meier survival curves, and Cox proportional hazards models. Use when the user mentions claims data, cohort identification, ICD-10 codes, NDC codes, CPT codes, medication adheren…

No reviews yet
0 installs
0 views
view→install

Install

$ agentstack add skill-awslabs-hcls-agent-skills-rwd-cohort-analysis

✓ scanned · ✓ verified — works with Claude Code, Cursor, and more.

Security review

✓ Passed

No issues found. Passed automated security review. · v0.1.0 How review works →

  • Prompt-injection patterns
  • Secret / credential exfiltration
  • Dangerous shell & filesystem operations
  • Untrusted network calls
  • Known-malicious package signatures

What it can access

  • Network access No
  • Filesystem access No
  • Shell / process execution No
  • Environment & secrets No
  • Dynamic code execution No

From automated source analysis of v0.1.0. “Used” means the capability is present in the source — more access means more to trust, not that it’s unsafe.

Are you the author of Rwd Cohort Analysis? Claim this listing to set pricing, connect Stripe payouts, and keep 70% of every sale.
Sign up to claim

About

Real-World Data Cohort Analysis Pipeline

Overview

This skill encodes a reproducible pipeline for analyzing administrative claims or EHR data to estimate comparative treatment effects. It covers the full workflow from raw claims tables to adjusted survival estimates:

  1. Cohort identification using diagnosis (ICD-10-CM), procedure (CPT/HCPCS),

and drug (NDC) code lists

  1. Medication adherence measurement (PDC and MPR)
  2. Propensity score estimation and balance diagnostics
  3. Kaplan-Meier curves and Cox proportional hazards regression

Code snippets use Python (pandas, scikit-learn, lifelines) and R (MatchIt, cobalt, survival, survminer). The user is responsible for data access, IRB/privacy compliance, and clinical code-list validation.

Out of scope: data extraction from source systems, natural language processing of clinical notes, interrupted time series, instrumental variable analyses.

Usage

Invoke when the user asks to:

  • Build a cohort from claims or EHR data using diagnosis/procedure/drug codes
  • Calculate PDC or MPR for medication adherence
  • Estimate propensity scores and check covariate balance
  • Run Kaplan-Meier or Cox regression on a claims-derived cohort
  • Assess the proportional hazards assumption

The skill emits exact code. The user supplies the data and code lists.

Response Format

  • Lead with the command or code the user needs — explain after
  • Structure as: confirm inputs → working code → key parameters explained → gotchas
  • One complete working example per task; do not show every alternative
  • Keep code comments minimal and functional (what, not why-it-exists)
  • Target: 50-100 lines of code with brief surrounding explanation

Core Concepts

Data model assumptions

The pipeline expects these tables (column names are illustrative):

| Table | Key columns | |---|---| | enrollment | patient_id, enroll_start, enroll_end | | diagnoses | patient_id, dx_date, icd10_code, dx_position | | procedures | patient_id, proc_date, cpt_code | | pharmacy | patient_id, fill_date, ndc_code, days_supply, quantity | | demographics | patient_id, birth_date, sex, race |

1. Cohort Identification

Define code lists
# ICD-10-CM codes for type 2 diabetes (example)
T2D_ICD10 = ["E11.0", "E11.1", "E11.2", "E11.3", "E11.4",
             "E11.5", "E11.6", "E11.8", "E11.9"]

# NDC codes for Drug A (user supplies actual NDCs)
DRUG_A_NDC = ["12345678901", "12345678902"]
DRUG_B_NDC = ["98765432101", "98765432102"]

# CPT codes for outcome procedure (e.g., amputation)
OUTCOME_CPT = ["27590", "27591", "27592"]
Identify new users with washout
import pandas as pd

def identify_new_users(pharmacy, ndc_list, washout_days=180):
    """Identify incident users: first fill after washout with no prior fills."""
    drug_fills = pharmacy[pharmacy["ndc_code"].isin(ndc_list)].copy()
    drug_fills = drug_fills.sort_values(["patient_id", "fill_date"])

    # First-ever fill
    first_fill = drug_fills.groupby("patient_id")["fill_date"].min().reset_index()
    first_fill.columns = ["patient_id", "index_date"]

    # Require continuous enrollment for washout_days before index
    cohort = first_fill.merge(enrollment, on="patient_id")
    cohort["washout_start"] = cohort["index_date"] - pd.Timedelta(days=washout_days)
    cohort = cohort[cohort["enroll_start"] = cohort["washout_start"]) &
        (cohort["fill_date"] = min_age]

    # Required diagnosis in pre-index window
    if required_dx_codes:
        pre_dx = diagnoses[diagnoses["icd10_code"].isin(required_dx_codes)]
        pre_dx = pre_dx.merge(cohort[["patient_id", "index_date"]], on="patient_id")
        pre_dx = pre_dx[
            (pre_dx["dx_date"] >= pre_dx["index_date"] - pd.Timedelta(days=pre_index_window)) &
            (pre_dx["dx_date"] = min_followup
    ]
    return cohort

2. Medication Adherence

PDC (Proportion of Days Covered)

PDC is the preferred measure (endorsed by PQA). It counts the number of days in the measurement period covered by at least one fill, capped at 1.0.

def calculate_pdc(pharmacy, cohort, ndc_list, period_days=365):
    """Calculate PDC for each patient over a fixed measurement period."""
    fills = pharmacy[pharmacy["ndc_code"].isin(ndc_list)].copy()
    fills = fills.merge(cohort[["patient_id", "index_date"]], on="patient_id")
    fills["period_end"] = fills["index_date"] + pd.Timedelta(days=period_days)

    # Keep fills within measurement period
    fills = fills[fills["fill_date"] = fills["index_date"]) &
                  (fills["fill_date"]  0 else 0
        results.append({"covariate": col, "smd": round(smd, 4),
                        "balanced": smd  0.05 | PH assumption holds; proceed |
| Covariate p < 0.05 | Stratify on that covariate or add time-interaction term |
| Global p < 0.05 | Consider restricted mean survival time (RMST) or parametric AFT model |

### Pipeline summary
  1. Define code lists (ICD-10, NDC, CPT)
  2. Identify new users with washout → cohort table
  3. Apply inclusion/exclusion criteria → filtered cohort
  4. Build baseline covariate matrix → pre-index features
  5. Estimate propensity scores → ps column
  6. Match or weight (IPTW) → balanced cohort
  7. Check balance (SMD < 0.1) → Love plot / SMD table
  8. Calculate adherence (PDC/MPR) → adherence metrics
  9. Define outcome + follow-up → duration + event columns
  10. Kaplan-Meier curves → survival plot
  11. Cox PH model (adjusted or weighted) → hazard ratios + CIs
  12. Check PH assumption (Schoenfeld) → assumption diagnostics

## Common Mistakes

- **Wrong:** Using MPR instead of PDC for medication adherence
  **Right:** Use PDC (Proportion of Days Covered) as the adherence metric
  **Why:** MPR double-counts overlapping fills, can exceed 1.0, and is not endorsed by PQA

- **Wrong:** Not requiring continuous enrollment during the study period
  **Right:** Verify continuous enrollment for both washout and follow-up windows
  **Why:** Gaps in enrollment create unobservable periods where events and fills are missed

- **Wrong:** Omitting a washout period before the index date
  **Right:** Require a clean washout (typically 180 days) with no prior fills of the study drug
  **Why:** Without washout, prevalent users contaminate the new-user cohort and bias results

- **Wrong:** Including post-index covariates in the propensity score model
  **Right:** Only use pre-index (baseline) variables in the PS model
  **Why:** Post-index variables may be affected by treatment, introducing collider bias

- **Wrong:** Using p-values to assess covariate balance after matching/weighting
  **Right:** Use standardized mean differences (SMD < 0.1) for all covariates
  **Why:** P-values conflate balance with sample size and are uninformative for this purpose

- **Wrong:** Not checking propensity score overlap between treatment groups
  **Right:** Plot PS distributions for both groups and trim non-overlapping regions
  **Why:** Estimates in non-overlapping regions are extrapolated and unreliable

- **Wrong:** Reporting Cox model results without testing the proportional hazards assumption
  **Right:** Always run Schoenfeld residual tests and inspect plots for time trends
  **Why:** Violated PH produces misleading hazard ratios; consider RMST or stratification instead

- **Wrong:** Using naive standard errors with IPTW-weighted analyses
  **Right:** Use robust (sandwich) variance estimators when fitting weighted models
  **Why:** Naive SEs underestimate uncertainty because they ignore the weight estimation step

- **Wrong:** Truncating IPTW weights too aggressively (e.g., at 5th/95th percentile)
  **Right:** Truncate conservatively (1st/99th) and report sensitivity across thresholds
  **Why:** Over-truncation reintroduces confounding by effectively unweighting key observations

- **Wrong:** Defining follow-up end as a fixed calendar date for all patients
  **Right:** End follow-up at the earliest of: event, disenrollment, end of study, or death
  **Why:** Incorrect censoring biases survival estimates and violates non-informative censoring assumptions

## References

- Hernán MA, Robins JM. Causal Inference: What If. Chapman & Hall/CRC 2020, https://www.hsph.harvard.edu/miguel-hernan/causal-inference-book/
- Austin PC. An introduction to propensity score methods. Multivariate Behav Res 2011, https://doi.org/10.1080/00273171.2011.568786
- Pharmacy Quality Alliance. PDC measure specifications, https://www.pqaalliance.org/
- Davidson-Pilon C. lifelines documentation, https://lifelines.readthedocs.io/
- Ho DE et al. MatchIt: nonparametric preprocessing for parametric causal inference. J Stat Softw 2011, https://doi.org/10.18637/jss.v042.i08
- Therneau TM, Grambsch PM. Modeling Survival Data. Springer 2000

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [awslabs](https://github.com/awslabs)
- **Source:** [awslabs/hcls-agent-skills](https://github.com/awslabs/hcls-agent-skills)
- **License:** MIT-0

Install and usage instructions live in the source repository linked above.

Reviews

No reviews yet — be the first.

Versions

  • v0.1.0 Imported from the upstream source.