Пример #1
0
 def test_match_rr(self, df):
     aipw = AIPTW(df, exposure='art', outcome='dead')
     aipw.exposure_model('male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0', print_results=False)
     aipw.outcome_model('art + male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0',
                        print_results=False)
     aipw.fit()
     npt.assert_allclose(aipw.risk_ratio, 0.5319812235)
Пример #2
0
 def test_match_rd(self, df):
     aipw = AIPTW(df, exposure='art', outcome='dead')
     aipw.exposure_model('male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0', print_results=False)
     aipw.outcome_model('art + male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0',
                        print_results=False)
     aipw.fit()
     npt.assert_allclose(aipw.risk_difference, -0.0848510605)
Пример #3
0
 def test_poisson_outcomes(self, cf):
     aipw = AIPTW(cf, exposure='art', outcome='cd4_wk45')
     aipw.exposure_model('male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0', print_results=False)
     aipw.outcome_model('art + male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0',
                        continuous_distribution='poisson', print_results=False)
     aipw.fit()
     npt.assert_allclose(aipw.average_treatment_effect, 225.13767, rtol=1e-3)
     npt.assert_allclose(aipw.average_treatment_effect_ci, [118.64677, 331.62858], rtol=1e-3)
Пример #4
0
 def test_weighted_continuous_outcomes(self, cf):
     cf['weights'] = 2
     aipw = AIPTW(cf, exposure='art', outcome='cd4_wk45', weights='weights')
     aipw.exposure_model('male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0', print_results=False)
     aipw.outcome_model('art + male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0',
                        print_results=False)
     aipw.fit()
     npt.assert_allclose(aipw.average_treatment_effect, 225.13767, rtol=1e-3)
     assert aipw.average_treatment_effect_ci is None
Пример #5
0
    def test_bounds2(self, df):
        aipw = AIPTW(df, exposure='art', outcome='dead')
        aipw.exposure_model('male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0',
                            bound=[0.2, 0.9], print_results=False)
        aipw.outcome_model('art + male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0',
                           print_results=False)
        aipw.fit()

        npt.assert_allclose(aipw.risk_difference, -0.0700780176)
        npt.assert_allclose(aipw.risk_difference_ci, (-0.1277925885, -0.0123634468))
Пример #6
0
    def test_bounds(self, df):
        aipw = AIPTW(df, exposure='art', outcome='dead')
        aipw.exposure_model('male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0',
                            bound=0.1, print_results=False)
        aipw.outcome_model('art + male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0',
                           print_results=False)
        aipw.fit()

        npt.assert_allclose(aipw.risk_difference, -0.0819506956)
        npt.assert_allclose(aipw.risk_difference_ci, (-0.1498808287, -0.0140205625))
Пример #7
0
 def test_error_when_no_models_specified3(self, df):
     aipw = AIPTW(df, exposure='art', outcome='dead')
     aipw.outcome_model(
         'art + male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0',
         print_results=False)
     with pytest.raises(ValueError):
         aipw.fit()
Пример #8
0
def causal_check():
    data = load_sample_data(False).drop(columns=['cd4_wk45'])
    data[['cd4_rs1', 'cd4_rs2']] = spline(data,
                                          'cd40',
                                          n_knots=3,
                                          term=2,
                                          restricted=True)
    data[['age_rs1', 'age_rs2']] = spline(data,
                                          'age0',
                                          n_knots=3,
                                          term=2,
                                          restricted=True)

    # Check TimeFixedGFormula diagnostics
    g = TimeFixedGFormula(data, exposure='art', outcome='dead')
    g.outcome_model(
        model=
        'art + male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0'
    )
    g.run_diagnostics(decimal=3)

    # Check IPTW plots
    ipt = IPTW(data, treatment='art', outcome='dead')
    ipt.treatment_model(
        'male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0',
        stabilized=True)
    ipt.marginal_structural_model('art')
    ipt.fit()
    ipt.plot_love()
    plt.tight_layout()
    plt.show()
    ipt.plot_kde()
    plt.show()
    ipt.plot_kde(measure='logit')
    plt.show()
    ipt.plot_boxplot()
    plt.show()
    ipt.plot_boxplot(measure='logit')
    plt.show()
    ipt.run_diagnostics()

    # Check AIPTW Diagnostics
    aipw = AIPTW(data, exposure='art', outcome='dead')
    aipw.exposure_model(
        'male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0')
    aipw.outcome_model(
        'art + male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0'
    )
    aipw.fit()
    aipw.run_diagnostics()
    aipw.plot_kde(to_plot='exposure')
    plt.show()
    aipw.plot_kde(to_plot='outcome')
    plt.show()
    aipw.plot_love()
    plt.show()

    # Check TMLE diagnostics
    tmle = TMLE(data, exposure='art', outcome='dead')
    tmle.exposure_model(
        'male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0')
    tmle.outcome_model(
        'art + male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0'
    )
    tmle.fit()
    tmle.run_diagnostics()
    tmle.plot_kde(to_plot='exposure')
    plt.show()
    tmle.plot_kde(to_plot='outcome')
    plt.show()
    tmle.plot_love()
    plt.show()

    # Check SurvivalGFormula plots
    df = load_sample_data(False).drop(columns=['cd4_wk45'])
    df['t'] = np.round(df['t']).astype(int)
    df = pd.DataFrame(np.repeat(df.values, df['t'], axis=0),
                      columns=df.columns)
    df['t'] = df.groupby('id')['t'].cumcount() + 1
    df.loc[((df['dead'] == 1) & (df['id'] != df['id'].shift(-1))), 'd'] = 1
    df['d'] = df['d'].fillna(0)
    df['t_sq'] = df['t']**2
    df['t_cu'] = df['t']**3
    sgf = SurvivalGFormula(df,
                           idvar='id',
                           exposure='art',
                           outcome='d',
                           time='t')
    sgf.outcome_model(
        model='art + male + age0 + cd40 + dvl0 + t + t_sq + t_cu')
    sgf.fit(treatment='all')
    sgf.plot()
    plt.show()
    sgf.plot(c='r', linewidth=3, alpha=0.8)
    plt.show()
Пример #9
0
    def test_double_robustness(self, dat):
        aipw = AIPTW(dat, exposure='A', outcome='Y')
        aipw.exposure_model('L', print_results=False)
        aipw.outcome_model('L + A + A:L', print_results=False)
        aipw.fit()
        both_correct_rd = aipw.risk_difference
        both_correct_rr = aipw.risk_ratio

        aipw = AIPTW(dat, exposure='A', outcome='Y')
        aipw.exposure_model('L', print_results=False)
        aipw.outcome_model('A + L', print_results=False)
        aipw.fit()
        wrong_y_rd = aipw.risk_difference
        wrong_y_rr = aipw.risk_ratio

        # Testing
        npt.assert_allclose(both_correct_rd, wrong_y_rd)
        npt.assert_allclose(both_correct_rr, wrong_y_rr)

        aipw = AIPTW(dat, exposure='A', outcome='Y')
        aipw.exposure_model('1', print_results=False)
        aipw.outcome_model('A + L + A:L', print_results=False)
        aipw.fit()
        wrong_a_rd = aipw.risk_difference
        wrong_a_rr = aipw.risk_ratio

        # Testing
        npt.assert_allclose(both_correct_rd, wrong_a_rd)
        npt.assert_allclose(both_correct_rr, wrong_a_rr)
Пример #10
0
 def test_error_when_no_models_specified1(self, df):
     aipw = AIPTW(df, exposure='art', outcome='dead')
     with pytest.raises(ValueError):
         aipw.fit()
Пример #11
0
 def test_drop_missing_data(self):
     df = ze.load_sample_data(False)
     aipw = AIPTW(df, exposure='art', outcome='dead')
     assert df.dropna(subset=['cd4_wk45']).shape[0] == aipw.df.shape[0]
Пример #12
0
#############################
# IPTW
from zepid.causal.ipw import IPTW

iptw = IPTW(df, treatment='art', outcome='dead')
iptw.treatment_model('male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0',
                     bound=0.01, print_results=False)
iptw.marginal_structural_model('art')
iptw.fit()
iptw.summary()

#############################
# AIPTW
from zepid.causal.doublyrobust import AIPTW

aipw = AIPTW(df, exposure='art', outcome='dead')
# Treatment model
aipw.exposure_model('male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0',
                    print_results=False, bound=0.01)
# Outcome model
aipw.outcome_model('art + male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0',
                   print_results=False)
# Calculating estimate
aipw.fit()
# Printing summary results
aipw.summary()

#############################
# TMLE
from zepid.causal.doublyrobust import TMLE