Skip to content

Add unit tests for SE/MOE formula functions in calculate_cvs.R #84

@wcurrangroome

Description

@wcurrangroome

Problem

R/calculate_cvs.R (484 lines) has only 41 lines of tests. The existing tests only verify:

  1. No MOE column is entirely NA
  2. _pct variables have corresponding _M columns

No tests verify the actual statistical formulas produce correct values.

Gaps

Formula verification

  • se_simple(): trivial but untested
  • se_sum(): untested, especially the Census zero-estimate special case (keep only the largest MOE among zero-estimate addends)
  • se_proportion_ratio(): untested edge cases — p > 1 (degenerate proportion, should fall back to ratio formula), zero denominators (should return NA), negative radical (should fall back to ratio formula)
  • se_product(): completely untested
  • se_weighted_mean(): completely untested
  • cv(): defined but never called — may be dead code

Suggested test approach

  1. Use Census Bureau worked examples from the ACS Accuracy documentation as ground truth
  2. Test each function independently with known inputs/outputs
  3. Test edge cases: zero estimates, NA values, p > 1 proportions, zero denominators
  4. Test calculate_moes() end-to-end with a small synthetic dataset

Context

Identified during code review. These functions produce the margins of error that users rely on for statistical inference. Incorrect MOEs could lead to wrong conclusions about statistical significance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions