Skip to content

Add support for group-level estimates from coxme #1087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions R/extract_random_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,50 @@
.extract_random_parameters.MixMod <- function(model, ...) {
NULL
}


.extract_random_parameters.coxme <- function(model, ci = NULL, effects = "random", ...) {
insight::check_if_installed("lme4")

# extract random effects
re_grp <- insight::find_random(model, split_nested = TRUE, flatten = TRUE)

## TODO: this currently only works for one grouping variable
if (length(re_grp) > 1) {
re_grp <- re_grp[1]
}

out <- as.data.frame(lme4::ranef(model, condVar = TRUE), stringsAsFactors = FALSE)
out$grp <- unique(insight::get_data(model)[[re_grp]])
out <- datawizard::reshape_longer(out, select = -"grp")
out <- datawizard::data_separate(out, select = "name", new_columns = c("grpvar", "term"))
out <- datawizard::data_arrange(out, select = c("grpvar", "term"))
out <- out[c("grpvar", "term", "grp", "value")]
out$condsd <- NA

colnames(out) <- c("Group", "Parameter", "Level", "Coefficient", "SE")

# coerce to character
out$Parameter <- as.character(out$Parameter)
out$Level <- as.character(out$Level)
out$Group <- as.character(out$Group)
out$Effects <- "random"

out$CI_low <- out$CI_high <- NA
ci_cols <- c("CI_low", "CI_high")

stat_column <- gsub("-statistic", "", insight::find_statistic(model), fixed = TRUE)

# to match rbind
out[[stat_column]] <- NA
out$df_error <- NA
out$p <- NA

out <- out[c("Parameter", "Level", "Coefficient", "SE", ci_cols, stat_column, "df_error", "p", "Effects", "Group")]

if (effects == "random") {
out[c(stat_column, "df_error", "p")] <- NULL
}

out
}
60 changes: 60 additions & 0 deletions R/methods_coxme.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,63 @@
#' @export
model_parameters.merMod <- function(model,
ci = 0.95,
ci_method = NULL,
bootstrap = FALSE,
iterations = 1000,
standardize = NULL,
effects = "fixed",
group_level = FALSE,
exponentiate = FALSE,
p_adjust = NULL,
vcov = NULL,
vcov_args = NULL,
include_info = getOption("parameters_info", FALSE),
keep = NULL,
drop = NULL,
verbose = TRUE,
...) {
# which component to return?
effects <- insight::validate_argument(
effects,
c("fixed", "total", "random_total", "all")
)

if (effects == "fixed") {
return(model_parameters.default(
model,
ci = ci,
ci_method = ci_method,
bootstrap = bootstrap,
iterations = iterations,
standardize = standardize,
exponentiate = exponentiate,
p_adjust = p_adjust,
vcov = vcov,
vcov_args = vcov_args,
include_info = include_info,
keep = keep,
drop = drop,
verbose = verbose,
...
))
}

# for coef(), we don't need all the attributes and just stop here
if (effects %in% c("total", "random_total")) {
params <- .group_level_total(model)
params$Effects <- "total"
class(params) <- c("parameters_coef", "see_parameters_coef", class(params))
return(params)
}

if (effects %in% c("random", "all") && isTRUE(group_level)) {
params_random <- .extract_random_parameters(model, ci = ci, effects = effects)
}

params
}


#' @export
standard_error.coxme <- function(model, ...) {
beta_coef <- model$coefficients
Expand Down
Loading