Skip to contents

This 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 is NULL, which sets all weights to 1.

...

Additional arguments passed to the underlying distribution function:

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:

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