|
8 | 8 | params_cond <- params$cond |
9 | 9 | params_zi <- params$zi |
10 | 10 |
|
| 11 | + # handle random effects in conditional component |
11 | 12 | if (!is.null(params_cond)) { |
| 13 | + # extract levels of group factors |
12 | 14 | group_levels <- insight::compact_list(lapply( |
13 | 15 | x$modelInfo$reTrms$cond$flist, |
14 | 16 | levels |
15 | 17 | )) |
| 18 | + # extract names of slopes |
| 19 | + slope_names <- insight::compact_list(x$modelInfo$reTrms$cond$cnms) |
| 20 | + # reshape "coef()" data |
16 | 21 | params_cond <- .reshape_group_level_coefficients( |
17 | 22 | x, |
18 | | - params_cond, |
19 | | - group_levels = group_levels |
| 23 | + params = params_cond, |
| 24 | + group_levels = group_levels, |
| 25 | + slope_names = slope_names |
20 | 26 | ) |
21 | 27 | params_cond$Component = "conditional" |
22 | 28 | } |
| 29 | + |
| 30 | + # handle random effects in zero-inflation component |
23 | 31 | if (!is.null(params_zi)) { |
| 32 | + # extract levels of group factors |
24 | 33 | group_levels <- insight::compact_list(lapply( |
25 | 34 | x$modelInfo$reTrms$zi$flist, |
26 | 35 | levels |
27 | 36 | )) |
| 37 | + # extract names of slopes |
| 38 | + slope_names <- insight::compact_list(x$modelInfo$reTrms$zi$cnms) |
| 39 | + # reshape "coef()" data |
28 | 40 | params_zi <- .reshape_group_level_coefficients( |
29 | 41 | x, |
30 | | - params_zi, |
| 42 | + params = params_zi, |
31 | 43 | group_levels = group_levels, |
| 44 | + slope_names = slope_names, |
32 | 45 | component = "zero_inflated_random" |
33 | 46 | ) |
34 | 47 | params_zi$Component = "zero_inflated" |
35 | 48 | } |
36 | 49 |
|
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 |
38 | 64 | } |
39 | 65 |
|
40 | 66 |
|
41 | 67 | # helper ---------------------------------------------------------------------- |
42 | 68 |
|
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 | +) { |
47 | 76 | group_factors <- insight::find_random(x) |
48 | 77 | random_slopes <- insight::find_random_slopes(x) |
49 | 78 |
|
|
52 | 81 |
|
53 | 82 | # iterate all random effects, add group name and levels |
54 | 83 | 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 |
55 | 91 | params[[i]] <- params[[i]][cols] |
| 92 | + # add information about group factor and levels |
56 | 93 | params[[i]]$Group <- i |
57 | 94 | params[[i]]$Level <- group_levels[[i]] |
58 | 95 | } |
59 | 96 |
|
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), ] |
62 | 121 | } |
0 commit comments