NHANES (design-correct survey analysis)

com.blackswancausallabsv0.5.1更新於 Oct 5, 2026

Design-correct NHANES analysis: survey weights, pooled cycles, CIs and NCHS reliability flags.

已驗證STDIO僅桌面Data & AnalyticsKnowledge & Memory

概覽

AI 產生的概覽

讓助理以符合調查設計的方式分析 NHANES 資料,正確使用權重、合併週期、信賴區間與 NCHS 可靠性標記。

功能
提供 17 個工具,用於定位、查找、建構、清理、分析與匯出 NHANES 公共使用資料。它會尋找檔案、依 SEQN 合併、選擇最嚴格的調查權重、保留完整調查設計,並產生基於設計的估計、Taylor 線性化變異數、Korn-Graubard 信賴區間與 NCHS 可靠性標記。也支援年齡調整、週期合併、衍生變數、長表折疊,以及可選的關聯死亡資料合併與基於設計的 Cox 模型。
適用情境
當助理需要從 NHANES 公共使用檔案得到可辯護的估計,而非未加權彙總時使用,例如盛行率、次群組比較、迴歸或跨週期死亡分析。主要面向公共衛生、流行病學與調查研究工作流程。
執行需求
以本機 stdio 程序執行,從 PyPI 套件 nhanes-mcp 安裝(通常透過 uvx),或從原始碼安裝並需要 Python 及其相依套件。首次使用時從 cdc.gov 下載資料並快取;NHANES_MCP_CACHE 可覆寫快取目錄,NHANES_MCP_DATA_DIR 指向手動下載的 .xpt 檔案以供離線使用。未宣告驗證。
安裝前請注意
它會從 cdc.gov 下載資料並寫入本機快取目錄。使用者需自行遵守 NCHS 資料使用協議,專案聲明與 NCHS 或 CDC 無隸屬或背書關係。可選的 Results Explorer 附加元件是獨立套件,採用非商業授權,不屬於 MIT 儲存庫。仍有已知問題,包括 2009-2010 及更早的年齡調整肥胖率偏差、CMV 參與者數量無法解釋,以及不支援 NHANES III。

安裝

在 SourceWeft 中

  1. 開啟 儀表板中的 NHANES (design-correct survey analysis),將其新增到工作區。
  2. 為需要使用其工具的對話啟用該服務。

Desktop only,透過 STDIO。 STDIO 服務會啟動本機處理程序,因此需要 SourceWeft 桌面主機。

其他 MCP 客戶端

參照 儲存庫 中的啟動說明。

README

nhanes-mcp

An MCP server for design-correct, conversational access to NHANES public-use data. Black Swan Causal Labs · MIT license · v0.5 · PyPI · MCP Registry

Most "chat with a dataset" layers let an agent compute an unweighted mean. With NHANES that answer is wrong. This server makes the defensible analysis the default: the agent asks a question in plain language, and the server finds the files, merges them on SEQN, picks the right weight, keeps the full survey design, and reports design-based estimates with NCHS reliability flags.

Not affiliated with, or endorsed by, NCHS or CDC. Data are the public-use NHANES files published by the National Center for Health Statistics and downloaded directly from cdc.gov. Users are responsible for following the NCHS data use agreement.

What it handles for you

PitfallWhat the server does
Wrong / no weightbuild_dataset picks the most restrictive weight (interview → MEC → fasting / phlebotomy / surplus-serum subsample → dietary day-1/day-2), explains why, and stores it in WT_ANALYSIS. Overrides (build_dataset(weight=...), set_weight) are reported with every result; WT_ANALYSIS cannot be overwritten silently
Subsetting before estimationdomain= expressions keep the full design (zero weight outside the domain)
Pooling cyclesWeights rescaled by cycle years / total years (2017–March 2020 counts as 3.2 years); 1999–2002 uses 4-year weights; strata made cycle-unique; refuses 2017–2018 + 2017–2020 overlap
Long-format tables (e.g. prescriptions)Refuses to join tables with repeated SEQN (which would silently duplicate weights); flag_from_long_table collapses them to one row per person
Refused / don't-know codesdescribe_variable reads the CDC codebook and suggests sentinel codes; set_missing recodes them
Silent 0 for missingderive_variable propagates missingness (any / all / none); coalesce, fillna, isna, notna, where handle missingness deliberately
Irregular file namesTries known variants (e.g. 1999–2000 surplus-serum files SSCMV_A, SSMUMP_A)
VarianceTaylor linearization, strata × PSU, design df; Korn–Graubard CIs and NCHS 2017 reliability flags for proportions
Age adjustmentDirect adjustment to the 2000 US standard (20–39 / 40–59 / 60+) with linearized SE, or to any caller-supplied standard (age groups + population), with a warning for in-domain records outside the groups
MortalityOptional join of the public-use Linked Mortality File (follow-up through 2019) and a design-based Cox model

Tools (17)

StepTools
Orientlist_cycles, analysis_guidance
Findlist_files, search_variables, describe_variable
Buildbuild_dataset (optional mortality join), describe_dataset
Clean / deriveset_missing, derive_variable, flag_from_long_table, set_weight
Analyzesurvey_frequency, survey_estimate, survey_regression (linear / logistic), survey_cox (Cox PH, Binder variance)
Presentshow_results — text + structured results; interactive view with the optional Results Explorer add-on
Exportexport_dataset

Cycles: 1999–2000 through 2017–2018, 2017–March 2020 (pre-pandemic, P_ files) and August 2021–August 2023.

Results Explorer (optional add-on)

show_results returns design-based results as text plus structured data in every client. With the optional NHANES Results Explorer add-on installed, MCP Apps hosts (Claude Desktop/web, ChatGPT, VS Code, Goose) also render an interactive view: headline estimate with CI and NCHS reliability badge, a crude / age-adjusted toggle that re-runs the estimate on the server, the analysis plan, subgroup panels, server warnings, benchmarks against published estimates, and design provenance.

The add-on is a separate package from Black Swan Causal Labs under the PolyForm Noncommercial License 1.0.0 (free for academic, public-health and other noncommercial use; commercial use needs a license — https://blackswancausallabs.com). It is not part of this MIT repository. nhanes-mcp finds it if it is installed in the same Python environment, if NHANES_MCP_EXPLORER_PATH points to it, or if a folder named nhanes-mcp-explorer sits next to the nhanes-mcp folder.

Validation

Estimates were checked against published NCHS results (validation/):

  • Prevalence: 57 of 57 published NCHS estimates reproduced (Data Briefs 360, 363, 508, 515; pooled 2015–2018; 2017–March 2020 pre-pandemic).
  • Standard errors: 20 of 20 match; 11 of 12 published 95% CI bounds identical (the 12th differs by 0.1 at a rounding edge).
  • Mortality: a design-based Cox model on NHANES 1999–2006 (adults 25+) reproduces 6 of 7 published hazard ratios within their CIs (NHSR 155). The Mexican American contrast does not reproduce (0.71 vs 1.12 published); this is under investigation and the linked file here has longer follow-up (2019 vs 2015).
  • Hypertension (NCHS Data Brief 511, 2021–2023, adults 18+): prevalence (crude and age-adjusted, by sex and age), awareness, treatment and control — 17 of 17 published estimates reproduced exactly.
  • CMV seroprevalence (Bate et al., Clin Infect Dis 2010; NHANES 1999–2004, ages 6–49, surplus-serum weights): see tests/benchmark_nchs.py. Before v0.4 the server silently used MEC weights here and could not load the 1999–2000 file.
  • Unit tests (tests/): variance checked against an independent loop implementation and a delete-one-PSU jackknife; Cox model checked against statsmodels PHReg and a jackknife; weight selection, pooling, guards, expression semantics, long-table and dietary-weight handling.

Install

Easiest: let your AI assistant do it

Paste this into Claude (Cowork or Claude Code), or any agent that can run commands on your computer:

Install the nhanes-mcp MCP server (PyPI package nhanes-mcp) for Claude Desktop. Install uv if it is missing, then add this entry to my Claude Desktop config (claude_desktop_config.json), using the full path to uvx: "nhanes": {"command": "uvx", "args": ["nhanes-mcp"]}. Keep my existing servers. Then tell me to restart Claude Desktop.

One line in the config (uvx)

With uv installed, add to claude_desktop_config.json and restart Claude Desktop (on macOS use the full path from which uvx, e.g. /Users/<you>/.local/bin/uvx):

json
"nhanes": {  "command": "uvx",  "args": ["nhanes-mcp"]}

uvx fetches the server from PyPI into an isolated environment on first launch; no clone or virtual environment to manage. To run the latest code from GitHub instead, use "args": ["--from", "git+https://github.com/Black-Swan-Causal-Labs/nhanes-mcp", "nhanes-mcp"].

From source (for development)

bash
python3 -m venv ~/.nhanes-mcp-venv~/.nhanes-mcp-venv/bin/pip install "mcp>=1.2,<2" pandas numpy scipy pyreadstat httpx beautifulsoup4 lxmlgit clone https://github.com/Black-Swan-Causal-Labs/nhanes-mcp.git ~/nhanes-mcp
json
"nhanes": {  "command": "/Users/<you>/.nhanes-mcp-venv/bin/python",  "args": ["-m", "nhanes_mcp"],  "env": {"PYTHONPATH": "/Users/<you>/nhanes-mcp"}}

Any MCP client that runs local stdio servers works the same way. Data are downloaded from cdc.gov on first use and cached in ~/.cache/nhanes-mcp (override with NHANES_MCP_CACHE). Set NHANES_MCP_DATA_DIR to a folder of manually downloaded .xpt files to work offline.

Need help setting it up for your team, or adapting it to another survey or dataset? Contact Black Swan Causal Labs.

Tests

bash
python tests/test_offline.py            # synthetic NHANES-shaped data, no networkpython tests/test_cox.pypython tests/test_long_and_dietary.pypython tests/benchmark_nchs.py          # reproduces published NCHS estimates (needs network)

Known open issues

  • Age-adjusted adult obesity for 2009–2010 and earlier runs 0.1–0.7 points below NCHS Health E-Stat 111 (2011–2012 onward matches exactly). Pooling and pregnancy-code handling were ruled out; cause under investigation.
  • The CMV analysis finds 14,198 tested participants aged 6–49 in the public surplus-serum files versus 15,310 reported by Bate et al.; unexplained.
  • NHANES III (1988–1994) is not supported.

Changelog

  • 0.5.1 — First release on PyPI (uvx nhanes-mcp) and the official MCP Registry (com.blackswancausallabs/nhanes-mcp). Packaging metadata only; no analysis changes.
  • 0.5.0 — show_results tool (text + structured results; interactive view via the optional Results Explorer add-on); one-command install with uvx; analysis guidance rule 11.
  • 0.4.1 — Comparisons inside missing-aware functions now return missing when an operand is missing, so skip-pattern definitions such as where(BPQ020 == 1, fillna(BPQ150, 2) == 1, 0) are missing (not 0) for people never asked the screener. Hypertension benchmark (NCHS Data Brief 511) added.
  • 0.4.0 — Surplus-serum and other file-specific subsample weights with 2Y/4Y suffixes are detected and pooled; 1999–2000 _A file names resolved; build_dataset(weight=...) and new set_weight tool, both recorded in every result; design columns protected from derive_variable; missing-aware expression functions; custom age standards; CMV benchmark added.
  • 0.3.0 — Initial public release.

Limitations

  • Public-use files only. Restricted-use data (including the NHANES–CMS Medicare/Medicaid linkage) require an NCHS Research Data Center.
  • Variance uses Taylor linearization with PSUs treated as sampled with replacement, as NCHS recommends; replicate weights are not used.
  • The server reports what the data support; it does not choose a study design for you.

來源:README.md,提交 d3bbbab

工具

0
工具後設資料尚未被收錄。

版本歷史

1
  1. v0.5.1最新Oct 5, 2026