Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for compare_parameters(effects = "random") #338

Merged
merged 4 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -22,7 +22,7 @@
#' result <- parameters::compare_parameters(lm1, lm2, lm3)
#' plot(result)
#' @export
plot.see_compare_parameters <- function(x,

Check warning on line 25 in R/plot.compare_parameters.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/plot.compare_parameters.R,line=25,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 44 to at most 40.
show_intercept = FALSE,
size_point = 0.8,
size_text = NA,
Expand Down Expand Up @@ -75,9 +75,13 @@
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 @@
# 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)

Check warning on line 132 in R/plot.compare_parameters.R

View check run for this annotation

Codecov / codecov/patch

R/plot.compare_parameters.R#L130-L132

Added lines #L130 - L132 were not covered by tests
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)

Check warning on line 136 in R/plot.compare_parameters.R

View check run for this annotation

Codecov / codecov/patch

R/plot.compare_parameters.R#L136

Added line #L136 was not covered by tests
}
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])

Check warning on line 142 in R/plot.compare_parameters.R

View check run for this annotation

Codecov / codecov/patch

R/plot.compare_parameters.R#L140-L142

Added lines #L140 - L142 were not covered by tests
)
}

Expand Down Expand Up @@ -188,7 +192,7 @@

#' @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 @@
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)

Check warning on line 227 in R/plot.compare_parameters.R

View check run for this annotation

Codecov / codecov/patch

R/plot.compare_parameters.R#L222-L227

Added lines #L222 - L227 were not covered by tests
}

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)
)
})
Loading