Skip to main content

Crate mixeff_rs

Crate mixeff_rs 

Source
Expand description

Mixed-effects models in Rust.

This crate provides types and algorithms for fitting linear and generalized linear mixed-effects models. It is developed against Julia’s MixedModels.jl as the reference and parity target — the same PLS/PIRLS formulation, cross-checked numerically — but is an independent implementation with deliberate divergences (notably optimizer selection and the fallback strategy), not a line-for-line port.

§Tutorial

New to the crate? Read the guide module — five short, doctested pages:

  1. Getting started — build a frame, parse a formula, fit an LMM.
  2. Reading results — coefficients, variance components, summaries, Wald vs. profile vs. bootstrap CIs.
  3. GLMMs — families, links, and the GLMM estimation semantics.
  4. When the crate refuses — typed errors and typed inference refusals (the no-fake-statistics contract from a caller’s POV).
  5. What is supported — a concrete inventory: model classes, families/links, formula syntax, inference paths, and what is deliberately out of scope.

§Quick start

Fit a linear mixed model y ~ 1 + x + (1 | g) on a small balanced dataset. The prelude pulls in the common types and the LinearMixedModelBuilder collapses construction and the ML/REML choice into one chain:

use mixeff_rs::prelude::*;
use mixeff_rs::model::{FitOptions, LinearMixedModelBuilder};

let group_offsets = [-3.0, -1.5, 0.5, 2.0, -2.0, 1.0, 3.0, -0.5];
let jitter = [0.12, -0.20, 0.05, 0.17, -0.09, 0.22];

let mut y = Vec::new();
let mut x = Vec::new();
let mut g = Vec::new();
for (gi, off) in group_offsets.iter().enumerate() {
    for (k, j) in jitter.iter().enumerate() {
        let xv = k as f64;
        x.push(xv);
        y.push(2.0 + 1.5 * xv + off + j);
        g.push(format!("g{gi}"));
    }
}

let mut df = DataFrame::new();
df.add_numeric("y", y)?;
df.add_numeric("x", x)?;
df.add_categorical("g", g)?;

let model = LinearMixedModelBuilder::new(parse_formula("y ~ 1 + x + (1 | g)")?, &df)
    .fit(FitOptions::reml())?; // or FitOptions::ml()

let coef = model.coef(); // fixed effects, ~[2.0, 1.5]
assert_eq!(coef.len(), 2);
assert!(coef.iter().all(|v| v.is_finite()));

The lower-level form (LinearMixedModel::new(formula, &df, None)? then model.fit(false)) remains available; the builder is purely additive.

Re-exports§

pub use nalgebra;

Modules§

error
Error types for the mixeff-rs crate.
formula
Formula parsing for mixed-effects models.
guide
Narrative tutorial pages (rendered on docs.rs, doctested in CI). Narrative tutorial for newcomers, in reading order.
model
Mixed model types and fitting algorithms.
prelude
Common imports for fitting and inspecting mixed models.
stats
Post-fit statistical summaries and inference.
types
Typed support values for advanced callers and tests.