The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

Getting started with fewclust

fewclust fits regression models for a binary outcome measured repeatedly within a small number of groups: patients followed over a few visits, clinics observed over a few months, classes over a few terms. With few groups the usual analyses (GEE with the standard sandwich, mixed models with Wald tests on a normal reference) give tests that reject too often. fewclust fits the methods that stayed calibrated in a simulation study of these designs (see vignette("simulation-study")).

A first fit

fc_trial is a small synthetic trial: a binary outcome y measured at 4 occasions (time) in each of 24 groups (id), with a group-level treatment arm and a covariate x.

library(fewclust)
fit <- fit_fewclust(fc_trial, y ~ time * arm, id = id, time = time)
fit
#> Marginalized multilevel model
#> Formula: y ~ time * arm
#> Data:    96 observations in 24 groups (id), 4 occasions (time)
#> Tests:   t reference with 20 df (groups - coefficients)
#> 
#>             estimate std.error statistic p.value conf.low conf.high
#> (Intercept)  -0.8991    0.5697   -1.5783  0.1302   -2.087    0.2892
#> time         -0.5357    0.8177   -0.6552  0.5198   -2.241    1.1699
#> arm           0.2711    0.7731    0.3506  0.7295   -1.342    1.8838
#> time:arm      0.4928    1.0673    0.4618  0.6492   -1.734    2.7192
#> 
#> Notes:
#>   * convergence check: the slope of the latent standard deviation is
#>     3.2e-08 per occasion (< 0.001); the fit without the time trend of
#>     the latent standard deviation is reported

The data come first and the columns are given by name, so the function works in a pipe and with tibbles:

fc_trial |>
  fit_fewclust(y ~ time * arm + x, id = id, time = time) |>
  tidy(conf.int = TRUE)
#> # A tibble: 5 × 8
#>   term        estimate std.error statistic    df p.value conf.low conf.high
#>   <chr>          <dbl>     <dbl>     <dbl> <int>   <dbl>    <dbl>     <dbl>
#> 1 (Intercept)   -0.904     0.578    -1.57     19   0.134   -2.11      0.305
#> 2 time          -0.526     0.845    -0.622    19   0.541   -2.29      1.24 
#> 3 arm            0.265     0.784     0.338    19   0.739   -1.38      1.90 
#> 4 x              0.192     0.222     0.863    19   0.399   -0.273     0.657
#> 5 time:arm       0.528     1.10      0.479    19   0.638   -1.78      2.84

The coefficients are marginal (population-averaged) log odds ratios: they compare the probability of the event between groups of the population, as with GEE, not within a group as with a mixed model.

What the default does

method = "auto" (the default) fits the marginalized multilevel model of Heagerty (1999). With time the model has a first-order transition term between consecutive observations of a group and a latent standard deviation that may change linearly over the occasions. A convergence check replaces a trend fit at the boundary (or that did not converge) with the fit with a constant latent standard deviation, if that fit converges with finite estimates and standard errors; if it does not, the marginalized model gives no result for the dataset. The tests use a t distribution with N - p degrees of freedom: N groups minus p coefficients.

If the marginalized model gives no usable estimate, fit_fewclust() fits GEE with an AR(1) working correlation and the Fay-Graubard small-sample correction, with the same t reference, and says so with a message. A coefficient is usable when its estimate and standard error are finite and smaller than 10 on the logit scale.

summary() shows the details, including the convergence check:

summary(fit)
#> Marginalized multilevel model
#> Formula: y ~ time * arm
#> Data:    96 observations in 24 groups (id), 4 occasions (time)
#> Tests:   t reference with 20 df (groups - coefficients)
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)
#> (Intercept)  -0.8991     0.5697  -1.578    0.130
#> time         -0.5357     0.8177  -0.655    0.520
#> arm           0.2711     0.7731   0.351    0.730
#> time:arm      0.4928     1.0673   0.462    0.649
#> 
#> 95% confidence intervals:
#>              lower  upper
#> (Intercept) -2.087 0.2892
#> time        -2.241 1.1700
#> arm         -1.342 1.8840
#> time:arm    -1.734 2.7190
#> 
#> Model:
#>   latent SD constant; convergence check intervened (trend fit at the boundary)
#>   log-likelihood -55.117; nlm code 1 after 17 iterations
#>   outcome: event "1", prevalence 0.292
#> 
#> Notes:
#>   * convergence check: the slope of the latent standard deviation is
#>     3.2e-08 per occasion (< 0.001); the fit without the time trend of
#>     the latent standard deviation is reported

Choosing the method

gee <- fit_fewclust(fc_trial, y ~ time * arm,
  id = id, time = time,
  method = "gee"
)
tidy(gee)
#> # A tibble: 4 × 6
#>   term        estimate std.error statistic    df p.value
#>   <chr>          <dbl>     <dbl>     <dbl> <int>   <dbl>
#> 1 (Intercept)  -0.777      0.622   -1.25      20   0.226
#> 2 time         -0.676      0.482   -1.40      20   0.176
#> 3 arm          -0.0191     0.851   -0.0224    20   0.982
#> 4 time:arm      0.822      1.16     0.711     20   0.485

corstr sets the working correlation of GEE ("ar1", "exchangeable", "independence") and correction the small-sample correction ("fg", "md", "kc", "none"). Choosing anything other than the Fay-Graubard correction gives a warning, because with few groups it was the most reliable.

Errors, warnings, messages and notes

fewclust stops only when no estimate can be computed, and the error says what to do:

d <- fc_trial
d$y <- 0
fit_fewclust(d, y ~ time * arm, id = id, time = time)
#> Error in `fit_fewclust()`:
#> ! The outcome is constant (always 0): there is nothing to model.
#> ℹ Check the outcome column and the rows with missing values.

Warnings flag results that are less reliable than usual:

few <- simulate_clustered_binary(n_groups = 8, n_occasions = 4, seed = 5)
fit8 <- fit_fewclust(few, y ~ time + arm, id = id, time = time)
#> Warning: Only 8 groups: with fewer than 10 the tests and intervals may be unreliable.
#> ℹ `flip_test()` gives a p-value that keeps its level with few groups.

Notes, printed with the fit, describe small designs. With 2 or 3 occasions and at most 25 groups the models have little power and some datasets give no usable estimate:

short <- simulate_clustered_binary(n_groups = 12, n_occasions = 3, seed = 4)
fit_fewclust(short, y ~ time * arm, id = id, time = time)
#> Marginalized multilevel model
#> Formula: y ~ time * arm
#> Data:    36 observations in 12 groups (id), 3 occasions (time)
#> Tests:   t reference with 8 df (groups - coefficients)
#> 
#>             estimate std.error statistic p.value conf.low conf.high
#> (Intercept)  -0.7979    0.7393   -1.0794  0.3119   -2.503    0.9068
#> time         -0.1270    0.9528   -0.1333  0.8972   -2.324    2.0701
#> arm          -1.6416    1.4675   -1.1187  0.2957   -5.026    1.7424
#> time:arm      1.6740    1.8576    0.9012  0.3938   -2.609    5.9575
#> 
#> Notes:
#>   * With 3 occasions and 12 groups the tests have little power and some
#>     datasets give no usable estimate; flip_test() gives a p-value that
#>     keeps its level.
#>   * convergence check: the optimiser did not converge (nlm code 5); the
#>     fit without the time trend of the latent standard deviation is
#>     reported

Every condition has a class, "fewclust_error", "fewclust_warning" or "fewclust_message", so you can catch or silence them selectively, for example with tryCatch(..., fewclust_error = function(e) NULL).

A p-value for the smallest designs

flip_test() tests one coefficient with the sign-flip score test of Hemerik, Goeman and Finos (2020). It gives a p-value only, no estimate and no interval, but it keeps its level with few groups and short series:

flip_test(short, y ~ time * arm, id = id, time = time, seed = 1)
#> Sign-flip score test (p-value only; no estimate, no interval)
#> 
#>   H0        : coefficient of time:arm = 0
#>   groups    : 12; reference distribution: all 4,096 sign assignments
#>   statistic : 0.3079 (sum of the effective score contributions)
#>   p-value   : 0.3481

Many fits

Fits are ordinary objects, so they combine with the tidyverse:

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))
  })
#> # A tibble: 6 × 7
#> # Groups:   site [2]
#>   site  term        estimate std.error statistic    df p.value
#>   <chr> <chr>          <dbl>     <dbl>     <dbl> <int>   <dbl>
#> 1 A     (Intercept)   -1.60      0.565    -2.84     17  0.0114
#> 2 A     time           0.556     0.561     0.989    17  0.336 
#> 3 A     arm            0.563     0.648     0.869    17  0.397 
#> 4 B     (Intercept)   -1.61      0.573    -2.80     17  0.0122
#> 5 B     time           0.519     0.569     0.912    17  0.374 
#> 6 B     arm            0.459     0.661     0.694    17  0.497

Inside your own functions, pass the column names with {{ }}:

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

References

Heagerty PJ (1999). Marginally specified logistic-normal models for longitudinal binary data. Biometrics 55(3), 688-698.

Fay MP, Graubard BI (2001). Small-sample adjustments for Wald-type tests using sandwich estimators. Biometrics 57(4), 1198-1206.

Hemerik J, Goeman JJ, Finos L (2020). Robust testing in generalized linear models by sign flipping score contributions. Journal of the Royal Statistical Society: Series B 82(3), 841-864.

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.