#import libraries
import pandas as pd
import numpy as np
import seaborn as sns
import scipy

#retrieving the data
dataset = pd.read_csv("lalonde.csv")
dataset.groupby('treat').mean()    
pd.set_option('display.max_columns', None)

# get a list of all contrinuous variables' names
continuous_confounders = ["age", "educ", "re74", "re75"]
  
# create an empty dictionary
t_test_results = {}

# loop over column_list and execute code explained above
for variable in continuous_confounders:
    group1 = dataset.where(dataset.treat== 0).dropna()[variable]
    group2 = dataset.where(dataset.treat== 1).dropna()[variable]
    t_test_results[variable] = scipy.stats.ttest_ind(group1,group2)
results = pd.DataFrame.from_dict(t_test_results,orient='Index')
results.columns = ['statistic','pvalue']

#isolating treatment and confounders
treat = dataset.iloc[:, 0]
confounders= dataset.iloc[:, 1:-1]

#logistic regression
import statsmodels.api as sm
confounders = sm.add_constant(confounders)
propensity_model = sm.Logit(treat, confounders).fit()
propensity_model.summary()

#predicting the propensity of being treated
propensity_score = propensity_model.predict(confounders)

#create dataframe with treated and propensities
propensity_dataframe = np.vstack([treat, propensity_score])
propensity_dataframe = np.transpose(propensity_dataframe)

#finish preparations for common support region
non_treated = propensity_dataframe[:,0] == 0
non_treated = propensity_dataframe[non_treated]
non_treated = non_treated[:, 1]
treated = propensity_dataframe[:,0] == 1
treated = propensity_dataframe[treated]
treated = treated[:, 1]

#Common support region
plot_non_treated = sns.kdeplot(non_treated, shade = True, color = "r")
plot_treated = sns.kdeplot(treated, shade = True, color = "b")

#isolating Y, treat and confounders
treat = dataset.iloc[:, 0].values
confounders= dataset.iloc[:, 1:-1].values
Y = dataset.iloc[:, -1].values

#import causal inference library
#pip install CausalInference
from causalinference import CausalModel
propensity_model = CausalModel(Y, treat, confounders)
propensity_model.est_propensity_s()
propensity_model.est_via_matching(bias_adj = True)
print(propensity_model.estimates)
print(propensity_model.propensity)





























b643f0abfa1f9ec65f45089cf4f6fffa9eeccb45
<mask token>

<mask token>
dataset.groupby('treat').mean()
pd.set_option('display.max_columns', None)
<mask token>
for variable in continuous_confounders:
    group1 = dataset.where(dataset.treat == 0).dropna()[variable]
    group2 = dataset.where(dataset.treat == 1).dropna()[variable]
    t_test_results[variable] = scipy.stats.ttest_ind(group1, group2)
<mask token>
propensity_model.summary()
<mask token>
propensity_model.est_propensity_s()
propensity_model.est_via_matching(bias_adj=True)
print(propensity_model.estimates)
print(propensity_model.propensity)

<mask token>
dataset = pd.read_csv('lalonde.csv')
dataset.groupby('treat').mean()
pd.set_option('display.max_columns', None)
continuous_confounders = ['age', 'educ', 're74', 're75']
t_test_results = {}
for variable in continuous_confounders:
    group1 = dataset.where(dataset.treat == 0).dropna()[variable]
    group2 = dataset.where(dataset.treat == 1).dropna()[variable]
    t_test_results[variable] = scipy.stats.ttest_ind(group1, group2)
results = pd.DataFrame.from_dict(t_test_results, orient='Index')
results.columns = ['statistic', 'pvalue']
treat = dataset.iloc[:, 0]
confounders = dataset.iloc[:, 1:-1]
<mask token>
confounders = sm.add_constant(confounders)
propensity_model = sm.Logit(treat, confounders).fit()
propensity_model.summary()
propensity_score = propensity_model.predict(confounders)
propensity_dataframe = np.vstack([treat, propensity_score])
propensity_dataframe = np.transpose(propensity_dataframe)
non_treated = propensity_dataframe[:, 0] == 0
non_treated = propensity_dataframe[non_treated]
non_treated = non_treated[:, 1]
treated = propensity_dataframe[:, 0] == 1
treated = propensity_dataframe[treated]
treated = treated[:, 1]
plot_non_treated = sns.kdeplot(non_treated, shade=True, color='r')
plot_treated = sns.kdeplot(treated, shade=True, color='b')
treat = dataset.iloc[:, 0].values
confounders = dataset.iloc[:, 1:-1].values
Y = dataset.iloc[:, -1].values
<mask token>
propensity_model = CausalModel(Y, treat, confounders)
propensity_model.est_propensity_s()
propensity_model.est_via_matching(bias_adj=True)
print(propensity_model.estimates)
print(propensity_model.propensity)

import pandas as pd
import numpy as np
import seaborn as sns
import scipy
dataset = pd.read_csv('lalonde.csv')
dataset.groupby('treat').mean()
pd.set_option('display.max_columns', None)
continuous_confounders = ['age', 'educ', 're74', 're75']
t_test_results = {}
for variable in continuous_confounders:
    group1 = dataset.where(dataset.treat == 0).dropna()[variable]
    group2 = dataset.where(dataset.treat == 1).dropna()[variable]
    t_test_results[variable] = scipy.stats.ttest_ind(group1, group2)
results = pd.DataFrame.from_dict(t_test_results, orient='Index')
results.columns = ['statistic', 'pvalue']
treat = dataset.iloc[:, 0]
confounders = dataset.iloc[:, 1:-1]
import statsmodels.api as sm
confounders = sm.add_constant(confounders)
propensity_model = sm.Logit(treat, confounders).fit()
propensity_model.summary()
propensity_score = propensity_model.predict(confounders)
propensity_dataframe = np.vstack([treat, propensity_score])
propensity_dataframe = np.transpose(propensity_dataframe)
non_treated = propensity_dataframe[:, 0] == 0
non_treated = propensity_dataframe[non_treated]
non_treated = non_treated[:, 1]
treated = propensity_dataframe[:, 0] == 1
treated = propensity_dataframe[treated]
treated = treated[:, 1]
plot_non_treated = sns.kdeplot(non_treated, shade=True, color='r')
plot_treated = sns.kdeplot(treated, shade=True, color='b')
treat = dataset.iloc[:, 0].values
confounders = dataset.iloc[:, 1:-1].values
Y = dataset.iloc[:, -1].values
from causalinference import CausalModel
propensity_model = CausalModel(Y, treat, confounders)
propensity_model.est_propensity_s()
propensity_model.est_via_matching(bias_adj=True)
print(propensity_model.estimates)
print(propensity_model.propensity)

#import libraries
import pandas as pd
import numpy as np
import seaborn as sns
import scipy

#retrieving the data
dataset = pd.read_csv("lalonde.csv")
dataset.groupby('treat').mean()    
pd.set_option('display.max_columns', None)

# get a list of all contrinuous variables' names
continuous_confounders = ["age", "educ", "re74", "re75"]
  
# create an empty dictionary
t_test_results = {}

# loop over column_list and execute code explained above
for variable in continuous_confounders:
    group1 = dataset.where(dataset.treat== 0).dropna()[variable]
    group2 = dataset.where(dataset.treat== 1).dropna()[variable]
    t_test_results[variable] = scipy.stats.ttest_ind(group1,group2)
results = pd.DataFrame.from_dict(t_test_results,orient='Index')
results.columns = ['statistic','pvalue']

#isolating treatment and confounders
treat = dataset.iloc[:, 0]
confounders= dataset.iloc[:, 1:-1]

#logistic regression
import statsmodels.api as sm
confounders = sm.add_constant(confounders)
propensity_model = sm.Logit(treat, confounders).fit()
propensity_model.summary()

#predicting the propensity of being treated
propensity_score = propensity_model.predict(confounders)

#create dataframe with treated and propensities
propensity_dataframe = np.vstack([treat, propensity_score])
propensity_dataframe = np.transpose(propensity_dataframe)

#finish preparations for common support region
non_treated = propensity_dataframe[:,0] == 0
non_treated = propensity_dataframe[non_treated]
non_treated = non_treated[:, 1]
treated = propensity_dataframe[:,0] == 1
treated = propensity_dataframe[treated]
treated = treated[:, 1]

#Common support region
plot_non_treated = sns.kdeplot(non_treated, shade = True, color = "r")
plot_treated = sns.kdeplot(treated, shade = True, color = "b")

#isolating Y, treat and confounders
treat = dataset.iloc[:, 0].values
confounders= dataset.iloc[:, 1:-1].values
Y = dataset.iloc[:, -1].values

#import causal inference library
#pip install CausalInference
from causalinference import CausalModel
propensity_model = CausalModel(Y, treat, confounders)
propensity_model.est_propensity_s()
propensity_model.est_via_matching(bias_adj = True)
print(propensity_model.estimates)
print(propensity_model.propensity)




























