From b4e1826ee75cb8ec9f52be9028e58254863937aa Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 5 Dec 2024 13:17:45 +0100 Subject: [PATCH] Error in model_parameters.aov plot() method? (#376) * Error in model_parameters.aov plot() method? Fixes #286 * fix --- DESCRIPTION | 2 +- NEWS.md | 5 ++++- R/plot.parameters_model.R | 6 ++++++ tests/testthat/test-plot.parameters_model.R | 10 ++++++++++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c68bd98ba..7170c43d3 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.12 +Version: 0.9.0.13 Authors@R: c(person(given = "Daniel", family = "Lüdecke", diff --git a/NEWS.md b/NEWS.md index 9a6fbd7a1..193ce7374 100644 --- a/NEWS.md +++ b/NEWS.md @@ -11,9 +11,12 @@ - `plot()` for `model_parameters()` gets a `show_direction` argument, to turn off the direction of the effect in the plot. +- `plot()` for `model_parameters()` now gives an informative error message when + trying to plot Anova tables (which does not work). + - `plot()` for `simulate_parameters()` now better copes with models that have multiple response levels (e.g. multinomial models). - + - Gains `{patchwork}` as a hard dependency given its importance for the package. ## Bug fixes diff --git a/R/plot.parameters_model.R b/R/plot.parameters_model.R index 6575aa04f..14c78a825 100644 --- a/R/plot.parameters_model.R +++ b/R/plot.parameters_model.R @@ -65,6 +65,12 @@ plot.see_parameters_model <- function(x, # retrieve settings ---------------- model_attributes <- attributes(x)[!names(attributes(x)) %in% c("names", "row.names", "class")] + # no plot for anova + model_class <- attributes(x)$model_class + if (!is.null(model_class) && any(model_class %in% c("aov", "anova"))) { + insight::format_error("Plots cannot be created for Anova tables. Please fit a (linear) regression model and try again.") # nolint + } + # for GAMs, remove smooth terms if (!is.null(x$Component) && any(x$Component == "smooth_terms")) { x <- x[x$Component != "smooth_terms", ] diff --git a/tests/testthat/test-plot.parameters_model.R b/tests/testthat/test-plot.parameters_model.R index 96163d234..73565c1ac 100644 --- a/tests/testthat/test-plot.parameters_model.R +++ b/tests/testthat/test-plot.parameters_model.R @@ -14,6 +14,16 @@ test_that("`plot.see_parameters_model()` works", { ) }) + +test_that("`plot.see_parameters_model()` errors for anova tables", { + skip_if_not_installed("parameters") + data(iris) + m <- aov(Sepal.Length ~ Species, data = iris) + mp <- parameters::model_parameters(m) + expect_error(plot(mp), regex = "Plots cannot be created") +}) + + test_that("`plot.see_parameters_model()` random parameters works", { skip_if_not_installed("lme4") skip_if_not_installed("parameters")