
Dispatcher for multiple distribution log-likelihood functions
Source:R/calc_log_likelihood.R
calc_log_likelihood.RdThis function routes to the appropriate calc_log_likelihood_*() sub-function
based on the specified family. It supports the six distributions (Beta, Binomial, Gamma, Negative Binomial, Normal, Poisson)
and also allows a top-level weights argument that is passed down to the chosen sub-function.
Usage
calc_log_likelihood(
observed,
estimated,
family = c("beta", "binomial", "gamma", "negbin", "normal", "poisson"),
weights = NULL,
...
)Arguments
- observed
Numeric or integer vector of observed data (depends on distribution). NAs allowed.
- estimated
Numeric vector of model-predicted values (depends on distribution). NAs allowed.
- family
Character string, one of:
"beta""binomial""gamma""negbin""normal""poisson"
- weights
Optional numeric vector of non-negative weights, same length as
observed. Default isNULL, which sets all weights to 1.- ...
Additional arguments passed to the underlying distribution function:
mean_precision(forcalc_log_likelihood_beta)trials(forcalc_log_likelihood_binomial)k(forcalc_log_likelihood_negbin)eps_rel(forcalc_log_likelihood_negbinandcalc_log_likelihood_poisson) – the relative floor on the predicted mean. Passing it with any otherfamilyis an error, which is deliberate: a scoring knob that is silently dropped is worse than one that fails loudly.verbose(common to all distributions)
Value
The total log-likelihood as a numeric scalar, for every family
(including Normal); NA_real_ where the sub-function cannot score.
Details
Based on the value of family, this function internally calls:
calc_log_likelihood_betaforfamily = "beta"calc_log_likelihood_binomialforfamily = "binomial"calc_log_likelihood_gammaforfamily = "gamma"calc_log_likelihood_negbinforfamily = "negbin"calc_log_likelihood_normalforfamily = "normal"calc_log_likelihood_poissonforfamily = "poisson"
The weights argument (if not NULL) is passed to the chosen sub-likelihood
function, which multiplies each observation's log-likelihood contribution by weights[i].
Examples
# Poisson example with weights
calc_log_likelihood(
observed = c(2, 3, 4),
estimated = c(2.2, 2.9, 3.8),
family = "poisson",
weights = c(1, 2, 1),
verbose = TRUE
)
#> Poisson log-likelihood: -5.95
#> [1] -5.949537
# Negative Binomial with known k and weights
calc_log_likelihood(
observed = c(0, 5, 9),
estimated = c(3, 4, 5),
family = "negbin",
k = 2.0,
weights = c(1, 1, 2),
verbose = TRUE
)
#> Using provided k = 2.000
#> Negative Binomial log-likelihood (k=2.000): -10.73
#> [1] -10.72775