From 3b3a15798c969921871c31ebe178cebdd1c2d89e Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 6 Oct 2024 19:57:16 +0200 Subject: [PATCH] update tests --- R/1_model_parameters.R | 2 +- .../_snaps/model_parameters.fixest.new.md | 17 +++++ .../_snaps/model_parameters.glm.new.md | 39 ++++++++++ .../_snaps/model_parameters.mixed.new.md | 72 +++++++++++++++++++ tests/testthat/_snaps/printing.md | 3 +- tests/testthat/test-model_parameters.fixest.R | 2 +- tests/testthat/test-model_parameters.glm.R | 4 +- tests/testthat/test-model_parameters.mixed.R | 2 +- tests/testthat/test-p_adjust.R | 4 +- tests/testthat/test-printing.R | 2 +- vignettes/model_parameters.Rmd | 10 +-- vignettes/model_parameters_print.Rmd | 12 ++-- 12 files changed, 149 insertions(+), 20 deletions(-) create mode 100644 tests/testthat/_snaps/model_parameters.fixest.new.md create mode 100644 tests/testthat/_snaps/model_parameters.glm.new.md create mode 100644 tests/testthat/_snaps/model_parameters.mixed.new.md diff --git a/R/1_model_parameters.R b/R/1_model_parameters.R index 24b56d367..02555cd6d 100644 --- a/R/1_model_parameters.R +++ b/R/1_model_parameters.R @@ -605,7 +605,7 @@ model_parameters.default <- function(model, standardize = standardize, exponentiate = exponentiate, p_adjust = p_adjust, - summary = summary, + include_info = include_info, keep_parameters = keep, drop_parameters = drop, vcov = vcov, diff --git a/tests/testthat/_snaps/model_parameters.fixest.new.md b/tests/testthat/_snaps/model_parameters.fixest.new.md new file mode 100644 index 000000000..c095a3fe0 --- /dev/null +++ b/tests/testthat/_snaps/model_parameters.fixest.new.md @@ -0,0 +1,17 @@ +# model_parameters.fixest + + Code + model_parameters(m1, include_info = TRUE, verbose = FALSE) + Output + # Fixed Effects + + Parameter | Coefficient | SE | 95% CI | t(187) | p + ----------------------------------------------------------------- + time | 1.09 | 0.67 | [-0.23, 2.41] | 1.63 | 0.106 + phq4 | -3.66 | 0.67 | [-4.98, -2.34] | -5.45 | < .001 + + Model: QoL ~ time + phq4 (564 Observations) + Sigma: 12.365 (df = 561) + r2: 0.743; ar2: 0.613; wr2: 0.180; war2: 0.175 + RMSE : 10.069 + diff --git a/tests/testthat/_snaps/model_parameters.glm.new.md b/tests/testthat/_snaps/model_parameters.glm.new.md new file mode 100644 index 000000000..716d9eeb0 --- /dev/null +++ b/tests/testthat/_snaps/model_parameters.glm.new.md @@ -0,0 +1,39 @@ +# print digits model_parameters.lm + + Code + params + Output + Parameter | Coefficient | SE | 95% CI | t(30) | p + ------------------------------------------------------------------ + (Intercept) | 37.29 | 1.88 | [33.45, 41.12] | 19.86 | < .001 + wt | -5.34 | 0.56 | [-6.49, -4.20] | -9.56 | < .001 + + Model: mpg ~ wt (32 Observations) + Sigma: 3.046 (df = 30) + R2: 0.753; adjusted R2: 0.745 + RMSE : 2.949 + +--- + + Code + params + Output + Parameter | Coefficient | SE | 95% CI | t(30) | p + ------------------------------------------------------------------ + (Intercept) | 37.29 | 1.88 | [33.45, 41.12] | 19.86 | < .001 + wt | -5.34 | 0.56 | [-6.49, -4.20] | -9.56 | < .001 + +# model_parameters.glm - Gamma - print + + Code + mp + Output + Parameter | Prevalence Ratio | SE | 95% CI | t(7) | p + --------------------------------------------------------------------------- + (Intercept) | 245.48 | 46.72 | [173.66, 351.67] | 28.92 | < .001 + u [log] | 0.55 | 0.03 | [ 0.49, 0.61] | -10.88 | < .001 + Message + + Uncertainty intervals (profile-likelihood) and p-values (two-tailed) + computed using a Wald t-distribution approximation. + diff --git a/tests/testthat/_snaps/model_parameters.mixed.new.md b/tests/testthat/_snaps/model_parameters.mixed.new.md new file mode 100644 index 000000000..c89fefb71 --- /dev/null +++ b/tests/testthat/_snaps/model_parameters.mixed.new.md @@ -0,0 +1,72 @@ +# print-model_parameters + + Code + model_parameters(model, effects = "fixed") + Output + Parameter | Coefficient | SE | 95% CI | t(558) | p + ------------------------------------------------------------------- + (Intercept) | 71.53 | 1.56 | [68.48, 74.59] | 45.98 | < .001 + time | 1.09 | 0.64 | [-0.17, 2.34] | 1.70 | 0.089 + + # Within-Effects + + Parameter | Coefficient | SE | 95% CI | t(558) | p + ------------------------------------------------------------------- + phq4 within | -3.66 | 0.41 | [-4.46, -2.86] | -8.95 | < .001 + + # Between-Effects + + Parameter | Coefficient | SE | 95% CI | t(558) | p + -------------------------------------------------------------------- + phq4 between | -6.28 | 0.50 | [-7.27, -5.30] | -12.53 | < .001 + Message + + Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed + using a Wald t-distribution approximation. + +--- + + Code + model_parameters(m1, effects = "all") + Output + # Fixed Effects + + Parameter | Coefficient | SE | 95% CI | t(28) | p + ----------------------------------------------------------------- + (Intercept) | 0.65 | 0.50 | [-0.38, 1.68] | 1.29 | 0.206 + cyl | 0.40 | 0.08 | [ 0.25, 0.56] | 5.29 | < .001 + + # Random Effects + + Parameter | Coefficient | SE | 95% CI + -------------------------------------------------------- + SD (Intercept: gear) | 0.27 | 0.24 | [0.05, 1.54] + SD (Residual) | 0.59 | 0.08 | [0.46, 0.77] + Message + + Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed + using a Wald t-distribution approximation. Uncertainty intervals for + random effect variances computed using a Wald z-distribution + approximation. + +--- + + Code + model_parameters(m1, effects = "fixed", include_info = TRUE) + Output + # Fixed Effects + + Parameter | Coefficient | SE | 95% CI | t(28) | p + ----------------------------------------------------------------- + (Intercept) | 0.65 | 0.50 | [-0.38, 1.68] | 1.29 | 0.206 + cyl | 0.40 | 0.08 | [ 0.25, 0.56] | 5.29 | < .001 + + Model: wt ~ cyl (32 Observations) + Sigma: 0.594 (df = 28) + Conditional R2: 0.628; Marginal R2: 0.550 + RMSE : 0.564 + Message + + Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed + using a Wald t-distribution approximation. + diff --git a/tests/testthat/_snaps/printing.md b/tests/testthat/_snaps/printing.md index c3e657e4e..750751645 100644 --- a/tests/testthat/_snaps/printing.md +++ b/tests/testthat/_snaps/printing.md @@ -92,8 +92,9 @@ Species [virginica] * Petal Length | 0.45 | 0.29 | [-0.12, 1.03] | 1.56 | 0.120 Model: Sepal.Length ~ Species * Petal.Length (150 Observations) - Residual standard deviation: 0.336 (df = 144) + Sigma: 0.336 (df = 144) R2: 0.840; adjusted R2: 0.835 + RMSE : 0.330 Message Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed diff --git a/tests/testthat/test-model_parameters.fixest.R b/tests/testthat/test-model_parameters.fixest.R index f34f75866..567ba491f 100644 --- a/tests/testthat/test-model_parameters.fixest.R +++ b/tests/testthat/test-model_parameters.fixest.R @@ -35,7 +35,7 @@ test_that("model_parameters.fixest", { # currently, a bug for fixest 10.4 on R >= 4.3 # skip_if_not(getRversion() < "4.2.0") expect_snapshot( - model_parameters(m1, summary = TRUE, verbose = FALSE) + model_parameters(m1, include_info = TRUE, verbose = FALSE) ) # Poission, df = Inf diff --git a/tests/testthat/test-model_parameters.glm.R b/tests/testthat/test-model_parameters.glm.R index c5cebbb75..969a7a6f3 100644 --- a/tests/testthat/test-model_parameters.glm.R +++ b/tests/testthat/test-model_parameters.glm.R @@ -36,10 +36,10 @@ test_that("print digits model_parameters.lm", { skip_if_not_installed("performance") model <- lm(mpg ~ wt, data = mtcars) - params <- model_parameters(model, summary = TRUE, verbose = FALSE) + params <- model_parameters(model, include_info = TRUE, verbose = FALSE) expect_snapshot(params) - params <- model_parameters(model, summary = FALSE, verbose = FALSE) + params <- model_parameters(model, include_info = FALSE, verbose = FALSE) expect_snapshot(params) }) diff --git a/tests/testthat/test-model_parameters.mixed.R b/tests/testthat/test-model_parameters.mixed.R index 7cebab25e..4f7ddc432 100644 --- a/tests/testthat/test-model_parameters.mixed.R +++ b/tests/testthat/test-model_parameters.mixed.R @@ -129,5 +129,5 @@ test_that("print-model_parameters", { skip_if_not_installed("merDeriv") expect_snapshot(model_parameters(m1, effects = "all")) - expect_snapshot(model_parameters(m1, effects = "fixed", summary = TRUE)) + expect_snapshot(model_parameters(m1, effects = "fixed", include_info = TRUE)) }) diff --git a/tests/testthat/test-p_adjust.R b/tests/testthat/test-p_adjust.R index aada4dacd..95496035b 100644 --- a/tests/testthat/test-p_adjust.R +++ b/tests/testthat/test-p_adjust.R @@ -23,7 +23,7 @@ test_that("model_parameters, p-adjust after keep/drop", { ) expect_message( - mp <- model_parameters(model, summary = TRUE, keep = c("wt", "hp"), p_adjust = "bonferroni"), + mp <- model_parameters(model, include_info = TRUE, keep = c("wt", "hp"), p_adjust = "bonferroni"), "more than 1 element" ) expect_equal( @@ -34,7 +34,7 @@ test_that("model_parameters, p-adjust after keep/drop", { ) expect_message( - mp <- model_parameters(model, summary = TRUE, keep = c("cyl", "gear"), p_adjust = "bonferroni"), + mp <- model_parameters(model, include_info = TRUE, keep = c("cyl", "gear"), p_adjust = "bonferroni"), "more than 1 element" ) expect_equal( diff --git a/tests/testthat/test-printing.R b/tests/testthat/test-printing.R index ceff6e642..462689fa7 100644 --- a/tests/testthat/test-printing.R +++ b/tests/testthat/test-printing.R @@ -25,7 +25,7 @@ withr::with_options( # snapshot breaks between R CMD check "classic" and "strict" skip_if_not_installed("performance") model <- lm(Sepal.Length ~ Species * Petal.Length, data = iris) - out <- model_parameters(model, summary = TRUE) + out <- model_parameters(model, include_info = TRUE) expect_snapshot(print(out)) }) diff --git a/vignettes/model_parameters.Rmd b/vignettes/model_parameters.Rmd index 99946751f..28ba6f093 100644 --- a/vignettes/model_parameters.Rmd +++ b/vignettes/model_parameters.Rmd @@ -1,6 +1,6 @@ --- title: "Summary of Model Parameters" -output: +output: rmarkdown::html_vignette: toc: true fig_width: 10.08 @@ -10,7 +10,7 @@ vignette: > %\VignetteIndexEntry{Summary of Model Parameters} \usepackage[utf8]{inputenc} %\VignetteEngine{knitr::rmarkdown} -editor_options: +editor_options: chunk_output_type: console bibliography: bibliography.bib --- @@ -150,9 +150,9 @@ glm(vs ~ poly(mpg, 2) + cyl, data = mtcars, family = binomial()) |> ``` ```{r} -# show Odds Ratios and include model summary +# show Odds Ratios and include model info glm(vs ~ poly(mpg, 2) + cyl, data = mtcars, family = binomial()) |> - parameters(exponentiate = TRUE, summary = TRUE) + parameters(exponentiate = TRUE, include_info = TRUE) ``` ### Mixed Models @@ -269,7 +269,7 @@ parameters( The **parameters** package extends the support to structural models. -### Principal Component Analysis (PCA) and Exploratory Factor Analysis (EFA) +### Principal Component Analysis (PCA) and Exploratory Factor Analysis (EFA) ```{r eval=all(successfully_loaded[c("psych", "nFactors")])} psych::pca(mtcars, nfactors = 3) |> diff --git a/vignettes/model_parameters_print.Rmd b/vignettes/model_parameters_print.Rmd index c4a94810d..2a80f8f06 100644 --- a/vignettes/model_parameters_print.Rmd +++ b/vignettes/model_parameters_print.Rmd @@ -1,6 +1,6 @@ --- title: "Printing Model Parameters" -output: +output: rmarkdown::html_vignette: toc: true fig_width: 10.08 @@ -10,7 +10,7 @@ vignette: > %\VignetteIndexEntry{Printing Model Parameters} \usepackage[utf8]{inputenc} %\VignetteEngine{knitr::rmarkdown} -editor_options: +editor_options: chunk_output_type: console bibliography: bibliography.bib --- @@ -122,13 +122,13 @@ mp <- model_parameters(model) print(mp, split_component = FALSE) ``` -## Adding model summaries +## Adding model information -A model summary can be added to the table when `summary = TRUE` in the call to `model_parameters()`: +A model summary can be added to the table when `include_info = TRUE` in the call to `model_parameters()`: ```{r} model <- lm(Sepal.Length ~ Species * Petal.Length, data = iris) -model_parameters(model, summary = TRUE) +model_parameters(model, include_info = TRUE) ``` ## Including the reference level of categorical variables @@ -380,6 +380,6 @@ print(tab, table_width = 80) # More advances tables and markdown / HTML formatting -The `print_md()` as well as `print_html()` functions can be used to create markdown (for knitting to PDF or Word) and HTML tables. +The `print_md()` as well as `print_html()` functions can be used to create markdown (for knitting to PDF or Word) and HTML tables. Meanwhile, there are a lot of additional packages that allow users to have even more flexibility regarding table layouts. One package we can recommend is the [*modelsummary* package](https://vincentarelbundock.github.io/modelsummary/).