diff --git a/R/test_likelihoodratio.R b/R/test_likelihoodratio.R index 20a58f6fb..06f6b1cc3 100644 --- a/R/test_likelihoodratio.R +++ b/R/test_likelihoodratio.R @@ -71,6 +71,9 @@ print.test_likelihoodratio <- function(x, digits = 2, ...) { if ("LogLik" %in% names(x)) { best <- which.max(x$LogLik) footer <- c(sprintf("\nModel '%s' seems to have the best model fit.\n", x$Model[best]), "yellow") + } else if ("Dev" %in% names(x)) { + best <- which.min(x$Dev) + footer <- c(sprintf("\nModel '%s' seems to have the best model fit.\n", x$Name[best]), "yellow") } else { footer <- NULL } @@ -118,12 +121,14 @@ test_likelihoodratio.ListNestedRegressions <- function(objects, estimator = "ML" } else { # lmtest::lrtest() lls <- sapply(objects, insight::get_loglikelihood, REML = REML, check_response = TRUE) - chi2 <- abs(c(NA, -2 * diff(lls))) + devs <- -2 * lls + chi2 <- abs(c(NA, diff(devs))) p <- stats::pchisq(chi2, abs(dfs_diff), lower.tail = FALSE) out <- data.frame( df = dfs, df_diff = dfs_diff, + deviance = devs, Chi2 = chi2, p = p, stringsAsFactors = FALSE diff --git a/tests/testthat/_snaps/nestedLogit.md b/tests/testthat/_snaps/nestedLogit.md deleted file mode 100644 index f5c9a0bdd..000000000 --- a/tests/testthat/_snaps/nestedLogit.md +++ /dev/null @@ -1,12 +0,0 @@ -# model_performance - - Code - model_performance(mnl) - Output - # Indices of model performance - - Response | AIC | BIC | RMSE | Sigma | R2 - ---------------------------------------------------- - work | 325.733 | 336.449 | 0.456 | 1.000 | 0.138 - full | 110.495 | 118.541 | 0.398 | 1.000 | 0.333 - diff --git a/tests/testthat/test-test_likelihoodratio.R b/tests/testthat/test-test_likelihoodratio.R index 9967ae3d4..3e249f026 100644 --- a/tests/testthat/test-test_likelihoodratio.R +++ b/tests/testthat/test-test_likelihoodratio.R @@ -63,6 +63,7 @@ test_that("test_likelihoodratio - lme4 ML", { t1 <- test_lrt(m1, m2, m3) t2 <- suppressMessages(anova(m1, m2, m3)) expect_equal(attributes(t1)$estimator, "ml") + expect_equal(t1$deviance, c(202.2215, 116.9578, 116.1164), tolerance = 1e-3) expect_equal(t1$Chi2, c(NA, 85.26365, 0.84141), tolerance = 1e-3) expect_equal(t1$p, c(NA, 0, 0.35899), tolerance = 1e-3) # close, but not the same @@ -80,6 +81,7 @@ test_that("test_likelihoodratio - lme4 OLS", { test_that("test_likelihoodratio - lme4 REML", { expect_warning(t3 <- test_lrt(m1, m2, m3, estimator = "REML")) expect_equal(attributes(t3)$estimator, "reml") + expect_equal(t3$deviance, c(210.9834, 121.6540, 124.5104), tolerance = 1e-3) expect_equal(t3$Chi2, c(NA, 89.32933, 2.85635), tolerance = 1e-3) expect_equal(t3$p, c(NA, 0, 0.09101), tolerance = 1e-3) }) @@ -91,6 +93,7 @@ m3 <- glm(am ~ mpg + hp + vs, data = mtcars, family = binomial()) test_that("test_likelihoodratio - glm", { t1 <- anova(m1, m2, m3, test = "LRT") t2 <- test_lrt(m1, m2, m3) + expect_equal(t1$`Resid. Dev`, t2$deviance, tolerance = 1e-3) expect_equal(t1$`Pr(>Chi)`, t2$p, tolerance = 1e-3) expect_equal(t1$Deviance, t2$Chi2, tolerance = 1e-3) expect_equal(attributes(t2)$estimator, "ml")