coxme¶
Fit a Cox proportional hazards model with Gaussian frailty (shared frailty
model) via penalised partial likelihood — targeting parity with R’s
coxme::coxme().
The model is:
h(t | x_i, b_j) = h_0(t) exp(x_i' beta + z_i' b_j)
where b_j ~ N(0, sigma^2 I) for shared frailty. Estimation uses Laplace- approximated integrated partial likelihood with an inner Newton-Raphson loop and outer Brent / L-BFGS-B optimisation over variance parameters.
- coxme(formula, data, groups=None, random=None, optimizer='lbfgsb', theta0=None)¶
Fit a Cox PH model with Gaussian frailty.
- Parameters:
formula – Survival formula
"Surv(time, event) ~ x1 + x2".data – DataFrame (pandas, polars, or narwhals-compatible).
groups – Column name(s) for shared frailty grouping.
random – lme4-style random effect specs (takes precedence over
groups).optimizer –
"lbfgsb"(default).theta0 – Initial theta. Defaults to ones.
- Return type:
CoxmeResult- Returns:
CoxmeResult
- Parameters:
formula (str)
data (Any)
groups (str | list[str] | None)
random (list[str] | None)
optimizer (str)
theta0 (ndarray | None)
Key parameters¶
Parameter |
Type |
Description |
|---|---|---|
|
|
Survival formula: |
|
|
Input data (pandas, polars, or narwhals-compatible) |
|
|
Column name(s) for shared frailty grouping |
|
|
lme4-style random-effect specs (takes precedence over |
|
|
|
|
|
Initial variance parameters; defaults to ones |
Examples¶
Crossed frailty¶
result = interlace.coxme(
formula="Surv(time, status) ~ treatment",
data=df,
groups=["centre", "surgeon"],
)
Prediction¶
# Linear predictor (x'beta + z'b)
lp = result.predict() # in-sample
lp_new = result.predict(newdata=df_new) # new data
# Hazard ratios: exp(linear predictor)
hr = result.predict(type="risk")
# Fixed effects only (no frailty)
lp_marginal = result.predict(newdata=df_new, include_re=False)
Summary¶
print(result.summary())
Result object¶
coxme() returns a CoxmeResult with the following attributes:
Attribute |
Type |
Description |
|---|---|---|
|
|
Log hazard ratio estimates |
|
|
Standard errors |
|
|
Two-sided Wald p-values (z-test) |
|
|
95% confidence intervals |
|
|
BLUPs per grouping factor |
|
|
Frailty variance per grouping factor |
|
|
Raw variance parameters |
|
|
Whether the optimizer converged |
|
|
Number of observations |
|
|
Number of events (non-censored) |
|
|
Number of levels per grouping factor |
|
|
Integrated partial log-likelihood |
|
|
Akaike information criterion |
|
|
Bayesian information criterion |
|
|
Harrell’s C-statistic |
|
|
Breslow baseline cumulative hazard |
Methods¶
Method |
Description |
|---|---|
|
Linear predictor or hazard ratio |
|
Human-readable summary |
Comparison with R¶
interlace |
R (coxme) |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|