## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", echo = FALSE)
st <- fewclust:::.fc_study
t4 <- st$table4
fmt <- function(x) format(x, big.mark = ",")
pct <- function(x) sprintf("%.1f%%", x)

## -----------------------------------------------------------------------------
rows <- c(
  "mmm_cc", "gee_ar1_fg", "gee_exch_fg", "gee_ind_fg", "gee_ar1_md",
  "gee_ar1_kc", "gee_ar1_unc", "glmer", "flip"
)
rows <- intersect(rows, rownames(t4))
labels <- c(
  mmm_cc = "Marginalized multilevel model, convergence check",
  gee_ar1_fg = "GEE AR(1), Fay-Graubard",
  gee_exch_fg = "GEE exchangeable, Fay-Graubard",
  gee_ind_fg = "GEE independence, Fay-Graubard",
  gee_ar1_md = "GEE AR(1), Mancl-DeRouen",
  gee_ar1_kc = "GEE AR(1), Kauermann-Carroll",
  gee_ar1_unc = "GEE AR(1), no correction",
  glmer = "Mixed model, Wald test",
  flip = "Sign-flip score test"
)
tab <- data.frame(
  Method = unname(labels[rows]),
  `Above 7.5%` = t4[rows, "hi_all"],
  `Below 2.5%` = t4[rows, "lo_all"],
  `Usable` = pct(t4[rows, "pu"]),
  `Usable, 2-3 occasions and at most 25 clusters` = pct(t4[rows, "pu_small"]),
  check.names = FALSE
)
knitr::kable(tab, caption = sprintf(
  "Design points (of %s) clearly above 7.5%% or below 2.5%% type I error, and mean share of usable datasets.",
  fmt(st$design_points)
))

## ----include = FALSE----------------------------------------------------------
hi_mmm <- t4["mmm_cc", "hi_all"]
gee_rows <- setdiff(grep("^gee_", rownames(t4), value = TRUE), "gee_ar1_fg")
others_more <- all(t4[gee_rows, "hi_all"] > t4["gee_ar1_fg", "hi_all"])
flip_level <- t4["flip", "hi_all"] == 0 && t4["flip", "lo_all"] == 0

## -----------------------------------------------------------------------------
h <- st$h2h
parts <- do.call(rbind, strsplit(h$region, "|", fixed = TRUE))
tab2 <- data.frame(
  Clusters = parts[, 2], Occasions = parts[, 3], `Design points` = h$n_dp,
  `GEE AR(1) + FG` = sprintf("%.1f", h$mad_gee),
  `Marginalized model` = sprintf("%.1f", h$mad_mmm),
  Closer = ifelse(h$class %in% c("GEE", "MMM"), h$class, "neither (within the margin)"),
  check.names = FALSE
)
knitr::kable(tab2, caption = paste(
  "Mean absolute distance of the type I error from 5% (percentage points)",
  sprintf(
    "and which method was closer by more than the margin of %s %s.",
    format(unique(h$margin)), if (identical(unique(h$margin), 1L)) "point" else "points"
  )
))

