Skip to content

Commit

Permalink
minor
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Dec 17, 2024
1 parent 7619496 commit 06de812
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ lint: ## run the lint checkers
uv run --all-extras ruff format tests

install: ## install in poetry venv
uv pip install .
uv pip install -e .

docs: readme ## build docs
uv run mkdocs build
uv run mkdocs build
14 changes: 7 additions & 7 deletions marginaleffects/formulaic.py → marginaleffects/formula.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


@validate_types
def variables(formula: str):
def get_variables(formula: str):
"""Extract variable names from a model formula.
Parameters
Expand All @@ -28,9 +28,9 @@ def variables(formula: str):
Examples
--------
>>> variables("y ~ x1 + x2")
>>> get_variables("y ~ x1 + x2")
['y', 'x1', 'x2']
>>> variables("response ~ pred1 * pred2")
>>> get_variables("response ~ pred1 * pred2")
['response', 'pred1', 'pred2']
Notes
Expand Down Expand Up @@ -77,14 +77,14 @@ def lwd(formula: str | None = None, vars: list[str] | None = None, data: pl.Data
Either formula or vars must be provided to specify which variables to check.
"""
if formula is not None:
vars = variables(formula)
vars = get_variables(formula)
elif formula is None and vars is None:
raise ValueError("formula or vars must be provided")
return data.drop_nulls(subset=vars)


@validate_types
def design(formula: str, data: pl.DataFrame):
def model_matrices(formula: str, data: pl.DataFrame):
"""Create design matrices from formula and data.
Parameters
Expand All @@ -110,11 +110,11 @@ def design(formula: str, data: pl.DataFrame):
Converts polars DataFrame to pandas for compatibility with formulaic.
Flattens y to 1D array if response variable is categorical.
"""
vars = variables(formula)
vars = get_variables(formula)
data = data.drop_nulls(subset=vars)
y, X = formulaic.model_matrix(formula, data.to_pandas())
y = np.ravel(data[vars[0]]) # avoid matrix if LHS is a character
return y, X, data
return y, X


def _sanity_engine(engine):
Expand Down
4 changes: 2 additions & 2 deletions marginaleffects/model_sklearn.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import warnings
import polars as pl
from . import formulaic as fml
from .formula import get_variables
from .model_abstract import ModelAbstract


Expand Down Expand Up @@ -99,7 +99,7 @@ def get_variables_names(self, variables=None, newdata=None):
list
Names of predictor variables, excluding the response variable.
"""
out = fml.variables(self.formula)[1:]
out = get_variables(self.formula)[1:]
return out

def get_predict(self, params, newdata: pl.DataFrame):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ description = "Predictions, counterfactual comparisons, slopes, and hypothesis t
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
"formulaic >=1.0.2",
"narwhals >=1.10.0",
"numpy >=2.0.0",
"patsy >=0.5.6",
Expand All @@ -16,7 +17,6 @@ dependencies = [
[project.optional-dependencies]
test = [
"duckdb >=1.1.2",
"formulaic >=1.0.2",
"matplotlib >=3.7.1",
"pandas >=2.2.2",
"pyarrow >=17.0.0",
Expand Down
4 changes: 2 additions & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 06de812

Please sign in to comment.