R/KerLikelihood.R

Defines functions KerLikelihood

#' Likelihood and related routines
#' @author Oswaldo Gressani \email{oswaldo_gressani@hotmail.fr}
#' @noRd

KerLikelihood <- function(Dobs, BB) {
  # Kernel routine
  # Author: Oswaldo Gressani (oswaldo_gressani@hotmail.fr)
  n <- length(Dobs)
  loglik <- function(theta, rho) {# Log-likelihood
    Btheta <- as.numeric(BB %*% theta)
    equal <-  sum(lgamma(rho + Dobs) - lgamma(rho)) +
      sum(Dobs * Btheta) + n * (rho * log(rho)) -
      sum((Dobs + rho) * log(rho + exp(Btheta)))
    return(equal)
  }
  Dloglik <- function(theta, rho) {# Gradient
    Btheta <- as.numeric(BB %*% theta)
    res <-
      colSums((Dobs - (Dobs + rho) / (1 + rho * exp(-Btheta))) * BB)
    return(res)
  }
  D2loglik <- function(theta, rho) {# Hessian
    Btheta <- as.numeric(BB %*% theta)
    midvec <-
      rho * (Dobs + rho) * (exp(Btheta) / (exp(Btheta) + rho) ^ 2)
    Hess <- (-1) * (t(BB) %*% (midvec * BB))
    return(Hess)
  }

  outlist <- list(loglik = loglik, Dloglik = Dloglik, D2loglik = D2loglik)
  return(outlist)
}

Try the EpiLPS package in your browser

Any scripts or data that you put into this service are public.

EpiLPS documentation built on May 29, 2024, 9:40 a.m.