feat: fiscal-year periods on the verbs and survey-year helpers (#77)
`year` is the Census survey year, and Census files a government's fiscal year under survey year Y when it ended between July 1 of Y-1 and June 30 of Y, so one survey year covers different months for different governments. cog_spending(), cog_revenue() and cog_balances() gain a trailing `fiscal_period = FALSE`. With TRUE, every row gains fiscal_year_end (MMDD integer), period_start and period_end (Dates, inclusive), looked up per government-year from long.fiscal_year_end so a change of fiscal year is respected, plus provenance$transformations$fiscal_period. With the default, columns and provenance are exactly as before. cog_fiscal_periods(govid, years) returns the govid x years grid of periods; cog_survey_year(govid, date) returns the survey year whose period contains a date, using each candidate year's own fiscal_year_end and returning NA rather than borrowing another year's value. State governments, and the District of Columbia, cover the fiscal year ending in calendar year Y. That changes Alabama, Michigan (September 30) and Texas (August 31). Census's 2004 and 2006 technical documentation lists those exceptions, and the corpus agrees for survey year 2020: those states' federal aid rises with the CARES Act in 2020 while New York's and 15 large September-30 local governments' rises in 2021. D.C. rests on the documentation alone. Tests run against the bundled fixture (85 expectations). Refs #77 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Qbe6u2h83wgZdTgJgE4Xh5
This commit is contained in:
12 files changed
+899
-17
No files matched your search
+11
-3
@@ -15,7 +15,8 @@ cog_balances(
|
||||
state = NULL,
|
||||
type = NULL,
|
||||
limit = NULL,
|
||||
offset = NULL
|
||||
offset = NULL,
|
||||
fiscal_period = FALSE
|
||||
)
|
||||
}
|
||||
\arguments{
|
||||
@@ -86,6 +87,12 @@ returns everything. Cannot be combined with `recipe` -- see `offset` and
|
||||
|
||||
\item{offset}{Rows to skip before `limit` starts counting (0-based).
|
||||
Ignored if `limit` is `NULL`; defaults to `0L` when `limit` is set.}
|
||||
|
||||
\item{fiscal_period}{If `TRUE`, add `fiscal_year_end`, `period_start` and
|
||||
`period_end` to every row, as [cog_spending()] does. Holdings are a stock
|
||||
measured at the end of the fiscal year, so `period_end` is the date the
|
||||
balance describes and `period_start` is the start of that fiscal year.
|
||||
Defaults to `FALSE`, which leaves the result unchanged.}
|
||||
}
|
||||
\value{
|
||||
Tibble with columns `year`, `canonical_govid`, `gov_name`,
|
||||
@@ -94,8 +101,9 @@ Tibble with columns `year`, `canonical_govid`, `gov_name`,
|
||||
`pop_source` (when `per_capita = TRUE`), optional `amt_real` (when
|
||||
`adjust_to_year` is set), and optional `amt_per_capita_real` (only when
|
||||
**both** `per_capita = TRUE` and `adjust_to_year` are set -- there is no
|
||||
nominal per-capita column to deflate otherwise). Amounts are full US
|
||||
dollars.
|
||||
nominal per-capita column to deflate otherwise), and `fiscal_year_end`,
|
||||
`period_start` and `period_end` when `fiscal_period = TRUE`. Amounts are
|
||||
full US dollars.
|
||||
|
||||
Carries a `provenance` attribute matching
|
||||
`inst/schemas/provenance-v1.json`, whose `balance_caveats` block reports
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/fiscal.R
|
||||
\name{cog_fiscal_periods}
|
||||
\alias{cog_fiscal_periods}
|
||||
\title{Fiscal-year periods behind survey years}
|
||||
\usage{
|
||||
cog_fiscal_periods(govid, years)
|
||||
}
|
||||
\arguments{
|
||||
\item{govid}{Character vector of `canonical_govid` values, or a data frame
|
||||
with a `canonical_govid` column.}
|
||||
|
||||
\item{years}{Integer vector of survey years.}
|
||||
}
|
||||
\value{
|
||||
Tibble with one row per requested government and year, in the
|
||||
order `govid` x `years`: `canonical_govid`, `year`, `fiscal_year_end`
|
||||
(integer MMDD), `period_start` and `period_end` (Dates, inclusive).
|
||||
}
|
||||
\description{
|
||||
Returns the calendar months each survey year covers for each government.
|
||||
The corpus's `year` is the Census survey year, and a survey year covers
|
||||
different months for different governments.
|
||||
}
|
||||
\section{The survey-year rule}{
|
||||
|
||||
Census assigns each government's fiscal year to a survey year by when the
|
||||
fiscal year ended. Survey year Y holds the fiscal year that ended between
|
||||
July 1 of Y-1 and June 30 of Y. The corpus records each government-year's
|
||||
fiscal year end as `fiscal_year_end`, an MMDD integer such as `630` or
|
||||
`1231`.
|
||||
|
||||
Survey year 2017 therefore covers, for a county, city or township:
|
||||
|
||||
* `fiscal_year_end = 630` (June 30): the fiscal year ending June 30,
|
||||
2017, so July 2016 to June 2017.
|
||||
* `930` (September 30): the fiscal year ending September 30, 2016, so
|
||||
October 2015 to September 2016.
|
||||
* `1231` (December 31): the fiscal year ending December 31, 2016, so
|
||||
January to December 2016.
|
||||
|
||||
A city with a December year end and a city with a June year end are both
|
||||
"2017" in the corpus, but their figures are six months apart. A September
|
||||
year end is nine months behind a June one.
|
||||
|
||||
State governments (type 0) follow a different rule: survey year Y holds the
|
||||
fiscal year ending in calendar year Y. For Alabama and Michigan (September
|
||||
30) that means October 2016 to September 2017, and for Texas (August 31)
|
||||
September 2016 to August 2017. Census's 2004 and 2006 technical
|
||||
documentation lists these three states as exceptions to the June 30 window,
|
||||
and the corpus agrees in survey year 2020, the one year checked. The same
|
||||
documentation lists the District of Columbia, so it is treated the same
|
||||
way. That treatment has not been checked against the data. For New York
|
||||
(March 31) and the 46 states with a June 30 year end, the two rules give
|
||||
the same months.
|
||||
|
||||
A government can change its fiscal year end, so the periods are looked up
|
||||
per government-year. Where the corpus has no `fiscal_year_end` for a
|
||||
government-year, or the government has no rows that year, the period is
|
||||
`NA`.
|
||||
|
||||
From FY2023 the source files mark some rows as estimates carried forward
|
||||
from an earlier year. The periods here describe the survey year, not the
|
||||
year a carried-forward row's data pertain to.
|
||||
}
|
||||
|
||||
\examples{
|
||||
\dontrun{
|
||||
# A June 30 state, a September 30 state and a September 30 county
|
||||
cog_fiscal_periods(
|
||||
c("130000226087", "010000226085", "121011212191"),
|
||||
years = 2020
|
||||
)
|
||||
}
|
||||
}
|
||||
\seealso{
|
||||
[cog_survey_year()] for the reverse mapping; the `fiscal_period`
|
||||
argument of [cog_spending()], [cog_revenue()] and [cog_balances()] to get
|
||||
the same columns on every row.
|
||||
}
|
||||
+16
-2
@@ -17,7 +17,8 @@ cog_revenue(
|
||||
limit = NULL,
|
||||
offset = NULL,
|
||||
state = NULL,
|
||||
type = NULL
|
||||
type = NULL,
|
||||
fiscal_period = FALSE
|
||||
)
|
||||
}
|
||||
\arguments{
|
||||
@@ -154,13 +155,26 @@ Ignored if `limit` is `NULL`; defaults to `0L` when `limit` is set.}
|
||||
and `govids_missing` are empty -- there is no id list to report against --
|
||||
and `provenance$scope$cohort` carries `state`, `type` and
|
||||
`n_governments` instead. A `govid`-named cohort reports exactly as before.}
|
||||
|
||||
\item{fiscal_period}{If `TRUE`, add three columns to every row:
|
||||
`fiscal_year_end` (the government's fiscal year end that survey year, an
|
||||
MMDD integer such as `630`), and `period_start` and `period_end` (Dates,
|
||||
inclusive), the calendar months the row covers. `year` is the Census
|
||||
survey year, which covers different months for different governments;
|
||||
see "The survey-year rule" in [cog_fiscal_periods()]. The columns come
|
||||
from each government-year's own `fiscal_year_end`, so a change of fiscal
|
||||
year is respected, and are `NA` where the corpus has no value. The result
|
||||
also gains `provenance$transformations$fiscal_period`. Defaults to
|
||||
`FALSE`, which returns exactly the columns and provenance this verb
|
||||
returned before the argument existed.}
|
||||
}
|
||||
\value{
|
||||
Tibble with columns `year`, `canonical_govid`, `gov_name`,
|
||||
`revenue_subtype`, `category`, `amt_nominal`, optional `amt_real`,
|
||||
optional `amt_per_capita_nominal`, optional `amt_per_capita_real`,
|
||||
optional `pop_source`, `codes_included`, `aggregate_fallback`, `notes`,
|
||||
and `value_source` when `complete = TRUE`.
|
||||
`value_source` when `complete = TRUE`, and `fiscal_year_end`,
|
||||
`period_start` and `period_end` when `fiscal_period = TRUE`.
|
||||
}
|
||||
\description{
|
||||
Mirror of [cog_spending()] for revenue categories. One row per
|
||||
|
||||
+16
-2
@@ -17,7 +17,8 @@ cog_spending(
|
||||
limit = NULL,
|
||||
offset = NULL,
|
||||
state = NULL,
|
||||
type = NULL
|
||||
type = NULL,
|
||||
fiscal_period = FALSE
|
||||
)
|
||||
}
|
||||
\arguments{
|
||||
@@ -174,13 +175,26 @@ Ignored if `limit` is `NULL`; defaults to `0L` when `limit` is set.}
|
||||
and `govids_missing` are empty -- there is no id list to report against --
|
||||
and `provenance$scope$cohort` carries `state`, `type` and
|
||||
`n_governments` instead. A `govid`-named cohort reports exactly as before.}
|
||||
|
||||
\item{fiscal_period}{If `TRUE`, add three columns to every row:
|
||||
`fiscal_year_end` (the government's fiscal year end that survey year, an
|
||||
MMDD integer such as `630`), and `period_start` and `period_end` (Dates,
|
||||
inclusive), the calendar months the row covers. `year` is the Census
|
||||
survey year, which covers different months for different governments;
|
||||
see "The survey-year rule" in [cog_fiscal_periods()]. The columns come
|
||||
from each government-year's own `fiscal_year_end`, so a change of fiscal
|
||||
year is respected, and are `NA` where the corpus has no value. The result
|
||||
also gains `provenance$transformations$fiscal_period`. Defaults to
|
||||
`FALSE`, which returns exactly the columns and provenance this verb
|
||||
returned before the argument existed.}
|
||||
}
|
||||
\value{
|
||||
Tibble with columns `year`, `canonical_govid`, `gov_name`,
|
||||
`spend_subtype`, `category`, `amt_nominal`, optional `amt_real`,
|
||||
optional `amt_per_capita_nominal`, optional `amt_per_capita_real`,
|
||||
optional `pop_source`, `codes_included`, `aggregate_fallback`, `notes`,
|
||||
and `value_source` when `complete = TRUE`.
|
||||
`value_source` when `complete = TRUE`, and `fiscal_year_end`,
|
||||
`period_start` and `period_end` when `fiscal_period = TRUE`.
|
||||
Carries a `provenance` attribute matching `inst/schemas/provenance-v1.json`,
|
||||
whose `completion` block reports `applied`, `rows_filled`, and the
|
||||
per-year `absence_means` rule that was applied. When `limit` is set,
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/fiscal.R
|
||||
\name{cog_survey_year}
|
||||
\alias{cog_survey_year}
|
||||
\title{Survey year that covers a calendar date for a government}
|
||||
\usage{
|
||||
cog_survey_year(govid, date)
|
||||
}
|
||||
\arguments{
|
||||
\item{govid}{Character vector of `canonical_govid` values, or a data frame
|
||||
with a `canonical_govid` column.}
|
||||
|
||||
\item{date}{Dates, or character strings in `"YYYY-MM-DD"` form. `govid`
|
||||
and `date` must have the same length, or one of them must have length 1.}
|
||||
}
|
||||
\value{
|
||||
Tibble with one row per (`govid`, `date`) pair, in input order:
|
||||
`canonical_govid`, `date`, `year` (the survey year, `NA` when it cannot
|
||||
be determined), `fiscal_year_end`, `period_start` and `period_end`.
|
||||
}
|
||||
\description{
|
||||
Maps a date to the survey year whose fiscal period, for that government,
|
||||
contains it. This is the reverse of [cog_fiscal_periods()]: use it to place
|
||||
an event that happened on a known date against the corpus's `year`.
|
||||
}
|
||||
\details{
|
||||
The date can fall in survey year Y, Y+1 or Y+2 of its own calendar year Y,
|
||||
depending on the government's fiscal year end. Each candidate is looked up
|
||||
with the `fiscal_year_end` the corpus records for that government-year, so
|
||||
a change of fiscal year is respected. When the government has no rows, or
|
||||
no `fiscal_year_end`, in the survey year that would cover the date, the
|
||||
result is `NA`; the function does not borrow another year's value. If a
|
||||
change of fiscal year makes two survey years' periods overlap, the earlier
|
||||
survey year is returned.
|
||||
}
|
||||
\section{The survey-year rule}{
|
||||
|
||||
Census assigns each government's fiscal year to a survey year by when the
|
||||
fiscal year ended. Survey year Y holds the fiscal year that ended between
|
||||
July 1 of Y-1 and June 30 of Y. The corpus records each government-year's
|
||||
fiscal year end as `fiscal_year_end`, an MMDD integer such as `630` or
|
||||
`1231`.
|
||||
|
||||
Survey year 2017 therefore covers, for a county, city or township:
|
||||
|
||||
* `fiscal_year_end = 630` (June 30): the fiscal year ending June 30,
|
||||
2017, so July 2016 to June 2017.
|
||||
* `930` (September 30): the fiscal year ending September 30, 2016, so
|
||||
October 2015 to September 2016.
|
||||
* `1231` (December 31): the fiscal year ending December 31, 2016, so
|
||||
January to December 2016.
|
||||
|
||||
A city with a December year end and a city with a June year end are both
|
||||
"2017" in the corpus, but their figures are six months apart. A September
|
||||
year end is nine months behind a June one.
|
||||
|
||||
State governments (type 0) follow a different rule: survey year Y holds the
|
||||
fiscal year ending in calendar year Y. For Alabama and Michigan (September
|
||||
30) that means October 2016 to September 2017, and for Texas (August 31)
|
||||
September 2016 to August 2017. Census's 2004 and 2006 technical
|
||||
documentation lists these three states as exceptions to the June 30 window,
|
||||
and the corpus agrees in survey year 2020, the one year checked. The same
|
||||
documentation lists the District of Columbia, so it is treated the same
|
||||
way. That treatment has not been checked against the data. For New York
|
||||
(March 31) and the 46 states with a June 30 year end, the two rules give
|
||||
the same months.
|
||||
|
||||
A government can change its fiscal year end, so the periods are looked up
|
||||
per government-year. Where the corpus has no `fiscal_year_end` for a
|
||||
government-year, or the government has no rows that year, the period is
|
||||
`NA`.
|
||||
|
||||
From FY2023 the source files mark some rows as estimates carried forward
|
||||
from an earlier year. The periods here describe the survey year, not the
|
||||
year a carried-forward row's data pertain to.
|
||||
}
|
||||
|
||||
\examples{
|
||||
\dontrun{
|
||||
# April 15, 2020 for a September 30 county and a September 30 state
|
||||
cog_survey_year(c("121011212191", "010000226085"), as.Date("2020-04-15"))
|
||||
}
|
||||
}
|
||||
\seealso{
|
||||
[cog_fiscal_periods()]
|
||||
}
|
||||
Reference in new issue
Block a user