R/hypothesize.R
confint.hypothesis_test.RdExtracts a confidence interval from a hypothesis test object, exploiting the fundamental duality between hypothesis tests and confidence intervals.
A named numeric vector with elements lower and upper.
Hypothesis tests and confidence intervals are two views of the same underlying inference. For a test of \(H_0: \theta = \theta_0\) at level \(\alpha\), the \((1-\alpha)\) confidence interval contains exactly those values of \(\theta_0\) that would not be rejected.
This duality means:
A 95% CI contains all values where the two-sided test has p > 0.05
The CI boundary is where p = 0.05 exactly
Inverting a test "inverts" it into a confidence set
Confidence intervals are currently implemented for:
wald_test: Uses \(\hat{\theta} \pm z_{\alpha/2} \cdot SE\)
z_test: Uses \(\bar{x} \pm z_{\alpha/2} \cdot \sigma/\sqrt{n}\)
Tests without stored estimates (like lrt or fisher_combined_test)
cannot produce confidence intervals directly.
# Wald test stores estimate and SE, so CI is available
w <- wald_test(estimate = 2.5, se = 0.8)
confint(w) # 95% CI
#> lower upper
#> 0.9320288 4.0679712
confint(w, level = 0.99) # 99% CI
#> lower upper
#> 0.4393366 4.5606634
# The duality: 2.5 is in the CI, and testing H0: theta = 2.5
# would give p = 1 (not rejected)
wald_test(estimate = 2.5, se = 0.8, null_value = 2.5)
#> Hypothesis test ( wald_test )
#> -----------------------------
#> Test statistic: 0
#> P-value: 1
#> Degrees of freedom: 1
#> Significant at 5% level: FALSE
# z-test also supports confint
z <- z_test(rnorm(50, mean = 10, sd = 2), mu0 = 9, sigma = 2)
confint(z)
#> lower upper
#> 9.326016 10.434739