Skip to content

Commit

Permalink
fix GAM issues
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 29, 2024
1 parent 3d4383a commit 89174b6
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
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 `simulate_parameters()` now better copes with models that have
multiple response levels (e.g. multinomial models).

## Bug fixes

- Fixed issue in `plot()` for `parameters::model_parameters()` for GAM models.

# see 0.9.0

## Changes
Expand Down
13 changes: 11 additions & 2 deletions R/plot.parameters_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ plot.see_parameters_model <- function(x,
# retrieve settings ----------------
model_attributes <- attributes(x)[!names(attributes(x)) %in% c("names", "row.names", "class")]

# for GAMs, remove smooth terms
if (!is.null(x$Component) && any(x$Component == "smooth_terms")) {
x <- x[x$Component != "smooth_terms", ]
# if we only have one component left, remove Component column
if (insight::n_unique(x$Component) == 1) {
x$Component <- NULL
}
}

# user wants to plot random effects (group levels)?
if (isFALSE(model_attributes$ignore_group) &&
isTRUE(model_attributes$mixed_model) &&
Expand Down Expand Up @@ -366,7 +375,7 @@ plot.see_parameters_model <- function(x,
x$CI <- as.character(x$CI)

x$group <- factor(x$Coefficient < y_intercept, levels = c(FALSE, TRUE))
if (all(x$group == "TRUE")) {
if (all(x$group == "TRUE", na.rm = TRUE)) {
color_scale <- scale_color_material(reverse = TRUE)
} else {
color_scale <- scale_color_material()
Expand Down Expand Up @@ -401,7 +410,7 @@ plot.see_parameters_model <- function(x,
} else {
# plot setup for regular model parameters
x$group <- factor(x$Coefficient < y_intercept, levels = c(FALSE, TRUE))
if (all(x$group == "TRUE")) {
if (all(x$group == "TRUE", na.rm = TRUE)) {
color_scale <- scale_color_material(reverse = TRUE)
} else {
color_scale <- scale_color_material()
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 16 additions & 1 deletion tests/testthat/test-plot.parameters_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ test_that("`plot.see_parameters_model()` random parameters works", {
data(sleepstudy, package = "lme4")

set.seed(12345)
sleepstudy$grp <- sample(1:5, size = 180, replace = TRUE)
sleepstudy$grp <- sample.int(5, size = 180, replace = TRUE)
model <- lme4::lmer(
Reaction ~ Days + (1 | grp) + (1 | Subject),
data = sleepstudy
Expand All @@ -80,3 +80,18 @@ test_that("`plot.see_parameters_model()` random parameters works", {
fig = plot(out)
)
})


test_that("`plot.see_parameters_model()` random parameters works", {
skip_if_not_installed("vdiffr")
skip_if_not_installed("mgcv")
skip_if_not_installed("parameters")

data(mtcars)
m <- mgcv::gam(mpg ~ s(wt) + cyl + gear + disp, data = mtcars)
result <- parameters::model_parameters(m)
vdiffr::expect_doppelganger(
title = "plot.model_parameters_gam",
fig = plot(result)
)
})

0 comments on commit 89174b6

Please sign in to comment.