Skip to content

v0.3.9 - #20

Merged
mountainMath merged 7 commits into
mainfrom
v0.3.9
Oct 5, 2026
Merged

mountainMath merged 7 commits into
mainfrom
v0.3.9

Conversation

@mountainMath

@mountainMath mountainMath commented Oct 4, 2026 •

Copy link
Copy Markdown
Owner

Release branch for v0.3.9.

Fixes #19.
Closes #21.

Major changes

  • New experimental functions to detect and correct for likely geocoding anomalies in timelines on a common geography, where the same dwellings got assigned to different neighbouring regions in different years. This shows up as a surprising drop in one region that is offset by a jump in a neighbouring region (Incorporate functionality to detect anomalies in TongFen timelines #21).
    • tongfen_detect_anomalies lists the candidate regions with their best neighbour.
    • tongfen_anomaly_joins determines the regions to join, iterating until no more regions qualify.
    • tongfen_join_regions joins them in data that already is on a common geography.
    • tongfen_join_correspondence joins them in a correspondence for use with tongfen_aggregate.
    • New vignette "Geocoding anomalies in TongFen timelines", following the method from the blog post.
  • StatCan correspondence files are now downloaded as parquet files from a mirror on S3. Statistics Canada put the original files behind a browser check that blocks programmatic downloads, which broke method = "statcan" (StatCan disabled programmatic download of correspondence files #19).
  • Cached correspondence files are checked against the mirror via ETag once per session and only downloaded again if they changed. When the mirror can't be reached the cached files are used as they are.
  • The mirror location can be changed via the tongfen.statcan_correspondence_url option.
  • Previously cached statcan_correspondence_*.csv files in the tongfen cache directory are no longer used and can be removed.

Fixes with changed results

  • proportional_reaggregate gave wrong results when the finer level data already has values for the categories to reaggregate. The values were compared to the parent total across all categories instead of per category, and a missing value in one child region discarded the existing values of all its siblings.
  • tongfen_estimate underestimated values when the intersection of a source and a target region is a geometry collection.
  • tongfen_aggregate and aggregate_data_with_meta scaled averages by their parent variable more than once when the metadata lists the same variable name for several datasets, as is common with US census data. tongfen_aggregate now only uses the metadata of the dataset being aggregated, conflicting aggregation rules for the same variable are an error.
  • Averages aggregated with na.rm = TRUE are now taken over the regions that have a value. The parent variable of regions with a missing average still counted toward the total, pulling the result toward zero.
  • "Average to" variables sharing a parent variable no longer overwrite each other's base. In that case the base columns in the result are named base_<variable> instead of after the parent.

Minor changes

  • tongfen_estimate with na.rm = FALSE no longer returns NA for target regions that only share a boundary with a source region with missing values, returns NA for target regions that don't overlap the source instead of erroring out when none of them do, and gives a clear error when target already has a column named like one of the variables to estimate.
  • Combining correspondences across three or more datasets no longer runs into a cross join when consecutive correspondences don't share a geographic identifier. This gave a dplyr deprecation warning and needlessly large intermediate tables, results are unchanged.
  • estimate_tongfen_correspondence no longer requires the geometry column to be named geometry.
  • refresh = TRUE in get_tongfen_correspondence_ca_census and get_tongfen_ca_census now also refreshes the cached StatCan correspondence files.
  • US Census Bureau relationship files are downloaded to a temporary file first, an interrupted download no longer leaves a broken file in the cache. They are now cached in the same place as the StatCan correspondence files, also honouring the tongfen.cache_path environment variable and the custom_data_path option.
  • tongfen_estimate_ca_census returns its result visibly.
  • Documentation fixes: typos, missing \value sections, and the tongfen_aggregate example now passes a named list of datasets matching the metadata.
  • The R-CMD-check workflow no longer triggers on pushes to the removed master branch.

Notes

  • New dependency: nanoparquet (and tools) in Imports, dplyr requirement raised to 1.1.0, which the package already relied on.
  • data-raw/statcan_correspondence.R builds the parquet files from the manually downloaded StatCan originals. The eight files are hosted under https://mountainmath.s3.ca-central-1.amazonaws.com/tongfen/statcan_correspondence/v1/.
  • cran-comments.md is updated for the CRAN submission.

Testing

  • New tests for the anomaly functions (including a comparison against the original implementation from the blog post), the ETag cache, the correspondence join order, and the reaggregation, estimation and averaging fixes.
  • GitHub Actions R-CMD-check passes on macOS (release), Windows (release) and Ubuntu (devel, release, oldrel-1).
  • Local R CMD check --as-cran on macOS with R 4.6.0, with all six vignettes rebuilt against live data: 0 errors, 0 warnings, 0 notes. This ran before the spelling and pkgdown commits, which only touch documentation.
  • Live check against S3: cold download, cached repeat, unchanged and changed ETag, offline fallback, and get_tongfen_correspondence_ca_census with method = "statcan" for CT and DA levels across the 2001 to 2021 censuses.

🤖 Generated with Claude Code

https://claude.ai/code/session_01A4ixWqz6d5wpYg2DkoT4pm

mountainMath and others added 7 commits October 4, 2026 14:18
aggregate_correspondences joined the per-step correspondences in order of
size, so with three or more datasets consecutive tables could have no
identifier in common and got cross joined. That triggered a dplyr
deprecation warning and large intermediate tables. Tables now only get
joined once they share an identifier with the ones joined so far.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Statistics Canada put the correspondence files behind a browser check that
blocks programmatic downloads, breaking method = "statcan". The files are
now hosted as parquet on S3, built by data-raw/statcan_correspondence.R
from manually downloaded originals. Cached files are checked against the
mirror via ETag once per session and downloaded again if they changed.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The master branch has been removed, main is the default branch.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…dit fixes

Wrong results
- proportional_reaggregate compared existing child values to the parent total
  across all categories instead of per category, and a missing child value
  discarded the values of its siblings
- tongfen_estimate underestimated values when an intersection came back as a
  geometry collection, and picked up NA from source regions that only touch
  the target
- averages were scaled by their parent more than once when the metadata lists
  the same variable for several datasets, tongfen_aggregate now only uses the
  metadata of the dataset being aggregated
- averages aggregated with na.rm = TRUE kept the parent of regions with a
  missing value in the denominator
- "Average to" variables sharing a parent overwrote each other's base

Errors
- estimate_tongfen_correspondence required the geometry column to be named
  geometry, and the robust option failed on more than one region
- tongfen_estimate errored out when no target region overlaps the source, and
  gave an obscure error on clashing column names

Behaviour
- refresh is passed through to the StatCan correspondence download
- tongfen_estimate_ca_census returns visibly
- US relationship files are downloaded to a temporary file first and cached
  via the shared tongfen cache directory
- fix function names in the get_tongfen_ca_census_ct_from_da deprecation

Docs and housekeeping
- missing \value sections, broken or misleading examples, README and
  get_tongfen_us_census docs
- remove dead code in tongfen_ca.R
- require dplyr >= 1.1.0, which the package already relied on
- NEWS and cran-comments for 0.3.9

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Geocoding varies between censuses, the same dwellings can get assigned to
different neighbouring regions in different years. On a common geography this
shows up as a surprising drop in one region that is offset by a jump in a
neighbour. Adds experimental functions to check for this and to join the
affected regions on demand, following the method from
https://doodles.mountainmath.ca/posts/2024-07-26-geocoding-errors-in-aggregate-data/

- tongfen_detect_anomalies lists candidate regions with their best neighbour
- tongfen_anomaly_joins iterates joins until no more regions qualify
- tongfen_join_regions applies the joins to data on a common geography
- tongfen_join_correspondence applies them to a correspondence for use with
  tongfen_aggregate

The neighbour graph is contracted between rounds instead of recomputed from
the joined geometries, so joined regions keep all neighbours of their parts.
Missing values are never surprising and don't make up for a drop in a
neighbour, and ties are broken by identifier so results don't depend on the
order of the regions.

Hoists intersects_pairs into helpers.R for reuse, adds a vignette and tests
including a comparison against the original implementation.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A4ixWqz6d5wpYg2DkoT4pm
@mountainMath
mountainMath merged commit 5b1377a into main Oct 5, 2026
6 checks passed
@mountainMath
mountainMath deleted the v0.3.9 branch October 5, 2026 03:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorporate functionality to detect anomalies in TongFen timelines StatCan disabled programmatic download of correspondence files

1 participant