Skip to content

Bring your R analysis to Python

rparity provides familiar formula-based APIs for selected lme4, mgcv, glmmTMB and emmeans workflows. Fit models and carry their results into prediction and inference without an R installation or rpy2. The numerical implementation runs directly in Python.

If you are starting a new analysis, use the quickstart and analysis guides. If you already have R code, the migration pages below map its calls to the Python API.

Find the Python equivalent

Your R workflow Python entry point Guide
lme4::lmer, lmerTest inference lmer() Linear mixed models
lme4::glmer glmer() Binomial and Poisson mixed models
mgcv::gam gam(), gam_check() Smooths, REML and GAM prediction
glmmTMB::glmmTMB glmmTMB() Zero inflation and dispersion
emmeans, emtrends, pairwise contrasts emmeans(), emtrends(), pairs() Marginal means and contrasts
car::Anova, lmerTest::anova Anova(), anova() Term tests and model comparison
nlme::gls gls(), correlation and variance structures Residual covariance models

Formula arguments become Python strings. Random effects retain syntax such as "y ~ x + (1 | group)", and GAM smooths use "y ~ s(x, k=8)". Python keyword arguments use underscores: for example, re_form, allow_new_levels and lmer_df.

Fit, then extract results

With an existing pandas data frame data containing y, x and group:

from rparity import emmeans, lmer, pairs

model = lmer("y ~ x + (1 | group)", data)
coefficients = model.fixef()
population = model.predict(data, re_form="NA")
means = emmeans(model, "x", at={"x": [0, 1]})
comparisons = pairs(means).summary()

fixef() returns a named pandas series. Marginal-mean and contrast summaries return data frames, so you can export them, join them with other tables or use them in your existing Python reporting workflow. Consult the API reference for other result methods.

Preserve the question your analysis answers

The Python APIs cover the supported options documented in each migration guide. Set the family, fitting method, contrast coding and prediction scale explicitly when reproducing an R analysis. For interaction models, sum contrasts are appropriate for the usual Type III main-effect hypotheses. For nonlinear models, setting random effects to zero differs from averaging over their distribution.

Check numerical accuracy and limitations before relying on a migrated fit, especially with singular random effects, extreme smoothing or nearly absent zero-inflation and dispersion components.