Skip to content

Instantly share code, notes, and snippets.

@k-barton
Last active November 20, 2020 11:00
Show Gist options
  • Save k-barton/f2b21c2f6274ab4401978717f5a12a17 to your computer and use it in GitHub Desktop.
Save k-barton/f2b21c2f6274ab4401978717f5a12a17 to your computer and use it in GitHub Desktop.
Performs likelihood ratio test on `logLik` objects.
#' @param \dots two or more `logLik` objects
lrt <-
function(...) {
logL <- list(...)
nmodels <- length(logL)
rval <- matrix(rep(NA, 5 * nmodels), ncol = 5)
colnames(rval) <- c("#Df", "LogLik", "Df", "Chisq", "Pr(>Chisq)")
rownames(rval) <- 1:nmodels
rval[, 1] <- as.numeric(sapply(logL, function(x) attr(x, "df")))
rval[, 2] <- sapply(logL, as.numeric)
rval[2:nmodels, 3] <- rval[2:nmodels, 1] - rval[1:(nmodels - 1), 1]
rval[2:nmodels, 4] <- 2 * abs(rval[2:nmodels, 2] - rval[1:(nmodels - 1), 2])
rval[, 5] <- pchisq(rval[, 4], round(abs(rval[, 3])), lower.tail = FALSE)
rval
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment