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 get_parameters/find_parameters for glmmTMB 1.1.10 #934

Merged
merged 3 commits into from
Sep 27, 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: 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 @@ -774,7 +773,7 @@
# expect_warning(expect_equal(get_variance_fixed(m1), c(var.fixed = 1.09712435712435052437), tolerance = 1e-3))
# expect_warning(expect_equal(get_variance_random(m1), c(var.random = 0.86712737445492238386), tolerance = 1e-3))
# expect_warning(expect_equal(get_variance_residual(m1), c(var.residual = 0.02634500773355940087 ), tolerance = 1e-3))
# expect_warning(expect_equal(get_variance_distribution(m1), c(var.distribution = 0.02634500773355940087 ), tolerance = 1e-3))

Check warning on line 776 in tests/testthat/test-glmmTMB.R

View workflow job for this annotation

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

file=tests/testthat/test-glmmTMB.R,line=776,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 128 characters.
# expect_warning(expect_equal(get_variance_dispersion(m1), c(var.dispersion = 0), tolerance = 1e-3))
# })

Expand Down Expand Up @@ -975,7 +974,6 @@


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 @@
# 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
Loading