From 6533602c6395eb3c0db32959e58863b3b6ae9d78 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 4 Nov 2024 11:01:58 +0100 Subject: [PATCH] Fix snapshot tests (#1037) * Fix snapshot tests Fixes #1036 * fix * fix * fix * fix * add snapshot * typo, lintr --- R/1_model_parameters.R | 2 +- R/methods_lme4.R | 2 +- man/model_parameters.averaging.Rd | 2 +- man/model_parameters.cgam.Rd | 2 +- man/model_parameters.default.Rd | 2 +- man/model_parameters.glht.Rd | 2 +- man/model_parameters.htest.Rd | 2 +- man/model_parameters.merMod.Rd | 2 +- man/model_parameters.mlm.Rd | 2 +- man/model_parameters.rma.Rd | 2 +- man/model_parameters.zcpglm.Rd | 2 +- man/n_clusters.Rd | 2 +- tests/testthat/_snaps/printing2.md | 34 ++++++++++++++++++++++++ tests/testthat/test-compare_parameters.R | 2 +- tests/testthat/test-format_parameters.R | 2 +- tests/testthat/test-pipe.R | 27 +++++++++++-------- tests/testthat/test-print_AER_labels.R | 16 ++++++----- tests/testthat/test-printing.R | 16 +++++------ tests/testthat/test-printing2.R | 34 +++++++++++++++++++++--- vignettes/model_parameters_print.Rmd | 4 ++- 20 files changed, 115 insertions(+), 44 deletions(-) diff --git a/R/1_model_parameters.R b/R/1_model_parameters.R index 13c6e67cd..769656f54 100644 --- a/R/1_model_parameters.R +++ b/R/1_model_parameters.R @@ -504,7 +504,7 @@ parameters <- model_parameters #' - If `s_value = TRUE`, the p-value will be replaced by the S-value in the #' output (cf. _Rafi and Greenland 2020_). #' - `pd` adds an additional column with the _probability of direction_ (see -#' [`bayestestR::p_direction()`] for details). urthermore, see 'Examples' for +#' [`bayestestR::p_direction()`] for details). Furthermore, see 'Examples' for #' this function. #' - For developers, whose interest mainly is to get a "tidy" data frame of #' model summaries, it is recommended to set `pretty_names = FALSE` to speed diff --git a/R/methods_lme4.R b/R/methods_lme4.R index 7e282a20d..653bde9a5 100644 --- a/R/methods_lme4.R +++ b/R/methods_lme4.R @@ -45,7 +45,7 @@ #' - If `s_value = TRUE`, the p-value will be replaced by the S-value in the #' output (cf. _Rafi and Greenland 2020_). #' - `pd` adds an additional column with the _probability of direction_ (see -#' [`bayestestR::p_direction()`] for details). urthermore, see 'Examples' for +#' [`bayestestR::p_direction()`] for details). Furthermore, see 'Examples' for #' this function. #' - For developers, whose interest mainly is to get a "tidy" data frame of #' model summaries, it is recommended to set `pretty_names = FALSE` to speed diff --git a/man/model_parameters.averaging.Rd b/man/model_parameters.averaging.Rd index 8f3ab7f94..fa91a77ee 100644 --- a/man/model_parameters.averaging.Rd +++ b/man/model_parameters.averaging.Rd @@ -298,7 +298,7 @@ arguments will be passed to the print-method, or can directly be used in \item If \code{s_value = TRUE}, the p-value will be replaced by the S-value in the output (cf. \emph{Rafi and Greenland 2020}). \item \code{pd} adds an additional column with the \emph{probability of direction} (see -\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). urthermore, see 'Examples' for +\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). Furthermore, see 'Examples' for this function. \item For developers, whose interest mainly is to get a "tidy" data frame of model summaries, it is recommended to set \code{pretty_names = FALSE} to speed diff --git a/man/model_parameters.cgam.Rd b/man/model_parameters.cgam.Rd index 91b385eb8..e04cbeb3a 100644 --- a/man/model_parameters.cgam.Rd +++ b/man/model_parameters.cgam.Rd @@ -152,7 +152,7 @@ arguments will be passed to the print-method, or can directly be used in \item If \code{s_value = TRUE}, the p-value will be replaced by the S-value in the output (cf. \emph{Rafi and Greenland 2020}). \item \code{pd} adds an additional column with the \emph{probability of direction} (see -\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). urthermore, see 'Examples' for +\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). Furthermore, see 'Examples' for this function. \item For developers, whose interest mainly is to get a "tidy" data frame of model summaries, it is recommended to set \code{pretty_names = FALSE} to speed diff --git a/man/model_parameters.default.Rd b/man/model_parameters.default.Rd index a291af51e..e377fe27c 100644 --- a/man/model_parameters.default.Rd +++ b/man/model_parameters.default.Rd @@ -197,7 +197,7 @@ arguments will be passed to the print-method, or can directly be used in \item If \code{s_value = TRUE}, the p-value will be replaced by the S-value in the output (cf. \emph{Rafi and Greenland 2020}). \item \code{pd} adds an additional column with the \emph{probability of direction} (see -\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). urthermore, see 'Examples' for +\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). Furthermore, see 'Examples' for this function. \item For developers, whose interest mainly is to get a "tidy" data frame of model summaries, it is recommended to set \code{pretty_names = FALSE} to speed diff --git a/man/model_parameters.glht.Rd b/man/model_parameters.glht.Rd index ea52fa1a8..55ded27eb 100644 --- a/man/model_parameters.glht.Rd +++ b/man/model_parameters.glht.Rd @@ -70,7 +70,7 @@ arguments will be passed to the print-method, or can directly be used in \item If \code{s_value = TRUE}, the p-value will be replaced by the S-value in the output (cf. \emph{Rafi and Greenland 2020}). \item \code{pd} adds an additional column with the \emph{probability of direction} (see -\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). urthermore, see 'Examples' for +\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). Furthermore, see 'Examples' for this function. \item For developers, whose interest mainly is to get a "tidy" data frame of model summaries, it is recommended to set \code{pretty_names = FALSE} to speed diff --git a/man/model_parameters.htest.Rd b/man/model_parameters.htest.Rd index 2bc8ffd18..5254ca2ad 100644 --- a/man/model_parameters.htest.Rd +++ b/man/model_parameters.htest.Rd @@ -58,7 +58,7 @@ arguments will be passed to the print-method, or can directly be used in \item If \code{s_value = TRUE}, the p-value will be replaced by the S-value in the output (cf. \emph{Rafi and Greenland 2020}). \item \code{pd} adds an additional column with the \emph{probability of direction} (see -\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). urthermore, see 'Examples' for +\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). Furthermore, see 'Examples' for this function. \item For developers, whose interest mainly is to get a "tidy" data frame of model summaries, it is recommended to set \code{pretty_names = FALSE} to speed diff --git a/man/model_parameters.merMod.Rd b/man/model_parameters.merMod.Rd index 8ca2889ca..5f34e5714 100644 --- a/man/model_parameters.merMod.Rd +++ b/man/model_parameters.merMod.Rd @@ -307,7 +307,7 @@ arguments will be passed to the print-method, or can directly be used in \item If \code{s_value = TRUE}, the p-value will be replaced by the S-value in the output (cf. \emph{Rafi and Greenland 2020}). \item \code{pd} adds an additional column with the \emph{probability of direction} (see -\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). urthermore, see 'Examples' for +\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). Furthermore, see 'Examples' for this function. \item For developers, whose interest mainly is to get a "tidy" data frame of model summaries, it is recommended to set \code{pretty_names = FALSE} to speed diff --git a/man/model_parameters.mlm.Rd b/man/model_parameters.mlm.Rd index 637f81f70..ccc605b36 100644 --- a/man/model_parameters.mlm.Rd +++ b/man/model_parameters.mlm.Rd @@ -166,7 +166,7 @@ arguments will be passed to the print-method, or can directly be used in \item If \code{s_value = TRUE}, the p-value will be replaced by the S-value in the output (cf. \emph{Rafi and Greenland 2020}). \item \code{pd} adds an additional column with the \emph{probability of direction} (see -\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). urthermore, see 'Examples' for +\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). Furthermore, see 'Examples' for this function. \item For developers, whose interest mainly is to get a "tidy" data frame of model summaries, it is recommended to set \code{pretty_names = FALSE} to speed diff --git a/man/model_parameters.rma.Rd b/man/model_parameters.rma.Rd index e552f53d1..39d3d96dd 100644 --- a/man/model_parameters.rma.Rd +++ b/man/model_parameters.rma.Rd @@ -100,7 +100,7 @@ arguments will be passed to the print-method, or can directly be used in \item If \code{s_value = TRUE}, the p-value will be replaced by the S-value in the output (cf. \emph{Rafi and Greenland 2020}). \item \code{pd} adds an additional column with the \emph{probability of direction} (see -\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). urthermore, see 'Examples' for +\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). Furthermore, see 'Examples' for this function. \item For developers, whose interest mainly is to get a "tidy" data frame of model summaries, it is recommended to set \code{pretty_names = FALSE} to speed diff --git a/man/model_parameters.zcpglm.Rd b/man/model_parameters.zcpglm.Rd index e7f4965a6..4ed13d492 100644 --- a/man/model_parameters.zcpglm.Rd +++ b/man/model_parameters.zcpglm.Rd @@ -132,7 +132,7 @@ arguments will be passed to the print-method, or can directly be used in \item If \code{s_value = TRUE}, the p-value will be replaced by the S-value in the output (cf. \emph{Rafi and Greenland 2020}). \item \code{pd} adds an additional column with the \emph{probability of direction} (see -\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). urthermore, see 'Examples' for +\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). Furthermore, see 'Examples' for this function. \item For developers, whose interest mainly is to get a "tidy" data frame of model summaries, it is recommended to set \code{pretty_names = FALSE} to speed diff --git a/man/n_clusters.Rd b/man/n_clusters.Rd index 0a7f1561e..8a55f3a01 100644 --- a/man/n_clusters.Rd +++ b/man/n_clusters.Rd @@ -106,7 +106,7 @@ arguments will be passed to the print-method, or can directly be used in \item If \code{s_value = TRUE}, the p-value will be replaced by the S-value in the output (cf. \emph{Rafi and Greenland 2020}). \item \code{pd} adds an additional column with the \emph{probability of direction} (see -\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). urthermore, see 'Examples' for +\code{\link[bayestestR:p_direction]{bayestestR::p_direction()}} for details). Furthermore, see 'Examples' for this function. \item For developers, whose interest mainly is to get a "tidy" data frame of model summaries, it is recommended to set \code{pretty_names = FALSE} to speed diff --git a/tests/testthat/_snaps/printing2.md b/tests/testthat/_snaps/printing2.md index 088c53ee8..6e9f663db 100644 --- a/tests/testthat/_snaps/printing2.md +++ b/tests/testthat/_snaps/printing2.md @@ -141,6 +141,40 @@ ------------------------------------------------------------------------------------------- Observations | 150 | | 150 | +--- + + Code + print(out, groups = list(Species = c("Species [versicolor]", + "Species [virginica]"), Interactions = c( + "Species [versicolor] * Petal Length", "Species [virginica] * Petal Length"), + Controls = "Petal Length"), select = "{estimate}|{p}") + Output + Parameter | Estimate (lm1) | p (lm1) + ---------------------------------------------------------------- + Species | | + Species [versicolor] | -1.60 | <0.001 + Species [virginica] | -2.12 | <0.001 + Interactions | | + Species [versicolor] * Petal Length | | + Species [virginica] * Petal Length | | + Controls | | + Petal Length | 0.90 | <0.001 + ---------------------------------------------------------------- + Observations | 150 | + + Parameter | Estimate (lm2) | p (lm2) + ---------------------------------------------------------------- + Species | | + Species [versicolor] | -1.69 | 0.003 + Species [virginica] | -1.19 | 0.048 + Interactions | | + Species [versicolor] * Petal Length | -0.01 | 0.961 + Species [virginica] * Petal Length | -0.15 | 0.574 + Controls | | + Petal Length | 0.39 | 0.138 + ---------------------------------------------------------------- + Observations | 150 | + # combination of different models Code diff --git a/tests/testthat/test-compare_parameters.R b/tests/testthat/test-compare_parameters.R index 272127e37..270cdb400 100644 --- a/tests/testthat/test-compare_parameters.R +++ b/tests/testthat/test-compare_parameters.R @@ -2,7 +2,7 @@ skip_if_not_installed("withr") # make sure we have the correct interaction mark for tests withr::with_options( - list(parameters_interaction = "*"), + list(parameters_interaction = "*", easystats_table_width = Inf), { data(iris) m1 <- lm(Sepal.Length ~ Species, data = iris) diff --git a/tests/testthat/test-format_parameters.R b/tests/testthat/test-format_parameters.R index 5785f2720..2ef273c70 100644 --- a/tests/testthat/test-format_parameters.R +++ b/tests/testthat/test-format_parameters.R @@ -2,7 +2,7 @@ skip_if_not_installed("splines") # make sure we have the correct interaction mark for tests withr::with_options( - list(parameters_interaction = "*"), + list(parameters_interaction = "*", easystats_table_width = Inf), { # define here because messes up the expected output bs <- splines::bs diff --git a/tests/testthat/test-pipe.R b/tests/testthat/test-pipe.R index e9d047edf..9674c7458 100644 --- a/tests/testthat/test-pipe.R +++ b/tests/testthat/test-pipe.R @@ -14,15 +14,20 @@ test_that("print in pipe", { ) }) -test_that("print in pipe, on-the-fly factor", { - data(mtcars) - out <- capture.output({ - mtcars |> - lm(mpg ~ cut(wt, c(0, 2.5, 3, 5)), data = _) |> - model_parameters(include_reference = TRUE) + +skip_if_not_installed("withr") +withr::with_options( + list(easystats_table_width = Inf), + test_that("print in pipe, on-the-fly factor", { + data(mtcars) + out <- capture.output({ + mtcars |> + lm(mpg ~ cut(wt, c(0, 2.5, 3, 5)), data = _) |> + model_parameters(include_reference = TRUE) + }) + expect_identical( + out[4], + "cut(wt, c(0, 2.5, 3, 5)) [>0-2.5] | 0.00 | | | | " + ) }) - expect_identical( - out[4], - "cut(wt, c(0, 2.5, 3, 5)) [>0-2.5] | 0.00 | | | | " - ) -}) +) diff --git a/tests/testthat/test-print_AER_labels.R b/tests/testthat/test-print_AER_labels.R index 7420cc2b0..0c44edd22 100644 --- a/tests/testthat/test-print_AER_labels.R +++ b/tests/testthat/test-print_AER_labels.R @@ -1,9 +1,13 @@ skip_if_not_installed("AER") skip_if_not_installed("datawizard") +skip_if_not_installed("withr") -test_that("templates", { - data(efc, package = "datawizard") - model <- AER::tobit(neg_c_7 ~ e42dep + c172code, data = efc) - mp <- model_parameters(model) - expect_snapshot(print(mp, pretty_names = "labels")) -}) +withr::with_options( + list(easystats_table_width = Inf), + test_that("templates", { + data(efc, package = "datawizard") + model <- AER::tobit(neg_c_7 ~ e42dep + c172code, data = efc) + mp <- model_parameters(model) + expect_snapshot(print(mp, pretty_names = "labels")) + }) +) diff --git a/tests/testthat/test-printing.R b/tests/testthat/test-printing.R index 462689fa7..0c99e10fb 100644 --- a/tests/testthat/test-printing.R +++ b/tests/testthat/test-printing.R @@ -3,7 +3,7 @@ skip_if_not_installed("withr") skip_if(getRversion() < "4.0.0") withr::with_options( - list(parameters_interaction = "*"), + list(parameters_interaction = "*", easystats_table_width = Inf), { # Splitting model components ---- test_that("print model with multiple components", { @@ -111,12 +111,10 @@ withr::with_options( withr::with_options( list(parameters_warning_exponentiate = TRUE), - { - test_that("message about interpretation of log-resoponse", { - data(mtcars) - m <- lm(log(mpg) ~ gear, data = mtcars) - out <- model_parameters(m, exponentiate = TRUE) - expect_snapshot(print(out)) - }) - } + test_that("message about interpretation of log-resoponse", { + data(mtcars) + m <- lm(log(mpg) ~ gear, data = mtcars) + out <- model_parameters(m, exponentiate = TRUE) + expect_snapshot(print(out)) + }) ) diff --git a/tests/testthat/test-printing2.R b/tests/testthat/test-printing2.R index 57c186e6e..8e932fdb2 100644 --- a/tests/testthat/test-printing2.R +++ b/tests/testthat/test-printing2.R @@ -2,7 +2,7 @@ skip_if_not_installed("withr") skip_if(getRversion() < "4.0.0") withr::with_options( - list(parameters_interaction = "*"), + list(parameters_interaction = "*", easystats_table_width = Inf), { lm1 <- lm(Sepal.Length ~ Species, data = iris) lm2 <- lm(Sepal.Length ~ Species + Petal.Length, data = iris) @@ -66,7 +66,7 @@ withr::with_options( "Species [virginica]" ), Interactions = c( - "Species [versicolor] * Petal Length", # note the unicode char! + "Species [versicolor] * Petal Length", "Species [virginica] * Petal Length" ), Controls = "Petal Length" @@ -79,7 +79,7 @@ withr::with_options( "Species [virginica]" ), Interactions = c( - "Species [versicolor] * Petal Length", # note the unicode char! + "Species [versicolor] * Petal Length", "Species [virginica] * Petal Length" ), Controls = "Petal Length" @@ -112,3 +112,31 @@ withr::with_options( }) } ) + + +withr::with_options( + list(parameters_interaction = "*"), + { + lm1 <- lm(Sepal.Length ~ Species + Petal.Length, data = iris) + lm2 <- lm(Sepal.Width ~ Species * Petal.Length, data = iris) + + # remove intercept + out <- compare_parameters(lm1, lm2, drop = "^\\(Intercept") + + test_that("templates, glue-3, separate columnns", { + expect_snapshot( + print(out, groups = list( + Species = c( + "Species [versicolor]", + "Species [virginica]" + ), + Interactions = c( + "Species [versicolor] * Petal Length", + "Species [virginica] * Petal Length" + ), + Controls = "Petal Length" + ), select = "{estimate}|{p}") + ) + }) + } +) diff --git a/vignettes/model_parameters_print.Rmd b/vignettes/model_parameters_print.Rmd index 633458920..0b7eb47be 100644 --- a/vignettes/model_parameters_print.Rmd +++ b/vignettes/model_parameters_print.Rmd @@ -119,7 +119,9 @@ Redundant columns are removed. The related model component is shown as table hea ```{r eval=successfully_loaded["glmmTMB"]} mp <- model_parameters(model) -print(mp, split_component = FALSE) +# We use `table_width` here to print a wider table, +# which is not split into multiple tables +print(mp, split_component = FALSE, table_width = Inf) ``` ## Adding model information