Adds R/sysdata.rda with the annual-average CPIAUCSL index (1947-2026, 80 years, sourced from FRED) and an internal .inflate() helper that converts nominal amounts between two years via the ratio of CPI values. Bundling CPI in the package (rather than publishing a cpi_annual.parquet in the corpus) matches the reader-specification intent: real-dollar conversion is a verb-level option, not a corpus-level artifact, so the target_year stays flexible at query time. data-raw/cpi_annual.R carries the one-shot FRED refresh used to build sysdata.rda. Re-run when the CPI series needs to roll forward. Verified: CPI(2000)/CPI(2021) ≈ 0.635, matching the ~0.63 sanity anchor in cog_explorer's existing inflation logic. Tests: tests/testthat/test-adjust.R covers the known 2000→2021 ≈ 1.574x anchor, vectorized from_year, error cases for out-of-range years, and NA-amount preservation. 14 pass / 0 fail.
35 lines
1.4 KiB
R
35 lines
1.4 KiB
R
# data-raw/cpi_annual.R
|
|
#
|
|
# Refresh the bundled CPIAUCSL annual-average table from FRED.
|
|
# Run interactively when the CPI series needs to be extended forward:
|
|
# Rscript data-raw/cpi_annual.R
|
|
#
|
|
# Source: FRED series CPIAUCSL (Consumer Price Index for All Urban Consumers,
|
|
# All Items, 1982-84 = 100), monthly. Annual mean computed here. The bundled
|
|
# artifact is R/sysdata.rda (loaded automatically by the package).
|
|
|
|
stopifnot(requireNamespace("utils", quietly = TRUE),
|
|
requireNamespace("tibble", quietly = TRUE),
|
|
requireNamespace("usethis", quietly = TRUE))
|
|
|
|
fred_url <- "https://fred.stlouisfed.org/graph/fredgraph.csv?id=CPIAUCSL"
|
|
raw <- utils::read.csv(url(fred_url), stringsAsFactors = FALSE)
|
|
date_col <- intersect(c("observation_date", "DATE", "date"), names(raw))[1]
|
|
stopifnot(length(date_col) == 1L, !is.na(date_col))
|
|
raw$year <- as.integer(format(as.Date(raw[[date_col]]), "%Y"))
|
|
raw$CPIAUCSL <- suppressWarnings(as.numeric(raw$CPIAUCSL))
|
|
raw <- raw[!is.na(raw$CPIAUCSL), ]
|
|
|
|
annual <- stats::aggregate(
|
|
raw$CPIAUCSL, by = list(year = raw$year), FUN = mean, na.rm = TRUE
|
|
)
|
|
names(annual)[2] <- "cpi"
|
|
|
|
cpi_annual <- tibble::as_tibble(annual)
|
|
cpi_annual$cpi <- round(cpi_annual$cpi, 4)
|
|
|
|
message(sprintf("CPI range: %d-%d (%d years)",
|
|
min(cpi_annual$year), max(cpi_annual$year), nrow(cpi_annual)))
|
|
|
|
usethis::use_data(cpi_annual, internal = TRUE, overwrite = TRUE)
|