Ejemplo n.º 1
0
    def test_match_r_stddiff(self):
        # Simulated data for variable detection and standardized differences
        df = pd.DataFrame()
        df['treat'] = [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
        df['bin'] = [0, 1, 0, np.nan, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]
        df['con'] = [
            0.1, 0.0, 1.0, 1.1, 2.2, 1.3, 0.1, 0.5, 0.9, 0.5, 0.3, 0.2, 0.7,
            0.9, 1.4
        ]
        df['dis'] = [0, 1, 3, 2, 1, 0, 0, 0, 0, 0, 1, 3, 2, 2, 1]
        df['cat'] = [1, 2, 3, 1, 1, 2, 3, 1, 3, 2, 1, 2, 3, 2, 1]

        ipt = IPTW(df, treatment='treat', stabilized=True)
        ipt.regression_models('bin + con + dis + C(cat)')
        ipt.fit()
        smd = ipt.standardized_mean_differences()

        npt.assert_allclose(np.array(smd['smd_u']),
                            np.array([0.342997, 0.0, 0.06668, -0.513553]),
                            rtol=1e-4)  # for unweighted
        # TODO need to find an R package or something that calculates weighted SMD
        # currently compares to my own calculations
        npt.assert_allclose(np.array(smd['smd_w']),
                            np.array([0.206072, -0.148404, 0.035752,
                                      0.085844]),
                            rtol=1e-4)  # for weighted
Ejemplo n.º 2
0
    def test_match_r_stddiff(self):
        # Simulated data for variable detection and standardized differences
        df = pd.DataFrame()
        df['y'] = [1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0]
        df['treat'] = [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
        df['bin'] = [0, 1, 0, np.nan, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]
        df['con'] = [
            0.1, 0.0, 1.0, 1.1, 2.2, 1.3, 0.1, 0.5, 0.9, 0.5, 0.3, 0.2, 0.7,
            0.9, 1.4
        ]
        df['dis'] = [0, 1, 3, 2, 1, 0, 0, 0, 0, 0, 1, 3, 2, 2, 1]
        df['cat'] = [1, 2, 3, 1, 1, 2, 3, 1, 3, 2, 1, 2, 3, 2, 1]

        ipt = IPTW(df, treatment='treat', outcome='y')
        ipt.treatment_model(model_denominator='bin + con + dis + C(cat)',
                            print_results=False)
        ipt.marginal_structural_model('treat')
        ipt.fit()
        smd = ipt.standardized_mean_differences()

        npt.assert_allclose(np.array(smd['smd_u']),
                            np.array([0.342997, 0.0, 0.06668, -0.513553]),
                            rtol=1e-4)  # for unweighted
        # TODO need to find an R package or something that calculates weighted SMD
        # currently compares to my own calculations
        npt.assert_allclose(np.array(smd['smd_w']),
                            np.array([0.206072, -0.148404, 0.035683,
                                      0.085775]),
                            rtol=1e-4)  # for weighted
Ejemplo n.º 3
0
    def test_standardized_differences(self, sdata):
        ipt = IPTW(sdata, treatment='art', stabilized=True)
        ipt.regression_models('male + age0 + cd40 + dvl0')
        ipt.fit()
        smd = ipt.standardized_mean_differences()

        npt.assert_allclose(np.array(smd['smd_u']),
                            np.array([-0.015684, 0.022311, -0.4867, -0.015729]),
                            rtol=1e-4)  # for unweighted
        # TODO find R package to test these weighted SMD's
        npt.assert_allclose(np.array(smd['smd_w']),
                            np.array([-0.097789, -0.012395, -0.018591, 0.050719]),
                            rtol=1e-4)  # for weighted
Ejemplo n.º 4
0
    def test_standardized_differences(self, sdata):
        ipt = IPTW(sdata, treatment='art', outcome='dead')
        ipt.treatment_model(model_denominator='male + age0 + cd40 + dvl0', print_results=False)
        ipt.marginal_structural_model('art')
        ipt.fit()
        smd = ipt.standardized_mean_differences()

        npt.assert_allclose(np.array(smd['smd_u']),
                            np.array([-0.015684, 0.022311, -0.4867, -0.015729]),
                            rtol=1e-4)  # for unweighted
        # TODO find R package to test these weighted SMD's
        npt.assert_allclose(np.array(smd['smd_w']),
                            np.array([-0.097789, -0.012395, -0.018591, 0.050719]),
                            rtol=1e-4)  # for weighted