View source: R/export-ModelEstimation.R
fit_MAPC | R Documentation |
Fit a Bayesian multivariate age-period-cohort model, and obtain posteriors for identifiable cross-strata contrasts. The method is based on Riebler and Held (2010) \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1093/biostatistics/kxp037")}. For handling complex survey data, we follow Mercer et al. (2014) \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1016/j.spasta.2013.12.001")}, implemented using the survey package.
fit_MAPC(
data,
response,
family,
apc_format,
stratify_by,
reference_strata = NULL,
age,
period,
grid.factor = 1,
apc_prior = "rw1",
extra.fixed = NULL,
extra.random = NULL,
extra.models = NULL,
extra.hyper = NULL,
include.random = FALSE,
binomial.n = NULL,
poisson.offset = NULL,
inla_formula = NULL,
lincombs = NULL,
survey.design = NULL,
apc_hyperprior = NULL,
control.compute = list(dic = TRUE, waic = TRUE, cpo = TRUE),
verbose = FALSE
)
data |
A data frame containing the age, period, response, and stratification variables.
Age and period are assumed to be on the raw scale, not transformed to 1-indexed index columns.
Factor/character columns are handled, as long as they are properly sorted by |
response |
A string naming the response (outcome) variable in |
family |
A string indicating the likelihood family. The default is |
apc_format |
A specification of the APC structure, with options:
Note: It is also possible to specify models with only one or two time effects, by omitting the letters corresponding to the time effects to be excluded. |
stratify_by |
A string naming the column in |
reference_strata |
Level of |
age |
Name of the age variable in |
period |
Name of the period variable in |
grid.factor |
(Optional) Grid factor, defined as the ratio of age interval width to period interval width; defaults to 1. |
apc_prior |
(Optional) A string specifying the prior for the age, period, and cohort effects (e.g. |
extra.fixed |
(Optional) If desired, the user can specify additional fixed effects to be added. This is passed as a character argument,
specifying the name of the variable to be added. Multiple variables can be added by passing a character vector of names.
Defaults to |
extra.random |
(Optional) If desired, the user can specify additional random effects to be added. This is passed as a character argument,
specifying the name of the variable to be added. Multiple variables can be added by passing a character vector of names.
Defaults to |
extra.models |
(Optional) If the user specifies one or more additional random effects to be added in |
extra.hyper |
(Optional) If the user specifies one or more additional random effects to be added in |
include.random |
(Optional) Logical; if |
binomial.n |
(Optional) For the |
poisson.offset |
(Optional) For the |
inla_formula |
(Optional) If desired, the user can pass its own INLA-compatible formula to define the model. If not, a formula is generated automatically, with the models and priors defined. |
lincombs |
(Optional) If desired, the user can pass its own INLA-compatible linear combinations to be computed by the |
survey.design |
(Optional) In the case of complex survey data, explicit handling of unequal sampling probabilities can be required.
The user can pass a |
apc_hyperprior |
(Optional) If the user wants non-default hyperpriors for the time effects, this can be achieved by passing the entire
prior specification as a string. If e.g. |
control.compute |
(Optional) A list of control variables passed to the |
verbose |
(Optional) This is argument is passed along to the |
This function works as a wrapper around the inla()
-function from the INLA
package, which executes the model fitting procedures using Integrated Neste Laplace Approximations.
The returned object is of class mapc
. S3 methods are available for:
print()
: Displays a concise summary of the model, including the APC format used, CPU time,
number of estimated parameters (fixed, random, hyperparameters, linear combinations), and model fit scores (DIC, WAIC, log-score).
summary()
: Prints detailed posterior summaries of all estimated components, including fixed effects,
random effects, hyperparameters, and linear combinations, as estimated by the inla()
-function.
plot()
: Visualizes model estimates of cross-stata contrast trends, using precomputed plots stored in the object.
The available plots depends on the APC-format that was used.
You can control which effects to plot using the which
argument (e.g. which="age"
or which=c("age", "period")
).
An named list, containing the following arguments:
model_fit
An object of class "inla"
, containing posterior densities, posterior summaries, measures of model fit etc. See documentation for the inla()
-function for details.
plots
A named list of plots for each time effect. Extract them as plots\$age
/plots\$period
plots\$cohort
.
Rue, H., Martino, S., & Chopin, N. (2009). Approximate Bayesian inference for latent Gaussian models by using Integrated Nested Laplace Approximations. Journal of the Royal Statistical Society: Series B (Statistical Methodology), 71(2), 319-392. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1111/j.1467-9868.2008.00700.x")} See also https://www.r-inla.org for more information about the INLA method and software.
fit_all_MAPC
for fitting multiple models at once,
and the function inla()
from the INLA
package for the estimation machinery.
For complex survey data, see svydesign
for the creation of a survey design object which can be passed to survey.design
.
data("toy_data")
fit <- fit_MAPC(
data = toy_data,
response = count,
family = "poisson",
apc_format = "ApC",
stratify_by = education,
reference_strata = 1,
age = age,
period = period
)
# Print concise summary of the MAPC fit and the estimation procedure
print(fit)
# Plot estimated cross-strata contrast trends
plot(fit)
# Optional: view full summary of the model (can be long)
# summary(fit)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.