示例#1
0
 def test_unstabilized_weights(self):
     df = load_sample_data(False)
     ipm = IPMW(df, missing_variable='dead', stabilized=False)
     ipm.regression_models(model_denominator='male + age0 + dvl0 + cd40')
     ipm.fit()
     npt.assert_allclose(np.mean(ipm.Weight), 1.0579602715)
     npt.assert_allclose(np.std(ipm.Weight, ddof=1), 0.021019729152)
示例#2
0
 def test_nonmonotone_detection(self):
     df = pd.DataFrame()
     df['a'] = [0, 0, np.nan]
     df['b'] = [np.nan, 0, 1]
     ipm = IPMW(df, missing_variable=['a', 'b'])
     with pytest.raises(ValueError):
         ipm.regression_models(model_denominator=['b', 'a'])
示例#3
0
 def test_stabilized_weights(self):
     df = load_sample_data(False)
     ipm = IPMW(df, missing_variable='dead', stabilized=True)
     ipm.regression_models(model_denominator='male + age0 + dvl0 + cd40')
     ipm.fit()
     npt.assert_allclose(np.mean(ipm.Weight), 0.99993685627)
     npt.assert_allclose(np.std(ipm.Weight, ddof=1), 0.019866910369)
示例#4
0
 def test_error_numerator_with_unstabilized(self):
     df = load_sample_data(False)
     ipm = IPMW(df, missing_variable='dead', stabilized=False)
     with pytest.raises(ValueError):
         ipm.regression_models(
             model_denominator='male + age0 + dvl0 + cd40',
             model_numerator='male')
示例#5
0
    def test_weighted_model(self):
        df = load_sample_data(False).drop(columns=['dead'])
        df['age_sq'] = df['age0'] ** 2
        df['age_cu'] = df['age0'] ** 3
        df['cd4_sq'] = df['cd40'] ** 2
        df['cd4_cu'] = df['cd40'] ** 3

        ipmw = IPMW(df, missing_variable='cd4_wk45')
        ipmw.regression_models('art + male + age0 + age_sq + age_cu + cd40 + cd4_sq + cd4_cu + dvl0',
                               print_results=False)
        ipmw.fit()
        df['ipcw'] = ipmw.Weight

        snm = GEstimationSNM(df.dropna(), exposure='art', outcome='cd4_wk45', weights='ipcw')
        snm.exposure_model('male + age0 + age_sq + age_cu + cd40 + cd4_sq + cd4_cu + dvl0', print_results=False)
        snm.structural_nested_model('art')
        snm.fit()

        npt.assert_allclose(snm.psi, [244.379181], atol=1e-5)

        snm = GEstimationSNM(df.dropna(), exposure='art', outcome='cd4_wk45', weights='ipcw')
        snm.exposure_model('male + age0 + age_sq + age_cu + cd40 + cd4_sq + cd4_cu + dvl0', print_results=False)
        snm.structural_nested_model('art')
        snm.fit(solver='search')

        npt.assert_allclose(snm.psi, [244.379181], atol=1e-5)
示例#6
0
 def test_single_model_does_not_break(self):
     df = load_monotone_missing_data()
     ipm = IPMW(df,
                missing_variable=['B', 'C'],
                stabilized=False,
                monotone=True)
     ipm.regression_models(model_denominator='L')
     ipm.fit()
     x = ipm.Weight
示例#7
0
 def test_monotone_example(self):
     # TODO find R or SAS to test against
     df = load_monotone_missing_data()
     ipm = IPMW(df, missing_variable=['B', 'C'], stabilized=False, monotone=True)
     ipm.regression_models(model_denominator=['L + A', 'L + B'])
     ipm.fit()
     df['w'] = ipm.Weight
     dfs = df.dropna(subset=['w'])
     npt.assert_allclose(np.average(dfs['B'], weights=dfs['w']), 0.41877344861340654)
     npt.assert_allclose(np.average(dfs['C'], weights=dfs['w']), 0.5637116735464095)
示例#8
0
 def test_error_too_many_model(self):
     df = load_sample_data(False)
     ipm = IPMW(df, missing_variable='dead')
     with pytest.raises(ValueError):
         ipm.regression_models(
             model_denominator=['male + age0', 'male + age0 + dvl0'])
示例#9
0
 def test_missing_count2(self):
     df = load_sample_data(False)
     ipm = IPMW(df, missing_variable='dead', stabilized=True)
     ipm.regression_models(model_denominator='art')
     assert 517 == np.sum(ipm.df['_observed_indicator_'])
     assert 30 == np.sum(1 - ipm.df['_observed_indicator_'])
示例#10
0
 def test_missing_count(self, mdata):
     ipm = IPMW(mdata, missing_variable='M', stabilized=True)
     ipm.regression_models(model_denominator='A')
     assert 6 == np.sum(ipm.df['_observed_indicator_'])
     assert 4 == np.sum(1 - ipm.df['_observed_indicator_'])