From c89dcb139b05f751647e4ff7c2987735e8237238 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 29 Sep 2024 12:40:06 +0200 Subject: [PATCH] Issue plotting lme4 random effects from `model_parameters` (#366) * Issue plotting lme4 random effects from `model_parameters` Fixes #365 * add tests * update plots * Update plot-model-parameters-1.svg * update * fix * add snapshot * fix * better * remotes * update snapshots --- DESCRIPTION | 3 +- NEWS.md | 3 + R/plot.parameters_model.R | 171 +++++++++-- .../plot-check-dag-all-adjusted.svg | 18 +- .../plot.check_dag/plot-check-dag-all.svg | 18 +- .../plot.check_dag/plot-check-dag-direct1.svg | 18 +- .../plot.check_dag/plot-check-dag-direct2.svg | 18 +- .../plot.check_dag/plot-check-dag-direct3.svg | 18 +- .../plot.check_dag/plot-check-dag-direct4.svg | 18 +- .../plot-check-dag-multiple-adjustments.svg | 18 +- .../plot.check_dag/plot-check-dag-total1.svg | 18 +- .../plot.check_dag/plot-check-dag-total2.svg | 18 +- .../plot.check_dag/plot-check-dag-total3.svg | 18 +- .../plot.check_dag/plot-check-dag-total4.svg | 18 +- .../plot-model-parameters-1.svg | 62 ++++ .../plot-model-parameters-doublerandom.svg | 191 ++++++++++++ .../plot-model-parameters-random-0.svg | 93 ++++++ .../plot-model-parameters-random-1.svg | 256 ++++++++++++++++ .../plot-model-parameters-random-2.svg | 290 ++++++++++++++++++ .../plot-model-parameters-random-3.svg | 290 ++++++++++++++++++ .../plot-model-parameters-random-4.svg | 290 ++++++++++++++++++ .../plot-model-parameters-random-5.svg | 281 +++++++++++++++++ .../plot-model-parameters-random-6.svg | 162 ++++++++++ .../vdiffr_geoms_coords/geom-point2-works.svg | 20 +- tests/testthat/test-plot.parameters_model.R | 78 ++++- tests/testthat/test-vdiffr_geoms_coords.R | 30 +- vignettes/parameters.Rmd | 7 + 27 files changed, 2273 insertions(+), 152 deletions(-) create mode 100644 tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-1.svg create mode 100644 tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-doublerandom.svg create mode 100644 tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-0.svg create mode 100644 tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-1.svg create mode 100644 tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-2.svg create mode 100644 tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-3.svg create mode 100644 tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-4.svg create mode 100644 tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-5.svg create mode 100644 tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-6.svg diff --git a/DESCRIPTION b/DESCRIPTION index 30fb12014..d836e1090 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: see Title: Model Visualisation Toolbox for 'easystats' and 'ggplot2' -Version: 0.9.0.2 +Version: 0.9.0.3 Authors@R: c(person(given = "Daniel", family = "Lüdecke", @@ -121,3 +121,4 @@ Config/testthat/edition: 3 Config/testthat/parallel: true Config/Needs/website: easystats/easystatstemplate Config/rcmdcheck/ignore-inconsequential-notes: true +Remotes: easystats/performance, easystats/parameters diff --git a/NEWS.md b/NEWS.md index d0c299923..0246faa94 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,9 @@ - `plot()` for `p_function()` now checks the values of the `size_length` argument, to give an informative error message when the input is not valid. +- `plot()` for `model_parameters()` now also plots group-levels of random effects + (i.e. for mixed models, when `model_parameters(x, ..., group_level = TRUE)`). + # see 0.9.0 ## Changes diff --git a/R/plot.parameters_model.R b/R/plot.parameters_model.R index 6206c4066..3b4cf2b6a 100644 --- a/R/plot.parameters_model.R +++ b/R/plot.parameters_model.R @@ -53,6 +53,16 @@ plot.see_parameters_model <- function(x, # retrieve settings ---------------- model_attributes <- attributes(x)[!names(attributes(x)) %in% c("names", "row.names", "class")] + # user wants to plot random effects (group levels)? + if (isFALSE(model_attributes$ignore_group) && + isTRUE(model_attributes$mixed_model) && + !"brmsfit" %in% model_attributes$model_class) { + if (missing(show_intercept)) { + show_intercept <- TRUE + } + return(.group_level_plot(x, size_point, size_text, sort, n_columns, show_intercept, show_labels, ...)) + } + # show intercept for intercept-only models if (insight::is_model(x) && insight::is_nullmodel(x)) { show_intercept <- TRUE @@ -461,21 +471,21 @@ plot.see_parameters_model <- function(x, # values we can use as breaks and labels for the scale... if (exponentiated_coefs && log_scale) { - range <- 2^(-24:16) - x_low <- which.min(min_ci > range) - 1 - x_high <- which.max(max_ci < range) + axis_range <- 2^(-24:16) + x_low <- which.min(min_ci > axis_range) - 1 + x_high <- which.max(max_ci < axis_range) if (add_values) { # add some space to the right panel for text new_range <- pretty(2 * max_ci) - x_high <- which.max(max(new_range) < range) + x_high <- which.max(max(new_range) < axis_range) } p <- p + scale_x_continuous( trans = "log", - breaks = range[x_low:x_high], - limits = c(range[x_low], range[x_high]), - labels = sprintf("%g", range[x_low:x_high]) + breaks = axis_range[x_low:x_high], + limits = c(axis_range[x_low], axis_range[x_high]), + labels = sprintf("%g", axis_range[x_low:x_high]) ) } @@ -526,32 +536,145 @@ plot.see_parameters_model <- function(x, if (isTRUE(is_meta)) { measure <- .meta_measure(meta_measure) - p + labs( + p + ggplot2::labs( y = "", x = measure, colour = "CI" ) + } else if (isTRUE(axis_title_in_facet)) { + p + ggplot2::labs( + y = parameter_label, + x = NULL, + colour = "CI" + ) } else { - if (isTRUE(axis_title_in_facet)) { - p + labs( - y = parameter_label, - x = NULL, - colour = "CI" - ) - } else { - p + labs( - y = parameter_label, - x = ifelse(is.null(coefficient_name), - ifelse(exponentiated_coefs, "Exp(Estimate)", "Estimate"), # nolint - coefficient_name - ), - colour = "CI" - ) - } + p + ggplot2::labs( + y = parameter_label, + x = ifelse(is.null(coefficient_name), + ifelse(exponentiated_coefs, "Exp(Estimate)", "Estimate"), # nolint + coefficient_name + ), + colour = "CI" + ) } } +.group_level_plot <- function(x, size_point, size_text, sort, n_columns, show_intercept, show_labels, ...) { + # filter random effects + x <- x[x$Effects == "random", ] + # remove intercept? + if (isFALSE(show_intercept) && length(.is_intercept(x$Parameter)) > 0L) { + x <- x[!.is_intercept(x$Parameter), ] + } + # prepare group variable + x$Group <- paste(x$Group, x$Parameter, sep = ": ") + + # define columns + if (is.null(n_columns)) { + n_columns <- 1 + } + + # define text size + if (is.null(size_text) || is.na(size_text)) { + size_text <- 4 + } + + # for now, we fix the NULL to 0. Maybe we could exp() random parameters + # for logistic regression and similar models, but currently only link-scale + y_intercept <- 0 + + # plot setup for regular model parameters + x$colored <- factor(x$Coefficient < y_intercept, levels = c(FALSE, TRUE)) + if (all(x$colored == "TRUE")) { + color_scale <- scale_color_material(reverse = TRUE) + fill_scale <- scale_fill_material(reverse = TRUE) + } else { + color_scale <- scale_color_material() + fill_scale <- scale_fill_material() + } + + # create text string for estimate and CI + x$Estimate_CI <- sprintf( + "%.2f %s", + x$Coefficient, + insight::format_ci(x$CI_low, x$CI_high, ci = NULL, digits = 2, zap_small = TRUE) + ) + + # handle sorting + if (isTRUE(sort) || (!is.null(sort) && sort == "ascending")) { + x$Level <- factor(x$Level, levels = rev(unique(x$Level)[order(x$Coefficient)])) + } else if (!is.null(sort) && sort == "descending") { + x$Level <- factor(x$Level, levels = unique(x$Level)[order(x$Coefficient)]) + } else { + # sort coefficients as they appear in the classical summary output by default + x$Level <- factor(x$Level, levels = rev(unique(x$Level))) + } + + # find min/max range based on CI + min_ci <- min(x$CI_low, na.rm = TRUE) + max_ci <- max(x$CI_high, na.rm = TRUE) + + # here we check if all facets have the same scale. If so, we set the scales + # to fixed, otherwise we set them to free_y (in facet_wrap). This removes + # a redundant scale for plots with identical scales. + check_scales <- split(x$Level, x$Group) + if (isTRUE(identical(unname(check_scales[-length(check_scales)]), unname(check_scales[-1])))) { + facet_scales <- "fixed" + } else { + facet_scales <- "free_y" + } + + p <- ggplot2::ggplot( + x, + ggplot2::aes( + x = .data$Coefficient, + y = .data$Level, + xmin = .data$CI_low, + xmax = .data$CI_high, + colour = .data$colored, + fill = .data$colored + ) + ) + + ggplot2::geom_vline( + ggplot2::aes(xintercept = y_intercept), + linetype = "dotted", + colour = "black" + ) + + theme_modern(legend.position = "none") + + color_scale + + fill_scale + + ggplot2::geom_errorbarh( + height = 0, + linewidth = size_point + ) + + ggplot2::geom_point( + size = 4 * size_point, + colour = "white", + shape = 21 + ) + + ggplot2::facet_wrap(~Group, ncol = n_columns, scales = facet_scales) + + # add coefficients and CIs? + if (isTRUE(show_labels)) { + # add some space to the right panel for text + space_factor <- (n_columns^(size_text / 1.2)) * sqrt(ceiling(diff(c(min_ci, max_ci))) / 5) + new_range <- pretty(c(min_ci, max_ci + space_factor)) + + # expand scale range and add numbers to the right border + if (!any(is.infinite(new_range)) && !anyNA(new_range)) { + p <- p + + ggplot2::geom_text( + mapping = ggplot2::aes(label = .data$Estimate_CI, x = Inf), + colour = "black", hjust = "inward", size = size_text + ) + + ggplot2::xlim(c(min(new_range), max(new_range))) + } + } + + p +} + .funnel_plot <- function(x, size_point = 3, meta_measure = NULL) { max_y <- max(pretty(max(x$SE) * 105)) / 100 diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-all-adjusted.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-all-adjusted.svg index 95822a2db..d563dc99a 100644 --- a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-all-adjusted.svg +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-all-adjusted.svg @@ -38,6 +38,15 @@ + + + + + + + + + @@ -69,15 +78,6 @@ Current model - - - - - - - - - diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-all.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-all.svg index 8b86196b3..9fc49c707 100644 --- a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-all.svg +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-all.svg @@ -38,6 +38,15 @@ + + + + + + + + + @@ -69,15 +78,6 @@ Current model - - - - - - - - - diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct1.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct1.svg index e76ef0ca3..3bd5ada8c 100644 --- a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct1.svg +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct1.svg @@ -38,6 +38,15 @@ + + + + + + + + + @@ -72,15 +81,6 @@ Current model - - - - - - - - - diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct2.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct2.svg index da4d27376..9f67b8091 100644 --- a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct2.svg +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct2.svg @@ -38,6 +38,15 @@ + + + + + + + + + @@ -80,15 +89,6 @@ unadjusted Current model - - - - - - - - - diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct3.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct3.svg index f4abadf97..7c0225943 100644 --- a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct3.svg +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct3.svg @@ -38,6 +38,15 @@ + + + + + + + + + @@ -80,15 +89,6 @@ unadjusted Current model - - - - - - - - - diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct4.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct4.svg index 72f80e6af..309d2b0eb 100644 --- a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct4.svg +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct4.svg @@ -38,6 +38,15 @@ + + + + + + + + + @@ -72,15 +81,6 @@ Current model - - - - - - - - - diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-multiple-adjustments.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-multiple-adjustments.svg index 90c2c7d2e..edf9b6f1f 100644 --- a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-multiple-adjustments.svg +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-multiple-adjustments.svg @@ -38,6 +38,15 @@ + + + + + + + + + @@ -88,15 +97,6 @@ Current model - - - - - - - - - diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total1.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total1.svg index 428c5a28f..0f5d9db17 100644 --- a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total1.svg +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total1.svg @@ -38,6 +38,15 @@ + + + + + + + + + @@ -72,15 +81,6 @@ Current model - - - - - - - - - diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total2.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total2.svg index 58447c6e8..4f6ee0763 100644 --- a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total2.svg +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total2.svg @@ -38,6 +38,15 @@ + + + + + + + + + @@ -72,15 +81,6 @@ Current model - - - - - - - - - diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total3.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total3.svg index ce20d9a44..c6c7f251c 100644 --- a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total3.svg +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total3.svg @@ -38,6 +38,15 @@ + + + + + + + + + @@ -80,15 +89,6 @@ unadjusted Current model - - - - - - - - - diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total4.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total4.svg index e5ddeaada..77504bc48 100644 --- a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total4.svg +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total4.svg @@ -38,6 +38,15 @@ + + + + + + + + + @@ -72,15 +81,6 @@ Current model - - - - - - - - - diff --git a/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-1.svg b/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-1.svg new file mode 100644 index 000000000..626eb9f46 --- /dev/null +++ b/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-1.svg @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +disp +gear +cyl +wt + +-6 +-4 +-2 +0 +Coefficient +plot.model_parameters_1 + + diff --git a/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-doublerandom.svg b/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-doublerandom.svg new file mode 100644 index 000000000..b20ad27ee --- /dev/null +++ b/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-doublerandom.svg @@ -0,0 +1,191 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +grp: (Intercept) + + + + + + + + + +Subject: (Intercept) + + + +-100 +-50 +0 +50 +100 + +372 +371 +370 +369 +352 +351 +350 +349 +337 +335 +334 +333 +332 +331 +330 +310 +309 +308 + +5 +4 +3 +2 +1 +Coefficient +Level +plot.model_parameters_doublerandom + + diff --git a/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-0.svg b/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-0.svg new file mode 100644 index 000000000..c9efd6bd8 --- /dev/null +++ b/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-0.svg @@ -0,0 +1,93 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Random Effects + + + + + + + + + +Fixed Effects + + + +0 +10 +20 +30 + +0 +5 +10 + +Days + +SD (Observations) +SD (Days) +Coefficient +plot.model_parameters_random_0 + + diff --git a/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-1.svg b/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-1.svg new file mode 100644 index 000000000..aad093030 --- /dev/null +++ b/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-1.svg @@ -0,0 +1,256 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Subject: Days + + + + + + + + + +Subject: (Intercept) + + + +-60 +-30 +0 +30 +60 + +372 +371 +370 +369 +352 +351 +350 +349 +337 +335 +334 +333 +332 +331 +330 +310 +309 +308 + +372 +371 +370 +369 +352 +351 +350 +349 +337 +335 +334 +333 +332 +331 +330 +310 +309 +308 +Coefficient +Level +plot.model_parameters_random_1 + + diff --git a/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-2.svg b/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-2.svg new file mode 100644 index 000000000..7b60e0279 --- /dev/null +++ b/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-2.svg @@ -0,0 +1,290 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +2.26 [-21.40, 25.92] +-40.40 [-64.06, -16.74] +-38.96 [-62.62, -15.30] +23.69 [0.03, 47.35] +22.26 [-1.40, 45.92] +9.04 [-14.62, 32.70] +16.84 [-6.82, 40.50] +-7.23 [-30.89, 16.43] +-0.33 [-23.99, 23.32] +34.89 [11.23, 58.55] +-25.21 [-48.87, -1.55] +-13.07 [-36.73, 10.59] +4.58 [-19.08, 28.24] +20.86 [-2.79, 44.52] +3.28 [-20.38, 26.93] +-25.61 [-49.27, -1.95] +0.81 [-22.85, 24.47] +12.31 [-11.34, 35.97] + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +9.20 [4.68, 13.72] +-8.62 [-13.14, -4.10] +-5.45 [-9.97, -0.93] +-4.81 [-9.33, -0.30] +-3.07 [-7.59, 1.45] +-0.27 [-4.79, 4.25] +-0.22 [-4.74, 4.29] +1.07 [-3.44, 5.59] +-10.75 [-15.27, -6.23] +8.63 [4.11, 13.15] +1.17 [-3.34, 5.69] +6.61 [2.10, 11.13] +-3.02 [-7.53, 1.50] +3.54 [-0.98, 8.05] +0.87 [-3.65, 5.39] +4.82 [0.31, 9.34] +-0.99 [-5.51, 3.53] +1.28 [-3.23, 5.80] + + + + + + + + + +Subject: Days + + + + + + + + + +Subject: (Intercept) + + + +-50 +0 +50 + +372 +371 +370 +369 +352 +351 +350 +349 +337 +335 +334 +333 +332 +331 +330 +310 +309 +308 + +372 +371 +370 +369 +352 +351 +350 +349 +337 +335 +334 +333 +332 +331 +330 +310 +309 +308 +Coefficient +Level +plot.model_parameters_random_2 + + diff --git a/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-3.svg b/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-3.svg new file mode 100644 index 000000000..1cccf6187 --- /dev/null +++ b/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-3.svg @@ -0,0 +1,290 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +2.26 [-21.40, 25.92] +-40.40 [-64.06, -16.74] +-38.96 [-62.62, -15.30] +23.69 [0.03, 47.35] +22.26 [-1.40, 45.92] +9.04 [-14.62, 32.70] +16.84 [-6.82, 40.50] +-7.23 [-30.89, 16.43] +-0.33 [-23.99, 23.32] +34.89 [11.23, 58.55] +-25.21 [-48.87, -1.55] +-13.07 [-36.73, 10.59] +4.58 [-19.08, 28.24] +20.86 [-2.79, 44.52] +3.28 [-20.38, 26.93] +-25.61 [-49.27, -1.95] +0.81 [-22.85, 24.47] +12.31 [-11.34, 35.97] + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +9.20 [4.68, 13.72] +-8.62 [-13.14, -4.10] +-5.45 [-9.97, -0.93] +-4.81 [-9.33, -0.30] +-3.07 [-7.59, 1.45] +-0.27 [-4.79, 4.25] +-0.22 [-4.74, 4.29] +1.07 [-3.44, 5.59] +-10.75 [-15.27, -6.23] +8.63 [4.11, 13.15] +1.17 [-3.34, 5.69] +6.61 [2.10, 11.13] +-3.02 [-7.53, 1.50] +3.54 [-0.98, 8.05] +0.87 [-3.65, 5.39] +4.82 [0.31, 9.34] +-0.99 [-5.51, 3.53] +1.28 [-3.23, 5.80] + + + + + + + + + +Subject: Days + + + + + + + + + +Subject: (Intercept) + + + +-50 +0 +50 + +372 +371 +370 +369 +352 +351 +350 +349 +337 +335 +334 +333 +332 +331 +330 +310 +309 +308 + +372 +371 +370 +369 +352 +351 +350 +349 +337 +335 +334 +333 +332 +331 +330 +310 +309 +308 +Coefficient +Level +plot.model_parameters_random_3 + + diff --git a/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-4.svg b/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-4.svg new file mode 100644 index 000000000..87fa94a0e --- /dev/null +++ b/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-4.svg @@ -0,0 +1,290 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +2.26 [-21.40, 25.92] +-40.40 [-64.06, -16.74] +-38.96 [-62.62, -15.30] +23.69 [0.03, 47.35] +22.26 [-1.40, 45.92] +9.04 [-14.62, 32.70] +16.84 [-6.82, 40.50] +-7.23 [-30.89, 16.43] +-0.33 [-23.99, 23.32] +34.89 [11.23, 58.55] +-25.21 [-48.87, -1.55] +-13.07 [-36.73, 10.59] +4.58 [-19.08, 28.24] +20.86 [-2.79, 44.52] +3.28 [-20.38, 26.93] +-25.61 [-49.27, -1.95] +0.81 [-22.85, 24.47] +12.31 [-11.34, 35.97] + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +9.20 [4.68, 13.72] +-8.62 [-13.14, -4.10] +-5.45 [-9.97, -0.93] +-4.81 [-9.33, -0.30] +-3.07 [-7.59, 1.45] +-0.27 [-4.79, 4.25] +-0.22 [-4.74, 4.29] +1.07 [-3.44, 5.59] +-10.75 [-15.27, -6.23] +8.63 [4.11, 13.15] +1.17 [-3.34, 5.69] +6.61 [2.10, 11.13] +-3.02 [-7.53, 1.50] +3.54 [-0.98, 8.05] +0.87 [-3.65, 5.39] +4.82 [0.31, 9.34] +-0.99 [-5.51, 3.53] +1.28 [-3.23, 5.80] + + + + + + + + + +Subject: Days + + + + + + + + + +Subject: (Intercept) + + + +-50 +0 +50 + +337 +330 +331 +352 +333 +372 +332 +351 +369 +308 +371 +335 +334 +350 +349 +370 +310 +309 + +337 +330 +331 +352 +333 +372 +332 +351 +369 +308 +371 +335 +334 +350 +349 +370 +310 +309 +Coefficient +Level +plot.model_parameters_random_4 + + diff --git a/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-5.svg b/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-5.svg new file mode 100644 index 000000000..e69c31930 --- /dev/null +++ b/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-5.svg @@ -0,0 +1,281 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +2.26 [-21.40, 25.92] +-40.40 [-64.06, -16.74] +-38.96 [-62.62, -15.30] +23.69 [0.03, 47.35] +22.26 [-1.40, 45.92] +9.04 [-14.62, 32.70] +16.84 [-6.82, 40.50] +-7.23 [-30.89, 16.43] +-0.33 [-23.99, 23.32] +34.89 [11.23, 58.55] +-25.21 [-48.87, -1.55] +-13.07 [-36.73, 10.59] +4.58 [-19.08, 28.24] +20.86 [-2.79, 44.52] +3.28 [-20.38, 26.93] +-25.61 [-49.27, -1.95] +0.81 [-22.85, 24.47] +12.31 [-11.34, 35.97] + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +9.20 [4.68, 13.72] +-8.62 [-13.14, -4.10] +-5.45 [-9.97, -0.93] +-4.81 [-9.33, -0.30] +-3.07 [-7.59, 1.45] +-0.27 [-4.79, 4.25] +-0.22 [-4.74, 4.29] +1.07 [-3.44, 5.59] +-10.75 [-15.27, -6.23] +8.63 [4.11, 13.15] +1.17 [-3.34, 5.69] +6.61 [2.10, 11.13] +-3.02 [-7.53, 1.50] +3.54 [-0.98, 8.05] +0.87 [-3.65, 5.39] +4.82 [0.31, 9.34] +-0.99 [-5.51, 3.53] +1.28 [-3.23, 5.80] + + + + + + + + + +Subject: (Intercept) + + + + + + + + + +Subject: Days + + + +-100 +-50 +0 +50 +100 +150 + +-100 +-50 +0 +50 +100 +150 + +337 +330 +331 +352 +333 +372 +332 +351 +369 +308 +371 +335 +334 +350 +349 +370 +310 +309 +Coefficient +Level +plot.model_parameters_random_5 + + diff --git a/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-6.svg b/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-6.svg new file mode 100644 index 000000000..be78806e0 --- /dev/null +++ b/tests/testthat/_snaps/plot.parameters_model/plot-model-parameters-random-6.svg @@ -0,0 +1,162 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +9.20 [4.68, 13.72] +-8.62 [-13.14, -4.10] +-5.45 [-9.97, -0.93] +-4.81 [-9.33, -0.30] +-3.07 [-7.59, 1.45] +-0.27 [-4.79, 4.25] +-0.22 [-4.74, 4.29] +1.07 [-3.44, 5.59] +-10.75 [-15.27, -6.23] +8.63 [4.11, 13.15] +1.17 [-3.34, 5.69] +6.61 [2.10, 11.13] +-3.02 [-7.53, 1.50] +3.54 [-0.98, 8.05] +0.87 [-3.65, 5.39] +4.82 [0.31, 9.34] +-0.99 [-5.51, 3.53] +1.28 [-3.23, 5.80] + + + + + + + + + +Subject: Days + + + +-20 +-10 +0 +10 +20 + +308 +337 +350 +370 +352 +372 +349 +334 +369 +333 +332 +371 +351 +331 +330 +310 +309 +335 +Coefficient +Level +plot.model_parameters_random_6 + + diff --git a/tests/testthat/_snaps/vdiffr_geoms_coords/geom-point2-works.svg b/tests/testthat/_snaps/vdiffr_geoms_coords/geom-point2-works.svg index 26e4ff8bf..b63d4cd05 100644 --- a/tests/testthat/_snaps/vdiffr_geoms_coords/geom-point2-works.svg +++ b/tests/testthat/_snaps/vdiffr_geoms_coords/geom-point2-works.svg @@ -39,6 +39,16 @@ + + + + + + + + + + @@ -213,16 +223,6 @@ Petal.Width Sepal.Length - - - - - - - - - - diff --git a/tests/testthat/test-plot.parameters_model.R b/tests/testthat/test-plot.parameters_model.R index 7a52d8b6f..a8c2a8cdd 100644 --- a/tests/testthat/test-plot.parameters_model.R +++ b/tests/testthat/test-plot.parameters_model.R @@ -1,6 +1,82 @@ test_that("`plot.see_parameters_model()` works", { m <- lm(mpg ~ wt + cyl + gear + disp, data = mtcars) result <- parameters::model_parameters(m) - expect_s3_class(plot(result), "gg") + + skip_if_not_installed("vdiffr") + vdiffr::expect_doppelganger( + title = "plot.model_parameters_1", + fig = plot(result) + ) +}) + +test_that("`plot.see_parameters_model()` random parameters works", { + skip_if_not_installed("vdiffr") + skip_if_not_installed("lme4") + skip_if_not_installed("parameters") + + data(sleepstudy, package = "lme4") + s_mod <- lme4::lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy) + + out <- parameters::model_parameters(s_mod) + vdiffr::expect_doppelganger( + title = "plot.model_parameters_random_0", + fig = plot(out) + ) + + out <- parameters::model_parameters(s_mod, group_level = TRUE) + vdiffr::expect_doppelganger( + title = "plot.model_parameters_random_1", + fig = plot(out) + ) + + out <- parameters::model_parameters(s_mod, group_level = TRUE) + vdiffr::expect_doppelganger( + title = "plot.model_parameters_random_2", + fig = plot(out, show_labels = TRUE) + ) + + out <- parameters::model_parameters(s_mod, group_level = TRUE) + vdiffr::expect_doppelganger( + title = "plot.model_parameters_random_3", + fig = plot(out, show_labels = TRUE, size_text = 5) + ) + + out <- parameters::model_parameters(s_mod, group_level = TRUE) + vdiffr::expect_doppelganger( + title = "plot.model_parameters_random_4", + fig = plot(out, sort = "ascending", show_labels = TRUE) + ) + + out <- parameters::model_parameters(s_mod, group_level = TRUE) + vdiffr::expect_doppelganger( + title = "plot.model_parameters_random_5", + fig = plot(out, sort = "ascending", show_labels = TRUE, n_columns = 2) + ) + + out <- parameters::model_parameters(s_mod, group_level = TRUE) + vdiffr::expect_doppelganger( + title = "plot.model_parameters_random_6", + fig = plot(out, sort = "ascending", show_labels = TRUE, show_intercept = FALSE) + ) +}) + + +test_that("`plot.see_parameters_model()` random parameters works", { + skip_if_not_installed("vdiffr") + skip_if_not_installed("lme4") + skip_if_not_installed("parameters") + data(sleepstudy, package = "lme4") + + set.seed(12345) + sleepstudy$grp <- sample(1:5, size = 180, replace = TRUE) + model <- lme4::lmer( + Reaction ~ Days + (1 | grp) + (1 | Subject), + data = sleepstudy + ) + out <- parameters::model_parameters(model, group_level = TRUE) + vdiffr::expect_doppelganger( + title = "plot.model_parameters_doublerandom", + fig = plot(out) + ) }) diff --git a/tests/testthat/test-vdiffr_geoms_coords.R b/tests/testthat/test-vdiffr_geoms_coords.R index 845b95486..ed9f0a724 100644 --- a/tests/testthat/test-vdiffr_geoms_coords.R +++ b/tests/testthat/test-vdiffr_geoms_coords.R @@ -1,36 +1,32 @@ test_that("geom and coord functions work correctly", { - skip_if_not_installed("poorman") - suppressPackageStartupMessages(library(poorman)) - + skip_if_not_installed("ggplot2") # coord_radar() ------------------ - data <- iris %>% - group_by(Species) %>% - summarise(across(everything(), mean)) %>% - datawizard::reshape_longer(c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")) + data <- aggregate(iris[1:4], list(Species = iris$Species), mean) + data <- datawizard::reshape_longer(data, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")) set.seed(123) vdiffr::expect_doppelganger( title = "coord_radar() works", - fig = ggplot( + fig = ggplot2::ggplot( data, - aes( + ggplot2::aes( x = name, y = value, color = Species, group = Species ) ) + - geom_polygon(fill = NA, linewidth = 2) + + ggplot2::geom_polygon(fill = NA, linewidth = 2) + coord_radar(start = -pi / 4) ) # geom_point2() ---------------------- - normal <- ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) + - geom_point(size = 8, alpha = 0.3) + + normal <- ggplot2::ggplot(iris, ggplot2::aes(x = Petal.Width, y = Sepal.Length)) + + ggplot2::geom_point(size = 8, alpha = 0.3) + theme_modern() - new <- ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) + + new <- ggplot2::ggplot(iris, ggplot2::aes(x = Petal.Width, y = Sepal.Length)) + geom_point2(size = 8, alpha = 0.3) + theme_modern() @@ -45,7 +41,7 @@ test_that("geom and coord functions work correctly", { set.seed(123) vdiffr::expect_doppelganger( title = "`geom_poolpoint()` works - 1", - fig = ggplot(iris, aes(x = Petal.Width, y = Sepal.Length, color = Species)) + + fig = ggplot2::ggplot(iris, ggplot2::aes(x = Petal.Width, y = Sepal.Length, color = Species)) + geom_poolpoint(label = rownames(iris)) + scale_color_flat_d() + theme_modern() @@ -54,7 +50,7 @@ test_that("geom and coord functions work correctly", { set.seed(123) vdiffr::expect_doppelganger( title = "`geom_poolpoint()` works - 2", - fig = ggplot(iris, aes(x = Petal.Width, y = Sepal.Length, color = Species)) + + fig = ggplot2::ggplot(iris, ggplot2::aes(x = Petal.Width, y = Sepal.Length, color = Species)) + geom_pooljitter(label = rownames(iris)) + scale_color_flat_d() + theme_modern() @@ -65,7 +61,7 @@ test_that("geom and coord functions work correctly", { set.seed(123) vdiffr::expect_doppelganger( title = "geom_violindot() works", - fig = ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) + + fig = ggplot2::ggplot(iris, ggplot2::aes(x = Species, y = Sepal.Length, fill = Species)) + geom_violindot() + theme_modern() ) @@ -75,7 +71,7 @@ test_that("geom and coord functions work correctly", { set.seed(123) vdiffr::expect_doppelganger( title = "geom_violinhalf() works", - fig = ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) + + fig = ggplot2::ggplot(iris, ggplot2::aes(x = Species, y = Sepal.Length, fill = Species)) + geom_violinhalf() + theme_modern() + scale_fill_material_d() diff --git a/vignettes/parameters.Rmd b/vignettes/parameters.Rmd index 8eb0514eb..1ec2a730f 100644 --- a/vignettes/parameters.Rmd +++ b/vignettes/parameters.Rmd @@ -148,6 +148,13 @@ result <- model_parameters(model2) plot(result) ``` +### Including group levels of random effects + +```{r} +result <- model_parameters(model3, group_level = TRUE) +plot(result) +``` + ### Changing parameter names in the plot By default, `model_parameters()` returns a data frame, where the parameter names are found in the column `Parameter`. These names are used by default in the generated plot: