pymargins.PysmatchClient

class pymargins.PysmatchClient(matcher: Any, treatment_col: str, fit_scores_kwds: dict | None = None, match_kwds: dict | None = None)

Wrap a fitted pysmatch.Matcher for use with Margins.

This adapter satisfies the MatchingClient protocol consumed by Margins(..., matching=...). It extracts the matched subset and matched-set labels from a fitted pysmatch.Matcher, and implements rematch() so that bootstrap inference can re-run matching on each resampled replicate.

Parameters:
  • matcher (pysmatch.Matcher) – A fitted pysmatch.Matcher that has already had fit_scores(), predict_scores(), and match() called.

  • treatment_col (str) – Name of the binary treatment indicator column in the data. Used to reconstruct test/control groups during rematch().

  • fit_scores_kwds (dict, optional) – Keyword arguments passed to Matcher.fit_scores() on each rematch() call. If omitted, sensible defaults are used (balance=True, model_type='linear', nmodels=3, n_jobs=1).

  • match_kwds (dict, optional) – Keyword arguments passed to Matcher.match() on each rematch() call. If omitted, sensible defaults are used (method='min', nmatches=1, threshold=0.001, replacement=False).

matched_data

The matched subset (matcher.matched_data).

Type:

pandas.DataFrame

cluster_ids

Matched-set labels extracted from matched_data["match_id"].

Type:

numpy.ndarray

Examples

>>> from pysmatch.Matcher import Matcher
>>> from pymargins.matching import PysmatchClient
>>> matcher = Matcher(test, control, yvar="treated", exclude=["y"])
>>> matcher.fit_scores(balance=True, model_type="linear")
>>> matcher.predict_scores()
>>> matcher.match(method="min", nmatches=1, threshold=0.001)
>>> client = PysmatchClient(matcher, treatment_col="treated")
>>> m = Margins(fitted_model, matching=client)
__init__(matcher: Any, treatment_col: str, fit_scores_kwds: dict | None = None, match_kwds: dict | None = None)

Methods

__init__(matcher, treatment_col[, ...])

rematch(data)

Re-run pysmatch matching on a bootstrap resample.