|
85 | 85 |
|
86 | 86 | .group_level_total.brmsfit <- function(x, ...) { |
87 | 87 | # extract random effects information |
88 | | - group_factors <- insight::find_random(x) |
| 88 | + group_factors <- insight::find_random(x, split_nested = TRUE, flatten = TRUE) |
89 | 89 | random_slopes <- insight::find_random_slopes(x) |
| 90 | + params <- NULL |
90 | 91 |
|
91 | | - for (i in group_factors) { |
| 92 | + # create full data frame of all random effects retrieved from coef() |
| 93 | + params <- do.call(rbind, lapply(group_factors, function(i) { |
| 94 | + # we want the posterior distribution from coef(), so we can |
| 95 | + # use bayestestR |
92 | 96 | ranef <- stats::coef(x, summary = FALSE)[[i]] |
93 | 97 | parameter_names <- dimnames(ranef)[[3]] |
94 | 98 | out <- lapply( |
95 | 99 | parameter_names, |
96 | | - function(pn) bayestestR::describe_posterior(as.data.frame(x[, , pn]), ...) |
| 100 | + function(pn) { |
| 101 | + # summary of posterior |
| 102 | + d <- bayestestR::describe_posterior(as.data.frame(ranef[, , pn]), verbose = FALSE, ...) |
| 103 | + # add information about group factor and levels |
| 104 | + d$Group <- i |
| 105 | + # Parameters in the returned data frame are actually the levels |
| 106 | + # # from the group factors |
| 107 | + d$Level <- d$Parameter |
| 108 | + # the parameter names can be taken from dimnames |
| 109 | + d$Parameter <- pn |
| 110 | + d |
| 111 | + } |
97 | 112 | ) |
98 | 113 | names(out) <- parameter_names |
99 | | - } |
| 114 | + do.call(rbind, out) |
| 115 | + })) |
| 116 | + |
| 117 | + # select parameters to keep. We want all intercepts, and all random slopes |
| 118 | + # from conditional and potential zero-inflation component |
| 119 | + parameters_to_keep <- params$Parameter %in% c("Intercept", random_slopes$random) |
| 120 | + parameters_to_keep <- parameters_to_keep | params$Parameter %in% c("zi_Intercept", random_slopes$zero_inflated_random) |
| 121 | + # furthermore, categorical random slopes have levels in their name, so we |
| 122 | + # try to find those parameters here, too |
| 123 | + parameters_to_keep <- parameters_to_keep | startsWith(params$Parameter, random_slopes$random) |
| 124 | + parameters_to_keep <- parameters_to_keep | startsWith(params$Parameter, random_slopes$zero_inflated_random) |
| 125 | + |
| 126 | + # clean names |
| 127 | + params$Parameter <- gsub("^zi_", "", params$Parameter) |
| 128 | + |
| 129 | + params |
100 | 130 | } |
101 | 131 |
|
102 | 132 |
|
|
0 commit comments