Пример #1
0
    def test_compare_tmle_continuous(self, cf):
        cf['cd4_wk45'] = np.log(cf['cd4_wk45'])
        stmle = StochasticTMLE(cf, exposure='art', outcome='cd4_wk45')
        stmle.exposure_model(
            'male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0'
        )
        stmle.outcome_model(
            'art + male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0'
        )
        stmle.fit(p=1.0, samples=1)
        all_treat = stmle.marginal_outcome
        stmle.fit(p=0.0, samples=1)
        non_treat = stmle.marginal_outcome

        tmle = TMLE(cf, exposure='art', outcome='cd4_wk45')
        tmle.exposure_model(
            'male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0',
            print_results=False)
        tmle.outcome_model(
            'art + male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0',
            print_results=False)
        tmle.fit()
        expected = tmle.average_treatment_effect

        npt.assert_allclose(expected, all_treat - non_treat, atol=1e-3)
Пример #2
0
 def test_error_summary(self, df):
     stmle = StochasticTMLE(df=df, exposure='art', outcome='dead')
     stmle.exposure_model(
         'male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0'
     )
     stmle.outcome_model(
         'male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0'
     )
     with pytest.raises(ValueError):
         stmle.summary()
Пример #3
0
    def test_gmodel_bound(self, simple_df):
        # Comparing to SAS Poisson model
        sas_preds = [
            2, 1 / 0.55, 1 / 0.45, 1 / 0.55, 2, 2, 1 / 0.45, 1 / 0.55, 2
        ]

        stmle = StochasticTMLE(df=simple_df, exposure='A', outcome='C')
        stmle.exposure_model('W', bound=[0.45, 0.55])
        est_preds = 1 / stmle._denominator_

        npt.assert_allclose(sas_preds, est_preds, atol=1e-6)
Пример #4
0
    def test_gmodel_params(self, simple_df):
        # Comparing to SAS Poisson model
        sas_preds = [
            2.0, 1.6666666667, 2.5, 1.6666666667, 2, 2, 2.5, 1.6666666667, 2
        ]

        stmle = StochasticTMLE(df=simple_df, exposure='A', outcome='C')
        stmle.exposure_model('W')
        est_preds = 1 / stmle._denominator_

        npt.assert_allclose(sas_preds, est_preds, atol=1e-6)
Пример #5
0
    def test_error_p_cond_len(self, df):
        stmle = StochasticTMLE(df=df, exposure='art', outcome='dead')
        stmle.exposure_model(
            'male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0'
        )
        stmle.outcome_model(
            'male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0'
        )
        with pytest.raises(ValueError):
            stmle.fit(p=[0.1], conditional=["df['male']==1", "df['male']==0"])

        with pytest.raises(ValueError):
            stmle.fit(p=[0.1, 0.3], conditional=["df['male']==1"])
Пример #6
0
    def test_machine_learning_runs(self, df):
        # Only verifies that machine learning doesn't throw an error
        log = LogisticRegression(penalty='l1',
                                 solver='liblinear',
                                 random_state=201)

        tmle = StochasticTMLE(df, exposure='art', outcome='dead')
        tmle.exposure_model(
            'male + age0 + cd40 + cd4_rs1 + cd4_rs2 + dvl0 + male:dvl0',
            custom_model=log)
        tmle.outcome_model('art + male + age0 + dvl0  + cd40',
                           custom_model=log)
        tmle.fit(p=0.4, samples=20)
Пример #7
0
    def test_calculate_epsilon2(self, cf):
        stmle = StochasticTMLE(cf, exposure='art', outcome='cd4_wk45')
        stmle.exposure_model(
            'male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0'
        )
        stmle.outcome_model(
            'art + male + age0 + age_rs1 + age_rs2 + dvl0  + cd40 + cd4_rs1 + cd4_rs2'
        )

        stmle.fit(p=0.15, samples=1)
        npt.assert_allclose(-0.0059476590, stmle.epsilon, atol=1e-6)

        stmle.fit(p=0.4, samples=1)
        npt.assert_allclose(-0.0154923643, stmle.epsilon, atol=1e-6)
Пример #8
0
    def test_calculate_epsilon1(self, df):
        stmle = StochasticTMLE(df, exposure='art', outcome='dead')
        stmle.exposure_model(
            'male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0'
        )
        stmle.outcome_model(
            'art + male + age0 + age_rs1 + age_rs2 + dvl0  + cd40 + cd4_rs1 + cd4_rs2'
        )

        stmle.fit(p=0.15, samples=1)
        npt.assert_allclose(-0.0157043107, stmle.epsilon, atol=1e-6)

        stmle.fit(p=0.4, samples=1)
        npt.assert_allclose(-0.0381559025, stmle.epsilon, atol=1e-6)
Пример #9
0
    def test_compare_tmle_binary(self, df):
        stmle = StochasticTMLE(df, exposure='art', outcome='dead')
        stmle.exposure_model(
            'male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0'
        )
        stmle.outcome_model(
            'art + male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0'
        )
        stmle.fit(p=1.0, samples=1)
        all_treat = stmle.marginal_outcome
        stmle.fit(p=0.0, samples=1)
        non_treat = stmle.marginal_outcome

        tmle = TMLE(df, exposure='art', outcome='dead')
        tmle.exposure_model(
            'male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0',
            print_results=False)
        tmle.outcome_model(
            'art + male + age0 + age_rs1 + age_rs2 + cd40 + cd4_rs1 + cd4_rs2 + dvl0',
            print_results=False)
        tmle.fit()
        expected = tmle.risk_difference

        npt.assert_allclose(expected, all_treat - non_treat, atol=1e-4)
Пример #10
0
     for p in prop_treated] + ['var_' + str(p) for p in prop_treated])

########################################
# Running simulation
########################################
for i in range(n_mc):
    # Generating Data
    H = naloxone_dgm(network=G, restricted=restrict)
    df = network_to_df(H)
    results.loc[i, 'inc_' + exposure] = np.mean(df[exposure])
    results.loc[i, 'inc_' + outcome] = np.mean(df[outcome])

    if independent:
        # Stochastic TMLE
        stmle = StochasticTMLE(df, exposure=exposure, outcome=outcome)
        stmle.exposure_model(gi_model, bound=0.01)
        stmle.outcome_model(qi_model)
        for p in prop_treated:  # loops through all treatment plans
            try:
                if shift:
                    z = odds_to_probability(np.exp(log_odds + p))
                    stmle.fit(p=z)
                else:
                    stmle.fit(p=p)
                results.loc[i, 'bias_' +
                            str(p)] = stmle.marginal_outcome - truth[p]
                results.loc[i, 'var_' + str(p)] = stmle.conditional_se**2
                results.loc[i, 'lcl_' + str(p)] = stmle.conditional_ci[0]
                results.loc[i, 'ucl_' + str(p)] = stmle.conditional_ci[1]
            except:
                results.loc[i, 'bias_' + str(p)] = np.nan
Пример #11
0
 def test_marginal_vector_length_stoch(self, df):
     stmle = StochasticTMLE(df=df, exposure='art', outcome='dead')
     stmle.exposure_model('male')
     stmle.outcome_model('art + male + age0')
     stmle.fit(p=0.4, samples=7)
     assert len(stmle.marginals_vector) == 7