Learn R Programming

depower (version 2025.1.20)

glm_nb: GLM for NB ratio of means

Description

Generalized linear model for two independent negative binomial outcomes.

Usage

glm_nb(data, equal_dispersion = FALSE, test = "wald", ci_level = NULL, ...)

Value

A list with the following elements:

SlotSubslotNameDescription
1chisq\(\chi^2\) test statistic for the ratio of means.
2dfDegrees of freedom.
3pp-value.
4ratioEstimated ratio of means (group 2 / group 1).
41estimatePoint estimate.
42lowerConfidence interval lower bound.
43upperConfidence interval upper bound.
5mean1Estimated mean of group 1.
51estimatePoint estimate.
52lowerConfidence interval lower bound.
53upperConfidence interval upper bound.
6mean2Estimated mean of group 2.
61estimatePoint estimate.
62lowerConfidence interval lower bound.
63upperConfidence interval upper bound.
7dispersion1Estimated dispersion of group 1.
71estimatePoint estimate.
72lowerConfidence interval lower bound.
73upperConfidence interval upper bound.
8dispersion2Estimated dispersion of group 2.
81estimatePoint estimate.
82lowerConfidence interval lower bound.
83upperConfidence interval upper bound.
9n1Sample size of group 1.
10n2Sample size of group 2.
11methodMethod used for the results.
12testType of hypothesis test.
13alternativeThe alternative hypothesis.
14equal_dispersionWhether or not equal dispersions were assumed.
15ci_levelConfidence level of the intervals.
16hessianInformation about the Hessian matrix.
17convergenceInformation about convergence.

Arguments

data

(list)
A list whose first element is the vector of negative binomial values from group 1 and the second element is the vector of negative binomial values from group 2. NAs are silently excluded. The default output from sim_nb().

equal_dispersion

(Scalar logical: FALSE)
If TRUE, the model is fit assuming both groups have the same population dispersion parameter. If FALSE (default), the model is fit assuming different dispersions.

test

(String: "wald"; "c("wald", "lrt"))
The statistical method used for the test results. test = "wald" performs a Wald test and optionally the Wald confidence intervals. test = "lrt" performs a likelihood ratio test and optionally the profile likelihood confidence intervals.

ci_level

(Scalar numeric: NULL; (0, 1))
If NULL, confidence intervals are set as NA. If in (0, 1), confidence intervals are calculated at the specified level. Profile likelihood intervals are computationally intensive, so intervals from test = "lrt" may be slow.

...

Optional arguments passed to glmmTMB::glmmTMB().

Details

Uses glmmTMB::glmmTMB() in the form

glmmTMB(
  formula = value ~ condition,
  data = data,
  dispformula = ~ condition,
  family = nbinom2
)

to model independent negative binomial outcomes \(X_1 \sim \text{NB}(\mu, \theta_1)\) and \(X_2 \sim \text{NB}(r\mu, \theta_2)\) where \(\mu\) is the mean of group 1, \(r\) is the ratio of the means of group 2 with respect to group 1, \(\theta_1\) is the dispersion parameter of group 1, and \(\theta_2\) is the dispersion parameter of group 2.

The hypotheses for the LRT and Wald test of \(r\) are

$$ \begin{aligned} H_{null} &: log(r) = 0 \\ H_{alt} &: log(r) \neq 0 \end{aligned} $$

where \(r = \frac{\bar{X}_2}{\bar{X}_1}\) is the population ratio of arithmetic means for group 2 with respect to group 1 and \(log(r_{null}) = 0\) assumes the population means are identical.

References

hilbe_2011depower

hilbe_2014depower

See Also

glmmTMB::glmmTMB()

Examples

Run this code
#----------------------------------------------------------------------------
# glm_nb() examples
#----------------------------------------------------------------------------
library(depower)

set.seed(1234)
d <- sim_nb(
  n1 = 60,
  n2 = 40,
  mean1 = 10,
  ratio = 1.5,
  dispersion1 = 2,
  dispersion2 = 8
)

lrt <- glm_nb(d, equal_dispersion = FALSE, test = "lrt", ci_level = 0.95)
lrt

wald <- glm_nb(d, equal_dispersion = FALSE, test = "wald", ci_level = 0.95)
wald

#----------------------------------------------------------------------------
# Compare results to manual calculation of chi-square statistic
#----------------------------------------------------------------------------
# Use the same data, but as a data frame instead of list
set.seed(1234)
d <- sim_nb(
  n1 = 60,
  n2 = 40,
  mean1 = 10,
  ratio = 1.5,
  dispersion1 = 2,
  dispersion2 = 8,
  return_type = "data.frame"
)

mod_alt <- glmmTMB::glmmTMB(
  formula = value ~ condition,
  data = d,
  dispformula = ~ condition,
  family = glmmTMB::nbinom2,
)
mod_null <- glmmTMB::glmmTMB(
  formula = value ~ 1,
  data = d,
  dispformula = ~ condition,
  family = glmmTMB::nbinom2,
)

lrt_chisq <- as.numeric(-2 * (logLik(mod_null) - logLik(mod_alt)))
lrt_chisq
wald_chisq <- summary(mod_alt)$coefficients$cond["condition2", "z value"]^2
wald_chisq

anova(mod_null, mod_alt)

Run the code above in your browser using DataLab