Package: pydtr
pydtrDescriptionThis 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.MethodSingle binary treatmentMultiple treatmentsMultinomial treatmentContinuous treatmentModeling flexibilityInterpretabilityIqLearnReg(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)GEstimationWIPWIPWIPWIPWIPIqLearnRegmeans 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 bycategory_encodersand combine the encoders with the sklearn model bysklearn.pipeline.G-estimation, a famous method of DTR, is now unavailable.Requirementspython>=3.6pandas>=1.1.2scikit-learn>=0.23.2numpy>=1.19.2statsmodels>=0.12.0InstallationFrom pypipip install pydtrFrom sourcegit clone https://github.com/fullflu/pydtr.git
cd pydtr
python setup.py installUsageIterative Q Learning (IqLearnReg)You need to import libraries and prepare data.# importimportnumpyasnpimportpandasaspdfromsklearn.ensembleimportRandomForestRegressorfrompydtr.iqlearn.regressionimportIqLearnReg# create sample dataframen=10thres=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 infomodel_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 modeldtr_model=IqLearnReg(n_stages=2,model_info=model_info)dtr_model.fit(df)# predict optimal atcionsopt_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 infomodel_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 modeldtr_model=IqLearnReg(n_stages=2,model_info=model_info)dtr_model.fit(df)# predict optimal atcionsopt_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 seeexamplesto get more information.Authors@fullfluContributorsPlease feel free to create issues or to send pull-requests!If all checkes have passed in pull-requests, I will merge and release them.LicenseBSDStructure├── .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.pyReferencesChakraborty, B, Moodie, EE.Statistical Methods for Dynamic Treatment Regimes.Springer, New York, 2013.