<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Simple example on using Instrumental Variables method for estimation
Step1:  Loading the dataset
We create a fictitious dataset with the goal of estimating the impact of education on future earnings of an individual. The ability  of the individual is a confounder and being given an education_voucher is the instrument.
Step2:  Using DoWhy to estimate the causal effect of education on future income
We follow the four steps
Step3:  We have an estimate, indicating that increasing education by one unit increases income by 4 points.  
Next we check the robustness of the estimate using a Placebo refutation test. In this test, the treatment is replaced by an independent random variable (while preserving the correlation with the instrument), so that the true causal effect should be zero. We check if our estimator also provides the correct answer of zero.
Step4:  The refutation gives confidence that the estimate is not capturing any noise in the data.
Since this is simulated data, we also know the true causal effect is 4 (see the income_education parameter of the data-generating process above)
Finally, we show the same estimation by another method to verify the result from DoWhy.
<ASSISTANT_TASK:>
Python Code:
%load_ext autoreload
%autoreload 2
import numpy as np
import pandas as pd
import patsy as ps
from statsmodels.sandbox.regression.gmm import IV2SLS
import os, sys
from dowhy import CausalModel
Explanation: Simple example on using Instrumental Variables method for estimation
End of explanation
n_points = 1000
education_abilty = 1
education_voucher = 2
income_abilty = 2
income_education = 4
# confounder
ability = np.random.normal(0, 3, size=n_points)
# instrument
voucher = np.random.normal(2, 1, size=n_points) 
# treatment
education = np.random.normal(5, 1, size=n_points) + education_abilty * ability +\
            education_voucher * voucher
# outcome
income = np.random.normal(10, 3, size=n_points) +\
         income_abilty * ability + income_education * education
# build dataset (exclude confounder `ability` which we assume to be unobserved)
data = np.stack([education, income, voucher]).T
df = pd.DataFrame(data, columns = ['education', 'income', 'voucher'])
Explanation: Loading the dataset
We create a fictitious dataset with the goal of estimating the impact of education on future earnings of an individual. The ability  of the individual is a confounder and being given an education_voucher is the instrument.
End of explanation
#Step 1: Model
model=CausalModel(
        data = df,
        treatment='education',
        outcome='income',
        common_causes=['U'],
        instruments=['voucher']
        )
model.view_model()
from IPython.display import Image, display
display(Image(filename="causal_model.png"))
# Step 2: Identify
identified_estimand = model.identify_effect(proceed_when_unidentifiable=True)
print(identified_estimand)
# Step 3: Estimate
#Choose the second estimand: using IV
estimate = model.estimate_effect(identified_estimand,
        method_name="iv.instrumental_variable", test_significance=True)
print(estimate)
Explanation: Using DoWhy to estimate the causal effect of education on future income
We follow the four steps: 
1) model the problem using causal graph, 
2) identify if the causal effect can be estimated from the observed variables, 
3) estimate the effect, and 
4) check the robustness of the estimate.
End of explanation
# Step 4: Refute
ref = model.refute_estimate(identified_estimand, estimate, method_name="placebo_treatment_refuter", placebo_type="permute") # only permute placebo_type works with IV estimate
print(ref)
Explanation: We have an estimate, indicating that increasing education by one unit increases income by 4 points.  
Next we check the robustness of the estimate using a Placebo refutation test. In this test, the treatment is replaced by an independent random variable (while preserving the correlation with the instrument), so that the true causal effect should be zero. We check if our estimator also provides the correct answer of zero.
End of explanation
income_vec, endog = ps.dmatrices("income ~ education", data=df)
exog = ps.dmatrix("voucher", data=df)
m = IV2SLS(income_vec, endog, exog).fit()
m.summary()
Explanation: The refutation gives confidence that the estimate is not capturing any noise in the data.
Since this is simulated data, we also know the true causal effect is 4 (see the income_education parameter of the data-generating process above)
Finally, we show the same estimation by another method to verify the result from DoWhy.
End of explanation
<END_TASK>