Fits a negative binomial generalized linear mixed model (GLMM) to
longitudinal count data using glmmTMB::glmmTMB() with
family = nbinom2(link = "log"). The fitted model and the estimated
dispersion parameter \(k\) (where Var(Y) = \(\mu + k\mu^2\)) are
returned for use in subsequent imputation steps. This mirrors PROC GLIMMIX
with dist=negbin link=log in SAS.
Arguments
- data
Data frame of observed (non-missing) records.
- formula
A two-sided formula specifying fixed and random effects, e.g.
count ~ base + trt + visit + (1 | id). The left-hand side should be the outcome variable; rows with a missing outcome should be excluded before calling this function (seeimpute_nb()).- replicate_col
Character or
NULL. Column indataidentifying bootstrap replicates. The model is fitted separately within each unique value. IfNULL(default), all rows form a single fit.
Value
A named list—one element per unique replicate—where each element contains:
modelThe fitted
glmmTMBobject.kEstimated NB dispersion \(k\) (=
glmmTMB::sigma(model)).
When replicate_col = NULL, the list has a single element named "1".
Details
The dispersion parameter follows the parameterisation used throughout
gsDesignNB: \(k\) such that \(\text{Var}(Y) = \mu + k\mu^2\). This
matches glmmTMB's nbinom2 family, where glmmTMB::sigma() returns
\(k\) directly.
Examples
if (FALSE) { # \dontrun{
# Requires glmmTMB
obs_data <- long_data[!is.na(long_data$count), ]
fits <- fit_nb_glmm(
data = obs_data,
formula = count ~ baseline + trt + visit + (1 | id)
)
fits[["1"]]$k # estimated dispersion
} # }