propensio 1.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- propensio-1.0.2/LICENSE +21 -0
- propensio-1.0.2/PKG-INFO +175 -0
- propensio-1.0.2/README.md +136 -0
- propensio-1.0.2/propensio/__init__.py +2 -0
- propensio-1.0.2/propensio/azuka_charts.py +999 -0
- propensio-1.0.2/propensio/lcgeuclideanmethod.py +764 -0
- propensio-1.0.2/propensio/matching.py +284 -0
- propensio-1.0.2/propensio.egg-info/PKG-INFO +175 -0
- propensio-1.0.2/propensio.egg-info/SOURCES.txt +12 -0
- propensio-1.0.2/propensio.egg-info/dependency_links.txt +1 -0
- propensio-1.0.2/propensio.egg-info/requires.txt +7 -0
- propensio-1.0.2/propensio.egg-info/top_level.txt +1 -0
- propensio-1.0.2/pyproject.toml +26 -0
- propensio-1.0.2/setup.cfg +4 -0
propensio-1.0.2/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Azka Rohbiya Ramadani
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
propensio-1.0.2/PKG-INFO
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: propensio
|
|
3
|
+
Version: 1.0.2
|
|
4
|
+
Summary: Causal inference using Propensity Score Matching and Euclidean LCG method
|
|
5
|
+
Author-email: azkarohbiya <azkarohbiya@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2022 Azka Rohbiya Ramadani
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Requires-Python: >=3.8
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
License-File: LICENSE
|
|
31
|
+
Requires-Dist: pandas>=1.3.0
|
|
32
|
+
Requires-Dist: numpy>=1.21.0
|
|
33
|
+
Requires-Dist: scipy>=1.7.0
|
|
34
|
+
Requires-Dist: matplotlib>=3.4.0
|
|
35
|
+
Requires-Dist: statsmodels>=0.13.0
|
|
36
|
+
Requires-Dist: scikit-learn>=1.0.0
|
|
37
|
+
Requires-Dist: plotly>=5.0.0
|
|
38
|
+
Dynamic: license-file
|
|
39
|
+
|
|
40
|
+
# Causal Inference using PSM
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
# Background
|
|
44
|
+
Propensity score matching is a statistical technique used to estimate the effect of a treatment or intervention on an outcome of interest. It is commonly used in observational studies, where the assignment of treatment or exposure to a particular group is not randomized.
|
|
45
|
+
|
|
46
|
+
The idea behind propensity score matching is to balance the characteristics of the treatment and control groups by matching individuals with similar propensity scores, which are the probabilities of receiving the treatment or intervention based on observed covariates. This helps to control for confounding factors and reduce selection bias, allowing for a more accurate estimation of the treatment effect.
|
|
47
|
+
|
|
48
|
+
Overall, propensity score matching is a useful tool for researchers to make causal inferences in observational studies, although it is important to consider the limitations and assumptions of this method.
|
|
49
|
+
|
|
50
|
+
# Installation Guide
|
|
51
|
+
This function has been uploaded to [pypi](https://pypi.org/project/causal-inference-aagm/) so you can type on your prompt as code below
|
|
52
|
+
```bash
|
|
53
|
+
pip install causal-inference-aagm
|
|
54
|
+
```
|
|
55
|
+
Then import the library
|
|
56
|
+
```python
|
|
57
|
+
from causalinference_aagm.matching import PropensityScoreMatch
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
If error, download (matching.py)[tsel_aagm/matching.py]
|
|
61
|
+
then
|
|
62
|
+
```
|
|
63
|
+
from matching import *
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
# Requirements Library
|
|
67
|
+
This python requires related package more importantly python_requires='>=3.1', so that package can be install Make sure the other packages meet the requirements below
|
|
68
|
+
- pandas>=1.1.5,
|
|
69
|
+
- numpy>=1.18.5,
|
|
70
|
+
- scipy>=1.2.0,
|
|
71
|
+
- matplotlib>=3.1.0,
|
|
72
|
+
- statsmodels>=0.8.0
|
|
73
|
+
|
|
74
|
+
# Usage Guide
|
|
75
|
+
This is a Python class named PropensityScoreMatch. It is designed to perform propensity score matching, a technique used to balance the distribution of confounding variables between treatment and control groups in observational studies. The class has four input arguments:
|
|
76
|
+
|
|
77
|
+
- df: a pandas DataFrame containing the data to be analyzed.
|
|
78
|
+
- features: a list of column names in df that contain the variables used to calculate propensity scores.
|
|
79
|
+
- treatment: a string that specifies the name of the column in df that contains the treatment variable.
|
|
80
|
+
- outcome: a string that specifies the name of the column in df that contains the outcome variable.
|
|
81
|
+
|
|
82
|
+
The output of the class is two pandas DataFrames:
|
|
83
|
+
|
|
84
|
+
- df_matched: a DataFrame containing the data for the matched pairs of treated and control observations.
|
|
85
|
+
- df_TE: a DataFrame containing the treatment effect estimates for each variable in features.
|
|
86
|
+
|
|
87
|
+
In addition to these output DataFrames, the class provides two methods for visualizing the results of the analysis:
|
|
88
|
+
|
|
89
|
+
- plot_smd(): a method that generates a plot of standardized mean differences (SMDs) between the treatment and control groups for each variable in features.
|
|
90
|
+
- plot_individual_treatment(): a method that generates a plot of the individual treatment effects for each observation in df_matched.
|
|
91
|
+
|
|
92
|
+
For Analysis:
|
|
93
|
+
- plot_smd() : plotting the df_smd
|
|
94
|
+
|
|
95
|
+
# Example Usage
|
|
96
|
+
Importing libraries
|
|
97
|
+
```python
|
|
98
|
+
from causalinference_aagm.matching import PropensityScoreMatch as psm
|
|
99
|
+
import pandas as pd
|
|
100
|
+
import numpy as np
|
|
101
|
+
import matplotlib.pyplot as plt
|
|
102
|
+
import seaborn as sns
|
|
103
|
+
plt.style.use('seaborn')
|
|
104
|
+
```
|
|
105
|
+
Initiating model
|
|
106
|
+
```python
|
|
107
|
+
# Importing Data
|
|
108
|
+
df = pd.read_csv('datasets/healtcare_stroke_data.csv')
|
|
109
|
+
def one_hot_encode(df):
|
|
110
|
+
"""
|
|
111
|
+
One-hot encodes all object data type columns of a Pandas DataFrame.
|
|
112
|
+
"""
|
|
113
|
+
# Get the object columns
|
|
114
|
+
obj_cols = df.select_dtypes(include=['object']).columns
|
|
115
|
+
# One-hot encode the object columns
|
|
116
|
+
df = pd.get_dummies(df, columns=obj_cols)
|
|
117
|
+
return df
|
|
118
|
+
|
|
119
|
+
df = one_hot_encode(df).fillna(0)
|
|
120
|
+
df.head()
|
|
121
|
+
df_model = df[['age','hypertension','heart_disease','bmi','stroke','gender_Male','smoking_status_smokes', 'avg_glucose_level']]
|
|
122
|
+
df_model.head()
|
|
123
|
+
|
|
124
|
+
# Defining Variable
|
|
125
|
+
features = ['age','hypertension','heart_disease','bmi','gender_Male', 'avg_glucose_level']
|
|
126
|
+
treatment = 'smoking_status_smokes'
|
|
127
|
+
outcome = 'stroke'
|
|
128
|
+
|
|
129
|
+
# Matching
|
|
130
|
+
model = psm(df_model, features, treatment, outcome)
|
|
131
|
+
```
|
|
132
|
+
Output:
|
|
133
|
+

|
|
134
|
+
|
|
135
|
+
Evaluating Plot
|
|
136
|
+
```python
|
|
137
|
+
def hist_all_features(df, features, hue):
|
|
138
|
+
width = 6*len(features)
|
|
139
|
+
fig, axes = plt.subplots(ncols=len(features), figsize=(width, 5))
|
|
140
|
+
for i in range(len(features)):
|
|
141
|
+
sns.histplot(data=df, x=features[i], ax=axes[i], hue=hue)
|
|
142
|
+
plt.show()
|
|
143
|
+
```
|
|
144
|
+
```python
|
|
145
|
+
features = ['age','hypertension','heart_disease','bmi','gender_Male','avg_glucose_level','proba']
|
|
146
|
+
hist_all_features(model.df, features, hue='smoking_status_smokes')
|
|
147
|
+
```
|
|
148
|
+
Output:
|
|
149
|
+

|
|
150
|
+
|
|
151
|
+
```python
|
|
152
|
+
hist_all_features(model.df_matched, features, hue='smoking_status_smokes')
|
|
153
|
+
```
|
|
154
|
+

|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
fig, axes = plt.subplots(ncols=2, figsize=(12, 5))
|
|
158
|
+
|
|
159
|
+
# Comparing Stroke Mean without Matching
|
|
160
|
+
stroke_by_treatment = model.df.groupby(treatment)[[outcome]].mean()
|
|
161
|
+
stroke_by_treatment.plot(kind='bar', ax=axes[0], title='Before Matching')
|
|
162
|
+
|
|
163
|
+
# Comparing Stroke Mean After Matching
|
|
164
|
+
stroke_by_treatment = model.df_matched.groupby(treatment)[[outcome]].mean()
|
|
165
|
+
stroke_by_treatment.plot(kind='bar', ax=axes[1], title='After Matching')
|
|
166
|
+
|
|
167
|
+
plt.show()
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+

|
|
171
|
+
|
|
172
|
+
# Further Analysis
|
|
173
|
+
---
|
|
174
|
+
Rather than direct comparison between matched test variant and control, you better try use Average Treatment Effect for deeper anaylysis. Here, medium article that I recommend
|
|
175
|
+
[ATE Causal Inference](https://medium.com/grabngoinfo/ate-vs-cate-vs-att-vs-atc-for-causal-inference-998a577f2f8c)
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# Causal Inference using PSM
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
# Background
|
|
5
|
+
Propensity score matching is a statistical technique used to estimate the effect of a treatment or intervention on an outcome of interest. It is commonly used in observational studies, where the assignment of treatment or exposure to a particular group is not randomized.
|
|
6
|
+
|
|
7
|
+
The idea behind propensity score matching is to balance the characteristics of the treatment and control groups by matching individuals with similar propensity scores, which are the probabilities of receiving the treatment or intervention based on observed covariates. This helps to control for confounding factors and reduce selection bias, allowing for a more accurate estimation of the treatment effect.
|
|
8
|
+
|
|
9
|
+
Overall, propensity score matching is a useful tool for researchers to make causal inferences in observational studies, although it is important to consider the limitations and assumptions of this method.
|
|
10
|
+
|
|
11
|
+
# Installation Guide
|
|
12
|
+
This function has been uploaded to [pypi](https://pypi.org/project/causal-inference-aagm/) so you can type on your prompt as code below
|
|
13
|
+
```bash
|
|
14
|
+
pip install causal-inference-aagm
|
|
15
|
+
```
|
|
16
|
+
Then import the library
|
|
17
|
+
```python
|
|
18
|
+
from causalinference_aagm.matching import PropensityScoreMatch
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
If error, download (matching.py)[tsel_aagm/matching.py]
|
|
22
|
+
then
|
|
23
|
+
```
|
|
24
|
+
from matching import *
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
# Requirements Library
|
|
28
|
+
This python requires related package more importantly python_requires='>=3.1', so that package can be install Make sure the other packages meet the requirements below
|
|
29
|
+
- pandas>=1.1.5,
|
|
30
|
+
- numpy>=1.18.5,
|
|
31
|
+
- scipy>=1.2.0,
|
|
32
|
+
- matplotlib>=3.1.0,
|
|
33
|
+
- statsmodels>=0.8.0
|
|
34
|
+
|
|
35
|
+
# Usage Guide
|
|
36
|
+
This is a Python class named PropensityScoreMatch. It is designed to perform propensity score matching, a technique used to balance the distribution of confounding variables between treatment and control groups in observational studies. The class has four input arguments:
|
|
37
|
+
|
|
38
|
+
- df: a pandas DataFrame containing the data to be analyzed.
|
|
39
|
+
- features: a list of column names in df that contain the variables used to calculate propensity scores.
|
|
40
|
+
- treatment: a string that specifies the name of the column in df that contains the treatment variable.
|
|
41
|
+
- outcome: a string that specifies the name of the column in df that contains the outcome variable.
|
|
42
|
+
|
|
43
|
+
The output of the class is two pandas DataFrames:
|
|
44
|
+
|
|
45
|
+
- df_matched: a DataFrame containing the data for the matched pairs of treated and control observations.
|
|
46
|
+
- df_TE: a DataFrame containing the treatment effect estimates for each variable in features.
|
|
47
|
+
|
|
48
|
+
In addition to these output DataFrames, the class provides two methods for visualizing the results of the analysis:
|
|
49
|
+
|
|
50
|
+
- plot_smd(): a method that generates a plot of standardized mean differences (SMDs) between the treatment and control groups for each variable in features.
|
|
51
|
+
- plot_individual_treatment(): a method that generates a plot of the individual treatment effects for each observation in df_matched.
|
|
52
|
+
|
|
53
|
+
For Analysis:
|
|
54
|
+
- plot_smd() : plotting the df_smd
|
|
55
|
+
|
|
56
|
+
# Example Usage
|
|
57
|
+
Importing libraries
|
|
58
|
+
```python
|
|
59
|
+
from causalinference_aagm.matching import PropensityScoreMatch as psm
|
|
60
|
+
import pandas as pd
|
|
61
|
+
import numpy as np
|
|
62
|
+
import matplotlib.pyplot as plt
|
|
63
|
+
import seaborn as sns
|
|
64
|
+
plt.style.use('seaborn')
|
|
65
|
+
```
|
|
66
|
+
Initiating model
|
|
67
|
+
```python
|
|
68
|
+
# Importing Data
|
|
69
|
+
df = pd.read_csv('datasets/healtcare_stroke_data.csv')
|
|
70
|
+
def one_hot_encode(df):
|
|
71
|
+
"""
|
|
72
|
+
One-hot encodes all object data type columns of a Pandas DataFrame.
|
|
73
|
+
"""
|
|
74
|
+
# Get the object columns
|
|
75
|
+
obj_cols = df.select_dtypes(include=['object']).columns
|
|
76
|
+
# One-hot encode the object columns
|
|
77
|
+
df = pd.get_dummies(df, columns=obj_cols)
|
|
78
|
+
return df
|
|
79
|
+
|
|
80
|
+
df = one_hot_encode(df).fillna(0)
|
|
81
|
+
df.head()
|
|
82
|
+
df_model = df[['age','hypertension','heart_disease','bmi','stroke','gender_Male','smoking_status_smokes', 'avg_glucose_level']]
|
|
83
|
+
df_model.head()
|
|
84
|
+
|
|
85
|
+
# Defining Variable
|
|
86
|
+
features = ['age','hypertension','heart_disease','bmi','gender_Male', 'avg_glucose_level']
|
|
87
|
+
treatment = 'smoking_status_smokes'
|
|
88
|
+
outcome = 'stroke'
|
|
89
|
+
|
|
90
|
+
# Matching
|
|
91
|
+
model = psm(df_model, features, treatment, outcome)
|
|
92
|
+
```
|
|
93
|
+
Output:
|
|
94
|
+

|
|
95
|
+
|
|
96
|
+
Evaluating Plot
|
|
97
|
+
```python
|
|
98
|
+
def hist_all_features(df, features, hue):
|
|
99
|
+
width = 6*len(features)
|
|
100
|
+
fig, axes = plt.subplots(ncols=len(features), figsize=(width, 5))
|
|
101
|
+
for i in range(len(features)):
|
|
102
|
+
sns.histplot(data=df, x=features[i], ax=axes[i], hue=hue)
|
|
103
|
+
plt.show()
|
|
104
|
+
```
|
|
105
|
+
```python
|
|
106
|
+
features = ['age','hypertension','heart_disease','bmi','gender_Male','avg_glucose_level','proba']
|
|
107
|
+
hist_all_features(model.df, features, hue='smoking_status_smokes')
|
|
108
|
+
```
|
|
109
|
+
Output:
|
|
110
|
+

|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
hist_all_features(model.df_matched, features, hue='smoking_status_smokes')
|
|
114
|
+
```
|
|
115
|
+

|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
fig, axes = plt.subplots(ncols=2, figsize=(12, 5))
|
|
119
|
+
|
|
120
|
+
# Comparing Stroke Mean without Matching
|
|
121
|
+
stroke_by_treatment = model.df.groupby(treatment)[[outcome]].mean()
|
|
122
|
+
stroke_by_treatment.plot(kind='bar', ax=axes[0], title='Before Matching')
|
|
123
|
+
|
|
124
|
+
# Comparing Stroke Mean After Matching
|
|
125
|
+
stroke_by_treatment = model.df_matched.groupby(treatment)[[outcome]].mean()
|
|
126
|
+
stroke_by_treatment.plot(kind='bar', ax=axes[1], title='After Matching')
|
|
127
|
+
|
|
128
|
+
plt.show()
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+

|
|
132
|
+
|
|
133
|
+
# Further Analysis
|
|
134
|
+
---
|
|
135
|
+
Rather than direct comparison between matched test variant and control, you better try use Average Treatment Effect for deeper anaylysis. Here, medium article that I recommend
|
|
136
|
+
[ATE Causal Inference](https://medium.com/grabngoinfo/ate-vs-cate-vs-att-vs-atc-for-causal-inference-998a577f2f8c)
|