Constructs a hypothesis test object that implements the hypothesize API.
This is the base constructor used by specific test functions like lrt(),
wald_test(), and z_test().
hypothesis_test(stat, p.value, dof, superclasses = NULL, ...)Numeric. The test statistic.
Numeric. The p-value (probability of observing a test
statistic as extreme as stat under the null hypothesis).
Numeric. Degrees of freedom. Use Inf for tests based on
the normal distribution.
Character vector. Additional S3 classes to prepend,
creating a subclass of hypothesis_test.
Additional named arguments stored in the object for introspection (e.g., input data, null hypothesis value).
An S3 object of class hypothesis_test (and any superclasses),
which is a list containing at least stat, p.value, dof, plus
any additional arguments passed via ....
The hypothesis_test object is the fundamental data abstraction in this
package. It represents the result of a statistical hypothesis test and
provides a consistent interface for extracting results.
This design follows the principle of data abstraction: the internal
representation (a list) is hidden behind accessor functions (pval(),
test_stat(), dof(), is_significant_at()).
To create a new type of hypothesis test:
Create a constructor function that computes the test statistic and p-value.
Call hypothesis_test() with appropriate superclasses.
The new test automatically inherits all generic methods.
Example:
my_test <- function(data, null_value) {
stat <- compute_statistic(data, null_value)
p.value <- compute_pvalue(stat)
hypothesis_test(
stat = stat, p.value = p.value, dof = length(data) - 1,
superclasses = "my_test",
data = data, null_value = null_value
)
}lrt(), wald_test(), z_test() for specific test constructors;
pval(), test_stat(), dof(), is_significant_at() for accessors
# Direct construction (usually use specific constructors instead)
test <- hypothesis_test(stat = 1.96, p.value = 0.05, dof = 1)
test
#> Hypothesis test ( hypothesis_test )
#> -----------------------------
#> Test statistic: 1.96
#> P-value: 0.05
#> Degrees of freedom: 1
#> Significant at 5% level: FALSE
# Extract components using the API
pval(test)
#> [1] 0.05
test_stat(test)
#> [1] 1.96
dof(test)
#> [1] 1
is_significant_at(test, 0.05)
#> [1] FALSE
# Create a custom test type
custom <- hypothesis_test(
stat = 2.5, p.value = 0.01, dof = 10,
superclasses = "custom_test",
method = "bootstrap", n_replicates = 1000
)
class(custom) # c("custom_test", "hypothesis_test")
#> [1] "custom_test" "hypothesis_test"
custom$method # "bootstrap"
#> [1] "bootstrap"