Skip to contents

Checks that American Community Survey (ACS) estimates for census tracts, such as a table from tidycensus::get_acs() with geometry = TRUE, have the form that cacs_intersect_weight() and the acs argument of cacs_run() require. cacs_run() checks at the start only that acs is an sf object, and makes the other checks at its intersection step, after the routing step.

Usage

cacs_acs_validate(x)

Arguments

x

An sf object of ACS estimates with one row for each tract and variable, or any other object to check.

Value

TRUE, invisibly, when every check passes. Otherwise the function stops with an error of class catchmentACS_error_schema at the first check that fails.

Details

The checks are made in this order, and the first one that fails stops the function with an error:

  1. x is an sf object.

  2. Its coordinate reference system is NAD83 (EPSG:4269).

  3. It has the columns GEOID, NAME, variable, estimate, moe (the margin of error), and geometry.

  4. It has at least one row.

  5. Every GEOID has 11 digits, like a census tract code.

  6. estimate and moe are numeric.

  7. No tract has two rows for the same variable.

Whether variable holds ACS codes, and the values of estimate and moe, are not checked. A name in place of an ACS code, such as "pop" from tidycensus::get_acs(variables = c(pop = "B01003_001")), gets NA values, and cacs_run() then stops with an error about ACS codes that are not combined. Census Bureau annotation codes, such as -555555555, pass the checks. cacs_intersect_weight() treats them as missing, and its warning counts them, except a margin-of-error code next to a missing estimate; the example data below have only such codes and give no warning. A tract with no row for one of the variables is not reported, and it is left out of that variable's total or average without a warning. Whether every tract has one row for each variable can be checked with all(table(acs$GEOID, acs$variable) == 1).

See also

Examples

acs <- readRDS(system.file("extdata", "sample_alabama_subset.rds",
                           package = "catchmentACS"))
cacs_acs_validate(acs)

# tidycensus downloads the data, which needs a Census API key.
if (FALSE) { # \dontrun{
my_acs <- tidycensus::get_acs(
  geography = "tract", variables = "B01003_001",
  state = "AL", year = 2023, geometry = TRUE, output = "tidy"
)
cacs_acs_validate(my_acs)
} # }