Given the name of a family of 1-dimensional distributions, this function chooses a particular member of the family that fits the data and returns a function in the selected p, d, q, or r format. When analytical solutions do not exist, MASS::fitdistr() is used to estimate the parameters by numerical maximum likelihood.

fit_distr_fun(data, formula, dist, start = NULL, ...)

Arguments

data

A data frame.

formula

A formula. A distribution will be fit to the data defined by the right side and evaluated in data.

dist

A string naming the function desired. Tyically this will be "d", "p", "q", or "r" followed by the (abbrevation for) a family of distributions such as "pnorm", "rgamma". Fitting is done use MASS::fitdistr(); see the help there for a list of distributions that are available.

start

Starting values for the numerical maximum likelihood method (passed to MASS::fitdistr).

...

Additional arguments to MASS::fitdistr()

Value

A function of one variable that acts like, say, pnorm(), dnorm(), qnorm(), or rnorm(), but with the default values of the parameters set to their maximum likelihood estimates.

Examples


fit_distr_fun( ~ cesd, data = mosaicData::HELPrct, dist = "dnorm")
#> function (x, mean = 32.8476821192053, sd = 12.5006392830986, 
#>     log = FALSE) 
#> .Call(C_dnorm, x, mean, sd, log)
#> <environment: namespace:stats>
fit_distr_fun( ~ cesd, data = mosaicData::HELPrct, dist = "pnorm")
#> function (q, mean = 32.8476821192053, sd = 12.5006392830986, 
#>     lower.tail = TRUE, log.p = FALSE) 
#> .Call(C_pnorm, q, mean, sd, lower.tail, log.p)
#> <environment: namespace:stats>
fit_distr_fun( ~ cesd, data = mosaicData::HELPrct, dist = "qpois")
#> function (p, lambda = 32.8476821192053, lower.tail = TRUE, log.p = FALSE) 
#> .Call(C_qpois, p, lambda, lower.tail, log.p)
#> <environment: namespace:stats>