| fitverse | R Documentation |
The primary function of the FitVerse package. Given a numeric data
vector, fitverse() fits one or more parametric probability distributions,
ranks them by an information criterion, selects the best-fitting model,
computes goodness-of-fit statistics, and optionally produces a
publication-quality diagnostic plot.
Three estimation methods are supported: Maximum Likelihood Estimation
("MLE"), Method of Moments ("MOM"), and L-Moments
("LMOM"). When multiple methods are requested, all results are
combined into a single ranking table and the global best model is returned.
Tip: For a point-and-click experience, launch the built-in
interactive Shiny application with FitVerseApp().
fitverse(x,
dists = "all",
method = "MLE",
criterion = "AIC",
xlab = "x",
plot = TRUE,
interactive = FALSE,
top_n = 3L,
progress = NULL,
verbose = TRUE,
...)
x |
A numeric vector of observations. At least 4 finite values are required.
Non-finite values ( |
dists |
Character vector of distribution keys to try, or |
method |
Character vector specifying which estimation method(s) to use. One or more of:
When multiple methods are supplied (e.g. |
criterion |
Character: the information criterion used for ranking and best-model
selection. Either AIC is recommended for exploratory analysis and prediction; BIC imposes a stronger penalty for model complexity and is preferred when the goal is identifying the true generating distribution. |
xlab |
Character: a label for the data variable used in plot axis titles and
the auto-generated report. Default |
plot |
Logical: should a diagnostic plot be produced and printed?
Default |
interactive |
Logical: if |
top_n |
Integer: number of top-ranked distributions to overlay on the density
panel of the diagnostic plot. The best-fit model is always drawn as a
solid line; the remaining |
progress |
Optional function of the form |
verbose |
Logical: print progress messages to the console? Default |
... |
Additional arguments passed to |
fitverse() executes the following steps:
Validate and clean – removes non-finite values; checks that at least 4 observations remain.
Filter distributions – distributions whose support is incompatible with the data (e.g. positive-only Gamma when data contains negative values) are silently excluded.
Fit – calls fit_mle_all(), fit_mom_all(),
and/or fit_lmom_all() according to method.
Goodness-of-fit – attaches Kolmogorov-Smirnov (KS), Anderson-Darling (AD), and Cramer-von Mises (CvM) test results to each fit result.
Rank – builds a table ordered by criterion
(ascending AIC or BIC).
Select – chooses the model with the lowest AIC (or BIC).
Summarise – computes compute_data_summary()
and stores it in the returned object.
Plot (optional) – calls plot.fitverse().
Maximises the log-likelihood
\ell(\boldsymbol\theta) = \sum_{i=1}^n \log f(x_i; \boldsymbol\theta).
Implemented via fitdistrplus::fitdist() with bounded L-BFGS-B
optimisation where parameter constraints exist.
Equates the first k sample moments to their theoretical counterparts
and solves for the k parameters. Closed-form solutions are used
where available (Normal, Gamma, Log-Normal, Exponential, Beta, Logistic,
Uniform, Gumbel, Pareto, Inverse Gamma); numerical solution via
stats::optimise() for Weibull and Log-Logistic.
fitdistrplus MME is used for standard stats distributions.
Equates sample L-moments (linear combinations of order statistics) to their theoretical counterparts. L-moments are more robust to outliers than conventional moments and are widely used in hydrology. Implemented via lmomco; available for Normal, Gamma, Exponential, Log-Normal, Weibull, GEV, GLO, Gumbel, Logistic, and Log-Logistic. Reference: Hosking (1990).
The best-fit model is the one with the lowest AIC (or BIC) across all fitted distributions and methods. Delta AIC values in the ranking table quantify evidence against each model relative to the best: models with Delta AIC < 2 have "substantial support", 4–7 "considerably less", and > 10 "essentially none" (Burnham and Anderson, 2002).
Three non-parametric tests are applied to the best-fit model:
Measures the maximum absolute difference between the empirical and fitted CDFs. Conservative; not recommended as the primary criterion.
Gives extra weight to the distribution tails. More powerful than KS for detecting tail departures. Implemented via goftest.
Integrates the squared difference between empirical and fitted CDFs. Implemented via goftest.
A large p-value (> 0.05) indicates insufficient evidence to reject the fitted distribution.
An S3 object of class "fitverse". It is a named list with the
following elements:
best_fitNamed list describing the best-fitting model, with elements:
dist_keyCharacter: distribution key (e.g. "gamma").
methodCharacter: estimation method ("MLE", "MOM", or "LMOM").
paramsNamed numeric vector of parameter estimates.
loglikNumeric: maximised log-likelihood.
aicNumeric: Akaike Information Criterion.
bicNumeric: Bayesian Information Criterion.
n_paramsInteger: number of estimated parameters.
nInteger: sample size used.
convergedLogical: did the optimiser converge?
gofData frame of goodness-of-fit test results (Test,
Statistic, P_value, Stars), or NULL if unavailable.
all_fitsNamed list of all fit-result lists, one per distribution-method
combination. Keys are of the form "distkey_METHOD"
(e.g. "gamma_MLE").
rankingData frame with columns: Rank, Distribution, Method, N_params, LogLik,
AIC, Delta_AIC, BIC, Delta_BIC, KS_p, AD_p, Converged, dist_key.
Ordered by the chosen criterion (ascending).
dataNumeric vector: the cleaned input data.
nInteger: number of finite observations used.
xlabCharacter: the variable label.
data_summaryNamed list from compute_data_summary(): n, n_missing,
mean, sd, cv, median, iqr, skewness, kurtosis, min, max, range, q25, q75.
criterionCharacter: "AIC" or "BIC".
methodCharacter vector: method(s) used.
callThe matched function call.
The object is returned invisibly. Use print(), summary(),
plot(), coef(), logLik(), AIC(), and BIC()
for standard S3 method access.
Karuna G. Reddy karuna.reddy@auckland.ac.nz
M. G. M. Khan mgm.khan@usp.ac.fj
Burnham, K. P. and Anderson, D. R. (2002). Model Selection and Multimodel Inference (2nd ed.). Springer.
Delignette-Muller, M. L. and Dutang, C. (2015). fitdistrplus: An R package for fitting distributions. Journal of Statistical Software, 64(4), 1–34. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.18637/jss.v064.i04")}
Hosking, J. R. M. (1990). L-moments: Analysis and estimation of distributions using linear combinations of order statistics. Journal of the Royal Statistical Society, Series B, 52(1), 105–124.
Akaike, H. (1974). A new look at the statistical model identification. IEEE Transactions on Automatic Control, 19(6), 716–723.
compare_dists for a ranking table only (no plot).
fitverse_distributions to list all supported distributions.
plot.fitverse, summary.fitverse,
bootstrap_ci, return_level,
generate_report.
## -----------------------------------------------------------
## Example 1: Basic MLE fit -- three distributions
## -----------------------------------------------------------
set.seed(42)
x <- rgamma(300, shape = 2, rate = 0.5)
fit <- fitverse(x,
dists = c("gamma", "lognormal", "weibull"),
method = "MLE",
xlab = "Waiting time (s)",
plot = FALSE,
verbose = FALSE)
print(fit)
coef(fit) # parameter estimates
AIC(fit) # AIC of the best-fit model
logLik(fit) # log-likelihood
## -----------------------------------------------------------
## Example 2: All three estimation methods, ranked by BIC
## -----------------------------------------------------------
fit2 <- fitverse(x,
method = c("MLE", "MOM", "LMOM"),
criterion = "BIC",
plot = FALSE,
verbose = FALSE)
summary(fit2)
## -----------------------------------------------------------
## Example 3: Automatic best-fit selection across all distributions
## -----------------------------------------------------------
set.seed(7)
x3 <- rlnorm(500, meanlog = 1.5, sdlog = 0.6)
fit3 <- fitverse(x3,
xlab = "Insurance claim size",
method = "MLE",
plot = FALSE,
verbose = FALSE)
fit3$ranking[1:5, c("Distribution","Method","AIC","Delta_AIC","AD_p")]
## -----------------------------------------------------------
## Example 4: Accessing the data summary
## -----------------------------------------------------------
fit4 <- fitverse(x, plot = FALSE, verbose = FALSE)
fit4$data_summary$mean
fit4$data_summary$skewness
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.