STATISTICS Course

Likelihood functions (examples)

Go back

You can easily switch from the Likelihood function to log-likelihood, so I won't show the log-likelihood implementations.


Bernoulli distribution

L_bern <- function(theta, x) {
  return(prod(dbinom(x = x, size = 1, prob = theta[1])))
}

Binomial distribution

L_binom <- function(theta, x) {
  # fitdistr --
  m <- mean(x)
  v <- var(x)
  size <- ifelse(v > m, m ^ 2 / (v - m), 100)
  # --
  
  return(prod(dbinom(x = x, size = size, prob = theta[1])))
}

Weibull distribution

L_weibull <- function(theta, x) {
  return(prod(dweibull(shape = theta[1], scale = theta[2], x)))
}

Poisson distribution

L_pois <- function(theta, x) {
  return(prod(dpois(lambda = theta[1], x)))
}

Normal distribution

L_norm <- function (theta, x) {
  return(prod(dnorm(mean = theta[1], sd = theta[2], x)))
}

Cauchy distribution

L_cauchy <- function (theta, x) {
  return(prod(dcauchy(location = theta[1], scale = theta[2], x)))
}

Gamma distribution

L_gamma <- function(theta, x) {
  return(prod(dgamma(shape = theta[1], rate = theta[2], x)))
}

Exponential distribution

L_exp <- function(theta, x) {
  return(prod(dexp(rate = theta[1], x)))
}