Add or remove stat_define implementations on a test or model function
Source:R/add-stat-define.R
add-stat-define.RdThese are developer-interface functions intended for package authors
extending the statim framework with new model types.
add_stat_define() registers a new stat_define() for a stat function,
enabling it to handle a previously unsupported variable mapper <var_id>.
Registering a model type that already exists — whether baked-in or
previously registered — is an error.
remove_stat_define() removes a previously registered "user"-originated
entry. "package"-scoped entries are self-cleaning via purge_stat_defines()
called in the registering package's .onUnload().
Arguments
- stat_fn
A test or model function built with
HTEST_FN()orMODEL_FN()(e.g.TTEST,P_TEST).- model_type
An S7
<var_id>class (e.g.x_by,S7::class_formula).- impl
An
agendas()object.- compatible_params
A list of param S7 classes (e.g.
list(MU)). Defaults tolist().- origin
One of
"user"(default) or"package". Use"user"for interactive or script-level registration scoped to the current session. Use"package"inside your package's.onLoad(), paired with.pkg = pkgname— see the Package authors section below.- .pkg
The registering package name. Required when
origin = "package"; ignored otherwise. Pass thepkgnameargument that R supplies to.onLoad()/.onAttach(). This is used to attribute the entry and to enablepurge_stat_defines()to clean it up on unload.
Scoping and lifecycle
Registrations have two scopes:
"user"-scoped entries live for the duration of the R session (or until explicitly removed withremove_stat_define()). Use this for interactive work or in scripts."package"-scoped entries are intended for package authors who ship extensions tostatim. They must be registered in.onLoad()and cleaned up in.onUnload()viapurge_stat_defines(). This keeps the registry tidy when the extending package is unloaded.
Package authors
To ship an extension as a package, register in zzz.R (or any file loaded
early):
.onLoad = function(libname, pkgname) {
statim::add_stat_define(
P_TEST,
my_var_id,
impl = agendas(
base = baseline(
fn = function(.proc, .p = 0.5, .alt = "two.sided", .ci = 0.95) {
# your implementation
}
)
),
compatible_params = list(PI),
origin = "package",
.pkg = pkgname
)
}
.onUnload = function(libpath) {
statim::purge_stat_defines("yourpackage")
}The .pkg = pkgname argument is how statim knows which package owns
the entry. Without it, origin = "package" is an error. Never hard-code
the string yourself — always forward the pkgname R passes to .onLoad().
See also
stat_define(), agendas(), remove_stat_define(),
purge_stat_defines()