Learn R Programming

bivarhr (version 0.1.5)

prewhiten_count_glm: Pre-whiten count series with GLM / NegBin model

Description

Fits a generalized linear model for count data using either a negative binomial model with log link and offset, or a Poisson fallback, and returns Pearson residuals to be used as a pre-whitened series.

Usage

prewhiten_count_glm(DT, yname)

Value

A numeric vector of Pearson residuals (one per row in DT

used in the fit).

Arguments

DT

A data.frame or data.table containing the response and covariates. It must include at least:

  • The count variable named by yname.

  • t_norm: normalized time index.

  • Regime, EconCycle, PopDensity, Epidemics, Climate, War.

  • log_exposure50: log exposure (offset).

yname

Character scalar; name of the count response column in DT.

Details

The function first attempts to fit a negative binomial GLM via MASS::glm.nb() with a log link and log_exposure50 as an offset. If the fit fails (e.g., due to convergence issues), it falls back to a Poisson GLM via glm(family = poisson()) with the same formula and offset.

Examples

Run this code
# \donttest{
if (interactive()) {
  n <- 100
  DT <- data.frame(
    t_norm = seq_len(n) / n,
    I = rpois(n, 5),
    Regime = factor(sample(c("A","B"), n, TRUE)),
    EconCycle = rnorm(n), PopDensity = runif(n),
    Epidemics = rbinom(n, 1, 0.1), Climate = rnorm(n), War = rbinom(n, 1, 0.05),
    log_exposure50 = log(runif(n, 40, 60))
  )
  r_I <- prewhiten_count_glm(DT, "I")
  head(r_I)
}
# }

Run the code above in your browser using DataLab