Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Longitudinal Bilirubin Trajectories Using Linear Mixed-Effects Models

Clinical Longitudinal Modeling with Python

This project demonstrates a reproducible clinical-statistical workflow for analyzing repeated serum bilirubin measurements among patients with primary biliary cirrhosis.

The analysis addresses a clinically meaningful question:

How do serum bilirubin levels change during follow-up, and do baseline patient characteristics predict meaningful differences in those longitudinal trajectories?

The project emphasizes:

  • transparent longitudinal data auditing,
  • clinically interpretable model selection,
  • patient-specific trajectory heterogeneity,
  • adjusted reference-profile predictions,
  • diagnostic assessment,
  • sensitivity analysis,
  • and reproducible repository outputs.

Dataset

The analysis uses the publicly available Mayo Clinic primary biliary cirrhosis sequential dataset (pbcseq).

Metric Value
Unique patients 312
Repeated clinical observations 1,945
Median observations per patient 5
Maximum observations per patient 16
Maximum observed follow-up 14.11 years

All patients have a baseline record at day 0.

Serum bilirubin, the primary repeated outcome, is fully observed across all longitudinal records.


Why a Longitudinal Model Is Necessary

Repeated measurements from the same patient are correlated and cannot be treated as statistically independent observations.

An intercept-only random-intercept model produced:

ICC = 0.765

Approximately 76.5% of the total variation in log-transformed bilirubin is attributable to differences between patients.

A mixed-effects framework is therefore necessary to represent:

  • within-patient change over time,
  • between-patient differences in typical bilirubin levels,
  • patient-specific rates of progression,
  • and clinically meaningful subgroup differences.

Analytical Workflow

The reproducible workflow includes:

  1. longitudinal data-structure inspection,
  2. duplicate and follow-up integrity checks,
  3. targeted clinical plausibility review,
  4. baseline cohort profiling,
  5. repeated-observation missingness assessment,
  6. descriptive trajectory visualization,
  7. raw-versus-log outcome-scale comparison,
  8. random-intercept and random-slope model comparison,
  9. linear-versus-quadratic time-specification assessment,
  10. adjusted mixed-effects modeling,
  11. baseline-stage-by-time interaction modeling,
  12. adjusted reference-profile trajectory prediction,
  13. follow-up-range sensitivity analysis,
  14. residual and random-effect diagnostics,
  15. focused extreme-residual sensitivity analysis,
  16. repository-output verification.

Data-Integrity Findings

The longitudinal dataset passed the core structural checks:

  • no fully duplicated rows,
  • no duplicated patient-day records,
  • no negative follow-up times,
  • no measurements recorded beyond documented follow-up,
  • and no missing serum bilirubin values.

Two isolated albumin spikes were reviewed within their patient-specific trajectories and recoded as missing in the working dataset only.

No rows were removed during the integrity audit.


Outcome Transformation

Raw serum bilirubin values were strongly right-skewed:

Outcome scale Skewness
Raw bilirubin 2.817
Log-transformed bilirubin 0.652

The primary mixed-effects models therefore use:

log_bilirubin = log(bili)


Selected Mixed-Effects Model

The final REML-refitted model includes:

  • linear follow-up time,
  • quadratic follow-up time,
  • baseline age,
  • baseline albumin,
  • sex,
  • treatment assignment,
  • baseline histologic-stage group,
  • stage-by-linear-time interaction,
  • stage-by-quadratic-time interaction,
  • patient-specific random intercepts,
  • and patient-specific linear-time slopes.

The adjusted coefficients describe observational longitudinal associations rather than causal effects.


Main Adjusted Findings

Baseline Histologic Stage

Patients with advanced baseline histologic stage (Stages 3–4) have higher bilirubin levels than patients with lower baseline stage (Stages 1–2).

The advanced-stage quadratic time interaction remains statistically supported:

β = 0.006, p = 0.007

This indicates that patients with advanced baseline histologic disease appear to follow a steeper bilirubin trajectory over time.

Baseline Albumin

A 0.5-unit higher baseline albumin value is associated with approximately 36.5% lower bilirubin across follow-up after adjustment:

β = −0.454, p < 0.001

Sex

Male patients show approximately 39.7% higher adjusted bilirubin than female patients:

β = 0.335, p = 0.041

This estimate should be interpreted cautiously because the male subgroup is relatively small.

Treatment Assignment

The D-penicillamine coefficient is not statistically supported:

β = −0.096, p = 0.348

This longitudinal biomarker model should not be interpreted as a definitive treatment-efficacy analysis.


Patient-Level Heterogeneity

Variance component Estimate
Random-intercept variance 0.769
Random-slope variance 0.031
Intercept–slope correlation 0.326
Within-patient residual variance 0.119

Patients differ meaningfully in both:

  • their typical bilirubin levels,
  • and their longitudinal rates of change.

Primary Adjusted Trajectory Figure

The primary figure focuses on the first 7 years of follow-up, where the longitudinal information base is stronger.

Adjusted bilirubin trajectories by baseline histologic stage

For a common reference profile, the predicted bilirubin values at 7 years are:

Baseline histologic-stage group Predicted bilirubin 95% CI
Lower stage: Stages 1–2 2.96 1.88 to 4.66
Advanced stage: Stages 3–4 8.79 6.36 to 12.17

These curves are back-transformed fixed-effect predictions for a common reference profile.

They are not arithmetic cohort means, individual patient forecasts, or causal effects.


Extended Sensitivity Visualization

Extended adjusted bilirubin sensitivity figure

Interpretation beyond 7 years requires additional caution because:

  • fewer patients remain under observation,
  • uncertainty intervals widen,
  • and polynomial time terms become more influential near the observed-data boundary.

Sensitivity Analyses

Restricted Follow-Up Range

The selected model was refitted using the first 7 years of follow-up only.

This retained:

  • 1,681 observations,
  • 86.43% of the full longitudinal records,
  • and all 312 patients.

Extreme-Residual Exclusion

A focused sensitivity model excluded only the 11 visits with:

|standardized conditional residual| > 3

This retained:

  • 1,934 observations,
  • 99.43% of the primary-analysis records,
  • and all 312 patients.

The advanced-stage quadratic interaction remained statistically supported:

Model p-value
Primary model 0.007
Extreme-residual sensitivity model 0.008

The primary model retains all observations.


Diagnostic Assessment

Diagnostic metric Value
Mean conditional residual 0.000
Median conditional residual −0.009
Residual excess kurtosis 2.898
Visits with absolute standardized residual greater than 3 11

Residual Distribution

Conditional residual distribution

Residuals versus Fitted Values

Residuals versus fitted values

Standardized Residual Q–Q Plot

Standardized residual Q–Q plot

Random Intercept versus Random Slope Relationship

Random intercept versus random slope relationship


Repository Structure

The repository contains:

  • README.md
  • requirements.txt
  • .gitignore
  • runtime_environment.txt
  • data/README.md
  • scripts/README.md
  • notebooks/README.md
  • notebooks/Longitudinal_Bilirubin_Mixed_Effects_Project.ipynb
  • outputs/ with verified tables and figures

Key Repository Outputs

Main Results

  • Machine-readable project summary
    outputs/project_summary.csv

  • Baseline patient-level cohort profile
    outputs/baseline_cohort_profile.csv

  • Longitudinal integrity audit
    outputs/repeated_observation_audit.csv

  • Final REML fixed-effect estimates
    outputs/primary_model_fixed_effect_summary.csv

  • Final random-effect variance components
    outputs/primary_model_random_effect_summary.csv

Primary Figure

  • Adjusted bilirubin trajectories by baseline histologic stage
    outputs/adjusted_bilirubin_trajectory_primary_0_to_7_years.png

Sensitivity Analyses

  • Follow-up-range sensitivity summary
    outputs/followup_range_sensitivity_summary.csv

  • Extreme-residual-exclusion sensitivity summary
    outputs/extreme_residual_exclusion_sensitivity_summary.csv

Reproducibility Checks

  • Repository-output audit
    outputs/repository_output_audit.csv

  • Complete outputs-folder inventory
    outputs/final_outputs_folder_inventory.csv


Interpretation Boundaries

This project demonstrates an observational longitudinal modeling workflow.

The results should not be interpreted as:

  • causal effects,
  • individual patient forecasts,
  • definitive treatment-efficacy conclusions,
  • or evidence that every patient follows the same disease-progression pattern.

Reproducibility

The workflow is designed for execution in Google Colab.

To reproduce the analysis:

  1. open the notebook inside the notebooks/ folder,
  2. install the packages listed in requirements.txt,
  3. obtain the publicly available pbcseq dataset,
  4. run the notebook cells sequentially,
  5. review the generated files inside the outputs/ folder.

Technical Stack

  • Python
  • pandas
  • NumPy
  • SciPy
  • statsmodels
  • patsy
  • matplotlib

Repository Verification

Verification metric Result
Verified repository-ready outputs 50
Total files in packaged outputs folder 53
CSV tables 40
PNG figures 13
Empty files 0

Author

Dr. Imran Sarmad
PhD Statistical Consultant | Clinical, RWE, HEOR/HTA, Causal Inference & Advanced Modeling
Lecturer, Virtual University of Pakistan

Professional website: https://drimransarmad.com
LinkedIn: https://www.linkedin.com/in/dr-imran-sarmad/

About

Reproducible longitudinal clinical modeling of serum bilirubin trajectories using linear mixed-effects models in Python.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages