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)
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"])
def test_error_p_oob(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=1.1) with pytest.raises(ValueError): stmle.fit(p=-0.1)
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)
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)
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)
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)
# 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 results.loc[i, 'var_' + str(p)] = np.nan results.loc[i, 'lcl_' + str(p)] = np.nan results.loc[i, 'ucl_' + str(p)] = np.nan else: # Network TMLE
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