Performs logistic regression for binary and ordinal clinical trial endpoints with explicit declaration of marginal vs conditional estimand per FDA 2023 Final Guidance "Adjusting for Covariates in Randomized Clinical Trials." Covers covariate adjustment, g-computation/standardisation for marginal effects, modified Poisson regression for direct RR estimation when prevalence > 10%, Brant test for proportional odds, Firth penalty for separation/rare events, and Hauck-Donner detection.
pip install statsmodels scipy numpy pandas scikit-learn firthmodels
R is recommended for production marginal effects:
install.packages(c('marginaleffects', 'RobinCar', 'riskCommunicator', 'brant', 'VGAM', 'MASS'))
Tell your AI agent what you want to do: - "Logistic regression with treatment + covariates; report BOTH conditional OR and marginal RD via g-computation per FDA 2023" - "Modified Poisson with HC3 sandwich SE to estimate RR directly when prevalence > 10%" - "Brant test for proportional-odds assumption in ordinal model; partial PO model if violated" - "Firth penalised logistic regression with penalised LR test (NOT Wald) when separation detected" - "Detect Hauck-Donner pathology and switch to profile-likelihood CI"
"Fit a logistic regression for response from treatment, age, sex, baseline_severity. Use Placebo as the reference category explicitly (do NOT rely on alphabetical default which gives 'Active' as reference)."
"Extract conditional ORs for each predictor and the marginal RD via g-computation. Report HC3 sandwich SE for the marginal estimand per FDA 2023."
"Per FDA 2023 covariate adjustment guidance, my primary estimand is marginal RD. The model-fit OR is the CONDITIONAL log-OR (a different parameter). Compute marginal RD via g-computation in marginaleffects (R) or manual standardisation (Python)."
"Explain to my reviewer why the conditional OR and marginal RD differ -- cite Permutt 2020 non-collapsibility."
"Outcome prevalence is 32%, so OR will substantially overstate RR. Switch to modified Poisson regression with cov_type='HC1' (Zou 2004) to estimate RR directly."
"Fit a proportional-odds cumulative-logit model for severity (mild/moderate/severe). Then run the Brant test (R brant::brant) to verify PO holds per predictor. If PO fails for one predictor, fit a partial PO model in VGAM."
"My binary outcome has only 3% events -- separation likely. Detect with firthmodels.detect_separation. Apply Firth penalty; use penalised LR test (firth.pvalues_lrt_) NOT Wald p-values for inference."
"Small cell count; coefficient is large but Wald p > 0.05. Hauck-Donner suspected. Switch to profile-likelihood CI (R MASS::confint.glm); detect with VGAM::hdeff."
"My trial used stratified randomisation by site and baseline severity. Include both as model covariates per Kahan-Morris 2012 -- ignoring is over-conservative (SE biased upward, power loss)."
C(ARM, Treatment(reference="Placebo")).cov_type='HC1' or 'HC3'). Without it, SEs are wrong because binary data are NOT Poisson.OrderedModel requires no intercept -- threshold parameters replace it.pvalues_ is Wald and is liberal under penalty. Use pvalues_lrt_ (firthmodels >=0.3) or compute penalised LR test manually (Heinze-Schemper 2002).sm.Logit provides pred_table() and prsquared that sm.GLM(family=Binomial) does not. Use Logit unless changing link functions.