Skip to content

Commit e6dca9d

Browse files
authored
Still mixed labelling with coviariance strucutres (#1215)
* Still mixed labelling with coviariance strucutres Fixes #1214 * don't show empty CI columns when `ci_random = FALSE` * address comments * remove duplicated code * add lme4 test * lintr * fix test
1 parent d373835 commit e6dca9d

7 files changed

Lines changed: 255 additions & 132 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: parameters
33
Title: Processing of Model Parameters
4-
Version: 0.28.3.16
4+
Version: 0.28.3.17
55
Authors@R:
66
c(person(given = "Daniel",
77
family = "Lüdecke",

R/extract_random_variances.R

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,11 @@
188188
# extract covariance structures. we now have the names of the covariance
189189
# structures ("us", "ar1", etc.) as character vector, and the related grouping
190190
# variables as names of its elements
191-
cov_struc <- vapply(varcorr, function(i) gsub("vcmat_", "", class(i)[1]), character(1))
191+
cov_struc <- vapply(
192+
varcorr,
193+
function(i) gsub("vcmat_", "", class(i)[1], fixed = TRUE),
194+
character(1)
195+
)
192196

193197
# extract parameters from SD and COR separately, for sorting
194198
re_sd_intercept <- re_data$var1 == "(Intercept)" &
@@ -244,8 +248,9 @@
244248
}
245249
)
246250
# fix label for cov_structure correlation
247-
re_data[which(re_cor_slope[to_select]), "var1"] <- new_label
248-
re_data[which(re_cor_slope[to_select]), "var2"] <- ""
251+
idx <- which(re_cor_slope & to_select)
252+
re_data[idx, "var1"] <- new_label
253+
re_data[idx, "var2"] <- ""
249254
}
250255
}
251256

@@ -345,6 +350,10 @@
345350
component,
346351
verbose = verbose
347352
)
353+
} else if (effects == "random") {
354+
# no CI requested and we have only random effects variances? then we
355+
# can safely remove the ci-columns CI_low and CI_high
356+
ci_cols <- NULL
348357
}
349358

350359
out <- out[c(

R/methods_lme4.R

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
############# .merMod -----------------
22

3-
43
#' @export
54
model_parameters.merMod <- model_parameters.coxme
65

76
# helper ----------------------------------------------------------------------
87

9-
.add_random_effects_lme4 <- function(model,
10-
params,
11-
ci,
12-
ci_method,
13-
ci_random,
14-
effects,
15-
group_level,
16-
verbose = TRUE,
17-
...) {
8+
.add_random_effects_lme4 <- function(
9+
model,
10+
params,
11+
ci,
12+
ci_method,
13+
ci_random,
14+
effects,
15+
group_level,
16+
verbose = TRUE,
17+
...
18+
) {
1819
params_random <- params_variance <- NULL
1920

2021
# only proceed if random effects are requested
@@ -51,18 +52,23 @@ model_parameters.merMod <- model_parameters.coxme
5152

5253

5354
#' @export
54-
ci.merMod <- function(x,
55-
ci = 0.95,
56-
dof = NULL,
57-
method = "wald",
58-
iterations = 500,
59-
...) {
55+
ci.merMod <- function(x, ci = 0.95, dof = NULL, method = "wald", iterations = 500, ...) {
6056
method <- tolower(method)
61-
method <- insight::validate_argument(method, c(
62-
"wald", "ml1", "betwithin", "kr",
63-
"satterthwaite", "kenward", "boot",
64-
"profile", "residual", "normal"
65-
))
57+
method <- insight::validate_argument(
58+
method,
59+
c(
60+
"wald",
61+
"ml1",
62+
"betwithin",
63+
"kr",
64+
"satterthwaite",
65+
"kenward",
66+
"boot",
67+
"profile",
68+
"residual",
69+
"normal"
70+
)
71+
)
6672

6773
# bootstrapping
6874
if (method == "boot") {
@@ -86,12 +92,14 @@ ci.merMod <- function(x,
8692

8793

8894
#' @export
89-
standard_error.merMod <- function(model,
90-
effects = "fixed",
91-
method = NULL,
92-
vcov = NULL,
93-
vcov_args = NULL,
94-
...) {
95+
standard_error.merMod <- function(
96+
model,
97+
effects = "fixed",
98+
method = NULL,
99+
vcov = NULL,
100+
vcov_args = NULL,
101+
...
102+
) {
95103
dots <- list(...)
96104
effects <- insight::validate_argument(effects, c("fixed", "random"))
97105

@@ -102,17 +110,16 @@ standard_error.merMod <- function(model,
102110

103111
if (is.null(method)) {
104112
method <- "wald"
105-
} else if ((method == "robust" && is.null(vcov)) ||
106-
# deprecated argument
107-
isTRUE(list(...)[["robust"]])) {
113+
} else if (
114+
(method == "robust" && is.null(vcov)) ||
115+
# deprecated argument
116+
isTRUE(list(...)[["robust"]])
117+
) {
108118
vcov <- "vcovHC"
109119
}
110120

111121
if (!is.null(vcov) || isTRUE(dots[["robust"]])) {
112-
fun_args <- list(model,
113-
vcov = vcov,
114-
vcov_args = vcov_args
115-
)
122+
fun_args <- list(model, vcov = vcov, vcov_args = vcov_args)
116123
fun_args <- c(fun_args, dots)
117124
out <- do.call("standard_error.default", fun_args)
118125
return(out)
@@ -121,12 +128,11 @@ standard_error.merMod <- function(model,
121128
# kenward approx
122129
if (method %in% c("kenward", "kr")) {
123130
out <- se_kenward(model)
124-
return(out)
125131
} else {
126132
# Classic and Satterthwaite SE
127133
out <- se_mixed_default(model)
128-
return(out)
129134
}
135+
out
130136
}
131137

132138

@@ -148,7 +154,7 @@ standard_error.merMod <- function(model,
148154
rand.se[[m]] <- array(NA, c(J, K))
149155

150156
for (j in 1:J) {
151-
rand.se[[m]][j, ] <- sqrt(diag(as.matrix(vars.m[, , j])))
157+
rand.se[[m]][j, ] <- sqrt(diag(as.matrix(vars.m[,, j])))
152158
}
153159
dimnames(rand.se[[m]]) <- list(names.full[[1]], names.full[[2]])
154160
}
@@ -157,7 +163,8 @@ standard_error.merMod <- function(model,
157163

158164

159165
se_mixed_default <- function(model) {
160-
params <- insight::find_parameters(model,
166+
params <- insight::find_parameters(
167+
model,
161168
effects = "fixed",
162169
component = "conditional",
163170
flatten = TRUE

R/methods_mjoint.R

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#' @export
2-
model_parameters.mjoint <- function(model,
3-
ci = 0.95,
4-
effects = "fixed",
5-
component = "all",
6-
exponentiate = FALSE,
7-
p_adjust = NULL,
8-
keep = NULL,
9-
drop = NULL,
10-
verbose = TRUE,
11-
...) {
2+
model_parameters.mjoint <- function(
3+
model,
4+
ci = 0.95,
5+
effects = "fixed",
6+
component = "all",
7+
exponentiate = FALSE,
8+
p_adjust = NULL,
9+
keep = NULL,
10+
drop = NULL,
11+
verbose = TRUE,
12+
...
13+
) {
1214
effects <- insight::validate_argument(effects, c("fixed", "random", "all"))
1315
component <- insight::validate_argument(component, c("all", "conditional", "survival"))
1416

@@ -45,7 +47,6 @@ model_parameters.mjoint <- function(model,
4547
params_variance$Component <- "conditional"
4648
}
4749

48-
4950
# merge random and fixed effects, if necessary
5051
if (!is.null(params) && !is.null(params_variance)) {
5152
params$Level <- NA
@@ -121,7 +122,11 @@ ci.mjoint <- function(x, ci = 0.95, ...) {
121122

122123

123124
#' @export
124-
standard_error.mjoint <- function(model, component = c("all", "conditional", "survival"), ...) {
125+
standard_error.mjoint <- function(
126+
model,
127+
component = c("all", "conditional", "survival"),
128+
...
129+
) {
125130
component <- match.arg(component)
126131
s <- summary(model)
127132

0 commit comments

Comments
 (0)