Title: | Bayesian Age-Period-Cohort Modeling and Prediction |
---|---|
Description: | Bayesian Age-Period-Cohort Modeling and Prediction using efficient Markov Chain Monte Carlo Methods. This is the R version of the previous BAMP software as described in Volker Schmid and Leonhard Held (2007) <DOI:10.18637/jss.v021.i08> Bayesian Age-Period-Cohort Modeling and Prediction - BAMP, Journal of Statistical Software 21:8. This package includes checks of convergence using Gelman's R. |
Authors: | Volker Schmid [aut, cre], Florian Geressen [ctb], Leonhard Held [ctb], Evi Rainer [ctb] |
Maintainer: | Volker Schmid <[email protected]> |
License: | GPL-3 |
Version: | 2.1.4 |
Built: | 2024-11-07 16:21:13 UTC |
Source: | https://github.com/volkerschmid/bamp |
Class for (Bayesian) age-period-cohort objects
apc()
apc()
bamp
will return an object of class apc. Available functions are
plot.apc
plots main effects
print.apc
print summary of model and effects
effects.apc
extract effects (mean, median and quantiles)
apc class
A dataset containing case counts and population numbers in eight age groups for ten years. Each age group consists of five years.
data(apc)
data(apc)
population
: matrix of population data
cases
: matrix of case counts
cov_p
: covariate for period
cov_c
: covariate for cohort
This functions simulates a data set of cases on the Lexis diagram from given age, period and cohort effects. Population numbers have to be given; can be one number for all age group/period combinations.
apcSimulate(intercept, age, period, cohort, periods_per_agegroup, population)
apcSimulate(intercept, age, period, cohort, periods_per_agegroup, population)
intercept |
Intercept |
age |
Vector of effect for age groups |
period |
Vector of effects for periods |
cohort |
Vector of effect for cohorts |
periods_per_agegroup |
Periods per age group |
population |
Population number. Either a matrix or a scalar. |
List with number of cases (matrix) and population numbers (matrix).
vignette("simulation", package = "bamp")
age=sqrt(seq(5,0,length=10)); age<-1-age-mean(age) period=15:1; period[8:15]<-8:15; period<-period/6; period<-period-mean(period) periods_per_agegroup=5; number_of_cohorts <- periods_per_agegroup*(10-1)+15 cohort<-rep(0,60); cohort[1:10]<-10:1; cohort[41:60]<- -(1:20)/2; cohort<-cohort/10; cohort<-cohort-mean(cohort) simdata<-apcSimulate(-5, age, period, cohort, periods_per_agegroup, 1e6) par(mfrow=c(3,1)) plot(age, type="l") plot(period, type="l") plot(cohort, type="l") ## Not run: simmod <- bamp(cases = simdata$cases, population = simdata$population, age = "rw1", period = "rw1", cohort = "rw1", periods_per_agegroup =periods_per_agegroup) plot(simmod) ## End(Not run)
age=sqrt(seq(5,0,length=10)); age<-1-age-mean(age) period=15:1; period[8:15]<-8:15; period<-period/6; period<-period-mean(period) periods_per_agegroup=5; number_of_cohorts <- periods_per_agegroup*(10-1)+15 cohort<-rep(0,60); cohort[1:10]<-10:1; cohort[41:60]<- -(1:20)/2; cohort<-cohort/10; cohort<-cohort-mean(cohort) simdata<-apcSimulate(-5, age, period, cohort, periods_per_agegroup, 1e6) par(mfrow=c(3,1)) plot(age, type="l") plot(period, type="l") plot(cohort, type="l") ## Not run: simmod <- bamp(cases = simdata$cases, population = simdata$population, age = "rw1", period = "rw1", cohort = "rw1", periods_per_agegroup =periods_per_agegroup) plot(simmod) ## End(Not run)
Bayesian Age-Period-Cohort Modeling for the analyze of incidence or mortality data on the Lexis diagram.
For each pixel in the Lexis diagram (that is for a specific age group and specific period) data must be available on the number of persons under risk (population number) and the number of disease cases (typically cancer incidence or mortality).
A hierarchical model is assumed with a binomial model in the first-stage. As smoothing priors for the age, period and cohort parameters random walks of first and second order (RW1 or RW2) available.
Deviance information criterion and effective number of parameters is computed for model comparison.
Note that there is a non-identifiability in the likelihood of the APC-model, see e.g. Clayton and Schifflers (1987, DOI:10.1002/sim.4780060406), which indices some problems in interpreting the latent effects. Only for RW1 model, the parameters are (weakly) identifiable.
Period and age groups do not need to be on the same grid, for example periods can be in one year intervals and age groups in five year intervals.
Additionally to the model described in Knorr-Held and Rainer (2001, DOI:10.1093/biostatistics/2.1.109), bamp
can handle
AP and AC models,
models with and without global heterogeneity parameter (overdispersion),
models with additional age, period and/or cohort heterogeneity,
additional covariates.
bamp( cases, population, age, period, cohort, overdisp = FALSE, period_covariate = NULL, cohort_covariate = NULL, periods_per_agegroup, mcmc.options = list(number_of_iterations = 1e+05, burn_in = 50000, step = 50, tuning = 500), hyperpar = list(age = c(1, 0.5), period = c(1, 5e-04), cohort = c(1, 5e-04), overdisp = c(1, 0.05)), dic = TRUE, parallel = TRUE, verbose = FALSE )
bamp( cases, population, age, period, cohort, overdisp = FALSE, period_covariate = NULL, cohort_covariate = NULL, periods_per_agegroup, mcmc.options = list(number_of_iterations = 1e+05, burn_in = 50000, step = 50, tuning = 500), hyperpar = list(age = c(1, 0.5), period = c(1, 5e-04), cohort = c(1, 5e-04), overdisp = c(1, 0.05)), dic = TRUE, parallel = TRUE, verbose = FALSE )
cases |
number of cases |
population |
population number |
age |
prior for age groups ("rw1", "rw2", "rw1+het", "rw2+het", " ") |
period |
prior for periods ("rw1", "rw2", "rw1+het", "rw2+het", " ") |
cohort |
prior for cohorts ("rw1", "rw2", "rw1+het", "rw2+het", " ") |
overdisp |
logical, add overdispersion to model |
period_covariate |
covariate for period |
cohort_covariate |
covariate for cohort |
periods_per_agegroup |
periods per age group |
mcmc.options |
list of options for MCMC.
|
hyperpar |
list of hyper parameters. The hyper prior for the precision (inverse variance) in the random walk priors is a Gamma distribution with parameters |
dic |
logical. If true. DIC will be computed |
parallel |
logical, should computation be done in parallel. This uses the parallel package, which does not allow parallel computing under Windows. |
verbose |
verbose mode |
This functions returns an apc
object.
Only samples from the posterior are computed, point estimates and credible intervals will be computed in effects.apc
, print.apc
and plot.apc
.
predict_apc
can be used for for prediction of the future rates and number of cases and for a retrospective prediction for model checking.
vignette("modeling", package = "bamp")
## Not run: data(apc) model <- bamp(cases, population, age="rw1", period="rw1", cohort="rw1", periods_per_agegroup = 5) ## End(Not run)
## Not run: data(apc) model <- bamp(cases, population, age="rw1", period="rw1", cohort="rw1", periods_per_agegroup = 5) ## End(Not run)
This functions uses Gelman and Rubin's R to check convergence for all main parameters.
All parameters should have R<1.1.
bamp
runs at least four MCMC chains by default (more if parallel is more than four).
checkConvergence(x, info = FALSE, level = 2, auto = FALSE)
checkConvergence(x, info = FALSE, level = 2, auto = FALSE)
x |
An apc object |
info |
logical; print more information |
level |
level of check; 1 uses point point estimation, 2 uses upper C.I. |
auto |
logical; should be TRUE if called automatically from |
logical; TRUE if check is fine.
## Not run: data(apc) model <- bamp(cases, population, age="rw1", period="rw1", cohort="rw1", periods_per_agegroup = 5) checkConvergence(model) ## End(Not run)
## Not run: data(apc) model <- bamp(cases, population, age="rw1", period="rw1", cohort="rw1", periods_per_agegroup = 5) checkConvergence(model) ## End(Not run)
Compute cohort index from age and period index
coh(agegroup, period, noa, periods_per_agegroup)
coh(agegroup, period, noa, periods_per_agegroup)
agegroup |
age group index |
period |
period index |
noa |
number of age groups in total |
periods_per_agegroup |
periods per age group |
cohort index
# last agegroup in first period equals first cohort coh(10, 1, 10, 5) # first agegroup in last period equals last cohort coh(1, 8, 10, 5)
# last agegroup in first period equals first cohort coh(10, 1, 10, 5) # first agegroup in last period equals last cohort coh(1, 8, 10, 5)
Effects from Fitted APC Model
## S3 method for class 'apc' effects(object, mean = FALSE, quantiles = 0.5, update = FALSE, ...)
## S3 method for class 'apc' effects(object, mean = FALSE, quantiles = 0.5, update = FALSE, ...)
object |
an apc object |
mean |
logical. If TRUE, mean effects are computed |
quantiles |
Scalar or vector of quantiles to compute (only if mean=FALSE) |
update |
logical. If TRUE, the apc object including the effects is returned |
... |
Additional arguments will be ignored |
List of age, period, cohort effects or apc object including effects (if update=TRUE)
## Not run: data(apc) model <- bamp(cases, population, age="rw1", period="rw1", cohort="rw1", periods_per_agegroup = 5) effects(model) ## End(Not run)
## Not run: data(apc) model <- bamp(cases, population, age="rw1", period="rw1", cohort="rw1", periods_per_agegroup = 5) effects(model) ## End(Not run)
Plot apc object
## S3 method for class 'apc' plot(x, quantiles = c(0.05, 0.5, 0.95), ...)
## S3 method for class 'apc' plot(x, quantiles = c(0.05, 0.5, 0.95), ...)
x |
apc object |
quantiles |
quantiles to plot. Default: |
... |
Additional arguments will be ignored |
Plot of age, period and cohort effects from apc objects. If covariates have been used for period/cohort, a second plot with covariate, absolute effect and relative effect is created. Absolute effect is relative effect times covariate.
plot
## Not run: data(apc) model <- bamp(cases, population, age="rw1", period="rw1", cohort="rw1", periods_per_agegroup = 5) plot(model) ## End(Not run)
## Not run: data(apc) model <- bamp(cases, population, age="rw1", period="rw1", cohort="rw1", periods_per_agegroup = 5) plot(model) ## End(Not run)
Prediction of rates and, if possible, cases from the Bayesian age-period-cohort model using the prior assumptions (random walks) of the model and the estimated variance of the random walk. For example, random walk of first order (rw1) for period effect predicts constant effects for future periods plus noise.
predict_apc( object, periods = 0, population = NULL, quantiles = c(0.05, 0.5, 0.95), update = FALSE )
predict_apc( object, periods = 0, population = NULL, quantiles = c(0.05, 0.5, 0.95), update = FALSE )
object |
apc object |
periods |
number of periods to predict |
population |
matrix of (predicted) population, if NULL, population data from original bamp call will be used |
quantiles |
vector of quantiles to compute |
update |
boolean. If TRUE, object will be returned with results added to the object |
This function will return predicted rates for future periods. For this, future period and cohort effects will be predicted. Further age group effects will not be predicted. The rates are random samples from the predictive distribution; number of samples is equal to number of MCMC iterations. Quantiles will be provided for convenience, but all samples are available. If population numbers are given, number of cases will also be predicted. Number of cases will not only be predicted for future periods, but also for the time periods where data are available; this can be used for model assessment.
list with quantiles of predicted probabilities (pr
), predicted cases (cases
) and predicted cases per period (cases_period
)
and a list samples with MCMC samples of pr, cases and cases_period.
If update=TRUE
, the apc object will be returned with this list (predicted) added.
vignette("prediction", package = "bamp")
## Not run: data(apc) model <- bamp(cases, population, age="rw1", period="rw1", cohort="rw1", periods_per_agegroup = 5) pred <- predict_apc(model, periods=1) plot(pred$pr[2,11,], main="Predicted rate per agegroup", ylab="p") ## End(Not run)
## Not run: data(apc) model <- bamp(cases, population, age="rw1", period="rw1", cohort="rw1", periods_per_agegroup = 5) pred <- predict_apc(model, periods=1) plot(pred$pr[2,11,], main="Predicted rate per agegroup", ylab="p") ## End(Not run)
Print apc objects
## S3 method for class 'apc' print(x, ...)
## S3 method for class 'apc' print(x, ...)
x |
apc object |
... |
additional arguments will be ignored |
## Not run: data(apc) model <- bamp(cases, population, age="rw1", period="rw1", cohort="rw1", periods_per_agegroup = 5) print(model) ## End(Not run)
## Not run: data(apc) model <- bamp(cases, population, age="rw1", period="rw1", cohort="rw1", periods_per_agegroup = 5) print(model) ## End(Not run)