Пример #1
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)
Пример #2
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)
Пример #3
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)
Пример #4
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
Пример #5
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)