예제 #1
0
    def test_mediation_analysis(self):
        """Test function mediation_analysis.
        """
        ma = mediation_analysis(data=df, x='X', m='M', y='Y', n_boot=500)

        # Compare against R package mediation
        assert_equal(ma['coef'].values,
                     [0.5610, 0.6542, 0.3961, 0.0396, 0.3565])

        _, dist = mediation_analysis(data=df,
                                     x='X',
                                     m='M',
                                     y='Y',
                                     n_boot=1000,
                                     return_dist=True)
        assert dist.size == 1000
        mediation_analysis(data=df, x='X', m='M', y='Y', alpha=0.01)

        # Check with a binary mediator
        ma = mediation_analysis(data=df, x='X', m='Mbin', y='Y', n_boot=2000)
        assert_almost_equal(ma['coef'][0], -0.0208, decimal=2)

        # Indirect effect
        assert_almost_equal(ma['coef'][4], 0.0033, decimal=2)
        assert ma['sig'][4] == 'No'

        # Direct effect
        assert_almost_equal(ma['coef'][3], 0.3956, decimal=2)
        assert_almost_equal(ma['CI[2.5%]'][3], 0.1714, decimal=2)
        assert_almost_equal(ma['CI[97.5%]'][3], 0.617, decimal=1)
        assert ma['sig'][3] == 'Yes'

        # With multiple mediator
        np.random.seed(42)
        df.rename(columns={"M": "M1"}, inplace=True)
        df['M2'] = np.random.randint(0, 10, df.shape[0])
        ma2 = mediation_analysis(data=df,
                                 x='X',
                                 m=['M1', 'M2'],
                                 y='Y',
                                 seed=42)
        assert ma['coef'][2] == ma2['coef'][4]

        # With covariate
        mediation_analysis(data=df, x='X', m='M1', y='Y', covar='M2')
        mediation_analysis(data=df, x='X', m='M1', y='Y', covar=['M2'])
        mediation_analysis(data=df,
                           x='X',
                           m=['M1', 'Ybin'],
                           y='Y',
                           covar=['Mbin', 'M2'])

        # Test helper function _pval_from_bootci
        np.random.seed(123)
        bt2 = np.random.normal(loc=2, size=1000)
        bt3 = np.random.normal(loc=3, size=1000)
        assert _pval_from_bootci(bt2, 0) == 1
        assert _pval_from_bootci(bt2, 0.9) < 0.10
        assert _pval_from_bootci(bt3, 0.9) < _pval_from_bootci(bt2, 0.9)
예제 #2
0
    def test_mediation_analysis(self):
        """Test function mediation_analysis.
        """
        ma = mediation_analysis(data=df, x='X', m='M', y='Y', n_boot=500)

        # Compare against R package mediation
        assert_equal(ma['coef'].round(4).to_numpy(),
                     [0.5610, 0.6542, 0.3961, 0.0396, 0.3565])

        _, dist = mediation_analysis(data=df,
                                     x='X',
                                     m='M',
                                     y='Y',
                                     n_boot=1000,
                                     return_dist=True)
        assert dist.size == 1000
        mediation_analysis(data=df, x='X', m='M', y='Y', alpha=0.01)

        # Check with a binary mediator
        ma = mediation_analysis(data=df, x='X', m='Mbin', y='Y', n_boot=2000)
        assert_almost_equal(ma['coef'][0], -0.0208, decimal=2)

        # Indirect effect
        assert_almost_equal(ma['coef'][4], 0.0033, decimal=2)
        assert ma['sig'][4] == 'No'

        # Direct effect
        assert_almost_equal(ma['coef'][3], 0.3956, decimal=2)
        assert_almost_equal(ma['CI[2.5%]'][3], 0.1714, decimal=2)
        assert_almost_equal(ma['CI[97.5%]'][3], 0.617, decimal=1)
        assert ma['sig'][3] == 'Yes'

        # Check if `logreg_kwargs` is being passed on to `LogisticRegression`
        with pytest.raises(ValueError):
            mediation_analysis(data=df,
                               x='X',
                               m='Mbin',
                               y='Y',
                               n_boot=2000,
                               logreg_kwargs=dict(max_iter=-1))
        # Solve with 0 iterations and make sure that the results are different
        ma = mediation_analysis(data=df,
                                x='X',
                                m='Mbin',
                                y='Y',
                                n_boot=2000,
                                logreg_kwargs=dict(max_iter=0))
        with pytest.raises(AssertionError):
            assert_almost_equal(ma['coef'][0], -0.0208, decimal=2)
        with pytest.raises(AssertionError):
            assert_almost_equal(ma['coef'][4], 0.0033, decimal=3)

        # With multiple mediator
        np.random.seed(42)
        df.rename(columns={"M": "M1"}, inplace=True)
        df['M2'] = np.random.randint(0, 10, df.shape[0])
        ma2 = mediation_analysis(data=df,
                                 x='X',
                                 m=['M1', 'M2'],
                                 y='Y',
                                 seed=42)
        assert ma['coef'][2] == ma2['coef'][4]

        # With covariate
        mediation_analysis(data=df, x='X', m='M1', y='Y', covar='M2')
        mediation_analysis(data=df, x='X', m='M1', y='Y', covar=['M2'])
        mediation_analysis(data=df,
                           x='X',
                           m=['M1', 'Ybin'],
                           y='Y',
                           covar=['Mbin', 'M2'])

        # Test helper function _pval_from_bootci
        np.random.seed(123)
        bt2 = np.random.normal(loc=2, size=1000)
        bt3 = np.random.normal(loc=3, size=1000)
        assert _pval_from_bootci(bt2, 0) == 1
        assert _pval_from_bootci(bt2, 0.9) < 0.10
        assert _pval_from_bootci(bt3, 0.9) < _pval_from_bootci(bt2, 0.9)