plot_deaths_fn: A function to plot the fitted and forecast number of deaths,...

View source: R/plot_deaths_fn.R

plot_deaths_fnR Documentation

A function to plot the fitted and forecast number of deaths, accompanied by credible intervals, from posterior samples generated for stochastic mortality models

Description

Plot the median fitted and forecast number of deaths, accompanied by credible intervals (user-specified level), using posterior samples stored in "fit_result" object.

Usage

plot_deaths_fn(
  result,
  expo_forecast = NULL,
  pred_int = 0.95,
  plot_type = "age",
  plot_ages = NULL,
  plot_years = NULL,
  legends = TRUE
)

Arguments

result

object of type either "fit_result" or "BayesMoFo".

expo_forecast

An optional 3-dimensional array (of dimensions p \times A \times h) containing exposure data for the forecast period. If not provided, the exposure data from the most recent year will be used for forecasting.

pred_int

A numeric value (between 0 and 1) specifying the credible level of uncertainty bands. Default is pred_int=0.95 (95\% intervals).

plot_type

A character string (c("age","time")) to indicate whether to plot by age (default) or by time/year.

plot_ages

A numeric vector specifying which range of ages to plot for visualisation. If not specified, use whatever ages that were used to fit the model (i.e. fit_result$death$ages). One panel will be constructed per age when plot_type="time", with a maximum of nine panels. If exceeded, only the first nine ages will be plotted.

plot_years

A numeric vector specifying which range of years to plot for visualisation. If not specified, use whatever years that were used to fit the model (i.e. fit_result$death$years). One panel will be constructed per year when plot_type="age", with a maximum of nine panels. If exceeded, only the first nine years will be plotted.

legends

A logical value to indicate if legends of the plots should be shown (default) or suppressed (e.g. to aid visibility).

Value

A plot illustrating the median fitted and forecast number of deaths, accompanied by credible intervals.

Examples


#load and prepare data
data("dxt_array_product");data("Ext_array_product")
death<-preparedata_fn(dxt_array_product,strat_name = c("ACI","DB","SCI"),ages=35:65)
expo<-preparedata_fn(Ext_array_product,strat_name = c("ACI","DB","SCI"),ages=35:65)

#fit any mortality model
runBayesMoFo_result<-runBayesMoFo(death=death,expo=expo,models="APCI",n_iter=1000,forecast=TRUE)

#default plot
plot_deaths_fn(runBayesMoFo_result)

#plot by age and changing pre-specified arguments 
plot_deaths_fn(runBayesMoFo_result,pred_int=0.8,plot_ages=40:60,plot_years=c(2017,2020))

#plot by time/year
plot_deaths_fn(runBayesMoFo_result,plot_type="time",plot_ages=c(40,50,60))


BayesMoFo documentation built on Aug. 11, 2025, 1:07 a.m.