The Census of Governments is a complete census only in years ending in 2 and
7. Every other year is a sample, and the sample varies enormously. Neither
cog_geographic_rollup() nor cog_peer_compare()/cog_find_peers() had any
concept of "the universe": each summed or labelled whichever govids happened
to have rows and returned that with nothing distinguishing "every government
reported" from "a fifth of them did".
On the bundled fixture, Wisconsin's 608-city universe rolls up 597
governments in FY2012 and 112 in FY2019. The peer side is worse exposure, not
better: a Madison-scale cohort looks stable because Madison is large, while
governments matched to a small target sit in exactly the population band the
sample cycle hits hardest. Chilton's 15-peer cohort reports 15 of 15 in
FY2012 and 3 of 15 in FY2019.
Implements the owner's settled design: coverage = c("all", "census",
"consistent") on all three verbs, defaulting to "all" so nothing currently
calling them changes, PLUS always-on provenance$coverage carrying per-year
n_units_reporting / n_units_expected / is_census_year and
provenance$coverage_mode. cog_explain() prints a "Reporting coverage"
section. The default mode can no longer mislead silently, which is the point
-- using these verbs correctly must not require knowing the survey calendar.
Decisions worth stating:
- n_units_expected is the universe the CALLER named, not the national one.
That is what makes the ratio mean something: "597 of the 608 Wisconsin
cities you asked about". For peers it is the cohort size, counted over
peer rows only -- including the target would inflate every count by one
and make a cohort that has entirely stopped reporting look non-empty.
- The coverage table is built from the REQUESTED years, not the years
present in the result, so a year in which nothing reported still appears
with n_units_reporting = 0. A year that vanishes silently is precisely
the disclosure failure at issue.
- "census" filters years BEFORE the query, and aborts when the range holds
no census year rather than returning an empty result for a query the
caller believes they made.
- "consistent" exempts the peer-comparison target: it is the subject of the
comparison, not a member of the cohort being balanced, and dropping it
would leave nothing to compare. The summary_* quantiles are computed
AFTER the filter so they describe the cohort actually returned.
- is_census_year is documented as a statement about the survey CALENDAR,
never a claim of completeness -- FY1967 is a census year in which only 97
of Wisconsin's 608 cities report (DoD 3). n_units_reporting is the number
that tells the truth.
On cog_find_peers(), where there is no year range, coverage governs the
cohort VINTAGE: "census" snaps to the most recent census year with an
observed population, so a cohort is not built from a sample year in which
most of the candidate universe is absent. "consistent" is a comparison-time
concept and selects like "all" there, carried on the result for
cog_peer_compare().
One fix to the committed test, which was internally inconsistent. It pinned
n_units_reporting == 597 for FY2012 AND asserted that number equals a raw
cross-check that answers 595. Both numbers are right for different questions:
VERNON VILLAGE and WAUKESHA VILLAGE carry type = 3 in `long` (their
as-of-year identity, as townships) while the xwalk lists them as govs_type =
2 (their present identity, as villages) -- schema v6 made the long table's
geography present-harmonized but `type` still reads as-of-year. The rollup
counts against the requested govid set, so 597 answers "how many of the
governments I asked about reported". The cross-check now scopes to that same
universe instead of to long.type/long.fips_state; it still reads raw parquet
rather than going through the verb under test.
Suite: 670 pass / 0 fail / 2 skip (was 658/0/3). rcmdcheck clean.
The two remaining skips are #11 and #12.
104 lines
5.4 KiB
R
104 lines
5.4 KiB
R
# Madison walkthrough audit -- findings F-020 and F-023. Tracked as uscogdata#13.
|
|
# See docs/walkthroughs/FINDINGS.md in cog_explorer.
|
|
#
|
|
# The owner's settled design (2026-07-28): a `coverage` argument on
|
|
# cog_geographic_rollup(), cog_find_peers()/cog_peer_compare() and their
|
|
# cog-api equivalents --
|
|
# "all" every unit that reported that year (today's behaviour, DEFAULT)
|
|
# "census" census years only (years ending 2 or 7)
|
|
# "consistent" only units reporting in every requested year (balanced panel)
|
|
# -- PLUS always-on coverage metadata on every result regardless of mode:
|
|
# n_units_reporting, n_units_expected, is_census_year.
|
|
#
|
|
# Motivating principle: using these verbs correctly must not require the user to
|
|
# know that the Census of Governments is a complete census only in years ending
|
|
# in 2 and 7.
|
|
#
|
|
# The helper below accepts that metadata either as columns on the returned
|
|
# tibble or as a per-year table in provenance$coverage -- the design fixes the
|
|
# three field names and that they reach the caller, not the container.
|
|
|
|
wt_coverage <- function(x) {
|
|
prov <- attr(x, "provenance")
|
|
cov <- prov$coverage
|
|
if (is.null(cov)) {
|
|
needed <- c("year", "n_units_reporting", "n_units_expected", "is_census_year")
|
|
expect_true(all(needed %in% names(x)))
|
|
cov <- unique(x[, needed])
|
|
}
|
|
cov[order(cov$year), ]
|
|
}
|
|
|
|
test_that("multi-government aggregates disclose reporting coverage on every result", {
|
|
|
|
# -- F-020: geographic rollups -------------------------------------------
|
|
# Wisconsin's city/village universe is 608 governments. On the bundled
|
|
# fixture, FY2012 (a census year) has 597 of them reporting while FY2019 and
|
|
# FY2020 (sample years) have 112 and 114 -- an 18%-98% swing that today's
|
|
# return value says nothing about. Counts cross-checked against the raw
|
|
# corpus, not through cog_geographic_rollup(), which is under test.
|
|
wi <- cog_gov_search(name = NULL, state = "WI", type = "city")
|
|
expect_equal(nrow(wi), 608L)
|
|
|
|
roll <- cog_geographic_rollup(govids = list(city = wi$canonical_govid),
|
|
category = NULL, years = c(2011L, 2012L, 2019L, 2020L))
|
|
cov <- wt_coverage(roll)
|
|
|
|
expect_equal(cov$n_units_expected, rep(608L, 4L))
|
|
expect_equal(cov$n_units_reporting, c(152L, 597L, 112L, 114L))
|
|
expect_equal(cov$is_census_year, c(FALSE, TRUE, FALSE, FALSE))
|
|
|
|
# Cross-check against the raw partitions, scoped to the SAME universe the
|
|
# rollup was given -- the 608 govids above. Scoping instead on the long
|
|
# table's own `type`/`fips_state` asks a different question and answers 595:
|
|
# VERNON VILLAGE and WAUKESHA VILLAGE carry type = 3 there (their as-of-year
|
|
# identity, when they were townships) while the xwalk lists them as
|
|
# govs_type = 2 (their present identity, as villages). Schema v6 made the
|
|
# long table's geography present-harmonized and moved as-of-year to the
|
|
# *_asof columns, but `type` still reads as-of-year -- see .validate_schema()
|
|
# in R/manifest.R. n_units_reporting counts against the requested universe,
|
|
# so 597 is the number that answers "how many of the governments I asked
|
|
# about reported".
|
|
raw_2012 <- wt_raw_query(paste0(
|
|
"SELECT COUNT(DISTINCT canonical_govid) n FROM read_parquet('", wt_corpus_glob(), "') ",
|
|
"WHERE year = 2012 AND LEFT(item_code, 1) IN ('E','F','G') AND NOT is_aggregate ",
|
|
"AND canonical_govid IN (",
|
|
paste0("'", wi$canonical_govid, "'", collapse = ","), ")"))
|
|
expect_equal(cov$n_units_reporting[cov$year == 2012], as.integer(raw_2012$n[[1]]))
|
|
|
|
# -- F-023: peer cohorts --------------------------------------------------
|
|
# CHILTON CITY, WI (ACS population 4,017): a 15-peer cohort fixed at FY2012
|
|
# reports 15 of 15 in FY2012 and only 3 of 15 in FY2019 and FY2020. Nothing
|
|
# in cog_peer_compare()'s return distinguishes those years today.
|
|
chilton <- "552015177095"
|
|
peers <- cog_find_peers(chilton, year = 2012L, max_peers = 15L)
|
|
expect_equal(nrow(peers), 15L)
|
|
|
|
cmp <- cog_peer_compare(target_govid = chilton, peers = peers, category = NULL,
|
|
years = c(2012L, 2019L, 2020L), per_capita = TRUE)
|
|
cov_peers <- wt_coverage(cmp)
|
|
expect_equal(cov_peers$n_units_expected, rep(15L, 3L))
|
|
expect_equal(cov_peers$n_units_reporting, c(15L, 3L, 3L))
|
|
expect_equal(cov_peers$is_census_year, c(TRUE, FALSE, FALSE))
|
|
|
|
# -- the three coverage modes --------------------------------------------
|
|
expect_equal(attr(cog_peer_compare(target_govid = chilton, peers = peers,
|
|
category = NULL, years = c(2012L, 2019L, 2020L),
|
|
per_capita = TRUE),
|
|
"provenance")$coverage_mode, "all") # unchanged default
|
|
|
|
consistent <- cog_peer_compare(target_govid = chilton, peers = peers,
|
|
category = NULL, years = c(2012L, 2019L, 2020L),
|
|
per_capita = TRUE, coverage = "consistent")
|
|
n_by_year <- tapply(consistent$canonical_govid[consistent$role == "peer"],
|
|
consistent$year[consistent$role == "peer"],
|
|
function(g) length(unique(g)))
|
|
expect_equal(unname(as.integer(n_by_year)), c(3L, 3L, 3L)) # balanced panel
|
|
|
|
census_only <- cog_geographic_rollup(govids = list(city = wi$canonical_govid),
|
|
category = NULL,
|
|
years = c(2011L, 2012L, 2019L, 2020L),
|
|
coverage = "census")
|
|
expect_equal(sort(unique(census_only$year)), 2012)
|
|
})
|