class statsmodels.stats.mediation.Mediation(outcome_model, mediator_model, exposure, mediator=None, moderators=None, outcome_fit_kwargs=None, mediator_fit_kwargs=None)
[source]
Conduct a mediation analysis.
Parameters: |
|
---|
The mediator model class must implement get_distribution
.
A basic mediation analysis using formulas:
>>> import statsmodels.api as sm >>> import statsmodels.genmod.families.links as links >>> probit = links.probit >>> outcome_model = sm.GLM.from_formula("cong_mesg ~ emo + treat + age + educ + gender + income", ... data, family=sm.families.Binomial(link=probit())) >>> mediator_model = sm.OLS.from_formula("emo ~ treat + age + educ + gender + income", data) >>> med = Mediation(outcome_model, mediator_model, "treat", "emo").fit() >>> med.summary()
A basic mediation analysis without formulas. This may be slightly faster than the approach using formulas. If there are any interactions involving the treatment or mediator variables this approach will not work, you must use formulas.
>>> import patsy >>> outcome = np.asarray(data["cong_mesg"]) >>> outcome_exog = patsy.dmatrix("emo + treat + age + educ + gender + income", data, ... return_type='dataframe') >>> probit = sm.families.links.probit >>> outcome_model = sm.GLM(outcome, outcome_exog, family=sm.families.Binomial(link=probit())) >>> mediator = np.asarray(data["emo"]) >>> mediator_exog = patsy.dmatrix("treat + age + educ + gender + income", data, ... return_type='dataframe') >>> mediator_model = sm.OLS(mediator, mediator_exog) >>> tx_pos = [outcome_exog.columns.tolist().index("treat"), ... mediator_exog.columns.tolist().index("treat")] >>> med_pos = outcome_exog.columns.tolist().index("emo") >>> med = Mediation(outcome_model, mediator_model, tx_pos, med_pos).fit() >>> med.summary()
A moderated mediation analysis. The mediation effect is computed for people of age 20.
>>> fml = "cong_mesg ~ emo + treat*age + emo*age + educ + gender + income", >>> outcome_model = sm.GLM.from_formula(fml, data, ... family=sm.families.Binomial()) >>> mediator_model = sm.OLS.from_formula("emo ~ treat*age + educ + gender + income", data) >>> moderators = {"age" : 20} >>> med = Mediation(outcome_model, mediator_model, "treat", "emo", ... moderators=moderators).fit()
Imai, Keele, Tingley (2010). A general approach to causal mediation analysis. Psychological Methods 15:4, 309-334. http://imai.princeton.edu/research/files/BaronKenny.pdf
Tingley, Yamamoto, Hirose, Keele, Imai (2014). mediation : R package for causal mediation analysis. Journal of Statistical Software 59:5. http://www.jstatsoft.org/v59/i05/paper
fit ([method, n_rep]) | Fit a regression model to assess mediation. |
© 2009–2012 Statsmodels Developers
© 2006–2008 Scipy Developers
© 2006 Jonathan E. Taylor
Licensed under the 3-clause BSD License.
http://www.statsmodels.org/stable/generated/statsmodels.stats.mediation.Mediation.html