1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
.group_level_total <- function(x, ...) {
UseMethod(".group_level_total")
}
.group_level_total.glmmTMB <- function(x, ...) {
params <- suppressWarnings(insight::compact_list(stats::coef(x)))
params_cond <- params$cond
params_zi <- params$zi
# handle random effects in conditional component
if (!is.null(params_cond)) {
# extract levels of group factors
group_levels <- insight::compact_list(lapply(
x$modelInfo$reTrms$cond$flist,
levels
))
# extract names of slopes
slope_names <- insight::compact_list(x$modelInfo$reTrms$cond$cnms)
# reshape "coef()" data
params_cond <- .reshape_group_level_coefficients(
x,
params = params_cond,
group_levels = group_levels,
slope_names = slope_names
)
params_cond$Component <- "conditional"
}
# handle random effects in zero-inflation component
if (!is.null(params_zi)) {
# extract levels of group factors
group_levels <- insight::compact_list(lapply(
x$modelInfo$reTrms$zi$flist,
levels
))
# extract names of slopes
slope_names <- insight::compact_list(x$modelInfo$reTrms$zi$cnms)
# reshape "coef()" data
params_zi <- .reshape_group_level_coefficients(
x,
params = params_zi,
group_levels = group_levels,
slope_names = slope_names,
component = "zero_inflated_random"
)
params_zi$Component <- "zero_inflated"
}
# create list of data frames
out <- insight::compact_list(list(params_cond, params_zi))
if (length(out) == 1) {
# unlist if only one component
out <- out[[1]]
} else {
# else, join - we can't use rbind() here, because column
# names do not necessarily match
out <- datawizard::data_join(out, join = "bind")
}
rownames(out) <- NULL
out
}
.group_level_total.merMod <- function(x, ...) {
params <- suppressWarnings(stats::coef(x))
# extract levels of group factors
group_levels <- insight::compact_list(lapply(methods::slot(x, "flist"), levels))
# extract names of slopes
slope_names <- insight::compact_list(methods::slot(x, "cnms"))
# reshape "coef()" data
params <- .reshape_group_level_coefficients(
x,
params = params,
group_levels = group_levels,
slope_names = slope_names
)
params
}
.group_level_total.stanreg <- function(x, ...) {
params <- suppressWarnings(stats::coef(x))
# extract levels of group factors
group_levels <- insight::compact_list(lapply(x$glmod$reTrms$flist, levels))
# extract names of slopes
slope_names <- insight::compact_list(x$glmod$reTrms$cnms)
# reshape "coef()" data
params <- .reshape_group_level_coefficients(
x,
params = params,
group_levels = group_levels,
slope_names = slope_names
)
params
}
.group_level_total.brmsfit <- function(x, ...) {
# extract random effects information
group_factors <- insight::find_random(x, split_nested = TRUE, flatten = TRUE)
random_slopes <- insight::find_random_slopes(x)
params <- NULL
# create full data frame of all random effects retrieved from coef()
params <- do.call(
rbind,
lapply(group_factors, function(i) {
# we want the posterior distribution from coef(), so we can
# use bayestestR
ranef <- stats::coef(x, summary = FALSE)[[i]]
parameter_names <- dimnames(ranef)[[3]]
out <- lapply(
parameter_names,
function(pn) {
# summary of posterior
d <- bayestestR::describe_posterior(as.data.frame(ranef[, , pn]), verbose = FALSE, ...)
# add information about group factor and levels
d$Group <- i
# Parameters in the returned data frame are actually the levels
# # from the group factors
d$Level <- d$Parameter
# the parameter names can be taken from dimnames
d$Parameter <- pn
d
}
)
names(out) <- parameter_names
do.call(rbind, out)
})
)
# select parameters to keep. We want all intercepts, and all random slopes
components <- c(
"sigma", "mu", "nu", "shape", "beta", "phi", "hu", "ndt", "zoi",
"coi", "kappa", "bias", "bs", "zi", "alpha", "xi"
)
# standard components
parameters_to_keep <- params$Parameter %in% c("Intercept", random_slopes$random)
parameters_to_keep <- parameters_to_keep |
params$Parameter %in% c("zi_Intercept", random_slopes$zero_inflated_random)
# auxiliary components
for (comp in components) {
parameters_to_keep <- parameters_to_keep |
params$Parameter %in% c(paste0(comp, "_Intercept"), random_slopes[[paste0(comp, "_random")]])
}
# furthermore, categorical random slopes have levels in their name, so we
# try to find those parameters here, too
if (!is.null(random_slopes$random)) {
parameters_to_keep <- parameters_to_keep |
startsWith(params$Parameter, random_slopes$random)
}
if (!is.null(random_slopes$zero_inflated_random)) {
parameters_to_keep <- parameters_to_keep |
startsWith(params$Parameter, paste0("zi_", random_slopes$zero_inflated_random))
}
# auxiliary components
for (comp in components) {
rc <- paste0(comp, "_random")
if (!is.null(random_slopes[[rc]])) {
parameters_to_keep <- parameters_to_keep |
startsWith(params$Parameter, paste0(comp, "_", random_slopes[[rc]]))
}
}
# add Component column
params$Component <- "conditional"
params$Component[startsWith(params$Parameter, "zi_")] <- "zero_inflated"
for (comp in components) {
params$Component[startsWith(params$Parameter, paste0(comp, "_"))] <- comp
}
# clean names
params$Parameter <- gsub("^zi_", "", params$Parameter)
for (comp in components) {
params$Parameter <- gsub(paste0("^", comp, "_"), "", params$Parameter)
}
rownames(params) <- NULL
# make sure first columns are group and level
datawizard::data_relocate(params[parameters_to_keep, ], c("Group", "Level"))
}
# helper ----------------------------------------------------------------------
.reshape_group_level_coefficients <- function(x,
params,
group_levels,
slope_names = NULL,
component = "random") {
group_factors <- insight::find_random(x)
random_slopes <- insight::find_random_slopes(x)
# find all columns for which we can add fixed and random effects
cols <- c(random_slopes[[component]], "(Intercept)")
# iterate all random effects, add group name and levels
for (i in group_factors[[component]]) {
# overwrite cols? if random slopes are factors, the names are
# not the variable names, but name + factor level, so we need
# to upate the columns to select here
if (!is.null(slope_names) && length(slope_names)) {
cols <- slope_names[[i]]
}
# select columns
params[[i]] <- params[[i]][cols]
# add information about group factor and levels
params[[i]]$Group <- i
params[[i]]$Level <- group_levels[[i]]
}
# if only one component, unlist
if (length(params) == 1) {
out <- params[[1]]
} else {
# else, join - we can't use rbind() here, because column
# names do not necessarily match
class(params) <- "list"
out <- datawizard::data_join(params, join = "bind")
}
# reshape
to_reshape <- setdiff(colnames(out), c("Group", "Level"))
out <- datawizard::reshape_longer(out, select = to_reshape)
# rename
out <- datawizard::data_rename(
out,
select = c(Parameter = "name", Coefficient = "value")
)
# make sure first columns are group and level
out <- datawizard::data_relocate(out, c("Group", "Level"))
# remove those without valid values
out[stats::complete.cases(out), ]
}
|