Skip to content

Commit a19b698

Browse files
committed
refactor
1 parent de489ca commit a19b698

File tree

4 files changed

+163
-216
lines changed

4 files changed

+163
-216
lines changed

R/extract_random_variances.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,10 @@ as.data.frame.VarCorr.lme <- function(x, row.names = NULL, optional = FALSE, ...
839839
insight::check_if_installed("cplm", reason = reason)
840840
}
841841

842+
if (inherits(model, "coxme")) {
843+
insight::check_if_installed("coxme", reason = reason)
844+
}
845+
842846
if (inherits(model, "rstanarm")) {
843847
insight::check_if_installed("rstanarm", reason = reason)
844848
}

R/methods_coxme.R

Lines changed: 157 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,65 @@
22
model_parameters.coxme <- function(model,
33
ci = 0.95,
44
ci_method = NULL,
5+
ci_random = NULL,
56
bootstrap = FALSE,
67
iterations = 1000,
78
standardize = NULL,
8-
effects = "fixed",
9+
effects = "all",
910
group_level = FALSE,
1011
exponentiate = FALSE,
1112
p_adjust = NULL,
1213
vcov = NULL,
1314
vcov_args = NULL,
14-
include_info = getOption("parameters_info", FALSE),
15+
wb_component = FALSE,
16+
include_info = getOption("parameters_mixed_info", FALSE),
17+
include_sigma = FALSE,
1518
keep = NULL,
1619
drop = NULL,
1720
verbose = TRUE,
1821
...) {
22+
insight::check_if_installed("lme4")
23+
dots <- list(...)
24+
25+
# set default
26+
if (is.null(ci_method)) {
27+
if (isTRUE(bootstrap)) {
28+
ci_method <- "quantile"
29+
} else {
30+
ci_method <- switch(insight::find_statistic(model),
31+
`t-statistic` = "residual",
32+
"wald"
33+
)
34+
}
35+
}
36+
37+
# p-values, CI and se might be based of wald, or KR
38+
ci_method <- tolower(ci_method)
39+
40+
if (isTRUE(bootstrap)) {
41+
ci_method <- insight::validate_argument(
42+
ci_method,
43+
c("hdi", "quantile", "ci", "eti", "si", "bci", "bcai")
44+
)
45+
} else {
46+
ci_method <- insight::validate_argument(
47+
ci_method,
48+
c(
49+
"wald", "normal", "residual", "ml1", "betwithin", "satterthwaite",
50+
"kenward", "kr", "boot", "profile", "uniroot"
51+
)
52+
)
53+
}
54+
1955
# which component to return?
2056
effects <- insight::validate_argument(
2157
effects,
22-
c("fixed", "total", "random_total", "all")
58+
c("fixed", "random", "grouplevel", "total", "random_total", "all")
2359
)
60+
params <- NULL
2461

25-
if (effects == "fixed") {
26-
return(model_parameters.default(
27-
model,
28-
ci = ci,
29-
ci_method = ci_method,
30-
bootstrap = bootstrap,
31-
iterations = iterations,
32-
standardize = standardize,
33-
exponentiate = exponentiate,
34-
p_adjust = p_adjust,
35-
vcov = vcov,
36-
vcov_args = vcov_args,
37-
include_info = include_info,
38-
keep = keep,
39-
drop = drop,
40-
verbose = verbose,
41-
...
42-
))
43-
}
62+
# group level estimates =================================================
63+
# =======================================================================
4464

4565
# for coef(), we don't need all the attributes and just stop here
4666
if (effects %in% c("total", "random_total")) {
@@ -50,10 +70,123 @@ model_parameters.coxme <- function(model,
5070
return(params)
5171
}
5272

53-
if (effects %in% c("random", "all") && isTRUE(group_level)) {
54-
params_random <- .extract_random_parameters(model, ci = ci, effects = effects)
73+
# group grouplevel estimates (BLUPs), handle alias
74+
if (effects == "grouplevel") {
75+
effects <- "random"
76+
group_level <- TRUE
77+
}
78+
79+
# post hoc standardize only works for fixed effects...
80+
if (!is.null(standardize) && standardize != "refit") {
81+
if (!missing(effects) && effects != "fixed" && verbose) {
82+
insight::format_alert(
83+
"Standardizing coefficients only works for fixed effects of the mixed model."
84+
)
85+
}
86+
effects <- "fixed"
87+
}
88+
89+
# for refit, we completely refit the model, than extract parameters,
90+
# ci etc. as usual - therefor, we set "standardize" to NULL
91+
if (!is.null(standardize) && standardize == "refit") {
92+
model <- datawizard::standardize(model, verbose = FALSE)
93+
standardize <- NULL
94+
}
95+
96+
# fixed effects =================================================
97+
# ===============================================================
98+
99+
if (effects %in% c("fixed", "all")) {
100+
# Processing
101+
if (bootstrap) {
102+
params <- bootstrap_parameters(
103+
model,
104+
iterations = iterations,
105+
ci = ci,
106+
...
107+
)
108+
if (effects != "fixed") {
109+
effects <- "fixed"
110+
if (verbose) {
111+
insight::format_alert("Bootstrapping only returns fixed effects of the mixed model.")
112+
}
113+
}
114+
} else {
115+
fun_args <- list(
116+
model,
117+
ci = ci,
118+
ci_method = ci_method,
119+
standardize = standardize,
120+
p_adjust = p_adjust,
121+
wb_component = wb_component,
122+
keep_parameters = keep,
123+
drop_parameters = drop,
124+
verbose = verbose,
125+
include_sigma = include_sigma,
126+
include_info = include_info,
127+
vcov = vcov,
128+
vcov_args = vcov_args
129+
)
130+
fun_args <- c(fun_args, dots)
131+
params <- do.call(".extract_parameters_mixed", fun_args)
132+
}
133+
134+
params$Effects <- "fixed"
135+
136+
# exponentiate coefficients and SE/CI, if requested
137+
params <- .exponentiate_parameters(params, model, exponentiate)
138+
}
139+
140+
att <- attributes(params)
141+
142+
# add random effects, either group level or re variances
143+
# ======================================================
144+
145+
params <- .add_random_effects_lme4(
146+
model,
147+
params,
148+
ci,
149+
ci_method,
150+
ci_random,
151+
effects,
152+
group_level,
153+
verbose
154+
)
155+
156+
# clean-up
157+
# ======================================================
158+
159+
# remove empty column
160+
if (!is.null(params$Level) && all(is.na(params$Level))) {
161+
params$Level <- NULL
162+
}
163+
164+
# due to rbind(), we lose attributes from "extract_parameters()",
165+
# so we add those attributes back here...
166+
if (!is.null(att)) {
167+
attributes(params) <- utils::modifyList(att, attributes(params))
55168
}
56169

170+
params <- .add_model_parameters_attributes(
171+
params,
172+
model,
173+
ci = ci,
174+
exponentiate,
175+
bootstrap,
176+
iterations,
177+
ci_method = ci_method,
178+
p_adjust = p_adjust,
179+
verbose = verbose,
180+
include_info = include_info,
181+
group_level = group_level,
182+
wb_component = wb_component,
183+
...
184+
)
185+
186+
187+
attr(params, "object_name") <- insight::safe_deparse_symbol(substitute(model))
188+
class(params) <- c("parameters_model", "see_parameters_model", class(params))
189+
57190
params
58191
}
59192

0 commit comments

Comments
 (0)