Skip to main content

Formula to model matrix converter

Project description

wayne

logo


a formula to model matrix

Formula to Model Matrix in One Function Call

wyane is a Python package that converts statistical formulas into model matrices using Polars DataFrames. It does one thing: takes a formula and a DataFrame, returns a model matrix.

Usage

import wayne
import polars as pl

# Load your data
df = pl.read_csv("data/mtcars.csv")

# Define your formula
formula = 'mpg ~ cyl + wt*hp + poly(disp, 4) - 1'

# Get the model matrix
model_matrix = wayne.trade_formula_for_matrix(df, formula)
print(model_matrix)

logo

Features

  • Single Function: trade_formula_for_matrix(df, formula)
  • Polars Integration: Works with Polars DataFrames
  • R-Style Formulas: Supports R/Wilkinson notation
  • Fast Parsing: Rust + fiasto for formula parsing
  • Orthogonal Polynomials: NumPy-based polynomial generation
  • Interactions: Handles interaction terms (e.g., x*z)
  • Intercept Control: Add/remove intercept with +1/-1

Formula Syntax

Wayne supports R-style statistical formulas:

  • Basic terms: y ~ x + z
  • Interactions: y ~ x*z (expands to x + z + x:z)
  • Polynomial terms: y ~ poly(x, 3) (generates 3 orthogonal polynomial columns)
  • Intercept control: y ~ x - 1 (removes intercept)
  • Complex formulas: y ~ x + z + poly(w, 3) + x:z - 1

Examples

See the examples/ directory for complete examples:

# Main example
uv run python examples/final_example.py

# Single function demo
uv run python examples/single_function_example.py

What You Get

The model matrix contains:

  • Main effects: Original variables from your formula
  • Interaction terms: Generated interaction columns (e.g., wt_x_hp for wt*hp)
  • Polynomial terms: Orthogonal polynomial columns (e.g., poly_disp_1, poly_disp_2, etc.)
  • Intercept: Optional intercept column

Perfect for Statistical Modeling

Wayne creates model matrices that are ready for:

  • Linear regression
  • Generalized linear models
  • Mixed effects models
  • Any statistical modeling that needs a design matrix

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wayne_trade-0.1.0.tar.gz (913.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wayne_trade-0.1.0-cp312-cp312-macosx_11_0_arm64.whl (575.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file wayne_trade-0.1.0.tar.gz.

File metadata

  • Download URL: wayne_trade-0.1.0.tar.gz
  • Upload date:
  • Size: 913.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for wayne_trade-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0214a9e9420c36ebcb43f4b72ad02a1a4446a31af811d7171bb0482396409acb
MD5 0f6436ddb3179ad8b6a5a4b9d3bd912c
BLAKE2b-256 4956aff33b237d8d3c061ac716d6b69a36ded6638f8b05663e943a23e8523552

See more details on using hashes here.

File details

Details for the file wayne_trade-0.1.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for wayne_trade-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 32162fd1bb43e8c552c5111afba61dd93e67737b83f910e0f49d174f6844fc6e
MD5 535253864569d4f67153b77457bac63f
BLAKE2b-256 bc17fe2bfeb08615631449ad3aa1ba7a71c4ca3427af732970ce01abbdc5469a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page