Title: | Nonlinear Mixed Effects Models of Epidemic Growth |
---|---|
Description: | Maximum likelihood estimation of nonlinear mixed effects models of epidemic growth using Template Model Builder ('TMB'). Enables joint estimation for collections of disease incidence time series, including time series that describe multiple epidemic waves. Supports a set of widely used phenomenological models: exponential, logistic, Richards (generalized logistic), subexponential, and Gompertz. Provides methods for interrogating model objects and several auxiliary functions, including one for computing basic reproduction numbers from fitted values of the initial exponential growth rate. Preliminary versions of this software were applied in Ma et al. (2014) <doi:10.1007/s11538-013-9918-2> and in Earn et al. (2020) <doi:10.1073/pnas.2004904117>. |
Authors: | Mikael Jagan [aut, cre] , Ben Bolker [aut] , Jonathan Dushoff [ctb] , David Earn [ctb] , Junling Ma [ctb] |
Maintainer: | Mikael Jagan <[email protected]> |
License: | GPL-3 |
Version: | 0.15.3 |
Built: | 2024-11-16 06:54:09 UTC |
Source: | CRAN |
An R package for estimating nonlinear mixed effects models of epidemic growth.
The “main” model estimating function is egf
.
To render a list of available help topics, use
help(package = "epigrowthfit")
.
Many of these document methods for the class of objects returned
by egf
.
To report a bug or request a change, use
bug.report(package = "epigrowthfit")
.
Mikael Jagan [email protected]
Extracts the bottom level parameter vector c(beta, theta, b)
or a subset. Segments beta
, theta
, and b
contain
(respectively) fixed effect coefficients, random effect covariance
parameters, and random effect coefficients.
## S3 method for class 'egf' coef(object, random = FALSE, full = FALSE, ...)
## S3 method for class 'egf' coef(object, random = FALSE, full = FALSE, ...)
object |
an |
random |
a logical. If |
full |
a logical. If |
... |
unused optional arguments. |
A numeric vector concatenating beta
, theta
, and
b
, without b
if random = FALSE
and without
mapped elements if full = FALSE
.
Attribute len
is a named integer vector partitioning the
result by segment.
Attribute map
is a named list of integer vectors i
such that that a full segment y
and its condensed counterpart
x
are related by y = x[i]
, with the exception that
i[j]
is NA
if y[j]
is mapped to an initial value.
NULL
is used in place of an integer vector where x
and y
are identical.
The result inherits from class coef.egf
, which has methods
for print
, as.list
, and labels
.
The generic function coef
.
example("egf", package = "epigrowthfit") for (random in c(FALSE, TRUE)) { for (full in c(FALSE, TRUE)) { cat(sprintf("random = %s, full = %s :\n\n", random, full)) str(coef(m1, random = random, full = full)) cat("\n") } }
example("egf", package = "epigrowthfit") for (random in c(FALSE, TRUE)) { for (full in c(FALSE, TRUE)) { cat(sprintf("random = %s, full = %s :\n\n", random, full)) str(coef(m1, random = random, full = full)) cat("\n") } }
Computes confidence intervals on fixed effect coefficients,
random effect covariance parameters, and linear combinations thereof,
including population fitted values.
Intervals on individual fitted values accounting for random effects
are supported, but only by method = "wald"
.
## S3 method for class 'egf' confint(object, parm, level = 0.95, A = seq_along(par), method = c("wald", "profile", "uniroot"), scale = 7, parallel = egf_parallel(), trace = FALSE, top = egf_top(object), subset = NULL, select = NULL, class = FALSE, link = TRUE, random = FALSE, ...) ## S3 method for class 'confint.egf' plot(x, by = 12L, subset = NULL, order = NULL, label = NULL, main = NULL, ...)
## S3 method for class 'egf' confint(object, parm, level = 0.95, A = seq_along(par), method = c("wald", "profile", "uniroot"), scale = 7, parallel = egf_parallel(), trace = FALSE, top = egf_top(object), subset = NULL, select = NULL, class = FALSE, link = TRUE, random = FALSE, ...) ## S3 method for class 'confint.egf' plot(x, by = 12L, subset = NULL, order = NULL, label = NULL, main = NULL, ...)
object |
an |
parm |
unused argument, for consistency with the generic function. |
level |
a number in the interval |
A |
a numeric matrix with |
method |
a character string indicating how intervals are computed. |
scale |
a positive number, for |
parallel |
an |
trace |
a logical. If |
top |
a subset of |
subset , select
|
index vectors for the rows and columns of
|
class |
a logical. If |
link |
a logical. If |
random |
a logical, affecting only |
... |
additional arguments passed from or to other methods. |
x |
a |
by |
a positive integer indicating the number of intervals displayed in one plot. |
order |
a permutation of |
label |
a character or expression vector of length |
main |
a character or expression vector of length 1 indicating a plot title, to be recycled for all plots. |
Three methods for computing confidence intervals are available:
"wald"
confidence limits are calculated as
value + c(-1, 1) * sqrt(q) * se
where q = qchisq(level, df = 1)
.
"profile", "uniroot"
confidence limits are calculated as approximate solutions of the equation
2 * (f(x) - f(value)) = q
where q = qchisq(level, df = 1)
and f
is the negative log marginal likelihood function
expressed as a function of the parameter x
in question.
Solutions are approximated by interpolating a likelihood profile
("profile"
) or by rootfinding ("uniroot"
).
"wald"
assumes asymptotic normality of the maximum likelihood
estimator. "profile"
and "uniroot"
avoid this contraint
but are typically expensive, requiring estimation of many restricted
models.
They are parallelized at the C++ level when there is OpenMP
support and object[["control"]][["omp_num_threads"]]
is set
to an integer greater than 1. When there is no OpenMP support, they
can still be parallelized at the R level with appropriate setting
of argument parallel
.
A numeric array in 2 or 3 dimensions containing the lower and upper confidence limits in the last dimension.
When confidence intervals on fitted values are desired, the user will
set A = NULL
and in that case have the option of passing
class = TRUE
to obtain an augmented result. Thus, alternatively:
A data frame inheriting from class confint.egf
, with variables:
top |
top level nonlinear model parameter, from
|
ts |
time series, from
|
window |
fitting window, from
|
value |
fitted value. |
ci |
a numeric matrix with two columns giving the lower and upper confidence limits. |
... |
further variables from
model.frame(object, "combined")
specified by argument |
The confidence level level
is preserved as an attribute.
The generic function confint
.
example("egf", package = "epigrowthfit") zz1 <- confint(m1, A = NULL, method = "wald", class = TRUE, random = TRUE) str(zz1) op <- par(mar = c(4.5, 4, 2, 1), oma = c(0, 0, 0, 0)) plot(zz1) par(op) zz2 <- confint(m1, A = NULL, method = "profile", class = TRUE, top = "log(r)", subset = quote(country == "A" & wave == 1)) zz3 <- confint(m1, A = NULL, method = "uniroot", class = TRUE, top = "log(r)", subset = quote(country == "A" & wave == 1))
example("egf", package = "epigrowthfit") zz1 <- confint(m1, A = NULL, method = "wald", class = TRUE, random = TRUE) str(zz1) op <- par(mar = c(4.5, 4, 2, 1), oma = c(0, 0, 0, 0)) plot(zz1) par(op) zz2 <- confint(m1, A = NULL, method = "profile", class = TRUE, top = "log(r)", subset = quote(country == "A" & wave == 1)) zz3 <- confint(m1, A = NULL, method = "uniroot", class = TRUE, top = "log(r)", subset = quote(country == "A" & wave == 1))
Transform covariances matrices to a “packed” representation or compute the inverse transformation.
cov2theta(Sigma) theta2cov(theta)
cov2theta(Sigma) theta2cov(theta)
Sigma |
an |
theta |
a numeric vector of length |
An -by-
real, symmetric, positive definite matrix
can be factorized as
The upper triangular Cholesky factor can be written as
where
is a unit upper triangular matrix and
and
are diagonal matrices.
cov2theta
takes and returns the vector
of length
containing the log diagonal entries
of
followed by (in column-major order) the strictly
upper triangular entries of
.
theta2cov
computes the
inverse transformation.
A vector like theta
(cov2theta
) or a matrix like
Sigma
(theta2cov
); see ‘Details’.
Time series of COVID-19 cases and tests in Ontario, Canada, daily from February 8, 2020 to May 1, 2022.
data(covid19.ontario, package = "epigrowthfit")
data(covid19.ontario, package = "epigrowthfit")
A data frame with 814 rows and 3 variables:
date
a Date
vector.
cases
an integer vector. cases[i]
is the number of cases confirmed
by Ontario public health units prior to date[i]
.
This number includes resolved and fatal cases as well as reinfections.
tests
an integer vector. tests[i]
is the number of tests completed
prior to date[i]
. This number includes repeated tests by
individuals except prior to April 15, 2020, when individuals were
counted at most once.
This data set is a processed subset of a larger data set downloaded on 2024-01-10 from the It is updated using an installed script:
\link{system.file}("scripts", "covid19.ontario.R", package = "epigrowthfit")
data(covid19.ontario, package = "epigrowthfit") plot(1 + diff(c(NA, cases)) ~ date, data = covid19.ontario, log = "y")
data(covid19.ontario, package = "epigrowthfit") plot(1 + diff(c(NA, cases)) ~ date, data = covid19.ontario, log = "y")
Extracts from a model object the number of observations
(see nobs
)
minus the number of estimated parameters
(fixed effect coefficients and random effect covariance parameters).
## S3 method for class 'egf' df.residual(object, ...)
## S3 method for class 'egf' df.residual(object, ...)
object |
an |
... |
unused optional arguments. |
An integer.
The generic function df.residual
.
Fits nonlinear mixed effects models of epidemic growth to collections of one or more disease incidence time series.
egf(model, ...) ## S3 method for class 'egf_model' egf(model, formula_ts, formula_windows, formula_parameters = list(), formula_priors = list(), data_ts, data_windows, subset_ts = NULL, subset_windows = NULL, select_windows = NULL, na_action_ts = c("fail", "pass"), na_action_windows = c("fail", "omit"), control = egf_control(), init = list(), map = list(), fit = TRUE, se = FALSE, ...)
egf(model, ...) ## S3 method for class 'egf_model' egf(model, formula_ts, formula_windows, formula_parameters = list(), formula_priors = list(), data_ts, data_windows, subset_ts = NULL, subset_windows = NULL, select_windows = NULL, na_action_ts = c("fail", "pass"), na_action_windows = c("fail", "omit"), control = egf_control(), init = list(), map = list(), fit = TRUE, se = FALSE, ...)
model |
an R object specifying a top level nonlinear model,
typically of class |
formula_ts |
a formula of the form |
formula_windows |
a formula of the form |
formula_parameters |
a list of formulae of the form |
formula_priors |
a list of formulae of the form |
data_ts , data_windows
|
data frames, lists, or environments to be searched for variables
named in the corresponding |
subset_ts , subset_windows
|
expressions to be evaluated in the corresponding |
select_windows |
an expression indicating additional variables in |
na_action_ts |
a character string affecting the handling of |
na_action_windows |
a character string affecting the handling of |
control |
an |
init |
a named list of numeric vectors with possible elements |
map |
a named list of factors with possible elements |
fit |
a logical. If |
se |
a logical. If |
... |
additional arguments passed from or to other methods. |
Users attempting to set arguments formula_priors
, init
,
and map
should know the structure of the bottom level parameter
vector. It is described under topic egf-class
.
If
formula_ts = cbind(time, x) ~ ts1 formula_windows = cbind(start, end) ~ ts2
then it is expected that time
, start
, and end
(after coercion to numeric) measure time on the same scale.
To be precise, numeric times should have a common unit of measure
and, at least within time series, represent displacements from a
common reference time.
These conditions will always hold if time
, start
, and
end
all evaluate to Date
, POSIXct
,
or POSIXlt
vectors.
When day of week effects are estimated, numeric times are interpreted
as numbers of days since midnight on January 1, 1970, so that time
points can be mapped unambiguously to days of week.
Furthermore, in this case, time
(after coercion to numeric) is
required to be integer-valued with one day spacing in all time series.
This means that
isTRUE(all.equal(time, round(time))) && all(range(diff(round(time))) == 1)
must be TRUE
in each level of ts1
.
These conditions ensure that intervals between successive time points
represent exactly one day of week.
A list inheriting from class egf
.
See topic egf-class
for class documentation.
The many methods for class egf
,
listed by methods(class = "egf")
.
## Simulate 'N' incidence time series exhibiting exponential growth set.seed(180149L) N <- 10L f <- function(time, r, c0) { lambda <- diff(exp(log(c0) + r * time)) c(NA, rpois(lambda, lambda)) } time <- seq.int(0, 40, 1) r <- rlnorm(N, -3.2, 0.2) c0 <- rlnorm(N, 6, 0.2) data_ts <- data.frame(country = gl(N, length(time), labels = LETTERS[1:N]), time = rep.int(time, N), x = unlist(Map(f, time = list(time), r = r, c0 = c0))) rm(f, time) ## Define fitting windows (here, two per time series) data_windows <- data.frame(country = gl(N, 1L, 2L * N, labels = LETTERS[1:N]), wave = gl(2L, 10L), start = c(sample(seq.int(0, 5, 1), N, TRUE), sample(seq.int(20, 25, 1), N, TRUE)), end = c(sample(seq.int(15, 20, 1), N, TRUE), sample(seq.int(35, 40, 1), N, TRUE))) ## Estimate the generative model m1 <- egf(model = egf_model(curve = "exponential", family = "pois"), formula_ts = cbind(time, x) ~ country, formula_windows = cbind(start, end) ~ country, formula_parameters = ~(1 | country:wave), data_ts = data_ts, data_windows = data_windows, se = TRUE) ## Re-estimate the generative model with: ## * Gaussian prior on beta[1L] ## * LKJ prior on all random effect covariance matrices ## (here there happens to be just one) ## * initial value of 'theta' set explicitly ## * theta[3L] fixed at initial value m2 <- update(m1, formula_priors = list(beta[1L] ~ Normal(mu = -3, sigma = 1), Sigma ~ LKJ(eta = 2)), init = list(theta = c(log(0.5), log(0.5), 0)), map = list(theta = 3L))
## Simulate 'N' incidence time series exhibiting exponential growth set.seed(180149L) N <- 10L f <- function(time, r, c0) { lambda <- diff(exp(log(c0) + r * time)) c(NA, rpois(lambda, lambda)) } time <- seq.int(0, 40, 1) r <- rlnorm(N, -3.2, 0.2) c0 <- rlnorm(N, 6, 0.2) data_ts <- data.frame(country = gl(N, length(time), labels = LETTERS[1:N]), time = rep.int(time, N), x = unlist(Map(f, time = list(time), r = r, c0 = c0))) rm(f, time) ## Define fitting windows (here, two per time series) data_windows <- data.frame(country = gl(N, 1L, 2L * N, labels = LETTERS[1:N]), wave = gl(2L, 10L), start = c(sample(seq.int(0, 5, 1), N, TRUE), sample(seq.int(20, 25, 1), N, TRUE)), end = c(sample(seq.int(15, 20, 1), N, TRUE), sample(seq.int(35, 40, 1), N, TRUE))) ## Estimate the generative model m1 <- egf(model = egf_model(curve = "exponential", family = "pois"), formula_ts = cbind(time, x) ~ country, formula_windows = cbind(start, end) ~ country, formula_parameters = ~(1 | country:wave), data_ts = data_ts, data_windows = data_windows, se = TRUE) ## Re-estimate the generative model with: ## * Gaussian prior on beta[1L] ## * LKJ prior on all random effect covariance matrices ## (here there happens to be just one) ## * initial value of 'theta' set explicitly ## * theta[3L] fixed at initial value m2 <- update(m1, formula_priors = list(beta[1L] ~ Normal(mu = -3, sigma = 1), Sigma ~ LKJ(eta = 2)), init = list(theta = c(log(0.5), log(0.5), 0)), map = list(theta = 3L))
Set parameters controlling the behaviour of egf
.
egf_control(outer_optimizer = egf_optimizer(nlminb), inner_optimizer = egf_optimizer(newton), trace = FALSE, profile = FALSE, sparse_X = FALSE, omp_num_threads = getOption("egf.cores", 1L))
egf_control(outer_optimizer = egf_optimizer(nlminb), inner_optimizer = egf_optimizer(newton), trace = FALSE, profile = FALSE, sparse_X = FALSE, omp_num_threads = getOption("egf.cores", 1L))
outer_optimizer , inner_optimizer
|
|
trace |
an integer determining the amount of tracing performed; see ‘Details’. |
profile |
a logical. If |
sparse_X |
a logical. If |
omp_num_threads |
an integer indicating a number of OpenMP threads to be used when evaluating the objective function, provided that epigrowthfit was compiled with OpenMP support. |
trace
affects the amount of information printed during
likelihood evaluations:
0
likelihood evaluations are always silent.
1
a message is printed whenever a negative log marginal likelihood
term is NaN
or exceeds 1e+09
.
2
all negative log marginal likelihood terms are printed.
egf
passes silent = trace == 0L
to
MakeADFun
. A corollary is that nonzero values of
trace
have a number of additional side effects:
error messages are printed during function and gradient evaluations;
the maximum absolute gradient element is printed with each gradient evaluation; and
trace flags set by config
are turned on.
A list inheriting from class egf_control
containing the
validated arguments.
Setting trace > 0L
and omp_num_threads > 0L
simultaneously
should be avoided, because tracing messages are printed using R API
functions that are not thread-safe.
control <- egf_control() str(control)
control <- egf_control() str(control)
Sets parameters controlling the graphical output of
plot
for objects of class egf
.
Supplied values override package defaults
(retrievable as defaults <- egf_control_plot()
),
which in turn override global defaults set via par
.
Below, x
, type
, time_as
, and delta
refer to the so-named arguments of plot.egf
.
egf_control_plot(window, data, predict, asymptote, box, axis, title, doubling)
egf_control_plot(window, data, predict, asymptote, box, axis, title, doubling)
window |
a named list of arguments to |
data |
a named list of the form |
predict |
a named list of the form |
asymptote |
a named list of arguments to |
box |
a named list of arguments to |
axis |
a named list of the form |
title |
a named list of the form |
doubling |
a named list of the form |
Setting an argument (or an element thereof in the case of nested lists)
to NULL
has the effect of suppressing the corresponding
plot element.
A named list containing the package defaults modified according to the arguments in the call.
Performs simple diagnostic tests to assess whether the optimizer that produced an estimated model actually converged to a local minimum point of the negative log marginal likelihood function.
egf_has_converged(object, check = TRUE, tol = 1)
egf_has_converged(object, check = TRUE, tol = 1)
object |
an |
check |
a logical. If |
tol |
a positive number. Convergence requires all gradient elements
to be less than or equal to |
TRUE
if all tests pass. FALSE
if any test fails.
NA
if no test fails, but the test for a positive definite
Hessian matrix is indeterminate because the matrix has not been
computed.
Tests whether an object specifies a model with random effects.
egf_has_random(object, check = TRUE)
egf_has_random(object, check = TRUE)
object |
an |
check |
a logical. If |
TRUE
or FALSE
.
Sets flags defining a top level nonlinear model of epidemic growth
to be estimated by egf
.
egf_model(curve = c("logistic", "richards", "exponential", "subexponential", "gompertz"), excess = FALSE, family = c("nbinom", "pois"), day_of_week = FALSE)
egf_model(curve = c("logistic", "richards", "exponential", "subexponential", "gompertz"), excess = FALSE, family = c("nbinom", "pois"), day_of_week = FALSE)
curve |
a character string specifying a model for expected cumulative incidence as a function of time. |
excess |
a logical flag. If |
family |
a character string specifying a family of discrete probability distributions assigned to observations, which are the first order differences of observed cumulative incidence. |
day_of_week |
an integer flag. If positive, then day of week effects are estimated as offsets relative to the indicated day of week (1=Sunday, 2=Monday, and so on). |
A list inheriting from class egf_model
containing the
validated arguments.
model <- egf_model() str(model)
model <- egf_model() str(model)
Utilities for linking an optimizer with optional arguments and
control parameters to define an optimization method for use by
egf
.
egf_optimizer(f = nlminb, args = list(), control = list())
egf_optimizer(f = nlminb, args = list(), control = list())
f |
a function performing optimization. Supported are
|
args |
a list of optional arguments to |
control |
a list of control parameters to be passed to |
An optim
-like function is a function f
such that:
the first three arguments of f
specify an initial
parameter vector, an objective function, and a gradient function,
respectively;
f
accepts control
as a fourth (or later) argument;
and
f
returns a named list with elements
par
, value
, convergence
, and message
.
A list inheriting from class egf_optimizer
containing the
validated arguments, wherein f
may be a new function wrapping
the supplied one to make it optim
-like.
optimizer <- egf_optimizer(nlminb) str(optimizer)
optimizer <- egf_optimizer(nlminb) str(optimizer)
Defines instructions for parallelization by linking a method with options.
egf_parallel(method = c("serial", "multicore", "snow"), outfile = "", cores = getOption("egf.cores", 1L), args = list(), cl = NULL)
egf_parallel(method = c("serial", "multicore", "snow"), outfile = "", cores = getOption("egf.cores", 1L), args = list(), cl = NULL)
method |
a character string indicating a method of parallelization.
|
outfile |
a character string indicating a file path where console output
should be diverted. An empty string indicates no diversion.
If |
cores |
a positive integer indicating a number of threads/processes to
fork/spawn when |
args |
a list of optional arguments to
|
cl |
an existing socket cluster
( |
A list inheriting from class "egf_parallel"
containing the arguments (after possible matching and coercion).
vignette("parallel", "parallel")
.
parallel <- egf_parallel() str(parallel)
parallel <- egf_parallel() str(parallel)
Functions used by egf
to specify prior distributions
of bottom level mixed effects model parameters.
Normal(mu = 0, sigma = 1) LKJ(eta = 1) Wishart(df, scale, tol = 1e-06) InverseWishart(df, scale, tol = 1e-06)
Normal(mu = 0, sigma = 1) LKJ(eta = 1) Wishart(df, scale, tol = 1e-06) InverseWishart(df, scale, tol = 1e-06)
mu |
a numeric vector listing means. |
sigma |
a positive numeric vector listing standard deviations. |
eta |
a positive numeric vector listing values for the shape parameter, with 1 corresponding to a uniform distribution over the space of real, symmetric, positive definite matrices with unit diagonal elements. Lesser (greater) values concentrate the probability density around such matrices whose determinant is nearer to 0 (1). |
df |
a numeric vector listing degrees of freedom. |
scale |
a list of real, symmetric, positive definite matrices or a matrix to be placed in a list of length 1. |
tol |
a non-negative number specifying a tolerance for indefiniteness
of |
A list inheriting from class egf_prior
, with elements:
family |
a character string specifying a family of distributions. |
parameters |
a named list of numeric vectors specifying parameter values. |
Normal(mu = 0, sigma = 1) Normal(mu = -5:5, sigma = c(0.1, 1)) LKJ(eta = 2) u <- matrix(rnorm(9L), 3L, 3L) utu <- crossprod(u) uut <- tcrossprod(u) Wishart(df = 6, scale = utu) InverseWishart(df = 6, scale = list(utu, uut))
Normal(mu = 0, sigma = 1) Normal(mu = -5:5, sigma = c(0.1, 1)) LKJ(eta = 2) u <- matrix(rnorm(9L), 3L, 3L) utu <- crossprod(u) uut <- tcrossprod(u) Wishart(df = 6, scale = utu) InverseWishart(df = 6, scale = list(utu, uut))
Retrieves the names used internally for top level nonlinear model parameters.
egf_top(object, ...) ## S3 method for class 'egf_model' egf_top(object, link = TRUE, ...) ## S3 method for class 'egf' egf_top(object, link = TRUE, ...)
egf_top(object, ...) ## S3 method for class 'egf_model' egf_top(object, link = TRUE, ...) ## S3 method for class 'egf' egf_top(object, link = TRUE, ...)
object |
an R object specifying a top level nonlinear model,
typically of class |
link |
a logical flag. If |
... |
unused optional arguments. |
A character vector listing names relevant to object
.
egf
Class egf
designates models estimated by function egf
.
Objects of this class hold information about an estimated model.
Components can be accessed directly. However, as the components
are subject to change without notice, portable code will rely on
exported methods for interrogation.
Currently, a legitimate egf
object is a list with elements:
model
a copy of the so-named argument of egf
.
frame
a list of the form list(ts, windows, parameters, extra)
.
ts
and windows
are data frames preserving time series
and fitting window endpoints.
parameters
is a list of mixed effects model frames,
with one element for each top level nonlinear model parameter.
extra
is a data frame preserving additional variables
specified in call[["select_windows"]]
.
windows
, the model frames listed in parameters
,
and extra
all correspond rowwise.
priors
a list of the form list(top, bottom = list(beta, theta, Sigma))
,
where top
, beta
, theta
, and Sigma
are all
lists of egf_prior
objects.
control
a copy of the so-named argument of egf
.
tmb_out
the list output of MakeADFun
.
optimizer_out
the list output of the optimizer specified by control[["optimizer"]]
.
init, best
numeric vectors giving the values of the condensed bottom level parameter vector used in the first and maximal likelihood evaluations.
random
a logical vector indexing the elements of the condensed bottom level
parameter vector that are not arguments of the negative log
marginal likelihood function. It indexes
all elements of segment b
(random effect coefficients) and
(but only if control[["profile"]] = TRUE
)
all elements of segment beta
(fixed effect coefficients).
value, gradient
numeric vectors giving the value and gradient of the negative log
marginal likelihood function at best[!random]
.
hessian
a logical flag indicating whether the Hessian matrix of the negative log
marginal likelihood function is positive definite at best[!random]
.
NA
means that the matrix has not been computed.
coefficients
a list of the form list(fixed, random)
, where fixed
and random
are data frames preserving interpretive information
about fixed and random effect coefficients.
contrasts
a list of the form list(fixed, random)
, where fixed
and random
are lists preserving contrasts used to construct
the fixed and random effects design matrices.
call
the call to egf
, enabling updates to the object by
the default method of generic function update
.
An estimated model is specified by a bottom level parameter vector that is the concatenation of three segments:
beta
the result of unlist(lbeta)
, where lbeta
is a list
of numeric vectors of fixed effect coefficients,
with one vector for each top level nonlinear model parameter.
The order of top level parameters is specified by
egf_top(model)
.
theta
the result of unlist(ltheta)
, where ltheta
is a list
of numeric vectors of random effect covariance parameters,
with one vector for each distinct random effect term in
formula_parameters
.
Each vector parametrizes a random effect covariance matrix via
theta2cov
and its inverse cov2theta
.
The list Sigma
mentioned in the description of egf
argument formula_priors
is precisely
lapply(ltheta, theta2cov)
.
b
the result of unlist(lb)
, where lb
is a list
of numeric matrices of scaled random effect coefficients,
corresponding elementwise to ltheta
.
The columns of lb[[i]]
(one per level of the grouping variable)
are interpreted as samples from a zero mean, unit variance multivariate
normal distribution with covariance matrix
cov2cor(theta2cov(ltheta[[i]]))
.
When elements of this vector are “mapped” via egf
argument map
, likelihood is defined as a function of the condensed
vector that excludes mapped elements.
Methods are defined for generic functions
coef
,
fixef
, and
ranef
to allow users to interrogate the structure of the vector.
methods(class = "egf") help.search("\\.egf$", fields = "alias", package = "epigrowthfit") ## less verbosely: alias??`\\.egf$`
methods(class = "egf") help.search("\\.egf$", fields = "alias", package = "epigrowthfit") ## less verbosely: alias??`\\.egf$`
The functions and other objects listed here are no longer part of epigrowthfit as they are no longer needed.
## Nothing yet!
## Nothing yet!
These either are stubs reporting that they are defunct or have been removed completely (apart from being documented here).
Deprecated
, base-deprecated
,
epigrowthfit-deprecated
, epigrowthfit-notyet
.
The functions and other objects listed here are provided only for compatibility with older versions of epigrowthfit and may become defunct as soon as the next release.
## Nothing yet!
## Nothing yet!
Defunct
, base-defunct
,
epigrowthfit-defunct
, epigrowthfit-notyet
.
The functions listed here are defined but not yet implemented.
Use bug.report(package = "epigrowthfit")
to request
an implementation.
## Nothing yet!
## Nothing yet!
NotYetImplemented
,
epigrowthfit-deprecated
, epigrowthfit-defunct
.
Extracts from a model object the generalized Akaike Information Criterion (AIC).
## S3 method for class 'egf' extractAIC(fit, scale, k = 2, ...)
## S3 method for class 'egf' extractAIC(fit, scale, k = 2, ...)
fit |
an |
scale |
unused argument, for generic consistency. |
k |
a number giving a weight for the equivalent degrees of freedom.
|
... |
unused optional arguments. |
An numeric vector of length 2 giving the equivalent degrees of freedom and criterion value.
The generic function extractAIC
.
Computes the proportion of a population expected to be infected over the course of an epidemic, as a function of the basic reproduction number.
finalsize(R0, S0, I0)
finalsize(R0, S0, I0)
R0 |
a numeric vector listing non-negative values for the basic reproduction number. |
S0 , I0
|
numeric vectors listing values in the interval |
At least one of S0
and I0
must be supplied.
If S0
(I0
) is supplied but not I0
(S0
),
then the latter is assigned the value of one minus the former.
R0
, S0
, and I0
are recycled to a common length
(the maximum of their lengths).
A numeric vector listing values in the interval for the
expected epidemic final size.
The basic reproduction number R0
defines the expected
epidemic final size Z
through an implicit equation,
Z = S0 * (1 - exp(-R0 * (Z + I0))) ,
which admits an explicit solution
Z = S0 + (1/R0) * W(-R0 * S0 * exp(-R0 * (S0 + I0))) .
Here, W
denotes the
Lambert function.
finalsize
computes this solution, relying on function
lambertW
from package emdbook.
Ma, J. & Earn, D. J. D. (2006). Generality of the final size formula for an epidemic of a newly invading infectious disease. Bulletin of Mathetmatical Biology, 68(3), 679-702. doi:10.1007/s11538-005-9047-7
R0 <- 10^seq(-3, 1, length.out = 151L) plot(R0, finalsize(R0, S0 = 1, I0 = 0), type = "l", las = 1, xlab = "basic reproduction number", ylab = "final size")
R0 <- 10^seq(-3, 1, length.out = 151L) plot(R0, finalsize(R0, S0 = 1, I0 = 0), type = "l", las = 1, xlab = "basic reproduction number", ylab = "final size")
Retrieves fitted values of top level nonlinear model parameters. The fitted value of a given parameter for a given fitting window is obtained by adding (i) the population fitted value computed as a linear combination of fixed effect coefficients and (ii) all applicable random effects, with random effects set equal to their conditional modes.
## S3 method for class 'egf' fitted(object, top = egf_top(object), subset = NULL, select = NULL, class = FALSE, se = FALSE, ...) ## S3 method for class 'fitted.egf' confint(object, parm = seq_len(nrow(object)), level = 0.95, class = FALSE, ...)
## S3 method for class 'egf' fitted(object, top = egf_top(object), subset = NULL, select = NULL, class = FALSE, se = FALSE, ...) ## S3 method for class 'fitted.egf' confint(object, parm = seq_len(nrow(object)), level = 0.95, class = FALSE, ...)
object |
an |
top |
a subset of |
subset , select
|
index vectors for the rows and columns of
|
class |
a logical. If |
se |
a logical. If |
... |
additional arguments passed from or to other methods. |
parm |
a valid index vector for the rows of |
level |
a number in the interval |
A numeric matrix containing fitted values.
Users can pass class = TRUE
to obtain an augmented result.
Thus, alternatively:
A data frame inheriting from class fitted.egf
, with variables:
top |
top level nonlinear model parameter, from
|
ts |
time series, from
|
window |
fitting window, from
|
value |
fitted value. |
se |
approximate delta method standard error (only if requested). |
... |
further variables from
model.frame(object, "combined")
specified by argument |
The generic function fitted
.
example("egf", package = "epigrowthfit") zz <- fitted(m1, class = TRUE, se = TRUE) str(zz) confint(zz, class = TRUE)
example("egf", package = "epigrowthfit") zz <- fitted(m1, class = TRUE, se = TRUE) str(zz) confint(zz, class = TRUE)
Extracts from a model object details about the fixed effect
coefficients, namely segment beta
of the bottom level
parameter vector.
## S3 method for class 'egf' fixef(object, ...)
## S3 method for class 'egf' fixef(object, ...)
object |
an |
... |
unused optional arguments. |
A data frame with one row per coefficient and variables:
bottom |
label for a bottom level mixed effects model parameter,
in this case for a fixed effect coefficient.
This is a string with format |
top |
name of the top level nonlinear model parameter whose
fitted value is a function of |
term |
term from the fixed effects component of the mixed effects
model formula for parameter |
colname |
column name in the fixed effects design matrix
|
value |
coefficient estimate, from segment |
The generic function fixef
.
Extracts from a model object the mixed effects model formula corresponding to a top level nonlinear model parameter.
## S3 method for class 'egf' formula(x, top = egf_top(x), split = FALSE, ...)
## S3 method for class 'egf' formula(x, top = egf_top(x), split = FALSE, ...)
x |
an |
top |
a character string specifying a top level nonlinear model parameter. |
split |
a logical flag. If |
... |
unused optional arguments. |
By default, the mixed effects model formula corresponding to
top
. If split = TRUE
, then the same formula
without random effect terms. The deleted terms are stored in
an expression vector and preserved as attribute random
of the result.
The generic function formula
.
Extracts from a model object the call to egf
that produced it.
This method exists mainly to enable compatibility with the default method of
generic function update
.
## S3 method for class 'egf' getCall(x, ...)
## S3 method for class 'egf' getCall(x, ...)
x |
an |
... |
unused optional arguments. |
A call to egf
.
The generic function getCall
.
Generation interval
density function (dgi
), distribution function (pgi
),
quantile function (qgi
), and sampling (rgi
).
Results are conditional on supplied latent and infectious period
distributions. It is assumed
that the latent period and infectious waiting time are independent,
that infectiousness is constant over the infectious period, and
that the latent and infectious periods are positive and integer-valued (in arbitrary but common units of time).
dgi(x, latent, infectious) pgi(q, latent, infectious) qgi(p, latent, infectious) rgi(n, latent, infectious)
dgi(x, latent, infectious) pgi(q, latent, infectious) qgi(p, latent, infectious) rgi(n, latent, infectious)
x , q
|
a numeric vector listing generation intervals. |
p |
a numeric vector listing probabilities. |
n |
a non-negative integer indicating a sample size.
If |
latent , infectious
|
numeric vectors such that |
A numeric vector with length equal to the that of the first argument
or length n
in the case of rgi
.
Svensson, Å. (2007). A note on generation times in epidemic models. Mathematical Biosciences, 208(1), 300-311. doi:10.1016/j.mbs.2006.10.010
latent <- c(0.026, 0.104, 0.182, 0.246, 0.318, 0.104, 0.013, 0.004, 0.003) m <- length(latent) infectious <- c(0.138, 0.462, 0.256, 0.078, 0.041, 0.007, 0.004, 0.004, 0.006, 0.004) n <- length(infectious) ## Histogram of samples y <- rgi(1e06, latent, infectious) hist(y, breaks = seq(0, m + n + 1), freq = FALSE, las = 1, ylab = "relative frequency", main = "") ## Density and distribution functions x <- seq(0, m + n + 1, by = 0.02) fx <- dgi(x, latent, infectious) Fx <- pgi(x, latent, infectious) plot(x, fx, type = "l", las = 1, # consistent with histogram xlab = "generation interval", ylab = "density function") plot(x, Fx, type = "l", las = 1, xlab = "generation interval", ylab = "distribution function") ## Quantile function p <- seq(0, 1, by = 0.001) qp <- qgi(p, latent, infectious) plot(p, qp, type = "l", las = 1, xlab = "probability", ylab = "quantile function")
latent <- c(0.026, 0.104, 0.182, 0.246, 0.318, 0.104, 0.013, 0.004, 0.003) m <- length(latent) infectious <- c(0.138, 0.462, 0.256, 0.078, 0.041, 0.007, 0.004, 0.004, 0.006, 0.004) n <- length(infectious) ## Histogram of samples y <- rgi(1e06, latent, infectious) hist(y, breaks = seq(0, m + n + 1), freq = FALSE, las = 1, ylab = "relative frequency", main = "") ## Density and distribution functions x <- seq(0, m + n + 1, by = 0.02) fx <- dgi(x, latent, infectious) Fx <- pgi(x, latent, infectious) plot(x, fx, type = "l", las = 1, # consistent with histogram xlab = "generation interval", ylab = "density function") plot(x, Fx, type = "l", las = 1, xlab = "generation interval", ylab = "distribution function") ## Quantile function p <- seq(0, 1, by = 0.001) qp <- qgi(p, latent, infectious) plot(p, qp, type = "l", las = 1, xlab = "probability", ylab = "quantile function")
Extracts from a model object the value of the log marginal likelihood. Whether the result represents a local maximum depends on, among other things, convergence of the optimizer.
## S3 method for class 'egf' logLik(object, ...)
## S3 method for class 'egf' logLik(object, ...)
object |
an |
... |
unused optional arguments. |
A numeric vector of length 1 inheriting from class logLik
.
Attribute df
is the number of estimated parameters
(fixed effect coefficients and random effect covariance parameters).
Attribute nobs
is the number of observations of disease
incidence used in estimation.
The generic function logLik
.
Extracts from a model object any of several data frames used to specify the model, including the mixed effects model frames.
## S3 method for class 'egf' model.frame(formula, which = c("ts", "windows", "parameters", "extra", "combined"), top = egf_top(formula), full = FALSE, ...)
## S3 method for class 'egf' model.frame(formula, which = c("ts", "windows", "parameters", "extra", "combined"), top = egf_top(formula), full = FALSE, ...)
formula |
an |
which |
a character string controlling what is returned:
|
top |
a character string specifying a top level nonlinear model parameter,
for |
full |
a logical, for |
... |
unused optional arguments. |
A data frame.
The generic function model.frame
.
Extracts from a model object fixed and random effects design matrices.
## S3 method for class 'egf' model.matrix(object, which = c("fixed", "random"), top = NULL, random = NULL, ...)
## S3 method for class 'egf' model.matrix(object, which = c("fixed", "random"), top = NULL, random = NULL, ...)
object |
an |
which |
a character string controlling what is returned:
|
top |
a character string specifying a top level nonlinear model parameter.
|
random |
a random effect term, which is a call to binary operator |
... |
unused optional arguments. |
model.matrix(which = "fixed", top = NULL)
returns the result of combining (in the sense of cbind
)
all fixed effects design matrices.
model.matrix(which = "random", top = "<name>", random = NULL)
returns the result of combining all random effects design matrices
associated with parameter top
.
model.matrix(which = "random", top = NULL, random = NULL)
returns the result of combining all random effects design matrices
and permuting the columns to obtain a convenient ordering of
random effect coefficients.
(Coefficients are sorted by relation to a common random vector.
Random vectors are sorted by relation to a common covariance matrix.)
None of these “combined” design matrices possesses attributes
assign
and contrasts
.
A (sparse) dgCMatrix or a traditional
(dense) matrix, with attributes assign
and contrasts
except in special cases; see ‘Details’.
The generic function model.matrix
.
Returns the number of observations of disease incidence that were used in estimation of a model. This number excludes missing values and observations not belonging to a fitting window, which, despite being preserved in model objects, do not affect estimation.
## S3 method for class 'egf' nobs(object, ...)
## S3 method for class 'egf' nobs(object, ...)
object |
an |
... |
unused optional arguments. |
An integer.
The generic function nobs
.
A method for printing objects of class egf
.
## S3 method for class 'egf' plot(x, type = c("interval", "cumulative", "rt"), time_as = c("Date", "numeric"), delta = 1, log = TRUE, zero = NA, show_predict = TRUE, show_doubling = FALSE, level = 0.95, control = egf_control_plot(), cache = NULL, plot = TRUE, subset = NULL, order = NULL, xlim = NULL, ylim = NULL, main = NULL, sub = NULL, xlab = NULL, ylab = NULL, ...)
## S3 method for class 'egf' plot(x, type = c("interval", "cumulative", "rt"), time_as = c("Date", "numeric"), delta = 1, log = TRUE, zero = NA, show_predict = TRUE, show_doubling = FALSE, level = 0.95, control = egf_control_plot(), cache = NULL, plot = TRUE, subset = NULL, order = NULL, xlim = NULL, ylim = NULL, main = NULL, sub = NULL, xlab = NULL, ylab = NULL, ...)
x |
an |
type |
a character string indicating a type of plot. The options are:
interval incidence ( |
time_as |
a character string indicating how numeric times are displayed
on the bottom axis. The options are:
as is ( |
delta |
a positive number indicating a step size on the time axis.
Predicted curves are evaluated on a grid with this spacing.
When |
log |
a logical. If |
zero |
a positive number indicating a line on which to plot zeros when
|
show_predict |
an integer flag: 2 is to draw predicted curves with confidence bands, 1 is draw predicted curves only, 0 is to draw neither. |
show_doubling |
an integer flag: 2 is to print initial doubling time estimates in the top margin with confidence intervals, 1 is to print estimates only, 0 is to print neither. Nothing is printed for models without a well-defined initial exponential growth rate. |
level |
a number in the interval |
control |
an |
cache |
a |
plot |
a logical. If |
subset |
an index vector for the rows of
|
order |
a permutation of |
xlim , ylim
|
numeric vectors of length 2 specifying axis limits, which are
recycled for all plots.
If |
main , sub , xlab , ylab
|
character or expression vectors or ( |
... |
unused optional arguments. |
Computation of fitted and predicted values and standard errors is performed before any plots are created. To avoid waste of computation time, cached computations are returned even if an error is thrown during plotting. To ensure that the cache is preserved, assign the result of the function call to a name:
cache <- plot(x, \dots)
.
Caching functionality must be used with care, as mismatch between
x
and cache
will not be detected. Constructions such
as plot(y, cache = plot(x, ...), ...)
, where x
and y
are different egf
objects, should not be expected
to produce correct results.
A data frame inheriting from class plot.egf
.
If argument cache
was supplied in the function call,
then this data frame is the result of augmenting cache
with any new computations.
The generic function plot
.
example("egf", package = "epigrowthfit") l <- list(legend = list(cex = 0.8), value = list(cex = 0.8, font = 2), ci = list(cex = 0.8)) control <- egf_control_plot(doubling = l) op <- par(mar = c(3.5, 5, 5, 1)) plot(m1, type = "interval", show_predict = 2L, show_doubling = 2L, control = control) plot(m1, type = "cumulative", main = "Fitted exponential model", sub = quote(paste("Country", country))) par(op) op <- par(mar = c(3.5, 9.5, 5, 1)) plot(m1, type = "rt", subset = quote(country %in% LETTERS[4:6])) par(op)
example("egf", package = "epigrowthfit") l <- list(legend = list(cex = 0.8), value = list(cex = 0.8, font = 2), ci = list(cex = 0.8)) control <- egf_control_plot(doubling = l) op <- par(mar = c(3.5, 5, 5, 1)) plot(m1, type = "interval", show_predict = 2L, show_doubling = 2L, control = control) plot(m1, type = "cumulative", main = "Fitted exponential model", sub = quote(paste("Country", country))) par(op) op <- par(mar = c(3.5, 9.5, 5, 1)) plot(m1, type = "rt", subset = quote(country %in% LETTERS[4:6])) par(op)
Computes predicted values of top level nonlinear model parameters. These are conditional on an estimated nonlinear mixed effects model and, optionally, new data.
## S3 method for class 'egf' predict(object, newdata = NULL, class = FALSE, se = FALSE, ...)
## S3 method for class 'egf' predict(object, newdata = NULL, class = FALSE, se = FALSE, ...)
object |
an |
newdata |
a data frame containing variables to replace those in the model frame. The default is to use the model frame as is, and currently that is the only implemented behaviour. |
class |
a logical. If |
se |
a logical. If |
... |
additional arguments passed from or to other methods. |
A numeric matrix containing predicted values.
Users can pass class = TRUE
to obtain an augmented result.
Thus, alternatively:
A data frame inheriting from class predict.egf
, with variables:
top |
top level nonlinear model parameter, from
|
ts |
time series, from
|
window |
fitting window, from
|
value |
predicted value. |
se |
approximate delta method standard error (only if requested). |
The generic function predict
.
A method for printing objects of class egf
.
## S3 method for class 'egf' print(x, width = 0.9 * getOption("width"), indent = 2L, ...)
## S3 method for class 'egf' print(x, width = 0.9 * getOption("width"), indent = 2L, ...)
x |
an |
width |
an integer width for header text. |
indent |
an integer indent for body text. |
... |
unused optional arguments. |
The argument x
, unchanged but invisible.
The generic function print
.
Computes univariate likelihood profiles of fixed effect coefficients, random effect covariance parameters, and linear combinations thereof, including population fitted values.
## S3 method for class 'egf' profile(fitted, level = 0.95, A = seq_along(par), grid = 12L, parallel = egf_parallel(), trace = FALSE, top = egf_top(fitted), subset = NULL, select = NULL, ...) ## S3 method for class 'profile.egf' confint(object, parm = seq_along(object), level = attr(object, "level"), class = FALSE, ...) ## S3 method for class 'profile.egf' plot(x, parm = seq_along(x), level = attr(x, "level"), type = c("z", "abs(z)", "z^2"), ...)
## S3 method for class 'egf' profile(fitted, level = 0.95, A = seq_along(par), grid = 12L, parallel = egf_parallel(), trace = FALSE, top = egf_top(fitted), subset = NULL, select = NULL, ...) ## S3 method for class 'profile.egf' confint(object, parm = seq_along(object), level = attr(object, "level"), class = FALSE, ...) ## S3 method for class 'profile.egf' plot(x, parm = seq_along(x), level = attr(x, "level"), type = c("z", "abs(z)", "z^2"), ...)
fitted |
an |
level |
a number in the interval |
A |
a numeric matrix with |
grid |
a positive integer. Step sizes chosen adaptively by
|
parallel |
an |
trace |
a logical. If |
top |
a subset of |
subset , select
|
index vectors for the rows and columns of
|
... |
additional arguments passed from or to other methods. |
object , x
|
a |
parm |
a valid index vector for |
class |
a logical. If |
type |
a character string indicating which of |
Computation of likelihood profiles is typically expensive, requiring
estimation of many restricted models.
It is parallelized at the C++ level when there is OpenMP
support and fitted[["control"]][["omp_num_threads"]]
is set
to an integer greater than 1. When there is no OpenMP support, it
can still be parallelized at the R level with appropriate setting
of argument parallel
.
A list of length nrow(A)
inheriting from classes
profile.egf
and profile
. Each element is a data frame
specifying a profile, with two variables:
z |
a numeric vector containing profile |
par.vals |
a numeric matrix with one column containing values of the linear
combination specified by |
The confidence level level
is preserved as an attribute.
The generic function profile
.
The more basic “next” method for generic function
plot
, namely plot.profile
.
example("egf", package = "epigrowthfit") zz <- profile(m1, A = NULL, top = "log(r)", subset = quote(country == "A" & wave == 1)) str(zz) confint(zz, class = TRUE) pty <- c("z", "abs(z)", "z^2") bty <- c("l", "u", "u") for (i in 1:3) plot(zz, type = pty[i], bty = bty[i], las = 1)
example("egf", package = "epigrowthfit") zz <- profile(m1, A = NULL, top = "log(r)", subset = quote(country == "A" & wave == 1)) str(zz) confint(zz, class = TRUE) pty <- c("z", "abs(z)", "z^2") bty <- c("l", "u", "u") for (i in 1:3) plot(zz, type = pty[i], bty = bty[i], las = 1)
Computes the basic reproduction number as a function of the initial exponential growth rate, conditional on a binned generation interval distribution.
R0(r, breaks, probs)
R0(r, breaks, probs)
r |
a non-negative numeric vector listing initial exponential growth rates. |
breaks |
an increasing numeric vector of length 2 or greater listing break
points in the support of the generation interval distribution,
in reciprocal units of |
probs |
a numeric vector of length |
A numeric vector listing basic reproduction numbers.
For an initial exponential growth rate r
,
the basic reproduction number is computed as
r / sum(probs * (exp(-r * breaks[-n]) - exp(-r * breaks[-1L])) / (breaks[-1L] - breaks[-n])) ,
where n = length(breaks)
.
Wallinga, J. & Lipsitch M. (2007). How generation intervals shape the relationship between growth rates and reproductive numbers. Proceedings of the Royal Society B: Biological Sciences, 274(1609), 599-604. doi:10.1098/rspb.2006.3754
r <- seq(0, 1, 0.02) breaks <- seq(0, 20, 1) probs <- diff(pgamma(breaks, shape = 1, scale = 2.5)) plot(r, R0(r, breaks, probs), las = 1, xlab = "initial exponential growth rate", ylab = "basic reproduction number")
r <- seq(0, 1, 0.02) breaks <- seq(0, 20, 1) probs <- diff(pgamma(breaks, shape = 1, scale = 2.5)) plot(r, R0(r, breaks, probs), las = 1, xlab = "initial exponential growth rate", ylab = "basic reproduction number")
Extracts from a model object details about the random effect
coefficients, namely segment b
of the bottom level
parameter vector.
## S3 method for class 'egf' ranef(object, makeSigma = FALSE, ...)
## S3 method for class 'egf' ranef(object, makeSigma = FALSE, ...)
object |
an |
makeSigma |
a logical flag. If |
... |
unused optional arguments. |
A data frame with one row per coefficient and variables:
cov |
label for a covariance matrix.
This is the interaction of |
vec |
label for a random vector.
This is the interaction of |
bottom |
label for a bottom level mixed effects model parameter,
in this case for a random effect coefficient;
this is a string with format |
top |
name of the top level nonlinear model parameter whose
fitted value is a function of |
term , group
|
term from the random effects component of the mixed effects
model formula for parameter |
level |
level of the factor or interaction indicated by |
colname |
column name in the random effects design matrix
|
value |
random effect conditional mode (unit variance scale),
from segment |
If makeSigma = TRUE
, then the result has attribute Sigma
,
a list of covariance matrices corresponding to the levels of variable
cov
.
The generic function ranef
.
Simulates incidence data conditional on a fitted nonlinear mixed effects model of epidemic growth. Optionally re-estimates the model given the simulated data, thus generating samples from the conditional distribution of the bottom level parameter vector.
## S3 method for class 'egf' simulate(object, nsim = 1, seed = NULL, bootstrap = FALSE, control = list(), parallel = egf_parallel(), trace = FALSE, ...)
## S3 method for class 'egf' simulate(object, nsim = 1, seed = NULL, bootstrap = FALSE, control = list(), parallel = egf_parallel(), trace = FALSE, ...)
object |
an |
nsim |
a positive integer indicating a number of replications. |
seed |
an integer used to set the RNG state before simulation or,
otherwise, |
bootstrap |
a logical. If |
control |
passed to |
parallel |
an |
trace |
a logical. If |
... |
additional arguments passed from or to other methods. |
Bootstrap optimizations are typically expensive for nontrivial models.
They are parallelized at the C++ level when there is OpenMP
support and object[["control"]][["omp_num_threads"]]
is set
to an integer greater than 1. When there is no OpenMP support, they
can still be parallelized at the R level with appropriate setting
of argument parallel
.
Arguments control
, parallel
, and trace
are unused
when bootstrap = FALSE
.
A list inheriting from class simulate.egf
, with elements:
simulation |
a data frame containing simulated incidence data. It has variables
|
bootstrap |
a numeric matrix with |
Attribute RNGstate
preserves the RNG state prior to simulation,
making the result reproducible.
The generic function simulate
.
example("egf", package = "epigrowthfit") zz <- simulate(m2, nsim = 6L, seed = 181952L, bootstrap = TRUE) str(zz) matplot(t(zz[["bootstrap"]][!m2[["random"]], ]), type = "o", las = 1, xlab = "simulation", ylab = "value")
example("egf", package = "epigrowthfit") zz <- simulate(m2, nsim = 6L, seed = 181952L, bootstrap = TRUE) str(zz) matplot(t(zz[["bootstrap"]][!m2[["random"]], ]), type = "o", las = 1, xlab = "simulation", ylab = "value")
Simulates equally spaced incidence time series according to a specified
nonlinear model. Top level nonlinear model parameters vary between
time series according to a fixed intercept model ~ts
or random
intercept model ~(1 | ts)
.
## S3 method for class 'egf_model' simulate(object, nsim = 1, seed = NULL, mu, Sigma = NULL, tol = 1e-06, cstart = 0, tend = 100, ...)
## S3 method for class 'egf_model' simulate(object, nsim = 1, seed = NULL, mu, Sigma = NULL, tol = 1e-06, cstart = 0, tend = 100, ...)
object |
an |
nsim |
a positive integer indicating a number of time series. |
seed |
an integer used to set the RNG state before simulation or,
otherwise, |
mu |
a numeric vector listing means across time series of top level
nonlinear model parameters (link scale). It is assumed that
elements are ordered as |
Sigma |
a real, symmetric positive definite matrix to be used as the
covariance matrix corresponding to |
tol |
a non-negative number indicating a tolerance for indefinite
|
cstart |
a number indicating a threshold value of cumulative incidence. Left endpoints of suggested fitting windows are those times when cumulative incidence first exceeds this threshold. |
tend |
a positive number. Simulated time series run from time 0 to time
|
... |
unused optional arguments. |
A list inheriting from class simulate.egf_model
, with elements:
model |
copy of argument |
formula_ts |
a formula, always |
formula_windows |
a formula, always |
formula_parameters |
a formula specifying the generative model.
If |
data_ts |
a data frame with variables |
data_windows |
a data frame with |
init |
a named list of the form |
Y |
a numeric matrix with |
call |
the call to |
Attribute RNGstate
preserves the RNG state prior to simulation,
making the result reproducible.
The generic function simulate
.
r <- 0.04 c0 <- 400 s <- 0.2 mu <- log(c(r, c0)) Sigma <- diag(rep.int(s^2, length(mu))) zz <- simulate(object = egf_model(curve = "exponential", family = "pois"), nsim = 20L, seed = 202737L, mu = mu, Sigma = Sigma, cstart = 10) str(zz) mm <- egf(zz) (pp <- cbind(actual = coef(zz), fitted = coef(mm)))
r <- 0.04 c0 <- 400 s <- 0.2 mu <- log(c(r, c0)) Sigma <- diag(rep.int(s^2, length(mu))) zz <- simulate(object = egf_model(curve = "exponential", family = "pois"), nsim = 20L, seed = 202737L, mu = mu, Sigma = Sigma, cstart = 10) str(zz) mm <- egf(zz) (pp <- cbind(actual = coef(zz), fitted = coef(mm)))
Summarizes fitted values of top level nonlinear model parameters and gathers diagnostic information that can be used to quickly assess convergence of the optimizer.
## S3 method for class 'egf' summary(object, ...)
## S3 method for class 'egf' summary(object, ...)
object |
an |
... |
additional arguments passed from or to other methods. |
A list inheriting from class summary.egf
, with elements:
fitted |
a numeric matrix. Each column is the result of applying
|
convergence |
an integer code returned by the optimizer, with 0 indicating successful convergence within the specified absolute or relative tolerance. |
value , gradient
|
numeric vectors giving the value and gradient of the negative log marginal likelihood function at the parameter vector returned by the optimizer. |
hessian |
a logical flag indicating whether the Hessian matrix of the
negative log marginal likelihood function is positive definite
at the parameter vector returned by the optimizer. |
The generic function summary
.
example("egf", package = "epigrowthfit") zz <- summary(m1) str(zz)
example("egf", package = "epigrowthfit") zz <- summary(m1) str(zz)
Extracts the terms
object
corresponding to a top level nonlinear model parameter.
## S3 method for class 'egf' terms(x, top = egf_top(x), ...)
## S3 method for class 'egf' terms(x, top = egf_top(x), ...)
x |
an |
top |
a character string specifying a top level nonlinear model parameter. |
... |
unused optional arguments. |
A terms
object.
The generic function terms
.
Computes characteristic time scales corresponding to exponential growth rates.
timescale(r, units)
timescale(r, units)
r |
a non-negative numeric vector listing exponential growth rates. |
units |
a character string indicating units for the result. If missing, then the result is “unitless”. |
1/r
, as a difftime
if units
is not missing.
r <- 10^(-2:0) units <- "days" stopifnot(all.equal(timescale(r), 1 / r), all.equal(timescale(r, units), .difftime(1 / r, units)))
r <- 10^(-2:0) units <- "days" stopifnot(all.equal(timescale(r), 1 / r), all.equal(timescale(r, units), .difftime(1 / r, units)))
Extracts (or, if necessary, computes) the covariance matrix of
bottom level parameters beta
and theta
,
corresponding to the output of coef(object)
.
## S3 method for class 'egf' vcov(object, ...)
## S3 method for class 'egf' vcov(object, ...)
object |
an |
... |
unused optional arguments. |
If the resulting matrix is not finite or not positive definite,
then the fit specified by object
should be investigated,
as the optimizer that produced the fit may have failed to converge.
A real, symmetric matrix.
The generic function vcov
.