View source: R/transfer-entropy.R
| prewhiten_count_glm | R Documentation |
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.
prewhiten_count_glm(DT, yname)
DT |
A
|
yname |
Character scalar; name of the count response column in
|
When the MASS package (listed under Suggests) is available,
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 MASS is not installed or 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.
A numeric vector of Pearson residuals (one per row in DT
used in the fit).
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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.