| Title: | Distributions on the Unit Interval via Transformations of Baseline Laws |
|---|---|
| Description: | Builds new probability distributions on (0,1) by applying a monotone transformation (rational, exponential, logit, 'tanh', probit, complementary log-log) to a baseline random variable Y with any available d/p/q/r functions. Provides density, distribution, quantile and random generation functions, maximum likelihood fitting with information criteria, profile-likelihood and bootstrap confidence intervals, automatic model search over baseline/transformation pairs, goodness-of-fit tests with parametric bootstrap, moments, hazard and simulation studies. |
| Authors: | Mohamad Yamen Al-Mohamad [aut, cre] (ORCID: <https://orcid.org/0009-0009-1665-7203>), Ali Akbar Hydari [aut] |
| Maintainer: | Mohamad Yamen Al-Mohamad <[email protected]> |
| License: | GPL-3 |
| Version: | 0.2.0 |
| Built: | 2026-11-27 09:23:01 UTC |
| Source: | https://github.com/yamenetoo/statistical-repository |
Reads the parameter names from the formals of the 'd<dist>' function, dropping the first argument and arguments such as 'log'.
baseline_params(dist)baseline_params(dist)
dist |
Name of the baseline distribution, e.g. '"gamma"'. |
Character vector of parameter names.
baseline_params("gamma")baseline_params("gamma")
Extract the best model from a [fit_all()] result
best_model(object, criterion = object$criterion)best_model(object, criterion = object$criterion)
object |
A 'transfit_all' object. |
criterion |
Criterion to use; defaults to the one used for ranking. |
The best 'transfit' object.
Verifies that the support of Y is compatible with the transformation. When the baseline cdf is available the probability mass that Y places outside the admissible domain of the transformation is computed exactly (for example 'P(Y < 0)' for '"rational"'); otherwise a grid of quantiles is inspected. The range of V actually reached is also reported.
check_domain(dist, par, trans, tol = 1e-08)check_domain(dist, par, trans, tol = 1e-08)
dist |
Baseline distribution name. |
par |
Baseline parameters (named vector/list or unnamed in formals order). |
trans |
Transformation name or object. |
tol |
Largest tolerated probability mass outside the admissible domain. |
A list with 'valid' (logical), 'range' (approximate support of V), 'mass_outside' and 'message'.
check_domain("norm", c(0, 1), "logit") check_domain("norm", c(0, 1), "rational")check_domain("norm", c(0, 1), "logit") check_domain("norm", c(0, 1), "rational")
Confidence intervals for a fitted model
## S3 method for class 'transfit' confint( object, parm, level = 0.95, method = c("wald", "bootstrap", "profile"), B = 200, seed = NULL, ... )## S3 method for class 'transfit' confint( object, parm, level = 0.95, method = c("wald", "bootstrap", "profile"), B = 200, seed = NULL, ... )
object |
A 'transfit' object. |
parm |
Parameter names or indices (default: all). |
level |
Confidence level. |
method |
'"wald"' (from the Hessian), '"bootstrap"' (parametric percentile bootstrap) or '"profile"' (profile likelihood, usually better for positive or skewed parameters). A limit returned as 'NA' means that the profile did not cross the cut-off in the searched range. |
B |
Bootstrap replications. |
seed |
Optional seed for the bootstrap (the caller's random number stream is restored afterwards). |
... |
Unused. |
Matrix with lower and upper limits. For the bootstrap the number of failed replications is stored in the attribute '"failed"'.
set.seed(1) fit <- fit_trans(rtrans(150, "norm", c(0, 1), "logit"), "norm", "logit") confint(fit) confint(fit, method = "profile")set.seed(1) fit <- fit_trans(rtrans(150, "norm", c(0, 1), "logit"), "norm", "logit") confint(fit) confint(fit, method = "profile")
Detect which transformations suit a baseline distribution
detect_transformations(dist, par)detect_transformations(dist, par)
dist |
Baseline distribution name. |
par |
Baseline parameters. |
A data frame listing each transformation, whether it is valid and the approximate range of V.
detect_transformations("gamma", c(shape = 3, rate = 1))detect_transformations("gamma", c(shape = 3, rate = 1))
Functions for the distribution of on (0,1), where
follows a baseline distribution 'dist' with parameters 'par'.
dtrans(v, dist, par, trans, log = FALSE) ptrans(q, dist, par, trans, lower.tail = TRUE, log.p = FALSE) qtrans(p, dist, par, trans, lower.tail = TRUE, log.p = FALSE) rtrans(n, dist, par, trans)dtrans(v, dist, par, trans, log = FALSE) ptrans(q, dist, par, trans, lower.tail = TRUE, log.p = FALSE) qtrans(p, dist, par, trans, lower.tail = TRUE, log.p = FALSE) rtrans(n, dist, par, trans)
v, q
|
Vector of quantiles in (0,1). |
dist |
Baseline distribution name, e.g. '"gamma"' (uses 'dgamma', 'pgamma', 'qgamma'). |
par |
Baseline parameters: named vector/list, or unnamed in the order of the baseline's formals. |
trans |
Transformation name (see [trans_list()]) or object. |
log |
Logical; return log-density. |
lower.tail |
Logical; if 'FALSE', return |
log.p |
Logical; probabilities are given/returned as 'log(p)'. |
p |
Vector of probabilities. |
n |
Number of observations. |
Tail probabilities are computed directly from the baseline (for a
decreasing transformation the upper tail of is the lower tail of
), never as '1 - p', so 'lower.tail = FALSE' and 'log.p = TRUE' stay
accurate far into the tails.
Numeric vector. 'rtrans' may return values numerically equal to 0 or 1 for extreme draws.
dtrans(c(.2, .5, .8), "norm", c(0, 1), "logit") ptrans(0.5, "gamma", c(2, 1), "exponential") qtrans(c(.25, .5), "norm", c(0, 1), "probit") set.seed(1); rtrans(5, "gamma", c(2, 1), "rational")dtrans(c(.2, .5, .8), "norm", c(0, 1), "logit") ptrans(0.5, "gamma", c(2, 1), "exponential") qtrans(c(.25, .5), "norm", c(0, 1), "probit") set.seed(1); rtrans(5, "gamma", c(2, 1), "rational")
Fits every valid combination of baseline distributions and transformations and ranks them by an information criterion. Combinations whose baseline support is incompatible with the transformation, whose optimizer did not converge or whose fit puts mass outside the transformation domain are dropped (reported when 'verbose = TRUE').
fit_all( data, dists, trans = names(.registry()), criterion = c("AIC", "BIC", "AICc"), verbose = FALSE, drop_nonconverged = TRUE, seed = NULL, cores = 1L, adjust = FALSE )fit_all( data, dists, trans = names(.registry()), criterion = c("AIC", "BIC", "AICc"), verbose = FALSE, drop_nonconverged = TRUE, seed = NULL, cores = 1L, adjust = FALSE )
data |
Numeric vector in (0,1). |
dists |
Character vector of baseline distribution names. |
trans |
Character vector of transformation names (default: all). |
criterion |
'"AIC"', '"BIC"' or '"AICc"'. |
verbose |
Print skipped combinations. |
drop_nonconverged |
Drop fits whose optimizer reported non-convergence. |
seed |
Optional seed passed to every [fit_trans()] call (the same seed for each combination, so results do not depend on the order of the combinations or on 'cores'). |
cores |
Number of processes (Unix-alike systems only; ignored on Windows). |
adjust |
Passed to [fit_trans()]. |
An object of class 'transfit_all': a ranking 'table' (with 'delta' and Akaike 'weight' for the chosen criterion) and the list of 'fits'.
set.seed(2) x <- rtrans(200, "gamma", c(2, 1), "rational") res <- fit_all(x, c("gamma", "lnorm", "weibull"), c("rational", "exponential")) res$table best_model(res)set.seed(2) x <- rtrans(200, "gamma", c(2, 1), "rational") res <- fit_all(x, c("gamma", "lnorm", "weibull"), c("rational", "exponential")) res$table best_model(res)
Fits the distribution of to data in (0,1) by maximizing the
log-likelihood over the baseline parameters. The optimization is carried
out on an unconstrained scale (logarithmic for positive parameters), starting
from the default values, from a quantile-matching solution and from random
perturbations; the best optimum is kept.
fit_trans( data, dist, trans, start = NULL, lower = NULL, upper = NULL, n_start = 3, seed = NULL, adjust = FALSE, check_support = TRUE )fit_trans( data, dist, trans, start = NULL, lower = NULL, upper = NULL, n_start = 3, seed = NULL, adjust = FALSE, check_support = TRUE )
data |
Numeric vector with values strictly inside (0,1). |
dist |
Baseline distribution name. |
trans |
Transformation name or object. |
start |
Optional named starting values. Defaults to 0 for location-type parameters and 1 otherwise. |
lower, upper
|
Optional bounds for the parameters. |
n_start |
Number of starting points (the default or user start, a quantile-matching start, plus random perturbations); the best optimum is kept. |
seed |
Optional seed for the random perturbations. The caller's random number stream is restored afterwards. |
adjust |
If 'TRUE', data are mapped into the open interval with the Smithson-Verkuilen correction '(y (n - 1) + 0.5) / n', which allows observations equal to 0 or 1. |
check_support |
If 'TRUE' (default), stop when the baseline puts mass outside the domain of the transformation (see [check_domain()]). |
An object of class 'transfit' with 'coef', 'se', 'vcov', 'logLik', 'AIC', 'BIC', 'AICc', 'n', 'convergence' and 'hessian_ok' (whether the Hessian was positive definite, so that standard errors are available).
set.seed(1) x <- rtrans(200, "norm", c(0.5, 1), "logit") fit <- fit_trans(x, "norm", "logit") fitset.seed(1) x <- rtrans(200, "norm", c(0.5, 1), "logit") fit <- fit_trans(x, "norm", "logit") fit
Forward transformation
forward_transformation(y, trans)forward_transformation(y, trans)
y |
Numeric vector on the baseline scale. |
trans |
Transformation name or object. |
Numeric vector V = g(Y).
forward_transformation(1.2, "rational")forward_transformation(1.2, "rational")
Get a transformation object by name
get_transformation(name)get_transformation(name)
name |
One of the names returned by [trans_list()]. The old name '"arcsech"' is still accepted (with a warning) and refers to '"tanh"': the map 'V = 2/(1 + exp(Y))' equals '1 - tanh(Y/2)' and is not the inverse hyperbolic secant. |
An object of class 'unitrans' with elements 'g' (forward map), 'ginv' (inverse map), 'logjac' (log |dY/dV|), 'domain' and 'decreasing'.
tr <- get_transformation("logit") tr$g(0)tr <- get_transformation("logit") tr$g(0)
Computes Kolmogorov-Smirnov, Cramer-von Mises and Anderson-Darling statistics for a fitted model and obtains p-values by a parametric bootstrap that re-estimates the parameters in every replication (so the p-values are valid with estimated parameters). The p-value is '(1 + number of T* >= T) / (B + 1)', so it is never exactly zero.
gof_trans(fit, B = 200, refit = TRUE, seed = NULL)gof_trans(fit, B = 200, refit = TRUE, seed = NULL)
fit |
A 'transfit' object. |
B |
Number of bootstrap replications. |
refit |
Re-estimate the parameters in each replication. |
seed |
Optional seed (the caller's random number stream is restored). |
A data frame with statistic and bootstrap p-value for each test, and 'valid_boot', the number of replications that could be fitted. A warning is issued when some replications fail.
set.seed(3) fit <- fit_trans(rtrans(100, "norm", c(0, 1), "logit"), "norm", "logit") gof_trans(fit, B = 50)set.seed(3) fit <- fit_trans(rtrans(100, "norm", c(0, 1), "logit"), "norm", "logit") gof_trans(fit, B = 50)
The survival function is the upper tail computed directly from the baseline and the hazard is evaluated on the log scale, so both stay accurate near 1.
hazard_trans(v, dist, par, trans) survival_trans(v, dist, par, trans)hazard_trans(v, dist, par, trans) survival_trans(v, dist, par, trans)
v |
Values in (0,1). |
dist, par, trans
|
As in [dtrans()]. |
Numeric vector.
hazard_trans(c(.2, .5), "gamma", c(2, 1), "rational")hazard_trans(c(.2, .5), "gamma", c(2, 1), "rational")
Maps values of V in (0,1) back to the baseline scale Y.
inverse_transformation(v, trans)inverse_transformation(v, trans)
v |
Numeric vector in (0,1). |
trans |
Transformation name or object. |
Numeric vector Y = ginv(V), the inverse of g.
inverse_transformation(0.3, "logit")inverse_transformation(0.3, "logit")
Log-likelihood of the transformed distribution
loglik_trans(par, data, dist, trans)loglik_trans(par, data, dist, trans)
par |
Baseline parameters. |
data |
Observations in (0,1). |
dist |
Baseline distribution name. |
trans |
Transformation name or object. |
Numeric scalar.
loglik_trans(c(0, 1), c(.2, .4, .7), "norm", "logit")loglik_trans(c(0, 1), c(.2, .4, .7), "norm", "logit")
Computed by numerical integration of the quantile function,
. Central moments in 'summary_trans' are
integrated directly (not obtained from raw moments), which avoids
cancellation errors.
moments_trans(dist, par, trans, r = 1:4) summary_trans(dist, par, trans)moments_trans(dist, par, trans, r = 1:4) summary_trans(dist, par, trans)
dist, par, trans
|
As in [dtrans()]. |
r |
Order(s) of the raw moments. |
'moments_trans' returns a named vector of raw moments; 'summary_trans' returns mean, variance, skewness and excess kurtosis.
moments_trans("gamma", c(2, 1), "rational", r = 1:2) summary_trans("norm", c(0, 1), "logit")moments_trans("gamma", c(2, 1), "rational", r = 1:2) summary_trans("norm", c(0, 1), "logit")
Histogram with fitted density, plus fitted vs empirical CDF, Q-Q and P-P.
## S3 method for class 'transfit' plot(x, which = 1:4, ...)## S3 method for class 'transfit' plot(x, which = 1:4, ...)
x |
A 'transfit' object. |
which |
Which of the four panels to draw (subset of 1:4). |
... |
Unused. |
Invisibly 'x'.
set.seed(1) fit <- fit_trans(rtrans(150, "norm", c(0, 1), "logit"), "norm", "logit") plot(fit)set.seed(1) fit <- fit_trans(rtrans(150, "norm", c(0, 1), "logit"), "norm", "logit") plot(fit)
Repeatedly simulates from a transformed distribution, refits it and summarizes bias, root mean squared error and the coverage of Wald intervals. Coverage is computed over the replications with available standard errors.
simulation_study(dist, par, trans, n = c(50, 100, 200), R = 200, seed = NULL)simulation_study(dist, par, trans, n = c(50, 100, 200), R = 200, seed = NULL)
dist, par, trans
|
True model. |
n |
Sample size(s). |
R |
Number of replications. |
seed |
Optional seed (the caller's random number stream is restored). |
A data frame with one row per sample size and parameter, including the number of 'failed' replications.
simulation_study("norm", c(mean = 0, sd = 1), "logit", n = c(50, 100), R = 20, seed = 1)simulation_study("norm", c(mean = 0, sd = 1), "logit", n = c(50, 100), R = 20, seed = 1)
List the available transformations
trans_list()trans_list()
A data frame with the name, forward formula, inverse formula, admissible baseline domain and monotonicity of each transformation.
trans_list()trans_list()
'residuals(type = "quantile")' returns normalized quantile residuals
, which are approximately standard normal when the
model is adequate; 'type = "pit"' returns the probability integral
transform , approximately uniform.
## S3 method for class 'transfit' residuals(object, type = c("quantile", "pit"), ...) ## S3 method for class 'transfit' predict(object, newdata = NULL, type = c("density", "cdf", "quantile"), ...)## S3 method for class 'transfit' residuals(object, type = c("quantile", "pit"), ...) ## S3 method for class 'transfit' predict(object, newdata = NULL, type = c("density", "cdf", "quantile"), ...)
object |
A 'transfit' object. |
type |
For 'predict': '"density"', '"cdf"' or '"quantile"'. |
... |
Unused. |
newdata |
Numeric vector of values in (0,1) (or probabilities when 'type = "quantile"'). Defaults to the data used for the fit. |
Numeric vector.
set.seed(1) fit <- fit_trans(rtrans(150, "norm", c(0, 1), "logit"), "norm", "logit") head(residuals(fit)) predict(fit, c(.2, .5), type = "cdf")set.seed(1) fit <- fit_trans(rtrans(150, "norm", c(0, 1), "logit"), "norm", "logit") head(residuals(fit)) predict(fit, c(.2, .5), type = "cdf")