Nothing
skip_if_not_installed("geepack")
strip <- function(x) trimws(x, whitespace = "[\\h\\v]")
ohio_data <- function() {
e <- new.env()
utils::data("ohio", package = "geepack", envir = e)
e$ohio
}
# geepack::QIC() refits a model from its call, so the data must be an object
# that the call can find
ohio <- ohio_data()
gee_fit <- function(formula = resp ~ age + smoke, ...) {
geepack::geeglm(
formula,
id = id,
data = ohio,
family = binomial,
corstr = "exchangeable",
...
)
}
test_that("GEE tables show robust SEs, odds ratios, z tests, and alpha", {
fit <- gee_fit()
tbl <- apa_parameters(fit)
expect_s3_class(tbl, "apa7_tbl")
expect_equal(colnames(tbl), c("Variable", "*B*", "*SE*", "*OR*", "*z*", "*p*"))
expect_equal(strip(tbl$Variable), c("Constant", "Age", "Smoke", "Working correlation (*α*)"))
co <- summary(fit)$coefficients
expect_equal(strip(tbl$`*SE*`[1:3]), sprintf("%.2f", co[, "Std.err"]))
# z squared is geepack's Wald statistic
z <- as.numeric(sub("−", "-", strip(tbl$`*z*`[1:3])))
expect_equal(z, round(co[, "Estimate"] / co[, "Std.err"], 2), ignore_attr = TRUE)
expect_equal((co[, "Estimate"] / co[, "Std.err"])^2, co[, "Wald"], ignore_attr = TRUE)
# The correlation row has an estimate and SE but no test
expect_equal(strip(tbl$`*B*`[4]), sprintf("%.2f", summary(fit)$corr[1, "Estimate"]))
expect_equal(tbl$`*z*`[4], "")
expect_equal(tbl$`*OR*`[4], "")
})
test_that("the GEE note names the link, correlation, SEs, and clusters", {
note <- apa_note(apa_parameters(gee_fit()))
expect_match(note, "generalized estimating equations (Liang & Zeger, 1986) with a logit link and an exchangeable working correlation, fit with geepack (Halekoh et al., 2006).", fixed = TRUE)
expect_match(note, "Standard errors are robust (sandwich) estimates.", fixed = TRUE)
expect_match(note, "*N*\u00A0=\u00A02,148 observations in 537 clusters (4 per cluster).", fixed = TRUE)
# Without the correlation row, alpha is in the note
note <- apa_note(apa_parameters(gee_fit(), correlation_rows = FALSE))
expect_match(note, "exchangeable working correlation (*α*\u00A0=\u00A0.35)", fixed = TRUE)
# Jackknife standard errors
note <- apa_note(apa_parameters(gee_fit(std.err = "jack")))
expect_match(note, "approximate jackknife estimates", fixed = TRUE)
})
test_that("unstructured correlations are shown only on request", {
fit <- geepack::geeglm(resp ~ age, id = id, data = ohio_data(), family = binomial, corstr = "unstructured")
expect_false(any(grepl("Working correlation", apa_parameters(fit)$Variable)))
labels <- strip(apa_parameters(fit, correlation_rows = TRUE)$Variable)
expect_equal(sum(grepl("Working correlation", labels)), 6)
expect_true("Working correlation (*α*~1,2~)" %in% labels)
# Independence has no correlation parameters
fit <- geepack::geeglm(resp ~ age, id = id, data = ohio_data(), family = binomial)
expect_equal(strip(apa_parameters(fit)$Variable), c("Constant", "Age"))
expect_match(apa_note(apa_parameters(fit)), "an independence working correlation", fixed = TRUE)
})
test_that("GEE performance is QIC, without AIC or R2", {
# Fit here, so that geepack::QIC() can refit it from its call
fit <- geepack::geeglm(resp ~ age + smoke, id = id, data = ohio, family = binomial, corstr = "exchangeable")
tbl <- apa_performance(fit)
expect_equal(colnames(tbl), c("*QIC*", "*QIC*~u~"))
q <- geepack::QIC(fit)
expect_equal(tbl$`*QIC*`, format(round(q[["QIC"]], 2), big.mark = ",", nsmall = 2))
expect_true("*CIC*" %in% colnames(apa_performance(fit, "all")))
})
test_that("lists of GEE models get QIC and Wald tests, not likelihood ratio tests", {
f1 <- gee_fit(resp ~ age)
f2 <- gee_fit()
tbl <- apa_parameters(list(f1, f2))
labels <- strip(tbl$Variable)
expect_equal(sum(labels == "*QIC*"), 2)
expect_equal(sum(labels == "*χ*^2^(1)"), 2)
expect_false(any(grepl("AIC|R\\*\\^2", labels)))
values <- strip(tbl$`*B*`[labels == "*χ*^2^(1)"])
# Model 2 against model 1: geepack's Wald test of smoke
a <- anova(f2, f1)
expect_equal(sub("\\^.*$", "", values[2]), sprintf("%.2f", a$X2))
# Model 1 against a model with only a constant, fit to the same clusters
d <- ohio_data()
f0 <- geepack::geeglm(resp ~ 1, id = id, data = d, family = binomial, corstr = "exchangeable")
expect_equal(sub("\\^.*$", "", values[1]), sprintf("%.2f", anova(f1, f0)$X2))
expect_match(apa_note(tbl), "*χ*^2^\u00A0=\u00A0Wald test of the change in fit from the previous model", fixed = TRUE)
expect_false(apa7:::has_fixed_dispersion(f1))
})
test_that("GEE models are compared by QIC and Wald tests", {
f1 <- gee_fit(resp ~ age)
f2 <- gee_fit()
tbl <- apa_performance_comparison(f1, f2)
expect_equal(colnames(tbl), c("Model", "*QIC*", "*χ*^2^", "*df*", "*p*"))
expect_equal(strip(tbl$`*χ*^2^`[2]), sprintf("%.2f", anova(f2, f1)$X2))
})
test_that("GEE names with spaces are formatted", {
d <- ohio_data()
d$`Maternal Smoking` <- d$smoke
fit <- geepack::geeglm(resp ~ age + `Maternal Smoking`, id = id, data = d, family = binomial)
expect_true("Maternal Smoking" %in% strip(apa_parameters(fit)$Variable))
})
test_that("ci sets the level of GEE confidence intervals", {
fit <- gee_fit()
co <- summary(fit)$coefficients
tbl <- apa_parameters(fit, predictor_parameters = c("Parameter", "Coefficient", "CI"), exponentiate = FALSE, ci = .90)
ci_col <- grep("CI", colnames(tbl), value = TRUE)
expect_match(ci_col, "90%")
low <- co[2, "Estimate"] - stats::qnorm(.95) * co[2, "Std.err"]
expect_match(strip(tbl[[ci_col]][2]), sprintf("%.2f", abs(low)), fixed = TRUE)
})
test_that("GEE notes write out link names", {
fit <- geepack::geeglm(resp ~ age + smoke, id = id, data = ohio, family = binomial(link = "cloglog"), corstr = "exchangeable")
expect_match(apa_note(apa_parameters(fit)), "with a complementary log-log link", fixed = TRUE)
})
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.