66# ' which can be useful to add the random effects to the original data.
77# '
88# ' @param model A mixed model with random effects.
9- # ' @param type `"random"` or `"total"`.
10- # ' - If `"random"` (default), the
11- # ' coefficients correspond to the conditional estimates of the random effects
12- # ' (as they are returned by `lme4::ranef()`). They typically correspond to the
13- # ' deviation of each individual group from their fixed effect (assuming the
14- # ' random effect is also included as a fixed effect). As such, a coefficient
15- # ' close to 0 means that the participants' effect is the same as the
16- # ' population-level effect (in other words, it is "in the norm").
9+ # ' @param type String, describing the type of estimates that should be returned.
10+ # ' Can be `"random"`, `"total"`, or `"marginal"` (experimental).
11+ # ' - If `"random"` (default), the coefficients correspond to the conditional
12+ # ' estimates of the random effects (as they are returned by `lme4::ranef()`).
13+ # ' They typically correspond to the deviation of each individual group from
14+ # ' their fixed effect (assuming the random effect is also included as a fixed
15+ # ' effect). As such, a coefficient close to 0 means that the participants'
16+ # ' effect is the same as the population-level effect (in other words, it is
17+ # ' "in the norm").
1718# ' - If `"total"`, it will return the sum of the random effect and its
1819# ' corresponding fixed effects, which internally relies on the `coef()` method
1920# ' (see `?coef.merMod`). Note that `type = "total"` yet does not return
2021# ' uncertainty indices (such as SE and CI) for models from *lme4* or
2122# ' *glmmTMB*, as the necessary information to compute them is not yet
2223# ' available. However, for Bayesian models, it is possible to compute them.
23- # ' - If `"marginal"` (experimental), it returns marginal group-levels estimates.
24- # ' The random intercepts are computed using marginal means (see [estimate_means()]),
25- # ' and the random slopes using marginal effects (see [estimate_slopes()]).
26- # ' This method does not directly extract the parameters estimated by the model,
27- # ' but recomputes them using model predictions. While this is more computationally
28- # ' intensive, one of the benefits include interpretability: the random intercepts
29- # ' correspond to the "mean" value of the outcome for each group, and the random slopes
30- # ' correspond to the direct "effect" of the predictor for each group.
24+ # ' - If `"marginal"` (experimental), it returns marginal group-levels
25+ # ' estimates. The random intercepts are computed using marginal means (see
26+ # ' [estimate_means()]), and the random slopes using marginal effects (see
27+ # ' [estimate_slopes()]). This method does not directly extract the parameters
28+ # ' estimated by the model, but recomputes them using model predictions. While
29+ # ' this is more computationally intensive, one of the benefits include
30+ # ' interpretability: the random intercepts correspond to the "mean" value of
31+ # ' the outcome for each group, and the random slopes correspond to the direct
32+ # ' "effect" of the predictor for each group.
3133# ' @param dispersion,test,diagnostic Arguments passed to
3234# ' [parameters::model_parameters()] for Bayesian models. By default, it won't
3335# ' return significance or diagnostic indices (as it is not typically very
@@ -335,6 +337,8 @@ estimate_grouplevel.stanreg <- function(model,
335337# cor.test(m1$Coefficient, m3$Coefficient) # r = 1
336338# cor.test(m2$Coefficient, m3$Coefficient) # r = 1
337339.grouplevel_marginal <- function (model ) {
340+ insight :: check_if_installed(" lme4" )
341+
338342 out <- list ()
339343
340344 # Analyze random effect structure
@@ -352,7 +356,9 @@ estimate_grouplevel.stanreg <- function(model,
352356 pred <- insight :: find_predictors(model , effects = " all" , flatten = TRUE )
353357 s <- s [s %in% pred ]
354358
355- if (length(s ) > 0 ) randomslopes [[g ]] <- s
359+ if (length(s ) > 0 ) {
360+ randomslopes [[g ]] <- s
361+ }
356362 }
357363
358364 # TODO: check if it fixes are needed for cases where random intercept is suppressed (e.g., (0 + x | g) )
0 commit comments