## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
has_dplyr <- requireNamespace("dplyr", quietly = TRUE)

## -----------------------------------------------------------------------------
library(fewclust)
fit <- fit_fewclust(fc_trial, y ~ time * arm, id = id, time = time)
fit

## -----------------------------------------------------------------------------
fc_trial |>
  fit_fewclust(y ~ time * arm + x, id = id, time = time) |>
  tidy(conf.int = TRUE)

## -----------------------------------------------------------------------------
summary(fit)

## -----------------------------------------------------------------------------
gee <- fit_fewclust(fc_trial, y ~ time * arm,
  id = id, time = time,
  method = "gee"
)
tidy(gee)

## ----error = TRUE-------------------------------------------------------------
try({
d <- fc_trial
d$y <- 0
fit_fewclust(d, y ~ time * arm, id = id, time = time)
})

## -----------------------------------------------------------------------------
few <- simulate_clustered_binary(n_groups = 8, n_occasions = 4, seed = 5)
fit8 <- fit_fewclust(few, y ~ time + arm, id = id, time = time)

## -----------------------------------------------------------------------------
short <- simulate_clustered_binary(n_groups = 12, n_occasions = 3, seed = 4)
fit_fewclust(short, y ~ time * arm, id = id, time = time)

## -----------------------------------------------------------------------------
flip_test(short, y ~ time * arm, id = id, time = time, seed = 1)

## ----eval = has_dplyr---------------------------------------------------------
sites <- dplyr::bind_rows(
  A = simulate_clustered_binary(n_groups = 20, seed = 1),
  B = simulate_clustered_binary(n_groups = 20, seed = 2),
  .id = "site"
)
sites |>
  dplyr::group_by(site) |>
  dplyr::group_modify(\(d, key) {
    tidy(fit_fewclust(d, y ~ time + arm, id = id, time = time))
  })

## -----------------------------------------------------------------------------
fit_by <- function(data, cluster) {
  fit_fewclust(data, y ~ time + arm, id = {{ cluster }}, time = time)
}
coef(fit_by(fc_trial, id))

