Propensity Score Matcher

In this notebook, we show the basic usage of the PropensityScoreMatcher. The propensity score (Rosenbaum & Rubin, 1983) is the probability of belonging to the target population given baseline covariates:

\begin{equation} e(x) = P(Z = 1 \mid X = x) \end{equation}

where \(X\) are the covariates and \(Z = 1\) indicates membership in the target. Patients are matched on similar estimated scores.

Unlike the GeneticMatcher and ConstraintSatisfactionMatcher, the PropensityScoreMatcher does not directly optimize a particular balance score. Instead, it uses the given objective as a measure of “correctness” of the propensity score model, tries a (possibly large) number of potential models, and returns the one with the best score. In doing this, we automate the often manual hyperparameter optimization that accompanies propensity score matching. Because balance is not optimized directly, residual imbalance often remains; see the linear program demo for how to improve upon a propensity score match.

[1]:
import logging
logging.basicConfig(
    format="%(levelname)-4s [%(filename)s:%(lineno)d] %(message)s",
    level='INFO',
)

from pybalance.utils import *
from pybalance.sim import generate_toy_dataset
from pybalance.propensity import PropensityScoreMatcher
from pybalance.visualization import (
    plot_numeric_features,
    plot_categoric_features,
    plot_binary_features,
    plot_joint_numeric_distributions,
    plot_per_feature_loss
)
[2]:
m = generate_toy_dataset(n_pool=10000, n_target=1000, seed=123)
m
[2]:
Headers Numeric:
['age', 'height', 'weight']

Headers Categoric:
['gender', 'haircolor', 'country', 'binary_0', 'binary_1', 'binary_2', 'binary_3']

Populations
['pool', 'target']
age height weight gender haircolor country population binary_0 binary_1 binary_2 binary_3 patient_id
0 60.807949 173.610298 77.912924 0.0 1 4 pool 0 0 1 1 0
1 45.810836 170.541198 112.416988 0.0 1 4 pool 0 1 0 0 1
2 58.876976 188.138610 108.789013 0.0 0 2 pool 0 0 1 1 2
3 73.398077 162.939196 65.345017 0.0 1 4 pool 0 1 1 1 3
4 56.890587 156.386701 78.140295 0.0 0 3 pool 0 0 1 0 4
... ... ... ... ... ... ... ... ... ... ... ... ...
995 39.662026 162.692755 54.607476 0.0 2 4 target 0 0 1 1 10995
996 49.130301 141.583192 103.798145 1.0 0 2 target 1 0 0 0 10996
997 68.035281 168.744482 56.499644 1.0 1 1 target 0 0 0 1 10997
998 62.044564 177.796983 75.983973 1.0 1 1 target 0 0 0 1 10998
999 51.243734 161.013556 86.513956 0.0 0 1 target 0 0 0 0 10999

11000 rows × 12 columns

Optimize Beta (Mean Absolute SMD)

Using the given objective function, search max_iter possible different propensity score models and take the model that gives the best match given that objective function.

[3]:
# Note that using a caliper can result in matched population being
# smaller than target! If this is undesired, do not use a caliper.
objective = beta = BetaBalance(m)
matcher = PropensityScoreMatcher(
    matching_data=m,
    objective=objective,
    time_limit=300,
    max_iter=100)
matcher.get_params()
[3]:
{'objective': 'beta',
 'caliper': None,
 'max_iter': 100,
 'time_limit': 300,
 'method': 'greedy',
 'seed': None}
[4]:
matcher.match()
INFO [matcher.py:267] Training model LogisticRegression (iter 1/100, 0.0 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
sklearn/linear_model/_sag.py:348: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge
  warnings.warn(
INFO [matcher.py:219] Best propensity score match found:
INFO [matcher.py:220]   Model: LogisticRegression
INFO [matcher.py:222]   * C: 6.310110586108835
INFO [matcher.py:222]   * fit_intercept: False
INFO [matcher.py:222]   * max_iter: 500
INFO [matcher.py:222]   * penalty: l1
INFO [matcher.py:222]   * solver: saga
INFO [matcher.py:223]   Score (beta): 0.0346
INFO [matcher.py:224]   Solution time: 0.7 s
INFO [matcher.py:267] Training model LogisticRegression (iter 2/100, 0.8 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 3/100, 0.9 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 4/100, 0.9 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 5/100, 1.0 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 6/100, 1.1 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 7/100, 1.2 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 8/100, 1.3 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_sag.py:348: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 9/100, 1.8 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 10/100, 2.0 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 11/100, 2.0 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 12/100, 2.1 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
sklearn/linear_model/_sag.py:348: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 13/100, 2.9 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 14/100, 3.0 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 15/100, 3.1 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 16/100, 3.1 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 17/100, 3.2 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 18/100, 3.3 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 19/100, 3.3 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 20/100, 3.4 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 21/100, 3.5 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 22/100, 3.6 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 23/100, 3.7 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 24/100, 4.0 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 25/100, 4.1 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 26/100, 4.2 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 27/100, 4.3 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 28/100, 4.5 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 29/100, 4.6 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 30/100, 4.7 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 31/100, 4.9 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 32/100, 5.1 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 33/100, 5.1 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 34/100, 5.5 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 35/100, 5.6 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 36/100, 5.7 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
sklearn/linear_model/_sag.py:348: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 37/100, 6.5 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 38/100, 6.7 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 39/100, 6.7 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 40/100, 6.8 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
sklearn/linear_model/_sag.py:348: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 41/100, 7.6 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
sklearn/linear_model/_sag.py:348: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge
  warnings.warn(
INFO [matcher.py:219] Best propensity score match found:
INFO [matcher.py:220]   Model: LogisticRegression
INFO [matcher.py:222]   * C: 0.6191810056908827
INFO [matcher.py:222]   * fit_intercept: False
INFO [matcher.py:222]   * max_iter: 500
INFO [matcher.py:222]   * penalty: l1
INFO [matcher.py:222]   * solver: saga
INFO [matcher.py:223]   Score (beta): 0.0329
INFO [matcher.py:224]   Solution time: 8.5 s
INFO [matcher.py:267] Training model SGDClassifier (iter 42/100, 8.5 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 43/100, 8.6 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 44/100, 9.0 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 45/100, 9.2 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 46/100, 9.2 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 47/100, 9.3 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 48/100, 9.4 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 49/100, 9.5 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
sklearn/linear_model/_sag.py:348: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 50/100, 10.2 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 51/100, 10.6 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 52/100, 10.7 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 53/100, 10.8 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 54/100, 10.8 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 55/100, 11.0 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 56/100, 11.1 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 57/100, 11.2 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 58/100, 11.2 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
sklearn/linear_model/_sag.py:348: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 59/100, 12.0 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
sklearn/linear_model/_sag.py:348: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 60/100, 12.8 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 61/100, 13.0 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 62/100, 13.0 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 63/100, 13.1 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 64/100, 13.2 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 65/100, 13.3 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 66/100, 13.4 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 67/100, 13.4 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 68/100, 13.5 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
sklearn/linear_model/_sag.py:348: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 69/100, 14.3 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
INFO [matcher.py:219] Best propensity score match found:
INFO [matcher.py:220]   Model: LogisticRegression
INFO [matcher.py:222]   * C: 0.28866833556559457
INFO [matcher.py:222]   * fit_intercept: False
INFO [matcher.py:222]   * max_iter: 500
INFO [matcher.py:222]   * penalty: l1
INFO [matcher.py:222]   * solver: saga
INFO [matcher.py:223]   Score (beta): 0.0325
INFO [matcher.py:224]   Solution time: 15.0 s
INFO [matcher.py:267] Training model LogisticRegression (iter 70/100, 15.1 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 71/100, 15.7 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 72/100, 15.7 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 73/100, 15.8 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 74/100, 15.9 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 75/100, 15.9 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 76/100, 16.0 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 77/100, 16.1 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 78/100, 16.1 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 79/100, 16.5 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
sklearn/linear_model/_sag.py:348: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 80/100, 17.2 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 81/100, 17.4 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 82/100, 17.5 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 83/100, 17.6 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 84/100, 17.7 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 85/100, 17.8 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 86/100, 17.9 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 87/100, 18.0 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
sklearn/linear_model/_sag.py:348: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 88/100, 18.7 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 89/100, 18.8 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
sklearn/linear_model/_sag.py:348: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 90/100, 19.6 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 91/100, 19.7 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 92/100, 20.1 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 93/100, 20.5 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 94/100, 21.1 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 95/100, 21.3 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model SGDClassifier (iter 96/100, 21.3 s) ...
INFO [matcher.py:267] Training model SGDClassifier (iter 97/100, 21.4 s) ...
INFO [matcher.py:267] Training model LogisticRegression (iter 98/100, 21.5 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
sklearn/linear_model/_logistic.py:1407: UserWarning: Inconsistent values: penalty=l1 with l1_ratio=0.0. penalty is deprecated. Please use l1_ratio only.
  warnings.warn(
sklearn/linear_model/_sag.py:348: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 99/100, 22.2 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:267] Training model LogisticRegression (iter 100/100, 22.5 s) ...
sklearn/linear_model/_logistic.py:1381: FutureWarning: 'penalty' was deprecated in version 1.8 and will be removed in 1.10. To avoid this warning, leave 'penalty' set to its default value and use 'l1_ratio' or 'C' instead. Use l1_ratio=0 instead of penalty='l2', l1_ratio=1 instead of penalty='l1', l1_ratio set to a float between 0 and 1 instead of penalty='elasticnet', and C=np.inf instead of penalty=None.
  warnings.warn(
INFO [matcher.py:219] Best propensity score match found:
INFO [matcher.py:220]   Model: LogisticRegression
INFO [matcher.py:222]   * C: 0.28866833556559457
INFO [matcher.py:222]   * fit_intercept: False
INFO [matcher.py:222]   * max_iter: 500
INFO [matcher.py:222]   * penalty: l1
INFO [matcher.py:222]   * solver: saga
INFO [matcher.py:223]   Score (beta): 0.0325
INFO [matcher.py:224]   Solution time: 15.0 s
[4]:
Headers Numeric:
['age', 'height', 'weight']

Headers Categoric:
['gender', 'haircolor', 'country', 'binary_0', 'binary_1', 'binary_2', 'binary_3']

Populations
['pool', 'target']
age height weight gender haircolor country population binary_0 binary_1 binary_2 binary_3 patient_id
6790 22.679417 136.691851 66.809082 1.0 1 3 pool 1 0 0 1 6790
9008 54.977454 151.931111 85.475196 1.0 2 2 pool 1 1 0 1 9008
6914 67.271482 165.463117 68.851654 1.0 1 4 pool 1 1 1 1 6914
4709 57.941426 165.439594 106.897960 0.0 1 2 pool 1 0 1 0 4709
2133 30.647807 127.534778 82.919112 1.0 1 1 pool 0 1 0 0 2133
... ... ... ... ... ... ... ... ... ... ... ... ...
995 39.662026 162.692755 54.607476 0.0 2 4 target 0 0 1 1 10995
996 49.130301 141.583192 103.798145 1.0 0 2 target 1 0 0 0 10996
997 68.035281 168.744482 56.499644 1.0 1 1 target 0 0 0 1 10997
998 62.044564 177.796983 75.983973 1.0 1 1 target 0 0 0 1 10998
999 51.243734 161.013556 86.513956 0.0 0 1 target 0 0 0 0 10999

2000 rows × 12 columns

[5]:
%matplotlib inline
matcher.plot_propensity_score_distributions()
[5]:
<seaborn.axisgrid.FacetGrid at 0x13cea1370>
../_images/demos_matching_01_propensity_7_1.png
[6]:
%matplotlib inline
objective = beta = BetaBalance(m)

match = matcher.get_best_match()
m_data = m.copy().get_population('pool')
m_data.loc[:, 'population'] = m_data['population'] + ' (prematch)'
match.append(m_data)
fig = plot_per_feature_loss(match, beta, 'target', debin=False)
fig = plot_numeric_features(match, hue_order=['pool (prematch)', 'pool', 'target', ])
fig = plot_categoric_features(match,  hue_order=['pool (prematch)', 'pool', 'target'])
INFO [matcher.py:219] Best propensity score match found:
INFO [matcher.py:220]   Model: LogisticRegression
INFO [matcher.py:222]   * C: 0.28866833556559457
INFO [matcher.py:222]   * fit_intercept: False
INFO [matcher.py:222]   * max_iter: 500
INFO [matcher.py:222]   * penalty: l1
INFO [matcher.py:222]   * solver: saga
INFO [matcher.py:223]   Score (beta): 0.0325
INFO [matcher.py:224]   Solution time: 15.0 s
../_images/demos_matching_01_propensity_8_1.png
../_images/demos_matching_01_propensity_8_2.png
../_images/demos_matching_01_propensity_8_3.png