Skip to content

Commit 4fd4a4a

Browse files
committed
fix
1 parent f969ad8 commit 4fd4a4a

File tree

1 file changed

+69
-10
lines changed

1 file changed

+69
-10
lines changed

R/group_level_total.R

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,71 @@
88
params_cond <- params$cond
99
params_zi <- params$zi
1010

11+
# handle random effects in conditional component
1112
if (!is.null(params_cond)) {
13+
# extract levels of group factors
1214
group_levels <- insight::compact_list(lapply(
1315
x$modelInfo$reTrms$cond$flist,
1416
levels
1517
))
18+
# extract names of slopes
19+
slope_names <- insight::compact_list(x$modelInfo$reTrms$cond$cnms)
20+
# reshape "coef()" data
1621
params_cond <- .reshape_group_level_coefficients(
1722
x,
18-
params_cond,
19-
group_levels = group_levels
23+
params = params_cond,
24+
group_levels = group_levels,
25+
slope_names = slope_names
2026
)
2127
params_cond$Component = "conditional"
2228
}
29+
30+
# handle random effects in zero-inflation component
2331
if (!is.null(params_zi)) {
32+
# extract levels of group factors
2433
group_levels <- insight::compact_list(lapply(
2534
x$modelInfo$reTrms$zi$flist,
2635
levels
2736
))
37+
# extract names of slopes
38+
slope_names <- insight::compact_list(x$modelInfo$reTrms$zi$cnms)
39+
# reshape "coef()" data
2840
params_zi <- .reshape_group_level_coefficients(
2941
x,
30-
params_zi,
42+
params = params_zi,
3143
group_levels = group_levels,
44+
slope_names = slope_names,
3245
component = "zero_inflated_random"
3346
)
3447
params_zi$Component = "zero_inflated"
3548
}
3649

37-
rbind(params_cond, params_zi)
50+
# create list of data frames
51+
out <- insight::compact_list(list(params_cond, params_zi))
52+
53+
if (length(out) == 1) {
54+
# unlist if only one component
55+
out <- out[[1]]
56+
} else {
57+
# else, join - we can't use rbind() here, because column
58+
# names do not necessarily match
59+
out <- datawizard::data_join(out, join = "bind")
60+
}
61+
62+
rownames(out) <- NULL
63+
out
3864
}
3965

4066

4167
# helper ----------------------------------------------------------------------
4268

43-
.reshape_group_level_coefficients <- function(x,
44-
params,
45-
group_levels,
46-
component = "random") {
69+
.reshape_group_level_coefficients <- function(
70+
x,
71+
params,
72+
group_levels,
73+
slope_names = NULL,
74+
component = "random"
75+
) {
4776
group_factors <- insight::find_random(x)
4877
random_slopes <- insight::find_random_slopes(x)
4978

@@ -52,11 +81,41 @@
5281

5382
# iterate all random effects, add group name and levels
5483
for (i in group_factors[[component]]) {
84+
# overwrite cols? if random slopes are factors, the names are
85+
# not the variable names, but name + factor level, so we need
86+
# to upate the columns to select here
87+
if (!is.null(slope_names) && length(slope_names)) {
88+
cols <- slope_names[[i]]
89+
}
90+
# select columns
5591
params[[i]] <- params[[i]][cols]
92+
# add information about group factor and levels
5693
params[[i]]$Group <- i
5794
params[[i]]$Level <- group_levels[[i]]
5895
}
5996

60-
out <- do.call(rbind, params)
61-
datawizard::reshape_longer(out, select = seq_along(cols))
97+
# if only one component, unlist
98+
if (length(params) == 1) {
99+
out <- params[[1]]
100+
} else {
101+
# else, join - we can't use rbind() here, because column
102+
# names do not necessarily match
103+
class(params) <- "list"
104+
out <- datawizard::data_join(params, join = "bind")
105+
}
106+
107+
# make sure first columns are the one to reshape
108+
out <- datawizard::data_relocate(out, c("Group", "Level"), after = -1)
109+
110+
# reshape
111+
to_reshape <- seq_len(ncol(out) - 2)
112+
out <- datawizard::reshape_longer(out, select = to_reshape)
113+
114+
# rename
115+
out <- datawizard::data_rename(
116+
out,
117+
select = c(Parameter = "name", Coefficient = "value")
118+
)
119+
# remove those without valid values
120+
out[stats::complete.cases(out), ]
62121
}

0 commit comments

Comments
 (0)