Example #1
0
def test_darfur_Sensemakr():
    darfur_out = main.Sensemakr(model=model,
                                treatment='directlyharmed',
                                benchmark_covariates='female',
                                kd=[1, 2, 3])
    darfur_out.summary()
    darfur_out.print()
    darfur_out.ovb_minimal_reporting(format='html', display=False)
    darfur_out.ovb_minimal_reporting(display=False)
    ovb_contour_plot(sense_obj=darfur_out)
    ovb_extreme_plot(sense_obj=darfur_out)
    # info
    assert (darfur_out.treatment == 'directlyharmed')
    assert (darfur_out.q == 1)
    assert (darfur_out.alpha == 0.05)
    assert (darfur_out.reduce == True)
    assert (darfur_out.sensitivity_stats['dof'] == 783)
    np.testing.assert_allclose(darfur_out.sensitivity_stats['r2yd_x'],
                               0.02187,
                               atol=1e-5)
    np.testing.assert_allclose(darfur_out.sensitivity_stats['rv_q'],
                               0.13878,
                               atol=1e-5)
    np.testing.assert_allclose(darfur_out.sensitivity_stats['rv_qa'],
                               0.07626,
                               atol=1e-5)
    data = [["1x female", "2x female", "3x female"],
            [0.00916428667504862, 0.0183285733500972, 0.0274928600251459],
            [0.12464092303637, 0.249324064199975, 0.374050471038094],
            ["directlyharmed", "directlyharmed", "directlyharmed"],
            [0.0752202712144491, 0.0529151723844518, 0.0303960234641548],
            [0.0218733277437572, 0.0203500620779637, 0.0186700648170924],
            [3.43890386024675, 2.60024623913809, 1.62806202131271],
            [0.032282966, 0.012968035, -0.006253282],
            [0.11815758, 0.09286231, 0.06704533]]
    data = np.array(data).T.tolist()
    df = pd.DataFrame(data,
                      columns=[
                          "bound_label", "r2dz_x", "r2yz_dx", "treatment",
                          "adjusted_estimate", "adjusted_se", "adjusted_t",
                          "adjusted_lower_CI", "adjusted_upper_CI"
                      ])

    df[['r2dz_x','r2yz_dx','adjusted_estimate','adjusted_se','adjusted_t','adjusted_lower_CI','adjusted_upper_CI']] =\
    df[['r2dz_x','r2yz_dx','adjusted_estimate','adjusted_se','adjusted_t','adjusted_lower_CI','adjusted_upper_CI']].astype(float)
    assert (darfur_out.bounds.round(6).equals(df.round(6)))

    darfur_out2 = main.Sensemakr(model=model, treatment='directlyharmed')
    darfur_out3 = main.Sensemakr(model=model,
                                 treatment='directlyharmed',
                                 q=1.0,
                                 alpha=0.05,
                                 reduce=True)
    darfur_out3.ovb_minimal_reporting(format='html', display=False)
    darfur_out3.ovb_minimal_reporting(display=False)
    ovb_contour_plot(sense_obj=darfur_out2)
    ovb_extreme_plot(sense_obj=darfur_out2)
Example #2
0
def test_group_bench():
    # exact
    n = 1000
    X = list(range(1, n + 1))
    z1 = resid_maker(n, pd.DataFrame({'x': X}))
    z2 = resid_maker(n, pd.DataFrame({'Z1': z1}))
    x1 = resid_maker(n, pd.DataFrame({'Z1': z1, 'Z2': z2}))
    x2 = resid_maker(n, pd.DataFrame({'Z1': z1, 'Z2': z2, 'X1': x1}))
    d = 2 * x1 + x2 + 2 * z1 + z2 + resid_maker(
        n, pd.DataFrame({
            'Z1': z1,
            'Z2': z2,
            'X1': x1,
            "X2": x2
        })) * 5
    y = 2 * x1 + x2 + 2 * z1 + z2 + resid_maker(
        n, pd.DataFrame({
            'Z1': z1,
            'Z2': z2,
            'X1': x1,
            "X2": x2,
            'D': d
        })) * 5
    df = pd.DataFrame({'z1': z1, 'z2': z2, 'x1': x1, 'x2': x2, 'd': d, 'y': y})
    model = smf.ols(formula='y~d+x1+x2', data=df).fit()
    model_dz = smf.ols(formula='y~d+z1+z2', data=df).fit()
    r2yx = group_partial_r2(model=model, covariates=['x1', 'x2'])
    r2yz = group_partial_r2(model=model_dz, covariates=['z1', 'z2'])
    ky = r2yz / r2yx
    np.testing.assert_allclose(ky, 1, atol=1e-7)
    model_d = smf.ols(formula='d~x1+x2', data=df).fit()
    model_dz = smf.ols(formula='d~z1+z2', data=df).fit()
    r2dx = group_partial_r2(model=model_d, covariates=['x1', 'x2'])
    r2dz = group_partial_r2(model=model_dz, covariates=['z1', 'z2'])
    kd = r2dz / r2dx
    np.testing.assert_allclose(kd, 1, atol=1e-7)
    out = main.Sensemakr(model=model,
                         treatment="d",
                         benchmark_covariates=[['x1', 'x2']],
                         kd=kd,
                         ky=ky)
    out2 = main.Sensemakr(
        model=model,
        treatment="d",
        benchmark_covariates={'[' + "'x1', 'x2'" + ']': ['x1', 'x2']},
        kd=kd,
        ky=ky)
    out3 = main.Sensemakr(
        model=model,
        treatment="d",
        benchmark_covariates={'[' + "'x1', 'x2'" + ']': ['x1', 'x2']},
        kd=[1, 2, 3])
    bound = ovb_partial_r2_bound(model=model, treatment="d")
    bound2 = ovb_partial_r2_bound(model=model,
                                  treatment="d",
                                  benchmark_covariates='x1')
    assert (out.bounds.equals(out2.bounds))
Example #3
0
def test_darfur_manual_bounds():
    sense_out = main.Sensemakr(model=model,
                               treatment='directlyharmed',
                               benchmark_covariates='female',
                               r2dz_x=0.1)
    sense_out.summary()
    bounds_check = sense_out.bounds
    to_check = bounds_check.adjusted_se
    true_check = adjusted_se(model=model,
                             treatment='directlyharmed',
                             r2dz_x=0.1,
                             r2yz_dx=0.1)
    assert (to_check.values[0] == true_check)
Example #4
0
def test_darfur_different_q():
    darfur_out = main.Sensemakr(model=model,
                                treatment='directlyharmed',
                                benchmark_covariates='female',
                                q=2,
                                kd=[1, 2, 3])
    rvq = darfur_out.sensitivity_stats['rv_q']
    rvqa = darfur_out.sensitivity_stats['rv_qa']
    assert (rvq == robustness_value(model=model,
                                    covariates='directlyharmed',
                                    q=2).values)
    assert (rvqa == robustness_value(model=model,
                                     covariates='directlyharmed',
                                     q=2,
                                     alpha=0.05).values)
Example #5
0
def test_darfur_sensemakr_manually():
    model_treat = smf.ols(
        formula='directlyharmed ~  age + farmer_dar + herder_dar +\
                pastvoted + hhsize_darfur + female + village',
        data=darfur).fit()
    darfur_out = main.Sensemakr(estimate=0.09731582,
                                se=0.02325654,
                                dof=783,
                                treatment="directlyharmed",
                                benchmark_covariates="female",
                                r2dxj_x=partial_r2(model_treat,
                                                   covariates="female"),
                                r2yxj_dx=partial_r2(model,
                                                    covariates="female"),
                                kd=[1, 2, 3])
    assert (darfur_out.q == 1)
    assert (darfur_out.alpha == 0.05)
    assert (darfur_out.treatment == "D")
    assert (darfur_out.reduce == True)
    assert (darfur_out.sensitivity_stats['dof'] == 783)
    np.testing.assert_allclose(darfur_out.sensitivity_stats['r2yd_x'],
                               0.02187,
                               atol=1e-4)
    np.testing.assert_allclose(darfur_out.sensitivity_stats['rv_q'],
                               0.13878,
                               atol=1e-5)
    np.testing.assert_allclose(darfur_out.sensitivity_stats['rv_qa'],
                               0.07626,
                               atol=1e-5)
    data = [["1x female", "2x female", "3x female"],
            [0.00916428667504862, 0.0183285733500972, 0.0274928600251459],
            [0.12464092303637, 0.249324064199975, 0.374050471038094],
            [0.0752202698486415, 0.0529151689180575, 0.0303960178770157],
            [0.0218733298036818, 0.0203500639944344, 0.0186700665753491],
            [3.43890347394571, 2.60024582392121, 1.62806156873318],
            [0.0322829603180086, 0.0129680276030601, -0.00625329133645187],
            [0.118157579379274, 0.092862310233055, 0.0670453270904833]]
    data = np.array(data).T.tolist()
    df = pd.DataFrame(data,
                      columns=[
                          "bound_label", "r2dz_x", "r2yz_dx",
                          "adjusted_estimate", "adjusted_se", "adjusted_t",
                          "adjusted_lower_CI", "adjusted_upper_CI"
                      ])

    df[['r2dz_x','r2yz_dx','adjusted_estimate','adjusted_se','adjusted_t','adjusted_lower_CI','adjusted_upper_CI']] =\
    df[['r2dz_x','r2yz_dx','adjusted_estimate','adjusted_se','adjusted_t','adjusted_lower_CI','adjusted_upper_CI']].astype(float)
    assert (darfur_out.bounds.round(6).equals(df.round(6)))
def test_manual_input():
    out = main.Sensemakr(estimate=2,
                         se=2,
                         dof=100,
                         r2dz_x=0.2,
                         r2dxj_x=0.2,
                         r2yxj_dx=0.2,
                         kd=2,
                         ky=1)
    out.summary()
    est = out.bounds['adjusted_estimate'][0]
    est_check = adjusted_estimate(estimate=2,
                                  se=2,
                                  dof=100,
                                  r2dz_x=0.2,
                                  r2yz_dx=0.2)
    assert (est == est_check)
Example #7
0
def test_darfur_group_benchmarks():
    village = [k for k in model.params.index.values if 'village' in k]
    sensitivity = main.Sensemakr(model=model,
                                 treatment='directlyharmed',
                                 benchmark_covariates=[village],
                                 kd=0.3)
    r2y = group_partial_r2(model, covariates=village)
    treat_model = smf.ols(
        formula='directlyharmed ~  age + farmer_dar + herder_dar +\
                pastvoted + hhsize_darfur + female + village',
        data=darfur).fit()
    r2d = group_partial_r2(treat_model, covariates=village)
    bounds_check = ovb_partial_r2_bound(r2dxj_x=r2d,
                                        r2yxj_dx=r2y,
                                        kd=0.3,
                                        benchmark_covariates=['manual'])
    bounds = sensitivity.bounds
    np.testing.assert_allclose(bounds_check['r2dz_x'].values,
                               bounds['r2dz_x'].values,
                               atol=1e-5)
    np.testing.assert_allclose(bounds_check['r2yz_dx'].values,
                               bounds['r2yz_dx'].values,
                               atol=1e-5)
Example #8
0
model = smf.ols(
    formula='peacefactor ~ directlyharmed + age + farmer_dar + herder_dar +\
                pastvoted + hhsize_darfur + female + village',
    data=darfur).fit()
treatment = "directlyharmed"
q = 1.0
alpha = 0.05
reduce = True
benchmark_covariates = ["female"]
kd = [1, 2, 3]
ky = kd
s = main.Sensemakr(model,
                   treatment,
                   q=q,
                   alpha=alpha,
                   reduce=reduce,
                   benchmark_covariates=benchmark_covariates,
                   kd=kd)
s2 = main.Sensemakr(model,
                    treatment,
                    q=q,
                    alpha=alpha,
                    reduce=False,
                    benchmark_covariates=benchmark_covariates,
                    kd=kd)


def test_plots():
    ovb_contour_plot(model=model, treatment='directlyharmed', r2dz_x=0.1)
    ovb_contour_plot(model=model, treatment='directlyharmed')