Example #1
0
    def test_chi2_mcnemar(self):
        """Test function chi2_mcnemar."""
        # Setup
        np.random.seed(42)
        mean, cov = [0.5, 0.5], [(1, .6), (.6, 1)]
        x, y = np.random.multivariate_normal(mean, cov, 30).T
        data = pd.DataFrame({'x': x, 'y': y})
        mask_class_1 = data > 0.5
        data[mask_class_1] = 1
        data[~mask_class_1] = 0

        # Testing validations
        def expect_assertion_error(*params):
            with pytest.raises(AssertionError):
                pg.chi2_mcnemar(*params)

        expect_assertion_error(1, 'x', 'y')  # Not a pd.DataFrame
        expect_assertion_error(data, x, 'y')  # Not a string
        expect_assertion_error(data, 'x', y)  # Not a string
        expect_assertion_error(data, 'x', 'z')  # Not a column of data

        # Testing happy-day
        pg.chi2_mcnemar(data, 'x', 'y')

        # Testing NaN incompatibility
        data.iloc[0, 0] = np.nan
        with pytest.raises(ValueError):
            pg.chi2_mcnemar(data, 'x', 'y')

        # Testing invalid dichotomous value
        data.iloc[0, 0] = 3
        with pytest.raises(ValueError):
            pg.chi2_mcnemar(data, 'x', 'y')

        # Testing error when b == 0 and c == 0
        data = pd.DataFrame({'x': [0, 0, 0, 1, 1, 1], 'y': [0, 0, 0, 1, 1, 1]})
        with pytest.raises(ValueError):
            pg.chi2_mcnemar(data, 'x', 'y')

        # Comparing results with R
        # 2 x 2 contingency table (dof = 1)
        # >>> tbl = table(df$treatment_X, df$treatment_Y)
        # >>> mcnemar.test(tbl, correct = TRUE)
        _, stats = pg.chi2_mcnemar(df_mcnemar, 'treatment_X', 'treatment_Y')
        assert round(stats.at['mcnemar', 'chi2'], 3) == 20.021
        assert stats.at['mcnemar', 'dof'] == 1
        assert np.isclose(stats.at['mcnemar', 'p-approx'], 7.66e-06)
        # Results are compared to the exact2x2 R package
        # >>> exact2x2(tbl, paired = TRUE, midp = FALSE)
        assert np.isclose(stats.at['mcnemar', 'p-exact'], 3.305e-06)
Example #2
0
 def expect_assertion_error(*params):
     with pytest.raises(AssertionError):
         pg.chi2_mcnemar(*params)