def test_confint_2indep_propcis(): # unit tests compared to R package PropCis # alpha = 0.05 count1, nobs1 = 7, 34 count2, nobs2 = 1, 34 # > library(PropCIs) # > diffscoreci(7, 34, 1, 34, 0.95) ci = 0.0270416, 0.3452912 ci1 = confint_proportions_2indep(count1, nobs1, count2, nobs2, compare="diff", method="score", correction=False) assert_allclose(ci1, ci, atol=0.002) # lower agreement (iterative) # > wald2ci(7, 34, 1, 34, 0.95, adjust="AC") ci = 0.01161167, 0.32172166 ci1 = confint_proportions_2indep(count1, nobs1, count2, nobs2, compare="diff", method="agresti-caffo") assert_allclose(ci1, ci, atol=6e-7) # > wald2ci(7, 34, 1, 34, 0.95, adjust="Wald") ci = 0.02916942, 0.32377176 ci1 = confint_proportions_2indep(count1, nobs1, count2, nobs2, compare="diff", method="wald", correction=False) assert_allclose(ci1, ci, atol=6e-7) # > orscoreci(7, 34, 1, 34, 0.95) ci = 1.246309, 56.486130 ci1 = confint_proportions_2indep(count1, nobs1, count2, nobs2, compare="odds-ratio", method="score", correction=True) assert_allclose(ci1, ci, rtol=5e-4) # lower agreement (iterative) # > riskscoreci(7, 34, 1, 34, 0.95) ci = 1.220853, 42.575718 ci1 = confint_proportions_2indep(count1, nobs1, count2, nobs2, compare="ratio", method="score", correction=False) assert_allclose(ci1, ci, atol=6e-7)
def test_test_2indep(): # this checks the pvalue of the hypothesis test at value equal to the # confidence limit alpha = 0.05 count1, nobs1 = 7, 34 count2, nobs2 = 1, 34 methods_both = [ ('diff', 'agresti-caffo'), # ('diff', 'newcomb'), # only confint ('diff', 'score'), ('diff', 'wald'), ('ratio', 'log'), ('ratio', 'log-adjusted'), ('ratio', 'score'), ('odds-ratio', 'logit'), ('odds-ratio', 'logit-adjusted'), ('odds-ratio', 'logit-smoothed'), ('odds-ratio', 'score'), ] for co, method in methods_both: low, upp = confint_proportions_2indep(count1, nobs1, count2, nobs2, compare=co, method=method, alpha=alpha, correction=False) res = smprop.test_proportions_2indep( count1, nobs1, count2, nobs2, value=low, compare=co, method=method, correction=False) assert_allclose(res.pvalue, alpha, atol=1e-10) res = smprop.test_proportions_2indep( count1, nobs1, count2, nobs2, value=upp, compare=co, method=method, correction=False) assert_allclose(res.pvalue, alpha, atol=1e-10) _, pv = smprop.test_proportions_2indep( count1, nobs1, count2, nobs2, value=upp, compare=co, method=method, alternative='smaller', correction=False, return_results=False) assert_allclose(pv, alpha / 2, atol=1e-10) _, pv = smprop.test_proportions_2indep( count1, nobs1, count2, nobs2, value=low, compare=co, method=method, alternative='larger', correction=False, return_results=False) assert_allclose(pv, alpha / 2, atol=1e-10) # test Miettinen/Nurminen small sample correction co, method = 'ratio', 'score' low, upp = confint_proportions_2indep(count1, nobs1, count2, nobs2, compare=co, method=method, alpha=alpha, correction=True) res = smprop.test_proportions_2indep( count1, nobs1, count2, nobs2, value=low, compare=co, method=method, correction=True) assert_allclose(res.pvalue, alpha, atol=1e-10)
def test_equivalence_2indep(): # this checks the pvalue of the equivalence test at value equal to the # confidence limit alpha = 0.05 count1, nobs1 = 7, 34 count2, nobs2 = 1, 34 methods_both = [ ('diff', 'agresti-caffo'), # ('diff', 'newcomb'), # only confint ('diff', 'score'), ('diff', 'wald'), ('ratio', 'log'), ('ratio', 'log-adjusted'), ('ratio', 'score'), ('odds-ratio', 'logit'), ('odds-ratio', 'logit-adjusted'), ('odds-ratio', 'logit-smoothed'), ('odds-ratio', 'score'), ] for co, method in methods_both: low, upp = confint_proportions_2indep(count1, nobs1, count2, nobs2, compare=co, method=method, alpha=2 * alpha, correction=False) res = smprop.tost_proportions_2indep( count1, nobs1, count2, nobs2, low, upp, compare=co, method=method, correction=False) assert_allclose(res.pvalue, alpha, atol=1e-10)
def _ci(self, row, alpha_column: str) -> Tuple[float, float]: return confint_proportions_2indep(count1=row[self._numerator + SFX2], nobs1=row[self._denominator + SFX2], count2=row[self._numerator + SFX1], nobs2=row[self._denominator + SFX1], alpha=row[alpha_column], compare='diff', method='wald')
def two_props_diff_conf_interval(values1: np.ndarray, values2: np.ndarray, conf_level: float) -> tuple: """Calculates the confidence interval for the diff between two proportions Args: values1 (np.array): sample 1 binary(0/1) values values2 (np.array): sample 2 binary(0/1) values conf_level (float): confidence level Returns: tuple: lower and upper values of confidence interval """ count1, count2 = values1.sum(), values2.sum() nobs1, nobs2 = len(values1), len(values2) ci = prop_stats.confint_proportions_2indep(count1, nobs1, count2, nobs2, alpha=1 - conf_level, method="wald") return ci
def test_confint_2indep(): # alpha = 0.05 count1, nobs1 = 7, 34 count2, nobs2 = 1, 34 # result tables from Fagerland et al 2015 ''' diff: Wald 0.029 0.32 0.29 Agresti–Caffo 0.012 0.32 0.31 Newcombe hybrid score 0.019 0.34 0.32 Miettinen–Nurminen asymptotic score 0.028 0.34 0.31 Santner–Snell exact unconditional -0.069 0.41 0.48 Chan–Zhang exact unconditional 0.019 0.36 0.34 Agresti–Min exact unconditional 0.024 0.35 0.33 ratio: Katz log 0.91 54 4.08 Adjusted log 0.92 27 3.38 Inverse sinh 1.17 42 3.58 Koopman asymptotic score 1.21 43 3.57 Chan–Zhang 1.22 181 5.00 Agresti–Min 1.15 89 4.35 odds-ratio Woolf logit 0.99 74 4.31 Gart adjusted logit 0.98 38 3.65 Independence-smoothed logit 0.99 60 4.11 Cornfield exact conditional 0.97 397 6.01 Cornfield mid-p 1.19 200 5.12 Baptista–Pike exact conditional 1.00 195 5.28 Baptista–Pike mid-p 1.33 99 4.31 Agresti–Min exact unconditional 1.19 72 4.10 ''' # pylint: disable=W0105 ci = confint_proportions_2indep(count1, nobs1, count2, nobs2, method='newcomb', compare='diff', alpha=0.05) # one decimal to upp added from regression result assert_allclose(ci, [0.019, 0.340], atol=0.005) ci = confint_proportions_2indep(count1, nobs1, count2, nobs2, method='wald', compare='diff', alpha=0.05) assert_allclose(ci, [0.029, 0.324], atol=0.005) ci = confint_proportions_2indep(count1, nobs1, count2, nobs2, method='agresti-caffo', compare='diff', alpha=0.05) assert_allclose(ci, [0.012, 0.322], atol=0.005) ci = confint_proportions_2indep(count1, nobs1, count2, nobs2, compare='diff', method='score', correction=True) assert_allclose(ci, [0.028, 0.343], rtol=0.03) # ratio ci = confint_proportions_2indep(count1, nobs1, count2, nobs2, compare='ratio', method='log') assert_allclose(ci, [0.91, 54], rtol=0.01) ci = confint_proportions_2indep(count1, nobs1, count2, nobs2, compare='ratio', method='log-adjusted') assert_allclose(ci, [0.92, 27], rtol=0.01) ci = confint_proportions_2indep(count1, nobs1, count2, nobs2, compare='ratio', method='score', correction=False) assert_allclose(ci, [1.21, 43], rtol=0.01) # odds-ratio ci = confint_proportions_2indep(count1, nobs1, count2, nobs2, compare='or', method='logit') assert_allclose(ci, [0.99, 74], rtol=0.01) ci = confint_proportions_2indep(count1, nobs1, count2, nobs2, compare='or', method='logit-adjusted') assert_allclose(ci, [0.98, 38], rtol=0.01) ci = confint_proportions_2indep(count1, nobs1, count2, nobs2, compare='or', method='logit-smoothed') assert_allclose(ci, [0.99, 60], rtol=0.01) ci = confint_proportions_2indep(count1, nobs1, count2, nobs2, compare='odds-ratio', method='score', correction=True) # regression test assert_allclose(ci, [1.246622, 56.461576], rtol=0.01)