Nothing
# GEE parameter tables ----
#' @param correlation_rows for generalized estimating equations
#' (`geepack::geeglm`), whether the estimated parameters of the working
#' correlation are shown below the coefficients. If `NULL` (default), they
#' are shown when there is a single parameter (\eqn{\alpha}{alpha} of exchangeable and
#' AR(1) structures), and not for unstructured correlations, which have one
#' for each pair of occasions.
#' @details
#' For generalized estimating equations fit with `geepack::geeglm()`,
#' coefficients are shown as for `glm` models, with exponentiated
#' coefficients for logit and log links, and tested with Wald *z* tests
#' (*z*^2^ is geepack's Wald statistic). Standard errors are geepack's
#' robust (sandwich) estimates, or its jackknife estimates if the model was
#' fit with them (`std.err`). The estimated parameters of the working
#' correlation are shown below the coefficients, with their standard errors
#' but no tests (`correlation_rows`; when they are not shown, a single
#' \eqn{\alpha}{alpha} is given in the note). [apa_note()] names the link, the
#' working correlation, the kind of standard errors, and the numbers of
#' observations and clusters. Lists of models are compared with Wald tests
#' of the terms added to the previous model (from geepack's `anova()`) and
#' by QIC, because GEE models have no likelihood.
#' @references
#' Halekoh, U., Højsgaard, S., & Yan, J. (2006). The R package geepack for
#' generalized estimating equations. *Journal of Statistical Software,
#' 15*(2). \doi{10.18637/jss.v015.i02}
#'
#' Liang, K.-Y., & Zeger, S. L. (1986). Longitudinal data analysis using
#' generalized linear models. *Biometrika, 73*(1), 13--22.
#' \doi{10.1093/biomet/73.1.13}
#'
#' Pan, W. (2001). Akaike's information criterion in generalized estimating
#' equations. *Biometrics, 57*(1), 120--125.
#' \doi{10.1111/j.0006-341X.2001.00120.x}
#' @export
#' @rdname apa_parameters
#' @examples
#' # Generalized estimating equations
#' \donttest{
#' if (requireNamespace("geepack", quietly = TRUE)) {
#' data(ohio, package = "geepack")
#' fit_gee <- geepack::geeglm(
#' resp ~ age + smoke,
#' id = id,
#' data = ohio,
#' family = binomial,
#' corstr = "exchangeable"
#' )
#' apa_parameters(fit_gee)
#' }
#' }
apa_parameters.geeglm <- function(
fit,
predictor_parameters = c(
"Parameter",
"Coefficient",
"SE",
"Exp_Coefficient",
"z",
"p"
),
starred = NA,
bolded = NA,
column_formats = NULL,
t_with_df = TRUE,
exponentiate = NULL,
ci = .95,
correlation_rows = NULL,
...
) {
rlang::check_installed("geepack", reason = "for tables of generalized estimating equations.")
if (is.null(column_formats)) {
column_formats <- the$column_formats
}
names(predictor_parameters) <- NULL
link <- stats::family(fit)$link
if (is.null(exponentiate)) {
exponentiate <- link %in% c("logit", "log")
}
s <- summary(fit)
co <- s$coefficients
half <- stats::qnorm(1 - (1 - ci) / 2) * co[, "Std.err"]
d_b <- tibble::tibble(
Parameter = column_formats$Parameter@formatter(
pretty_parameter_names(fit, rownames(co))
),
Coefficient = co[, "Estimate"],
SE = co[, "Std.err"],
CI = ci,
CI_low = co[, "Estimate"] - half,
CI_high = co[, "Estimate"] + half,
z = co[, "Estimate"] / co[, "Std.err"],
p = co[, "Pr(>|W|)"]
)
if (exponentiate) {
d_b <- add_exp_columns(d_b, predictor_parameters)
column_formats <- label_exp_columns(column_formats, exp_coefficient_label(fit))
}
# Working correlation parameters, without tests
corr <- s$corr
if (is.null(correlation_rows)) {
correlation_rows <- !is.null(corr) && nrow(corr) == 1
}
if (isTRUE(correlation_rows) && !is.null(corr) && nrow(corr) > 0) {
d_b <- dplyr::bind_rows(
d_b,
tibble::tibble(
Parameter = gee_correlation_labels(rownames(corr)),
Coefficient = corr[, "Estimate"],
SE = corr[, "Std.err"]
)
)
}
# Names were formatted above
column_formats$Parameter@formatter <- \(x, ...) x
format_apa_parameters(
d_b,
predictor_parameters = predictor_parameters,
starred = starred,
bolded = bolded,
column_formats = column_formats,
t_with_df = t_with_df,
note = list(
model_sentences = gee_note_sentences(
fit,
corr = corr,
alpha_in_note = !isTRUE(correlation_rows)
)
)
) |>
labels_formatted()
}
# Working correlation labels ----
#' "Working correlation (*α*)" or, for unstructured correlations,
#' "Working correlation (*α*~1,2~)"
#' @keywords internal
#' @noRd
gee_correlation_labels <- function(names) {
index <- sub("^alpha[.]?", "", names)
index <- gsub(":", ",", index, fixed = TRUE)
ifelse(
nzchar(index),
paste0("Working correlation (*α*~", index, "~)"),
"Working correlation (*α*)"
)
}
# Note sentences ----
#' Note sentences of a GEE model
#' @param corr the estimated correlation parameters (summary(fit)$corr)
#' @param alpha_in_note whether to give the estimate of a single correlation
#' parameter in the note (when it is not in the table)
#' @keywords internal
#' @noRd
gee_note_sentences <- function(fit, corr = NULL, alpha_in_note = FALSE) {
link <- stats::family(fit)$link
corstr <- fit$corstr
structure_text <- switch(
corstr,
independence = "an independence",
exchangeable = "an exchangeable",
ar1 = "a first-order autoregressive (AR(1))",
unstructured = "an unstructured",
userdefined = "a user-defined",
fixed = "a fixed",
paste0("a ", corstr)
)
alpha_text <- ""
if (alpha_in_note && !is.null(corr) && nrow(corr) == 1) {
alpha_text <- paste0(" (*α* = ", the$trim_leading_zero(corr[1, "Estimate"]), ")")
}
se_text <- switch(
if (is.null(fit$std.err)) "san.se" else fit$std.err,
san.se = "robust (sandwich) estimates",
jack = "approximate jackknife estimates",
j1s = "one-step jackknife estimates",
fij = "fully iterated jackknife estimates",
"robust estimates"
)
cluster_sizes <- table(fit$id)
c(
paste0(
"Estimates are from generalized estimating equations ",
cite("liangLongitudinalDataAnalysis1986"),
" with a ",
link_name(link),
" link and ",
structure_text,
" working correlation",
alpha_text,
", fit with geepack ",
cite("halekohPackageGeepackGeneralized2006"),
"."
),
paste0("Standard errors are ", se_text, "."),
n_sentence(
stats::nobs(fit),
paste0(
" observations in ",
format(length(cluster_sizes), big.mark = ","),
" clusters (",
if (length(unique(cluster_sizes)) == 1) {
paste0(max(cluster_sizes), " per cluster")
} else {
paste0("up to ", max(cluster_sizes), " per cluster")
},
")"
)
)
)
}
# GEE fit statistics and comparisons ----
#' @details
#' For generalized estimating equations (`geepack::geeglm`), the default
#' metrics are `QIC` and `QICu` (Pan, 2001), from `geepack::QIC()`; `"all"`
#' adds `CIC`. GEE models have no likelihood, so there is no *AIC* or
#' likelihood-based *R*^2^.
#' @export
#' @rdname apa_performance
apa_performance.geeglm <- function(
fit,
metrics = c("QIC", "QICu"),
column_formats = NULL
) {
rlang::check_installed("geepack", reason = "for the QIC of generalized estimating equations.")
if (is.null(column_formats)) {
column_formats <- the$column_formats
}
names(metrics) <- NULL
if (any(metrics == "all")) {
metrics <- c("QIC", "QICu", "CIC")
}
format_performance(gee_performance(fit), metrics, column_formats)
}
#' QIC, QICu, and CIC of a GEE model
#' @keywords internal
#' @noRd
gee_performance <- function(fit) {
# QIC() refits the model from its call (with an independence working
# correlation), so the call gets the formula and the data themselves rather
# than names that may not be found where it is evaluated
f <- stats::formula(fit)
fit$call$formula <- f
if (!is.null(fit$data)) {
fit$call$data <- fit$data
}
q <- geepack::QIC(fit, env = environment(f))
tibble::tibble(
QIC = unname(q[["QIC"]]),
QICu = unname(q[["QICu"]]),
CIC = unname(q[["CIC"]])
)
}
#' Wald tests of the terms each GEE model adds to the previous one (the first
#' row is empty), from geepack's anova()
#' @keywords internal
#' @noRd
gee_wald_tests <- function(models) {
rows <- purrr::map(seq_along(models), \(i) {
if (i == 1) {
return(tibble::tibble(Chi2 = NA_real_, df = NA_real_, p = NA_real_))
}
a <- stats::anova(models[[i]], models[[i - 1]])
tibble::tibble(Chi2 = a$X2[1], df = a$Df[1], p = a[["P(>|Chi|)"]][1])
})
dplyr::bind_rows(rows)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.