Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion R/test_likelihoodratio.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions tests/testthat/_snaps/nestedLogit.md

This file was deleted.

3 changes: 3 additions & 0 deletions tests/testthat/test-test_likelihoodratio.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
m3 <- lm(mpg ~ wt + cyl, data = mtcars)

rez <- test_likelihoodratio(m1, m2, m3)
expect_equal(rez$Name, c("m1", "m2", "m3"))

Check warning on line 31 in tests/testthat/test-test_likelihoodratio.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-test_likelihoodratio.R,line=31,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.

rez <- test_likelihoodratio(list(m1, m2, m3))
expect_equal(rez$Name, c("Model 1", "Model 2", "Model 3"))

Check warning on line 34 in tests/testthat/test-test_likelihoodratio.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-test_likelihoodratio.R,line=34,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.

models <- list(m1, m2, m3)
rez <- test_likelihoodratio(models)
expect_equal(rez$Name, c("Model 1", "Model 2", "Model 3"))

Check warning on line 38 in tests/testthat/test-test_likelihoodratio.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-test_likelihoodratio.R,line=38,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
})

test_that("test_likelihoodratio - reversed order", {
Expand All @@ -62,7 +62,8 @@
test_that("test_likelihoodratio - lme4 ML", {
t1 <- test_lrt(m1, m2, m3)
t2 <- suppressMessages(anova(m1, m2, m3))
expect_equal(attributes(t1)$estimator, "ml")

Check warning on line 65 in tests/testthat/test-test_likelihoodratio.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-test_likelihoodratio.R,line=65,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
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
Expand All @@ -72,14 +73,15 @@

test_that("test_likelihoodratio - lme4 OLS", {
t2 <- test_lrt(m1, m2, m3, estimator = "ols")
expect_equal(attributes(t2)$estimator, "ols")

Check warning on line 76 in tests/testthat/test-test_likelihoodratio.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-test_likelihoodratio.R,line=76,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
expect_equal(t2$Chi2, c(NA, 105.73844, 1.04346), tolerance = 1e-3)
expect_equal(t2$p, c(NA, 0, 0.30702), tolerance = 1e-3)
})

test_that("test_likelihoodratio - lme4 REML", {
expect_warning(t3 <- test_lrt(m1, m2, m3, estimator = "REML"))

Check warning on line 82 in tests/testthat/test-test_likelihoodratio.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-test_likelihoodratio.R,line=82,col=18,[implicit_assignment_linter] Avoid implicit assignments in function calls. For example, instead of `if (x <- 1L) { ... }`, write `x <- 1L; if (x) { ... }`.
expect_equal(attributes(t3)$estimator, "reml")

Check warning on line 83 in tests/testthat/test-test_likelihoodratio.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-test_likelihoodratio.R,line=83,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
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)
})
Expand All @@ -91,9 +93,10 @@
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")

Check warning on line 99 in tests/testthat/test-test_likelihoodratio.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-test_likelihoodratio.R,line=99,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
})

m1 <- lm(Sepal.Length ~ Petal.Width, data = iris)
Expand All @@ -104,5 +107,5 @@
t1 <- anova(m1, m2, m3, test = "LRT")
t2 <- test_lrt(m1, m2, m3)
expect_equal(t1$`Pr(>Chi)`, t2$p, tolerance = 1e-3)
expect_equal(attributes(t2)$estimator, "ols")

Check warning on line 110 in tests/testthat/test-test_likelihoodratio.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-test_likelihoodratio.R,line=110,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
})
Loading