Helper function to call a fitting function across different models
Source:R/multi_fitdist.R
multi_fitdist.Rd
This is a utility function that allows comparing different
model fits to a single dataset. It does this by executing a specified
function on the data provided and over all models specified. The function
then organises the output and calculates the AIC and BIC and ranks the
output by model fit determined by the rank_by
argument.
Usage
multi_fitdist(data, models, func, rank_by = c("aic", "bic", "loglik"))
Arguments
- data
A vector or data frame containing data that is required by the function specified in
func
argument.- models
A character string or vector of character strings specifying the names of the candidate models. The naming of the models should match those required by the function specified in the
func
argument. The vector of models should be named with the name of the model argument from that specified infunc
when the argument is not second. See details.- func
A function (
closure
) used to fit the models. Could be user-defined or specified from another package's namespace.- rank_by
A character string, either "loglik", "aic" or "bic" to rank the order of the output data frame. Default is "aic".
Details
The vector of models given in the models
argument needs to be
named with the name of the model argument supplied to func
when it
is not the second argument in that function. All elements of the vector need
to be named, see example.
The data is assumed to always be the first argument of the function supplied
in func
, multi_fitdist()
will not work correctly if this is not the case.
Examples
if (FALSE) { # \dontrun{
multi_fitdist(
data = rgamma(n = 100, shape = 1, scale = 1),
models = c("gamma", "weibull", "lnorm"),
func = fitdistrplus::fitdist
)
# Where the model is not the second argument in the function specified,
# the models have to be named according to what they are called in the
# original function. argument of the function input. Here, `distr` is the
# name required in `fitdistrplus::fitdist()`
multi_fitdist(
data = rgamma(n = 100, shape = 1, scale = 1),
models = c(distr = "gamma", distr = "weibull", distr = "lnorm"),
func = fitdistrplus::fitdist
)
} # }