Skip to main content

Python library of Dynamic Treatment Regimes

Project description

pydtr

CircleCI codecov License

Description

This is a python library to conduct a dynamic treatment regime (DTR), pydtr.

A DTR is a paradigm that attempts to select optimal treatments adaptively for individual patients.

Pydtr enables you to implement DTR methods easily by using sklearn-based interfaces.

Method Single binary treatment Multiple treatments Multinomial treatment Continuous treatment Modeling flexibility Interpretability
IqLearnReg
(with sklearn)
:white_check_mark: :white_check_mark: :white_check_mark:
(with pipeline)
:white_check_mark:
(with arbitrary regression models)
IqLearnReg
(with statsmodels)
:white_check_mark: :white_check_mark: :white_check_mark: limited to OLS :white_check_mark:
(with confidence intervals)
GEstimation WIP WIP WIP WIP WIP

IqLearnReg means a regression method of iterative q-learning.

When there are categorical independent variables and you use a sklearn model as a regression function, you need to encode the categorical variables before using the model.

We recommend to encode categorical variables by category_encoders and combine the encoders with the sklearn model by sklearn.pipeline.

G-estimation, a famous method of DTR, is now unavailable.

Requirements

  • python>=3.6
  • pandas>=1.1.2
  • scikit-learn>=0.23.2
  • numpy>=1.19.2
  • statsmodels>=0.12.0

Installation

From pypi

pip install pydtr

From source

git clone https://github.com/fullflu/pydtr.git
cd pydtr
python setup.py install

Usage

Iterative Q Learning (IqLearnReg)

You need to import libraries and prepare data.

# import
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestRegressor

from pydtr.iqlearn.regression import IqLearnReg

# create sample dataframe
n = 10
thres = int(n / 2)
df = pd.DataFrame()
df["L1"] = np.arange(n)
df["A1"] = [0, 1] * int(n / 2)
df["A2"] = [0] * int(n / 2) + [1] * int(n / 2)
df["Y1"] = np.zeros(n)
df["Y2"] = np.zeros(n)

You can use sklearn-based models.

# set model info
model_info = [
    {
        "model": RandomForestRegressor(),
        "action_dict": {"A1": [0, 1]},
        "feature": ["L1", "A1"],
        "outcome": "Y1"
    },
    {
        "model": RandomForestRegressor(),
        "action_dict": {"A2": [0, 1]},
        "feature": ["L1", "A1", "Y1", "A2"],
        "outcome": "Y2"
    }
]
# fit model
dtr_model = IqLearnReg(
    n_stages=2,
    model_info=model_info
)
dtr_model.fit(df)

# predict optimal atcions
opt_action_stage_1 = dtr_model.predict(df, 0)
opt_action_stage_2 = dtr_model.predict(df, 1)
opt_action_all_stages = dtr_model.predict_all_stages(df)

You can also use statsmodels-based models.

# set model info
model_info = [
    {
        "model": "p_outcome ~ L1 * A1",
        "action_dict": {"A1": [0, 1]},
        "feature": ["L1", "A1"],
        "outcome": "Y1"
    },
    {
        "model": "p_outcome ~ L1 + A1 + Y1 * A2",
        "action_dict": {"A2": [0, 1]},
        "feature": ["L1", "A1", "Y1", "A2"],
        "outcome": "Y2"
    }
]
# fit model
dtr_model = IqLearnReg(
    n_stages=2,
    model_info=model_info
)
dtr_model.fit(df)

# predict optimal atcions
opt_action_stage_1 = dtr_model.predict(df, 0)
opt_action_stage_2 = dtr_model.predict(df, 1)
opt_action_all_stages = dtr_model.predict_all_stages(df)

Please see examples to get more information.

Authors

Contributors

Please feel free to create issues or to send pull-requests!

If all checkes have passed in pull-requests, I will merge and release them.

License

BSD

Structure

├── .circleci
│   ├── config.yml
├── .github
│   ├── CODEOWNERS
├── LICENSE
├── MANIFEST.IN
├── Makefile
├── README.md
├── examples
│   ├── ...several notebooks...
├── setup.cfg
├── setup.py
├── src
│   ├── pydtr
│   │   ├── __init__.py
│   │   └── iqlearn
│   │       ├── __init__.py
│   │       ├── base.py
│   │       └── regression.py
└── tests
    ├── test_iqlearn_sklearn_predict.py
    └── test_iqlearn_sm_predict.py

References

  • Chakraborty, B, Moodie, EE. Statistical Methods for Dynamic Treatment Regimes. Springer, New York, 2013.

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

pydtr-0.0.2.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

pydtr-0.0.2-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file pydtr-0.0.2.tar.gz.

File metadata

  • Download URL: pydtr-0.0.2.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.6.12

File hashes

Hashes for pydtr-0.0.2.tar.gz
Algorithm Hash digest
SHA256 63203b3a85e50e8f4a77c3b2a49528669f29af030d9a44e8f119ad050cb94be2
MD5 05f868eed40018538e7dc5d91dc59e6a
BLAKE2b-256 6b06c04f7e7117483975a3295dbbd7e7dea1574ab060c84f1f4c18bce546a3fa

See more details on using hashes here.

File details

Details for the file pydtr-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pydtr-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.6.12

File hashes

Hashes for pydtr-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 71550b82ad888834ca8c2591665e103e04d1a11719b406375797494f763bbf1e
MD5 31918ee5f84d0f00063604d0909bf9ba
BLAKE2b-256 8dbbcf0e8e193608f2594e82b10ddfb3f25316c4fcf5e100ea8c9bf750f3bb2b

See more details on using hashes here.

Supported by

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