Skip to content

Instantly share code, notes, and snippets.

@wviechtb
Created April 4, 2017 12:46
Show Gist options
  • Save wviechtb/20d625c6c5aeba3f0b007eb5d86dd7b9 to your computer and use it in GitHub Desktop.
Save wviechtb/20d625c6c5aeba3f0b007eb5d86dd7b9 to your computer and use it in GitHub Desktop.
Function to compute the ML/REML log-likelihood for a standard random-effects model.
ll <- function(mu, tau2, yi, vi, method="ML") {
k <- length(yi)
wi <- 1/(vi + tau2)
if (method == "ML") {
sum(dnorm(yi, mean=mu, sqrt(vi + tau2), log=TRUE))
} else {
-1/2 * (k-1) * log(2*pi) + 1/2 * log(k) - 1/2 * sum(log(vi + tau2)) - 1/2 * log(sum(wi)) - 1/2 * sum(wi * (yi - mu)^2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment