Lab · Programming · 5 sections

Good programming practices for analysis

Organize inputs, transformations, tests and outputs so another analyst can reproduce a result.

No account needed to read online.

What it covers

  • Write the rule before the implementation
  • Separate inputs, calculation and output
  • Give the next analyst one entry point
  • Practice a clean rerun

STATISTICAL PROGRAMMING LAB

Who had a return visit within 30 days?

Six people, two groups, and a visit log with repeat visits. Build an endpoint, compare risks, then audit three proposed pieces of code.

Inspect the source tables

Day 0 is enrollment. Follow-up is the number of days with complete capture of return visits. The visit log can include records outside the endpoint window.

cohort

idarmfollowup
1A30
2A30
3A12
4B30
5B30
6B30

visits

idday
18
112
231
430
60

SQL workbench

Return six rows ordered by id, with columns id and event. Count a visit on days 1 through 30 inclusive. A person with no qualifying visit needs 30 days of follow-up to be classified as event-free. Otherwise the outcome is unknown.

Explain the group comparison

With 12 days observed, person 3 has an unknown outcome. Group A has one event among two observed people (50%). With 30 days and no visit, that person becomes an observed zero and group A falls to one in three. Group B stays at one in three. The change comes entirely from outcome ascertainment.

Code review desk

events = [x for x in outcomes if x]
risk = sum(events) / len(events)

Which issue or review action matters here?

Read online

Good programming practices for analysis

Write the rule before the implementation

Start with an analysis specification. State the question, target population, row unit, time window, outcome definition, valid values, missing-data rule, numerator, denominator, and expected output. Write a tiny hand-worked example before coding. The five-person example has 5 enrolled, 4 observed, 2 events, and risk 0.50 among observed outcomes.

Separate inputs, calculation and output

Build in layers. Preserve raw inputs. Validate schema and identifiers. Transform into an analysis-ready table. Calculate in a small function or query. Produce the report table separately. Keep full precision internally and round only for display. Name intermediate objects for what they represent.

Give the next analyst one entry point

Organize a project so a colleague can run it from a fresh checkout. Include a README, source code, tests, dependency versions or lockfile, and a clearly identified output location. Use relative paths and synthetic data in examples. Never put passwords, API keys, identifiable data, or machine-specific paths in Git.

Practice a clean rerun

A practical review has several layers. Check the code against the written estimand and data definition. Test a hand-calculated example, missing values, invalid values, empty input, duplicates, and a boundary case. Inspect row counts after every join. Run the complete workflow from a clean session. Review the diff and resulting tables. Record who reviewed the analysis, which version ran, and any unresolved limitations.

Choose checks by consequence

A teaching script may need assertions and a clean rerun. A reusable package benefits from unit tests, continuous integration and clear error messages. A result that informs a scientific or operational decision also needs independent review of the cohort, estimand, data provenance, and reported output. Passing software tests alone does not establish that the scientific question or data are appropriate.

Discussion

Questions, clarifications and applications of this resource.

Loading discussion…