A lightweight, stats::fitdistr-inspired routine that fits the parameters of a one-dimensional distribution to a numeric vector by maximum likelihood, using stats::nlminb as the optimizer. Common distributions can be specified by name (in which case sensible defaults and closed-form/starting-value shortcuts are used where available), or an arbitrary density function can be supplied directly.

fitdistr_ahead(x, densfun, start = NULL, seed = 123, ...)

Arguments

x

A non-empty numeric vector of observations to fit. Must not contain NA, NaN, or infinite values.

densfun

Either a character string naming one of the supported distributions (see Details), or a density function taking x as its first argument, followed by the distribution's parameters, and optionally a log argument.

start

Optional named list of starting values for the distribution's parameters, passed on to the density function by name. Required (and must be a named list) unless densfun is "normal" or "t", for which defaults are supplied automatically. Ignored (and an error is raised if supplied) for "normal".

seed

Integer; random seed. Currently accepted for interface compatibility but not used internally. Defaults to 123.

...

Additional arguments passed on to densfun (e.g. fixed parameters not being estimated, such as lower/upper bounds for some distributions, which are excluded when checking for user-supplied starting parameters).

Value

For densfun = "normal", an object of class "fitdistr", a list with components:

estimate

Named numeric vector of estimated mean and standard deviation.

sd

Named numeric vector of standard errors of the estimates.

vcov

Variance-covariance matrix of the estimates.

n

Number of observations.

loglik

Log-likelihood at the estimate.

For all other distributions, a plain list with components:

estimate

Numeric vector of estimated parameters, as returned by stats::nlminb.

convergence

Convergence code returned by stats::nlminb (0 indicates success).

objective

Value of the (negative log-likelihood) objective function at the solution.

Details

If densfun is a function, it is called internally as densfun(x, <named parameters from start/parm>, ..., log = ), with matching done by name — so no reordering of densfun's formals is required. The negative log-likelihood is minimized with stats::nlminb, using densfun's own log = TRUE density when it exposes a log argument (for numerical stability), and falling back to summing log(density) otherwise.

If densfun is a character string, it is matched (case-insensitively) against a fixed set of supported distributions: "beta", "cauchy", "chi-squared", "exponential", "f", "gamma", "geometric", "log-normal" / "lognormal", "logistic", "negative binomial", "normal", "poisson", "t", and "weibull". An unrecognized name raises an error.

Two distributions receive special handling:

  • normal: the mean and (bias-corrected) standard deviation are computed in closed form (no optimization is performed), and supplying start for this distribution is not supported and raises an error.

  • t: if start is not supplied, it defaults to list(m = median(x), s = IQR(x) / 2, df = 10), with any of m, s, df passed via ... removed from the default starting list. The Student-t density is evaluated via an internal location-scale helper (mydt) built on stats::dt.

For all other distributions, start must be supplied as a named list of initial parameter values matching densfun's parameters.

Note: the seed argument is accepted but is not currently used inside the function body (the optimization is deterministic given start).

Examples

if (FALSE) { # \dontrun{
set.seed(1)
x <- rnorm(100, mean = 5, sd = 2)

# Named distribution, closed-form normal fit
fitdistr_ahead(x, "normal")

# Named distribution with default starting values (t distribution)
fitdistr_ahead(x, "t")

# Custom density function with explicit starting values
fitdistr_ahead(x, stats::dgamma, start = list(shape = 1, rate = 1))
} # }