diff --git a/DESCRIPTION b/DESCRIPTION index 4b042073a..cbb8e40c6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", diff --git a/NAMESPACE b/NAMESPACE index 5eda67e35..5975554fd 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) diff --git a/NEWS.md b/NEWS.md index 879e295bf..1ad002ae4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/test_likelihoodratio.R b/R/test_likelihoodratio.R index 20a58f6fb..42828572b 100644 --- a/R/test_likelihoodratio.R +++ b/R/test_likelihoodratio.R @@ -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) @@ -76,7 +83,7 @@ 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 <- "" @@ -84,14 +91,31 @@ print.test_likelihoodratio <- function(x, digits = 2, ...) { 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, ...) + } } diff --git a/tests/testthat/_snaps/test_likelihoodratio.md b/tests/testthat/_snaps/test_likelihoodratio.md new file mode 100644 index 000000000..21742371c --- /dev/null +++ b/tests/testthat/_snaps/test_likelihoodratio.md @@ -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" + diff --git a/tests/testthat/test-test_likelihoodratio.R b/tests/testthat/test-test_likelihoodratio.R index 9967ae3d4..b34676b40 100644 --- a/tests/testthat/test-test_likelihoodratio.R +++ b/tests/testthat/test-test_likelihoodratio.R @@ -106,3 +106,12 @@ test_that("test_likelihoodratio - lm", { expect_equal(t1$`Pr(>Chi)`, t2$p, tolerance = 1e-3) expect_equal(attributes(t2)$estimator, "ols") }) + +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)) +})