plot_lincombs: Plot Linear Combinations of Age-Period-Cohort Effects by...

View source: R/export-PlottingModels.R

plot_lincombsR Documentation

Plot Linear Combinations of Age-Period-Cohort Effects by Strata

Description

Generates ggplot2 line plots of estimated linear combinations for age, period, and/or cohort effects from an INLA fit, stratified by a factor. Returns a named list of ggplot objects for each requested effect.

Usage

plot_lincombs(
  inla_fit,
  apc_model,
  data,
  strata_col,
  reference_level,
  family = NULL,
  age_ind = "age",
  period_ind = "period",
  cohort_ind = "cohort",
  age_title = NULL,
  period_title = NULL,
  cohort_title = NULL,
  y_lab = NULL,
  age_vals = NULL,
  period_vals = NULL,
  cohort_vals = NULL,
  age_breaks = NULL,
  age_limits = NULL,
  period_breaks = NULL,
  period_limits = NULL,
  cohort_breaks = NULL,
  cohort_limits = NULL,
  PDF_export = FALSE
)

Arguments

inla_fit

An object returned by the inla()-function, containing the data frame summary.lincomb.derived, which holds the posterior summaries of the cross strata contrasts from the MAPC model. This function assumes that the rownames of the linear combinations are of the specific format produced by generate_apc_lincombs.

apc_model

Character string indicating the configuration of shared vs. stratum-specific time effects in the model.

data

The data frame used to fit inla_fit, containing columns for age, period, cohort, and the stratification variable.

strata_col

Character name of the factor column in data defining strata.

reference_level

Character value of strata_col to use as the reference.

family

Optional character; if NULL, y_lab defaults to "Mean differences". If "gaussian", same; if "poisson", "Log mean ratio"; if "binomial", "Log odds ratio".

age_ind

Character name of the age variable in data (default "age").

period_ind

Character name of the period variable in data (default "period").

cohort_ind

Character name of the cohort variable in data (default "cohort").

age_title

Optional plot title for the age effect.

period_title

Optional plot title for the period effect.

cohort_title

Optional plot title for the cohort effect.

y_lab

Optional y-axis label; if NULL, set according to family.

age_vals

Optional numeric vector of x-values for age; defaults to min(data\$age):max(data\$age).

period_vals

Optional numeric vector of x-values for period; defaults to min(data\$period):max(data\$period).

cohort_vals

Optional numeric vector of x-values for cohort; defaults to min(data\$cohort):max(data\$cohort).

age_breaks

Optional vector of breaks for the age plot x-axis.

age_limits

Optional numeric vector of length 2 giving x-axis limits for age.

period_breaks

Optional vector of breaks for the period plot x-axis.

period_limits

Optional numeric vector of length 2 giving x-axis limits for period.

cohort_breaks

Optional vector of breaks for the cohort plot x-axis.

cohort_limits

Optional numeric vector of length 2 giving x-axis limits for cohort.

PDF_export

Logical; if TRUE, use larger font sizes/layout for PDF output.

Value

A named list of ggplot objects. Elements are "age", "period", and/or "cohort" depending on apc_model.

Examples

if (requireNamespace("INLA", quietly = TRUE)) {
  # Load toy dataset
  data("toy_data")

  # Filter away unobserved cohorts (see plot_missing_data() function):
  require(dplyr)
  toy_data.f <- toy_data %>% filter(sex == "female") %>% subset(cohort > 1931)

  # Load precomputed 'mapc' object
  apC_fit.f <- readRDS(system.file("extdata", "quickstart-apC_fit_f.rds", package = "MAPCtools"))

  # Extract INLA object:
  apC_fit.inla <- apC_fit.f$model_fit
  apC_plots <- plot_lincombs(
    inla_fit    = apC_fit.inla,
    apc_model   = "apC",
    data        = toy_data.f,
    strata_col  = "education",
    reference_level = "1",
    family      = "poisson",

  )
  # Display the age effect plot
  print(apC_plots$age)
  # Display the period effect plot
  print(apC_plots$period)
}


MAPCtools documentation built on June 25, 2025, 5:09 p.m.