InstrumentalVariable#
- class causalpy.experiments.instrumental_variable.InstrumentalVariable[source]#
A class to analyse instrumental variable style experiments.
- Parameters:
instruments_data (
DataFrame
) – A pandas dataframe of instruments for our treatment variable. Should contain instruments Z, and treatment tdata (
DataFrame
) – A pandas dataframe of covariates for fitting the focal regression of interest. Should contain covariates X including treatment t and outcome yinstruments_formula (
str
) – A statistical model formula for the instrumental stage regression e.g. t ~ 1 + z1 + z2 + z3formula (
str
) –A statistical model formula for the
focal regression e.g. y ~ 1 + t + x1 + x2 + x3
model – A PyMC model
priors –
An optional dictionary of priors for the mus and sigmas of both regressions. If priors are not specified we will substitue MLE estimates for the beta coefficients. Greater control can be achieved by specifying the priors directly e.g. priors = {
”mus”: [0, 0], “sigmas”: [1, 1], “eta”: 2, “lkj_sd”: 2, }
Example
>>> import pandas as pd >>> import causalpy as cp >>> from causalpy.pymc_models import InstrumentalVariableRegression >>> import numpy as np >>> N = 100 >>> e1 = np.random.normal(0, 3, N) >>> e2 = np.random.normal(0, 1, N) >>> Z = np.random.uniform(0, 1, N) >>> ## Ensure the endogeneity of the the treatment variable >>> X = -1 + 4 * Z + e2 + 2 * e1 >>> y = 2 + 3 * X + 3 * e1 >>> test_data = pd.DataFrame({"y": y, "X": X, "Z": Z}) >>> sample_kwargs = { ... "tune": 1, ... "draws": 5, ... "chains": 1, ... "cores": 4, ... "target_accept": 0.95, ... "progressbar": False, ... } >>> instruments_formula = "X ~ 1 + Z" >>> formula = "y ~ 1 + X" >>> instruments_data = test_data[["X", "Z"]] >>> data = test_data[["y", "X"]] >>> iv = cp.InstrumentalVariable( ... instruments_data=instruments_data, ... data=data, ... instruments_formula=instruments_formula, ... formula=formula, ... model=InstrumentalVariableRegression(sample_kwargs=sample_kwargs), ... )
Methods
InstrumentalVariable.__init__
(...[, model, ...])InstrumentalVariable.bayesian_plot
(*args, ...)Abstract method for plotting the model.
Two Stage Least Squares Fit
Naive Ordinary Least Squares
Validate the input data and model formula for correctness
InstrumentalVariable.ols_plot
(*args, **kwargs)Abstract method for plotting the model.
InstrumentalVariable.plot
([round_to])Plot the results
Ask the model to print its coefficients.
InstrumentalVariable.summary
([round_to])Print summary of main results and model coefficients.
Attributes
idata
Return the InferenceData object of the model.
supports_bayes
supports_ols
- __init__(instruments_data, data, instruments_formula, formula, model=None, priors=None, **kwargs)[source]#
- __new__(**kwargs)#