From 3c0b9a50b101056f982c3581c5adbe3c4958f8ea Mon Sep 17 00:00:00 2001 From: Jonathan Taylor Date: Mon, 26 Oct 2026 17:23:51 -0700 Subject: [PATCH] Outcome: say that a dict works for any keyword, e.g. weights Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 4 +++- docs/responses.md | 3 ++- modelmatrix/outcome.py | 2 ++ tests/test_outcome.py | 7 +++++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b17f0c7..ee8c107 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,9 @@ `Outcome(['y1', 'y2'], offset={'y1': 'O1', 'y2': 'O2'})` gives the columns `y1`, `y2`, `offset[y1]` and `offset[y2]`, in the order of the responses; a missing or unknown key raises. A single column, - `offset='O'`, is still one column, the same for every response. + `offset='O'`, is still one column, the same for every response. The + dict works for any keyword, e.g. `weight={'y1': 'w1', 'y2': 'w2'}` for + weights of each response. ## 0.3.0 (2026-10-26) diff --git a/docs/responses.md b/docs/responses.md index 6d0c440..9fd05c5 100644 --- a/docs/responses.md +++ b/docs/responses.md @@ -206,7 +206,8 @@ A multi-response model, with a linear predictor for each response, takes an offset for each: a dict, keyed by the responses (the columns of a list of columns, or the classes of a `Multinomial`, whatever its output), gives the columns `offset[...]` in the order of the responses. -A single column is the same offset for every response. +A single column is the same offset for every response. The dict works +for any keyword, e.g. `weight={...}` for weights of each response. ```{code-cell} ipython3 Carseats['O'] = 0.5 diff --git a/modelmatrix/outcome.py b/modelmatrix/outcome.py index c95628d..e8ea477 100644 --- a/modelmatrix/outcome.py +++ b/modelmatrix/outcome.py @@ -95,6 +95,8 @@ class Outcome(Response): `offset={'y1': 'O1', 'y2': 'O2'}` gives the columns `offset[y1]` and `offset[y2]`, in the order of the responses. A single column, `offset='O'`, is the same for every response. + Any keyword can be a dict, e.g. `weight=` for weights of each + response. Notes ----- diff --git a/tests/test_outcome.py b/tests/test_outcome.py index 8f2a6ac..885f779 100644 --- a/tests/test_outcome.py +++ b/tests/test_outcome.py @@ -210,3 +210,10 @@ def test_offset_for_each_response_polars(M): y = Outcome(['y1', 'y2'], offset={'y1': 'O1', 'y2': 'O2'}).fit_transform(pl.from_pandas(M)) assert isinstance(y, pl.DataFrame) assert y.columns == ['y1', 'y2', 'offset[y1]', 'offset[y2]'] + + +def test_weights_for_each_response(M): + W = M.assign(w1=1.0, w2=0.5) + y = Outcome(['y1', 'y2'], offset='O', weight={'y1': 'w1', 'y2': 'w2'}).fit_transform(W) + assert list(y.columns) == ['y1', 'y2', 'offset', 'weight[y1]', 'weight[y2]'] + np.testing.assert_allclose(y['weight[y2]'], 0.5)