def run_mediation(self, predict_col_categorical=False): if predict_col_categorical: probit = links.probit outcome_model = sm.GLM.from_formula( "cont1 ~ rdt1 + jelt1", self.data.X, family=sm.families.Binomial(link=probit())) else: outcome_model = sm.GLM.from_formula("cont1 ~ rdt1 + jelt1", self.data.X) mediator_model = sm.OLS.from_formula("rdt1 ~ jelt1", self.data.X) med = Mediation(outcome_model, mediator_model, "jelt1", mediator="rdt1").fit() with pd.option_context('display.max_rows', None, 'display.max_columns', None): print(med.summary())
df_f.rename({'Full Scale IQ_2': 'IQ', 'le-rCEN': 'le_rCEN'}, axis=1, inplace=True) df_f.rename({('2', 'GID Post'): 'GIDPost'}, axis=1, inplace=True) big_df.rename({('2', 'GID Post'): 'GIDPost'}, axis=1, inplace=True) import statsmodels.api as sm from statsmodels.stats.mediation import Mediation, MediationResults outcome_model = sm.GLM.from_formula("Phy48Grade ~ le_rCEN + IQ", no_na_m) mediator_model = sm.OLS.from_formula("IQ ~ le_rCEN", no_na_m) med = Mediation(outcome_model, mediator_model, "le_rCEN", "IQ").fit() med.summary(alpha=0.01) outcome_model = sm.GLM.from_formula("Phy48Grade ~ le_rCEN + GIDPost", no_na_m) mediator_model = sm.OLS.from_formula("GIDPost ~ le_rCEN", no_na_m) med = Mediation(outcome_model, mediator_model, "le_rCEN", "GIDPost").fit() med.summary(alpha=0.01) #average causal mediation effect (ACME) = a*b = c - c' #average direct effect (ADE) = c' #total effect = a*b + c' = c df_f['HcDMN_phy_minus_gen'] = df_f['fc hippo-default mode phy'] - df_f['fc hippo-default mode gen'] spearmanr(df_f['HcDMN_phy_minus_gen'], df_f['GIDPost'], nan_policy='omit')