Skip to content

Commit

Permalink
Fix for compare_parameters(effects = "random") (#338)
Browse files Browse the repository at this point in the history
* Fix for compare_parameters(effects = "random")

* fix

* add test

* lintr, fix test
  • Loading branch information
strengejacke committed Apr 5, 2024
1 parent 6782928 commit 7af4340
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 17 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: see
Title: Model Visualisation Toolbox for 'easystats' and 'ggplot2'
Version: 0.8.3.5
Version: 0.8.3.6
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
* `plot()` for `check_predictions()` now supports Bayesian regression models from
*brms* and *rstanarm*.

## Bug fixes

* Corrected order of models for `plot.compare_parameters()`.

# see 0.8.3

## Major changes
Expand Down
43 changes: 27 additions & 16 deletions R/plot.compare_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,13 @@ plot.see_compare_parameters <- function(x,
x$Component <- factor(x$Component, levels = unique(x$Component))
}


# show/hide intercepts
if (!show_intercept) {
x <- x[!.is_intercept(x$Parameter), ]
# sanity check - any data left?
if (nrow(x) == 0) {
insight::format_warning("No data left after removing intercepts. Returning empty plot. Try `show_intercept = TRUE`.") # nolint
}
}

if (isTRUE(sort) || (!is.null(sort) && sort == "ascending")) {
Expand Down Expand Up @@ -123,19 +127,19 @@ plot.see_compare_parameters <- function(x,
# largest data points that are within this range. Thereby we have the pretty
# values we can use as breaks and labels for the scale...
if (exponentiated_coefs) {
range <- 2^(-24:16)
x_low <- which.min(min(x$CI_low) > range) - 1L
x_high <- which.max(max(x$CI_high) < range)
exp_range <- 2^(-24:16)
x_low <- which.min(min(x$CI_low) > exp_range) - 1L
x_high <- which.max(max(x$CI_high) < exp_range)
if (add_values) {
# add some space to the right panel for text
new_range <- pretty(2 * max(x$CI_high))
x_high <- which.max(max(new_range) < range)
x_high <- which.max(max(new_range) < exp_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 = exp_range[x_low:x_high],
limits = c(exp_range[x_low], exp_range[x_high]),
labels = sprintf("%g", exp_range[x_low:x_high])
)
}

Expand Down Expand Up @@ -188,7 +192,7 @@ plot.see_compare_parameters <- function(x,

#' @export
data_plot.see_compare_parameters <- function(x, ...) {
col_coefficient <- which(grepl("^(Coefficient|Log-Odds|Log-Mean|Odds Ratio|Risk Ratio|IRR)\\.", colnames(x)))
col_coefficient <- grep("^(Coefficient|Log-Odds|Log-Mean|Odds Ratio|Risk Ratio|IRR)\\.", colnames(x))
col_ci_low <- which(startsWith(colnames(x), "CI_low."))
col_ci_high <- which(startsWith(colnames(x), "CI_high."))
col_p <- which(startsWith(colnames(x), "p."))
Expand All @@ -210,15 +214,22 @@ data_plot.see_compare_parameters <- function(x, ...) {
values_to = "CI_high",
columns = colnames(x)[col_ci_high]
)["CI_high"]
dataplot <- cbind(out1, out2, out3)

# if we have effects = "random", we probably don't have p-values. so
# check if this column exists, and if not, we skip it...
if (length(col_p) != 0) {
out4 <- .reshape_to_long(
x,
values_to = "p",
columns = colnames(x)[col_p]
)["p"]
dataplot <- cbind(dataplot, out4)
}

out4 <- .reshape_to_long(
x,
values_to = "p",
columns = colnames(x)[col_p]
)["p"]

dataplot <- cbind(out1, out2, out3, out4)
dataplot$group <- gsub("(.*)\\.(.*)", "\\2", dataplot$group)
# make factor, so order in legend is preserved
dataplot$group <- factor(dataplot$group, levels = unique(dataplot$group))

rownames(dataplot) <- NULL

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions tests/testthat/test-plot.compare_parameters.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
test_that("`plot()` for compare_parameters", {
skip_if_not_installed("glmmTMB")
skip_if_not_installed("lme4")
skip_if_not_installed("parameters")
gdat <- readRDS(system.file("vignette_data", "gophertortoise.rds", package = "glmmTMB"))
form <- shells ~ prev + offset(log(Area)) + factor(year) + (1 | Site)
gmod_glmer <- lme4::glmer(form, family = poisson, data = gdat)
gprior <- data.frame(
prior = "gamma(1e8, 2.5)",
class = "theta",
coef = "",
stringsAsFactors = FALSE
)
gmod_glmmTMB <- glmmTMB::glmmTMB(form, family = poisson, priors = gprior, data = gdat)

cp <- parameters::compare_parameters(gmod_glmer, gmod_glmmTMB, effects = "random")
expect_warning(plot(cp), "No data left")

skip_if_not_installed("vdiffr")
vdiffr::expect_doppelganger(
title = "plot.compare_parameters works",
fig = plot(cp, show_intercept = TRUE)
)
})

0 comments on commit 7af4340

Please sign in to comment.