Skip to content

Commit

Permalink
Fix get_parameters/find_parameters for glmmTMB 1.1.10 (#934)
Browse files Browse the repository at this point in the history
* Fix get_parameters/find_parameters for glmmTMB 1.1.10

* find_params

* test on R > 4.3
  • Loading branch information
strengejacke authored Sep 27, 2024
1 parent fca4b12 commit 27dc704
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 16 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: insight
Title: Easy Access to Model Information for Various Model Objects
Version: 0.20.4.5
Version: 0.20.4.6
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
5 changes: 4 additions & 1 deletion R/find_parameters_mixed.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#' - `zero_inflated_random`, the "random effects" part from the zero-inflation
#' component of the model.
#' - `dispersion`, the dispersion parameters (auxiliary parameter)
#' - `dispersion_random`, the "random effects" part from the dispersion
#' parameters (auxiliary parameter)
#' - `nonlinear`, the parameters from the nonlinear formula.
#'
#' @examples
Expand Down Expand Up @@ -66,7 +68,8 @@ find_parameters.glmmTMB <- function(x,
random = lapply(lme4::ranef(x)$cond, colnames),
zero_inflated = names(lme4::fixef(x)$zi),
zero_inflated_random = lapply(lme4::ranef(x)$zi, colnames),
dispersion = names(lme4::fixef(x)$disp)
dispersion = names(lme4::fixef(x)$disp),
dispersion_random = names(lme4::ranef(x)$disp)
))
}

Expand Down
12 changes: 9 additions & 3 deletions R/get_parameters_mixed.R
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@ get_parameters.glmmTMB <- function(x,
random = lme4::ranef(x)$cond,
zero_inflated = lme4::fixef(x)$zi,
zero_inflated_random = lme4::ranef(x)$zi,
dispersion = lme4::fixef(x)$disp
dispersion = lme4::fixef(x)$disp,
dispersion_random = lme4::ranef(x)$disp
))
}

Expand Down Expand Up @@ -517,10 +518,15 @@ get_parameters.glmmTMB <- function(x,
text_remove_backticks(out)
} else if (effects == "random") {
switch(component,
all = compact_list(list(random = l$random, zero_inflated_random = l$zero_inflated_random)),
all = compact_list(list(
random = l$random,
zero_inflated_random = l$zero_inflated_random,
dispersion_random = l$dispersion_random
)),
conditional = l$random,
zi = ,
zero_inflated = l$zero_inflated_random
zero_inflated = l$zero_inflated_random,
dispersion_random = l$dispersion_random
)
}
}
Expand Down
16 changes: 8 additions & 8 deletions R/helper_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,12 @@
c(
"conditional", "conditional1", "conditional2", "conditional3", "precision",
"nonlinear", "random", "zi", "zero_inflated", "zero_inflated_random", "shape",
"dispersion", "instruments", "interactions", "simplex", "smooth_terms",
"sigma", "nu", "tau", "correlation", "slopes", "cluster", "extra", "scale",
"marginal", "alpha", "beta", "survival", "infrequent_purchase", "auxiliary",
"mix", "shiftprop", "phi", "ndt", "hu", "xi", "coi", "zoi", "aux", "dist",
"selection", "outcome", "time_dummies", "sigma_random", "beta_random", "car",
"nominal"
"dispersion", "dispersion_random", "instruments", "interactions", "simplex",
"smooth_terms", "sigma", "nu", "tau", "correlation", "slopes", "cluster",
"extra", "scale", "marginal", "alpha", "beta", "survival", "infrequent_purchase",
"auxiliary", "mix", "shiftprop", "phi", "ndt", "hu", "xi", "coi", "zoi",
"aux", "dist", "selection", "outcome", "time_dummies", "sigma_random",
"beta_random", "car", "nominal"
)
}

Expand All @@ -240,7 +240,7 @@
"aux", "dist",

# random parameters
"sigma_random", "beta_random"
"dispersion_random", "sigma_random", "beta_random"
)
}

Expand All @@ -255,7 +255,7 @@
auxiliary_parameters <- .aux_elements()

# random parameters
random_parameters <- c("random", "zero_inflated_random", "sigma_random", "beta_random", "car")
random_parameters <- c("random", "zero_inflated_random", "dispersion_random", "sigma_random", "beta_random", "car")

# conditional component
conditional_component <- setdiff(
Expand Down
2 changes: 2 additions & 0 deletions man/find_parameters.glmmTMB.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 69 additions & 2 deletions tests/testthat/test-glmmTMB.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
skip_on_os("mac") # error: FreeADFunObject
skip_if_not(getRversion() >= "4.0.0")
skip_if(getRversion() > "4.3.3")
skip_if_not_installed("TMB")
skip_if_not_installed("glmmTMB")

Expand Down Expand Up @@ -975,7 +974,6 @@ test_that("model_info, ordered beta", {


test_that("model_info, recognize ZI even without ziformula", {
skip_if_not_installed("glmmTMB")
data("fish", package = "insight")
fish$count <- fish$count + 1
m1 <- glmmTMB::glmmTMB(
Expand Down Expand Up @@ -1010,3 +1008,72 @@ test_that("model_info, recognize ZI even without ziformula", {
# expect_equal(out$var.distribution, 1.44250604187634, tolerance = 1e-4)
# })
# )

test_that("get/find_parameters with dispersion-random", {
skip_if_not_installed("glmmTMB", minimum_version = "1.1.10")
data(Salamanders, package = "glmmTMB")
m <- glmmTMB::glmmTMB(
count ~ spp + cover + mined + (1 | site),
ziformula = ~ spp + mined,
dispformula = ~ DOY + (1 | site),
data = Salamanders,
family = glmmTMB::nbinom2
)
out <- get_parameters(m)
expect_identical(nrow(out), 19L)
out <- get_parameters(m, effects = "random")
expect_length(out, 2)
expect_named(out, c("random", "dispersion_random"))

expect_equal(
find_parameters(m),
list(
conditional = c(
"(Intercept)", "sppPR", "sppDM", "sppEC-A",
"sppEC-L", "sppDES-L", "sppDF", "cover", "minedno"
),
random = list(site = "(Intercept)"),
zero_inflated = c(
"(Intercept)", "sppPR",
"sppDM", "sppEC-A", "sppEC-L", "sppDES-L", "sppDF", "minedno"
),
dispersion = c("(Intercept)", "DOY"),
dispersion_random = "site"
),
ignore_attr = TRUE
)
expect_equal(
find_parameters(m, effects = "fixed"),
list(
conditional = c(
"(Intercept)", "sppPR", "sppDM", "sppEC-A",
"sppEC-L", "sppDES-L", "sppDF", "cover", "minedno"
),
zero_inflated = c(
"(Intercept)", "sppPR",
"sppDM", "sppEC-A", "sppEC-L", "sppDES-L", "sppDF", "minedno"
),
dispersion = c("(Intercept)", "DOY")
),
ignore_attr = TRUE
)
expect_equal(
find_parameters(m, effects = "random"),
list(
random = list(site = "(Intercept)"),
dispersion_random = "site"
),
ignore_attr = TRUE
)
expect_equal(
find_parameters(m, component = "conditional"),
list(
conditional = c(
"(Intercept)", "sppPR", "sppDM", "sppEC-A",
"sppEC-L", "sppDES-L", "sppDF", "cover", "minedno"
),
random = list(site = "(Intercept)")
),
ignore_attr = TRUE
)
})
1 change: 0 additions & 1 deletion tests/testthat/test-null_model.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
skip_if(getRversion() > "4.3.3")
skip_if_not_installed("glmmTMB")
skip_if_not_installed("lme4")
skip_if_not_installed("TMB")
Expand Down

0 comments on commit 27dc704

Please sign in to comment.