Skip to content
Merged
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: performance
Title: Assessment of Regression Models Performance
Version: 0.14.0.1
Version: 0.14.0.2
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ S3method(cronbachs_alpha,parameters_pca)
S3method(display,check_itemscale)
S3method(display,compare_performance)
S3method(display,performance_model)
S3method(display,test_likelihoodratio)
S3method(display,test_performance)
S3method(fitted,BFBayesFactor)
S3method(format,compare_performance)
S3method(format,performance_model)
S3method(format,performance_rmse)
S3method(format,test_likelihoodratio)
S3method(format,test_performance)
S3method(logLik,cpglm)
S3method(logLik,iv_robust)
Expand Down Expand Up @@ -345,10 +347,12 @@ S3method(print,test_performance)
S3method(print_html,check_group_variation)
S3method(print_html,check_itemscale)
S3method(print_html,compare_performance)
S3method(print_html,test_likelihoodratio)
S3method(print_html,test_performance)
S3method(print_md,check_itemscale)
S3method(print_md,compare_performance)
S3method(print_md,performance_model)
S3method(print_md,test_likelihoodratio)
S3method(print_md,test_performance)
S3method(r2,Arima)
S3method(r2,BBreg)
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# performance (devel)

## Changes

* Formatting of p-values in `test_likelihoodratio()` is now consistent with
formatted p-values from other functions.

# performance 0.14.0

## Breaking Changes
Expand Down
40 changes: 32 additions & 8 deletions R/test_likelihoodratio.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ plot.test_likelihoodratio <- function(x, ...) {

#' @export
print.test_likelihoodratio <- function(x, digits = 2, ...) {
cat(insight::export_table(format(x, digits = digits, ...), digits = digits, ...))
invisible(x)
}


#' @export
format.test_likelihoodratio <- function(x, digits = 2, p_digits = 3, ...) {
# Footer
if ("LogLik" %in% names(x)) {
best <- which.max(x$LogLik)
Expand All @@ -76,22 +83,39 @@ print.test_likelihoodratio <- function(x, digits = 2, ...) {
}

# value formatting
x$p <- insight::format_p(x$p, name = NULL)
x$p <- insight::format_p(x$p, digits = p_digits, name = NULL, ...)

if (is.null(attributes(x)$estimator)) {
estimator_string <- ""
} else {
estimator_string <- sprintf(" (%s-estimator)", toupper(attributes(x)$estimator))
}

cat(insight::export_table(
x,
digits = digits,
caption = c(paste0("# Likelihood-Ratio-Test (LRT) for Model Comparison", estimator_string), "blue"),
footer = footer
))
attr(x, "table_footer") <- footer
attr(x, "table_caption") <- c(paste0("# Likelihood-Ratio-Test (LRT) for Model Comparison", estimator_string), "blue")
x
}

invisible(x)

#' @export
print_md.test_likelihoodratio <- function(x, digits = 2, ...) {
insight::export_table(format(x, digits = digits, ...), format = "markdown", ...)
}


#' @export
print_html.test_likelihoodratio <- function(x, digits = 2, ...) {
insight::export_table(format(x, digits = digits, ...), format = "html", ...)
}


#' @export
display.test_likelihoodratio <- function(object, format = "markdown", digits = 2, ...) {
if (format == "markdown") {
print_md(x = object, digits = digits, ...)
} else {
print_html(x = object, digits = digits, ...)
}
}


Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/_snaps/test_likelihoodratio.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# test_likelihoodratio - print p-digits

Code
test_likelihoodratio(m1, m2)
Output
# Likelihood-Ratio-Test (LRT) for Model Comparison (OLS-estimator)

Name | Model | df | df_diff | Chi2 | p
--------------------------------------------
m1 | lm | 3 | | |
m2 | lm | 7 | 4 | 40.32 | < .001

---

Code
insight::print_md(test_likelihoodratio(m1, m2), p_digits = 3)
Output
[1] "Table: # Likelihood-Ratio-Test (LRT) for Model Comparison (OLS-estimator)"
[2] ""
[3] "|Name | Model| df| df_diff| Chi2| p|"
[4] "|:----|-----:|--:|-------:|-----:|------:|"
[5] "|m1 | lm| 3| | | |"
[6] "|m2 | lm| 7| 4| 40.32| < .001|"
attr(,"format")
[1] "pipe"
attr(,"class")
[1] "knitr_kable" "character"

9 changes: 9 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,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")

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$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 +72,14 @@

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

Check warning on line 75 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=75,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 81 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=81,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 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=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$Chi2, c(NA, 89.32933, 2.85635), tolerance = 1e-3)
expect_equal(t3$p, c(NA, 0, 0.09101), tolerance = 1e-3)
})
Expand All @@ -93,7 +93,7 @@
t2 <- test_lrt(m1, m2, m3)
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 96 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=96,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 +104,14 @@
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 107 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=107,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 - print p-digits", {
data(mtcars)
m1 <- lm(mpg ~ hp, data = mtcars)
m2 <- lm(mpg ~ hp * as.factor(gear), data = mtcars)

expect_snapshot(test_likelihoodratio(m1, m2))
expect_snapshot(insight::print_md(test_likelihoodratio(m1, m2), p_digits = 3))
})
Loading