Crate polars_formula

Crate polars_formula 

Source
Expand description

§polars-formula

A parsing and materialization library for Rust that brings R-style and Python Patsy/Formulaic formula syntax to the Polars DataFrame ecosystem.

§Overview

This library provides a simple, focused API for parsing statistical model formulas and materializing them into design matrices. It seamlessly integrates with Polars DataFrames and provides efficient conversion to faer matrices for linear algebra operations.

§Simple API

The library exposes just 4 main functions for a clean, focused experience:

use polars::prelude::*;
use polars_formula::{canonicalize, materialize, print_formula, print_modelspec};

// 1. Parse and canonicalize a formula
let spec = canonicalize("y ~ x1 + x2")?;

// 2. Print the canonical formula with colors
print_formula(&spec);

// 3. Materialize against your data
let (y, x, z) = materialize(&spec, &df)?;

// 4. Inspect the full model specification
print_modelspec(&spec);

§Supported Syntax

SyntaxDescription
y ~ x1 + x2Linear regression
y ~ x1 * x2Product terms (expands to x1 + x2 + x1:x2)
y ~ x1:x2Interaction terms
y ~ poly(x1, 2)Polynomial terms (x, x², x³, …)
y ~ (1|group)Random intercepts
y ~ (x|group)Random slopes
y ~ (x||group)Uncorrelated random effects
y ~ I(x)Identity function (literal interpretation)
y ~ x^2Power terms
y ~ (a+b)^3Polynomial expansion
y ~ a/bNesting (a + a:b)
y ~ b %in% aNesting (b within a)
y | weights(w) ~ xAuxiliary terms (weights, se, trials, etc.)
Surv(time, event) ~ xSurvival analysis
cbind(success, failure) ~ xMultivariate responses
s(x, k=10, bs="tp")Smooth terms (s, t2, te, ti)
family=gaussian() y ~ xDistribution families
y ~ x + sigma ~ zDistributional parameters
y ~ x + ar(p=1)Autocorrelation terms

Enums§

Error

Functions§

canonicalize
Parse and canonicalize a formula string into a ModelSpec.
materialize
Materialize a ModelSpec against a DataFrame to produce design matrices.
print_formula
Print the canonical formula with syntax highlighting.
print_modelspec
Pretty print the full ModelSpec structure.