예제 #1
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')
예제 #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_check_uniform(self):
        df = pd.DataFrame()
        df['a'] = [0, 0, 1, np.nan]
        df['b'] = [1, 0, 0, np.nan]
        df['c'] = [1, np.nan, np.nan, np.nan]
        ipm = IPMW(df, missing_variable=['a', 'b', 'c'])

        # Should be uniform
        assert ipm._check_uniform(df, miss1='a', miss2='b')

        # Not uniform
        assert not ipm._check_uniform(df, miss1='a', miss2='c')
예제 #4
0
    def test_check_overall_uniform(self):
        df = pd.DataFrame()
        df['a'] = [0, 0, 1, np.nan]
        df['b'] = [1, 0, 0, np.nan]
        df['c'] = [1, 0, 0, np.nan]
        df['d'] = [1, np.nan, np.nan, np.nan]
        ipm = IPMW(df, missing_variable=['a', 'b', 'c'])

        # Should be uniform
        assert ipm._check_overall_uniform(df, miss_vars=['a', 'b', 'c'])[1]

        # Not uniform
        assert not ipm._check_overall_uniform(df, miss_vars=['a', 'b', 'c', 'd'])[1]
예제 #5
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)
예제 #6
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)
예제 #7
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)
예제 #8
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
예제 #9
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)
예제 #10
0
 def test_error_for_non_nan2(self):
     df = pd.DataFrame()
     df['a'] = [0, 0, 1]
     df['b'] = [0, 0, np.nan]
     with pytest.raises(ValueError):
         IPMW(df, missing_variable=['a', 'b'], stabilized=True)
예제 #11
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'])
예제 #12
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_'])
예제 #13
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_'])
예제 #14
0
 def test_error_for_non_nan(self, mdata):
     with pytest.raises(ValueError):
         IPMW(mdata, missing_variable='L', stabilized=True)
예제 #15
0
 def test_missing_count(self, mdata):
     ipm = IPMW(mdata, missing_variable='M', stabilized=True)
     assert 6 == np.sum(ipm.df['_observed_indicator_'])
     assert 4 == np.sum(1 - ipm.df['_observed_indicator_'])