# Overlaying household-registration demographics on the election map: three API pitfalls, and why the scatter plot shows two correlation coefficients

> I added an elderly-population layer and a scatter plot of demographics against votes to the election map. Notes on the interior ministry's API, a truncated Sanmin name, aligning population to election day, and why Kinmen made me add rank correlation.

- Date: 2026-11-30
- Topics: Data, Next.js
- Chinese original: https://kbs0830.com/blog/election-map-population-structure

[The election map](/tools/election-map) (a tool page in Chinese) used to draw only election results. This time I laid population structure over it: the map can draw "share aged 65 and over" directly, the detail panel has a new block with registered population and age structure, and under the map there is a scatter plot with one dot per township or district, showing how population relates to turnout and party vote share.

## Data: single-year-of-age population per village

The source is the Ministry of the Interior's Department of Household Registration dataset on the government open data platform, "households and population by single year of age, by village", under the Open Government Data License, version 1. It has an API that needs no key, queried by year and month in the Republic of China calendar, available monthly from 2018 (ROC year 107); the nation's 7,781 villages come in 4 pages of about 10 MB each. Each village has the number of men and women aged 0 to 100+, over 200 fields in all.

I only need a few numbers at township and district level, so the processing script (`scripts/build-election-pop.py`, using only the Python standard library) sums villages up to townships and keeps only five integers per month: population, ages 0–14, ages 20–39, ages 65+, and median age. A county's median age cannot be computed as the median of its townships' medians, so I sum the single-year-of-age counts first and recompute (linear interpolation inside the bracket). 7 months, 367 townships, 22 counties, 106 KB in total.

## Aligning in time: the last month-end before election day

Population is monthly, and an election is one specific day. I use one fixed rule: for each cycle, take the registered population at "the last month-end before election day" (the election of 2026-11-28 uses the end of October 2026). The upside is that the rule needs no judgment, and which months to fetch does not have to be written by hand: the script finds every election day since 2018 (including referendum days) in the existing election data files and works out the months itself. When a new cycle is added later, rerunning the script fetches one more month.

## Three pitfalls

**One: truncated names.** In the 2018 and 2019 data, Sanmin District and Fengshan District of Kaohsiung are each split into two household-registration districts, and the names are truncated to four Chinese characters, which come out as "Sanmin 1", "Sanmin 2", "Fengshan 1" and "Fengshan 2" (the district name followed by a number). My township key is "the current name minus its last character", and both truncated Sanmin names become "Sanmin" after dropping the last character, so the script immediately reported "two townships map to the same key". The fix is to restore them to the full "Sanmin District" first, then sum.

**Two: a piece missing from the map.** The household registration data has Wuqiu Township of Kinmen County (a few hundred people), and the election map's boundary file does not draw it. I did not patch this: Wuqiu's population still counts in Kinmen County's county-level numbers, there is just no township entry for it, and the script has this as an explicit exception list; any other name that does not match makes the script fail.

**Three: the data simply is not there.** The 2026 village chief data file has no turnout (`null`). The scatter plot's Y-axis options appear dynamically depending on the data: only listed with at least 20 points (5 when looking at a single county), and when village chiefs have no drawable axis, it honestly says "not enough data" instead of drawing an empty chart.

## One dot is an area, not a person

A scatter plot is the easiest thing to misread. "Townships with a higher share of elderly people have higher turnout" is a relationship at the area level, and cannot be turned into "older people are more likely to vote" (the ecological fallacy); correlation is not causation, and urban or rural character, industry and ethnic composition all affect both the age structure and the vote. The population used is the registered population, and young people studying or working elsewhere often keep their registration at their hometown. These three points are written directly under the chart, not only on some explanation page.

## Why show two correlation coefficients

Across Taiwan's 367 townships, the 2026 mayoral turnout against the 65+ share has a Pearson correlation r of only 0.04, which looks like "almost no relationship". But in the lower left of the chart there are five points with turnout of only about 38 to 40%, all of them townships of Kinmen County. Kinmen's past county-chief turnout was 45%, 43%, 39%, 39%, low throughout, a stable outlier rather than a data error, but it visibly affects r.

So I also give the **Spearman rank correlation ρ**: convert the values to ranks first and then compute the correlation, looking only at "who is higher than whom", which reduces the effect of a single outlier a lot. Putting the two numbers side by side lets readers see the difference themselves. One unit test checks this exactly: on a dataset that is almost perfectly monotonic with only the last point an outlier, ρ is closer to 1 than r.

## Verification

- For every snapshot month: all 367 townships on the map have population and there are no extras; county population equals the township sums (Kinmen County has Wuqiu extra).
- Nationwide registered population is between 22 and 24 million in every period, and the 65+ share rises period by period: 14.4% in October 2018, 20.8% in October 2026.
- The statistics functions (Pearson, Spearman, regression line, axis ticks) are verified on small examples that can be worked out by hand.
- Before 2018 there is no population data: the whole layer turns gray and the explanation says why, instead of forcing the nearest population onto it.

---

Author: PINGWEI LI (李秉威, Li Ping-Wei)
Original page: https://kbs0830.com/en/blog/election-map-population-structure
