Learn R Programming

hypothesize (version 0.11.0)

invert_test: Invert a Test into a Confidence Set (Test-Confidence Duality)

Description

Takes a test constructor function and returns the confidence set: the set of null values that are not rejected at level \(\alpha\).

Usage

invert_test(test_fn, grid, alpha = 0.05)

Value

An S3 object of class confidence_set containing:

set

Numeric vector of non-rejected null values

alpha

The significance level used

level

The confidence level (\(1 - \alpha\))

test_fn

The input test function

grid

The input grid

Arguments

test_fn

A function that takes a single numeric argument (the hypothesized null value) and returns a hypothesis_test object.

grid

Numeric vector of candidate null values to test.

alpha

Numeric. Significance level (default 0.05). The confidence level is \(1 - \alpha\).

Higher-Order Function (SICP Principle)

This function takes a function as input (test_fn) and returns a structured result. It demonstrates the power of the hypothesis_test abstraction: because all tests implement the same interface (pval()), invert_test can work with any test without knowing its internals.

Details

Hypothesis tests and confidence sets are dual: a \((1-\alpha)\) confidence set contains exactly those parameter values \(\theta_0\) for which the test of \(H_0: \theta = \theta_0\) would not reject at level \(\alpha\). This function makes that duality operational.

invert_test is the most general confidence set constructor in the package. Any test — including user-defined tests — can be inverted. The specialized confint() methods for wald_test and z_test give exact analytical intervals; invert_test gives numerical intervals for arbitrary tests at the cost of a grid search.

See Also

confint.wald_test(), confint.z_test() for analytical CIs

Examples

Run this code
# Invert a Wald test to get a confidence interval
cs <- invert_test(
  test_fn = function(theta) wald_test(estimate = 2.5, se = 0.8, null_value = theta),
  grid = seq(0, 5, by = 0.01)
)
cs
lower(cs)
upper(cs)

# Compare with the analytical confint (should agree up to grid resolution)
confint(wald_test(estimate = 2.5, se = 0.8))

# Invert ANY user-defined test — no special support needed
my_test <- function(theta) {
  stat <- (5.0 - theta)^2 / 2
  hypothesis_test(stat = stat,
    p.value = pchisq(stat, df = 1, lower.tail = FALSE), dof = 1)
}
invert_test(my_test, grid = seq(0, 10, by = 0.01))

Run the code above in your browser using DataLab