| dnbmix | R Documentation |
The function dnbmix() constructs a (vectorized) probability mass
function from a matrix of (simulated) means and corresponding size
parameters, as a function of the time point (row of means) within
the simulation period. The distribution at each time point is obtained
as a mixture of negative binomial (or Poisson) distributions.
dnbmix(means, size = NULL)
means |
a |
size |
the dispersion parameter of the |
a function(x, tp = 1, log = FALSE), which takes a vector of
counts x and calculates the (log-)probabilities of observing
each of these numbers at the tp'th time point of the simulation
period (indexing the rows of means).
Sebastian Meyer
logs_nbmix() where this function is used.
## a GLARMA example
library("glarma")
y <- as.vector(CHILI)
## fit a simple NegBin-GLARMA model
X <- t(sapply(2*pi*seq_along(y)/52.1775,
function (x) c(sin = sin(x), cos = cos(x))))
X <- cbind(intercept = 1, X)
fit <- glarma(y = y[1:883], X = X[1:883,], type = "NegBin", phiLags = 1)
## simulate the last four weeks (only 500 runs, for speed)
set.seed(1)
means <- replicate(500, {
forecast(fit, n.ahead = 4, newdata = X[884:887,], newoffset = rep(0,4))$mu
})
## derive the weekly forecast distributions
dfun <- dnbmix(means, coef(fit, type = "NB"))
dfun(4000, tp = 1)
dfun(4000, tp = 4)
curve(dfun(x, tp = 4), 0, 30000, type = "h",
main = "4-weeks-ahead forecast",
xlab = "No. infected", ylab = "Probability")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.