R/sarlogLik_i.R

Defines functions sarlogLik_i

#' @keywords internal
#' @noRd

########## sarlogLik_i: computes the log-likelihood function for the SAR model
sarlogLik_i <- function(Y, X, W, beta, rho, sigma2) {
  N <- length(Y)  # Number of observations
  I <- diag(N)    # Identity matrix
  W <- as.matrix(W)

  # Compute the log determinant term
  log_det_term <- log(det(as.matrix(I - rho * W)))

  # Compute the residuals
  residuals <- Y - rho * W %*% Y - X %*% beta

  # Compute the log-likelihood
  # Errore
  # log_likelihood <- - (1 / 2) * log(2 * pi * sigma2) + (log_det_term/N) - (1 / (2 * sigma2)) * residuals^2
  # log_likelihood <- - (N / 2) * log(pi * sigma2) + (log_det_term) - (residuals^2 / (2 * sigma2))
  # Corretta (3.6 di LeSage-Pace 2009)
  # log_likelihood <- - (N / 2) * log(pi * sigma2) + (log_det_term) - (residuals^2 / (2 * sigma2))
  # average log-likelihood: provides the average contribution of the single observation and numerical stability
  log_likelihood <- - (1 / 2) * log(2* pi * sigma2) + (log_det_term/N) - residuals^2 / (2 * sigma2)

  return(log_likelihood)
}

Try the SCDA package in your browser

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

SCDA documentation built on Oct. 30, 2024, 9:06 a.m.