Skip to content
Open
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

## Bug fixes

* `estimate_grouplevel(type = "total")` now excludes brms coefficients without
a random effect in the corresponding group and model component.
Comment on lines +14 to +15

* `estimate_means()`, `estimate_contrasts()` and `estimate_slopes()` with
`backend = "emmeans"` now return the probabilities of each response category
for `nnet::multinom()` models. Before, the results were wrong: emmeans
Expand Down
19 changes: 17 additions & 2 deletions R/estimate_grouplevel.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
#' vs. *absolute* coefficient values.
#' - If `"total"`, it will return the sum of the random effect and its
#' corresponding fixed effects, which internally relies on the `coef()` method
#' (see `?coef.merMod`). Note that `type = "total"` yet does not return
#' uncertainty indices (such as SE and CI) for models from *lme4* or
#' (see `?coef.merMod`). The result includes only coefficients that have a
#' random effect in their group and model component. `type = "total"` does not
#' return uncertainty indices (such as SE and CI) for models from *lme4* or
#' *glmmTMB*, as the necessary information to compute them is not yet
#' available. However, for Bayesian models, it is possible to compute them.
#' - If `"marginal"` (experimental), it returns marginal group-level
Expand Down Expand Up @@ -230,6 +231,20 @@ estimate_grouplevel.brmsfit <- function(
...
)

if (type == "total") {
# brms::coef() repeats fixed-only coefficients for each grouping factor.
random_terms <- lapply(brms::ranef(model, summary = FALSE), function(x) dimnames(x)[[3L]])
component <- ifelse(params$Component == "zero_inflated", "zi", params$Component)
model_terms <- ifelse(component == "conditional", params$Parameter, paste0(component, "_", params$Parameter))
group_rows <- split(seq_len(nrow(params)), params$Group)
keep <- rep(FALSE, nrow(params))
for (group in names(group_rows)) {
rows <- group_rows[[group]]
keep[rows] <- model_terms[rows] %in% random_terms[[group]]
}
params <- params[keep, , drop = FALSE]
}

# get cleaned parameter names with additional information
clean_parameters <- attributes(params)$clean_parameters

Expand Down
5 changes: 3 additions & 2 deletions man/estimate_grouplevel.Rd

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

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading