as.APC.NA.df: Create NA structure across age, period and cohort groups...

View source: R/export-DataPreprocessing.R

as.APC.NA.dfR Documentation

Create NA structure across age, period and cohort groups based on strata

Description

Creates a data frame where age, period, and cohort values are placed into columns specific to their stratum (defined by stratify_var), with other strata combinations marked as NA. This structure is often useful for specific modeling approaches, like certain Age-Period-Cohort (APC) models. Optionally includes unique indices for random effects.

Usage

as.APC.NA.df(data, stratify_by, age, period, cohort, include.random = FALSE)

Arguments

data

Data frame with age, period, cohort, and stratification columns.

stratify_by

Stratification variable column. This column will be used to create the stratum-specific NA structure. It should ideally be a factor or character vector.

age

Age column in data (must be a numeric/integer column).

period

Name of the period column (must be a numeric/integer column).

cohort

Name of the cohort column (must be a numeric/integer column).

include.random

Logical. Whether to include a unique index ('random') for each combination of age, period, and stratum, potentially for use as random effect identifiers in models. Defaults to FALSE.

Value

A data frame containing the original age, period, cohort, and stratify_by columns, plus:

  • Dummy indicator columns for each level of stratify_by (e.g., Region_North, Region_South if Region was a stratifying variable).

  • Stratum-specific age, period, and cohort columns (e.g., age_Region_North, period_Region_North, cohort_Region_North), containing the respective value if the row belongs to that stratum, and NA otherwise.

  • If include.random = TRUE, a column named random with unique integer indices. The rows are ordered primarily by the stratification variable levels. This is useful for defining random components in MAPC models.


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