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

Error in model_parameters.aov plot() method? #376

Merged
merged 2 commits into from
Dec 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.9.0.12
Version: 0.9.0.13
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions R/plot.parameters_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#' result
#' plot(result)
#' @export
plot.see_parameters_model <- function(x,

Check warning on line 50 in R/plot.parameters_model.R

View workflow job for this annotation

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

file=R/plot.parameters_model.R,line=50,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 144 to at most 40.
show_intercept = FALSE,
size_point = 0.8,
size_text = NA,
Expand All @@ -65,6 +65,12 @@
# 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", ]
Expand All @@ -91,7 +97,7 @@

# Clean up column names
if (!any(grepl("Coefficient", colnames(x), fixed = TRUE))) {
colnames(x)[which.min(match(colnames(x), c("Median", "Mean", "Map", "MAP", model_attributes$coefficient_name)))] <- "Coefficient"

Check warning on line 100 in R/plot.parameters_model.R

View workflow job for this annotation

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

file=R/plot.parameters_model.R,line=100,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 133 characters.
}

if (!any(grepl("Parameter", colnames(x), fixed = TRUE))) {
Expand Down Expand Up @@ -161,7 +167,7 @@
if (!is.null(cleaned_parameters)) {
x <- merge(x, cleaned_parameters, sort = FALSE)
x$Parameter <- x$Cleaned_Parameter
if (all(x$Group == "")) {

Check warning on line 170 in R/plot.parameters_model.R

View workflow job for this annotation

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

file=R/plot.parameters_model.R,line=170,col=15,[nzchar_linter] Use !nzchar(x) instead of x == "". Note that unlike nzchar(), EQ coerces to character, so you'll have to use as.character() if x is a factor. Whenever missing data is possible, please take care to use nzchar(., keepNA = TRUE); nzchar(NA) is TRUE by default.
x$Group <- NULL
} else {
x <- x[!startsWith(x$Group, "SD/Cor"), , drop = FALSE]
Expand All @@ -180,7 +186,7 @@
x <- .fix_facet_names(x)

if (is_bayesian && "Group" %in% colnames(x)) {
x$Effects[x$Group != ""] <- paste0(x$Effects[x$Group != ""], " (", x$Group[x$Group != ""], ")")

Check warning on line 189 in R/plot.parameters_model.R

View workflow job for this annotation

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

file=R/plot.parameters_model.R,line=189,col=15,[nzchar_linter] Use nzchar(x) instead of x != "". Note that unlike nzchar(), NE coerces to character, so you'll have to use as.character() if x is a factor. Whenever missing data is possible, please take care to use nzchar(., keepNA = TRUE); nzchar(NA) is TRUE by default.

Check warning on line 189 in R/plot.parameters_model.R

View workflow job for this annotation

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

file=R/plot.parameters_model.R,line=189,col=50,[nzchar_linter] Use nzchar(x) instead of x != "". Note that unlike nzchar(), NE coerces to character, so you'll have to use as.character() if x is a factor. Whenever missing data is possible, please take care to use nzchar(., keepNA = TRUE); nzchar(NA) is TRUE by default.

Check warning on line 189 in R/plot.parameters_model.R

View workflow job for this annotation

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

file=R/plot.parameters_model.R,line=189,col=80,[nzchar_linter] Use nzchar(x) instead of x != "". Note that unlike nzchar(), NE coerces to character, so you'll have to use as.character() if x is a factor. Whenever missing data is possible, please take care to use nzchar(., keepNA = TRUE); nzchar(NA) is TRUE by default.
}

# remember components
Expand All @@ -196,7 +202,7 @@
is_meta_bma <- !is.null(mc) && any(mc %in% c("meta_random", "meta_fixed", "meta_bma"))

# minor fixes for Bayesian models
if (!is.null(mc) && !is.null(cp) && any(mc %in% c("stanreg", "stanmvreg", "brmsfit")) && length(cp) == length(x$Parameter)) {

Check warning on line 205 in R/plot.parameters_model.R

View workflow job for this annotation

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

file=R/plot.parameters_model.R,line=205,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 127 characters.
x$Parameter <- cp
}

Expand All @@ -208,9 +214,9 @@

if (is_bootstrap || isTRUE(is_bayesian)) {
if (is_bootstrap) {
data <- model_attributes$boot_samples

Check warning on line 217 in R/plot.parameters_model.R

View workflow job for this annotation

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

file=R/plot.parameters_model.R,line=217,col=9,[object_overwrite_linter] 'data' is an exported object from package 'utils'. Avoid re-using such symbols.
} else {
data <- as.data.frame(.retrieve_data(x))

Check warning on line 219 in R/plot.parameters_model.R

View workflow job for this annotation

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

file=R/plot.parameters_model.R,line=219,col=9,[object_overwrite_linter] 'data' is an exported object from package 'utils'. Avoid re-using such symbols.
}

# MCMC or bootstrapped models
Expand All @@ -218,7 +224,7 @@
insight::format_error("Could not retrieve parameter simulations.")
}

data <- datawizard::reshape_longer(

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

View workflow job for this annotation

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

file=R/plot.parameters_model.R,line=227,col=7,[object_overwrite_linter] 'data' is an exported object from package 'utils'. Avoid re-using such symbols.
data,
names_to = "Parameter",
rows_to = "Iteration",
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-plot.parameters_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading