InstrumentalVariable#
- class causalpy.experiments.instrumental_variable.InstrumentalVariable[source]#
A class to analyse instrumental variable style experiments.
- Parameters:
instruments_data (pd.DataFrame) – A pandas dataframe of instruments for our treatment variable. Should contain instruments Z, and treatment t.
data (pd.DataFrame) – A pandas dataframe of covariates for fitting the focal regression of interest. Should contain covariates X including treatment t and outcome y.
instruments_formula (str) – A statistical model formula for the instrumental stage regression, e.g.
t ~ 1 + z1 + z2 + z3.formula (str) – A statistical model formula for the focal regression, e.g.
y ~ 1 + t + x1 + x2 + x3.model (BaseExperiment, optional) – A PyMC model. Defaults to None.
priors (dict, optional) – Dictionary of priors for the mus and sigmas of both regressions. If priors are not specified we will substitute MLE estimates for the beta coefficients. Example:
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.fit(*args, **kwargs)Two Stage Least Squares Fit
Naive Ordinary Least Squares
InstrumentalVariable.get_plot_data(*args, ...)Recover the data of an experiment along with the prediction and causal impact information.
Abstract method for recovering plot data.
Abstract method for recovering plot data.
Validate the input data and model formula for correctness
InstrumentalVariable.plot(*args, **kwargs)Plot the results
Ask the model to print its coefficients.
InstrumentalVariable.summary([round_to])Print summary of main results and model coefficients.
Attributes
idataReturn the InferenceData object of the model.
supports_bayessupports_olslabels- __init__(instruments_data, data, instruments_formula, formula, model=None, priors=None, **kwargs)[source]#
- classmethod __new__(*args, **kwargs)#