def test_data_conversion(self, flat_data): ipc = IPCW(flat_data, idvar='id', time='t', event='Y', flat_df=True) expected_data = pd.DataFrame.from_records([{'id': 1, 't_enter': 0, 't_out': 1, 'A': 1, 'Y': 0.0, '__uncensored__': 0}, {'id': 2, 't_enter': 0, 't_out': 1, 'A': 1, 'Y': 0.0, '__uncensored__': 1}, {'id': 2, 't_enter': 1, 't_out': 2, 'A': 1, 'Y': 0.0, '__uncensored__': 0}, {'id': 3, 't_enter': 0, 't_out': 1, 'A': 0, 'Y': 0.0, '__uncensored__': 1}, {'id': 3, 't_enter': 1, 't_out': 2, 'A': 0, 'Y': 0.0, '__uncensored__': 1}, {'id': 3, 't_enter': 2, 't_out': 3, 'A': 0, 'Y': 1.0, '__uncensored__': 1}, {'id': 4, 't_enter': 0, 't_out': 1, 'A': 0, 'Y': 0.0, '__uncensored__': 1}, {'id': 4, 't_enter': 1, 't_out': 2, 'A': 0, 'Y': 0.0, '__uncensored__': 1}, {'id': 4, 't_enter': 2, 't_out': 3, 'A': 0, 'Y': 0.0, '__uncensored__': 1}, {'id': 5, 't_enter': 0, 't_out': 1, 'A': 0, 'Y': 0.0, '__uncensored__': 1}, {'id': 5, 't_enter': 1, 't_out': 2, 'A': 0, 'Y': 1.0, '__uncensored__': 1}] ) pdt.assert_frame_equal(ipc.df[['id', 'A', 'Y', 't_enter', 't_out', '__uncensored__']], expected_data[['id', 'A', 'Y', 't_enter', 't_out', '__uncensored__']], check_dtype=False, check_index_type=False, check_like=True)
def test_data_late_entry_error(self): df = pd.DataFrame() df['id'] = [1, 1, 2, 2, 3, 4, 4, 5, 5] df['t'] = [1, 2, 1, 2, 2, 1, 2, 1, 2] df['Y'] = [0, 0, 0, 1, 0, 0, 1, 0, 1] with pytest.raises(ValueError): IPCW(df, idvar='id', time='t', event='Y', flat_df=False)
def test_data_conversion_late_entry(self, flat_data2): with pytest.raises(ValueError): IPCW(flat_data2, idvar='id', time='t', event='Y', enter='enter', flat_df=True)
def test_match_sas_weights(self): sas_w_mean = 0.9993069 sas_w_max = 1.7980410 sas_w_min = 0.8986452 df = load_sample_data(timevary=True) df['cd40_q'] = df['cd40'] ** 2 df['cd40_c'] = df['cd40'] ** 3 df['cd4_q'] = df['cd4'] ** 2 df['cd4_c'] = df['cd4'] ** 3 df['enter_q'] = df['enter'] ** 2 df['enter_c'] = df['enter'] ** 3 df['age0_q'] = df['age0'] ** 2 df['age0_c'] = df['age0'] ** 3 ipc = IPCW(df, idvar='id', time='enter', event='dead') cmodeln = 'enter + enter_q + enter_c' cmodeld = '''enter + enter_q + enter_c + male + age0 + age0_q + age0_c + dvl0 + cd40 + cd40_q + cd40_c + dvl + cd4 + cd4_q + cd4_c''' ipc.regression_models(model_denominator=cmodeld, model_numerator=cmodeln) ipc.fit() cw = ipc.Weight npt.assert_allclose(np.mean(cw), sas_w_mean) npt.assert_allclose(np.max(cw), sas_w_max) npt.assert_allclose(np.min(cw), sas_w_min)
def test_data_conversion_warning(self, flat_data): with pytest.warns(UserWarning): IPCW(flat_data, idvar='id', time='t', event='Y', flat_df=True)
def test_error_time_is_only_one(self, edata2): with pytest.raises(ValueError): IPCW(edata2, idvar='id', time='t', event='Y')
def test_error_missing_times(self, edata): with pytest.raises(ValueError): IPCW(edata, idvar='id', time='t', event='Y')