Contents/ Part X · Sampling & Data Collection/ Chapter 66

Study Design & Data Quality

How you collect data decides what you can conclude. Experiments establish cause; observation only shows association. We watch confounding distort an effect, randomization fix it, Simpson's paradox reverse a conclusion, and the dimensions that make data trustworthy.

⏱️ ~17 min read
🐍 Notebook included
📊 Chapter 66

Sampling decides who is in your data; study design decides what the data can prove. A perfectly sampled, perfectly clean dataset can still lead you to a false conclusion if the study was designed to only observe. This chapter is about turning data into trustworthy answers.

cause?
An observational study watches what happens and can show only association. An experiment assigns the treatment, ideally at random, and can establish causation, because randomization balances every confounder, known and unknown.
🧪
The chapter in one line

Correlation is not causation, randomization is what bridges the gap, a lurking variable can even reverse a conclusion (Simpson's paradox), and none of it matters if the data itself is dirty. Design and quality come before analysis.

1

Observational vs Experimental Studies

The deepest divide in data collection is whether you observe or intervene. Observation is cheap and often the only option, but it is vulnerable to confounding: a lurking variable that drives both things you are comparing.

A confounder fakes a correlation Temperature Ice-cream sales Drownings spurious r = 0.58 control for temperature and the correlation drops to −0.03

The notebook makes the classic case concrete: ice-cream sales and drownings correlate at 0.58, but once temperature is held constant the correlation collapses to −0.03. Neither causes the other; hot weather causes both. This is why an observational association, however strong, can never by itself prove causation.

2

Randomization & Control

An experiment assigns the treatment. The magic ingredient is random assignment: deciding each subject's group by a coin flip makes the treatment and control groups statistically identical on every variable at once, so any difference in outcome must be the treatment's doing.

Randomization balances the confounder (true effect = 8.0) Self-selected (observational) treatedage 55 controlage 36 estimate −1.5 (biased) Randomized experiment treatedage 45 controlage 45 estimate +8.0 (correct) same data-generating effect; only the design differs self-selection lets age contaminate the comparison; randomization removes it

In the notebook, when older subjects self-select into treatment the naive comparison gives −1.5, badly wrong, because the treated group is 19 years older. Flip a coin instead and the groups match on age (45 vs 45), so the estimate lands on the true effect of +8.0. A randomized controlled trial with a control group and, ideally, blinding, is the gold standard for establishing cause.

3

Confounding & Simpson's Paradox

Confounding has a spectacular extreme: an association can run one way overall and the opposite way in every subgroup. The real kidney-stone data of Charig et al. (1986) is the textbook case.

Treatment A wins both subgroups, yet loses overall 93% 87% Small stones 73% 69% Large stones 78% 83% OVERALL (reversed) A B

Treatment A succeeds more often for small stones (93% vs 87%) and for large stones (73% vs 69%), yet B beats A overall (83% vs 78%). The cause is the confounder stone size: doctors gave the tougher open surgery (A) to far more of the hard large-stone cases. Aggregating across a confounder can flip the truth, so before trusting any overall comparison, ask what variable might be hiding inside it.

4

Data Quality Dimensions

Even a flawless design fails on dirty data. Quality is not one thing but several, and a dataset is only as trustworthy as its weakest dimension.

DimensionQuestion it answersExample failure (from the audit)
CompletenessAre values present?a missing age (83% complete)
UniquenessAre records de-duplicated?a duplicated id (83% unique)
ValidityAre values in the legal range?an age of 200 (67% valid)
ConsistencySame units & format throughout?a height of 1.75 (meters, not cm)
Timeliness / formatParseable and up to date?an unreadable "bad-date"
AccuracyDo values match reality?a typo'd measurement (hard to detect)

The notebook audits a small dataset and scores each dimension separately, catching the missing value, the duplicate row, the impossible age, the wrong-unit height, and the malformed date. Crucially, these are different failures needing different checks: a single "percent clean" number would hide them. Profiling every dimension is the first step of any serious analysis.

5

Study Design & Data Quality in Machine Learning & AI

Machine learning is observational by default: models learn from whatever data they are fed, confounders and all. The hardest failures in applied ML are design and data-quality failures, not modeling failures.

Idea (this chapter)In ML / AI it appears asThe danger
ConfoundingSpurious features / shortcut learningthe model keys on a confounder, not the cause
Randomized experimentA/B tests & randomized rolloutsthe only way to measure true causal lift
Simpson's paradoxAggregate vs per-segment metricsoverall accuracy hides per-group failures
Data qualityValidation, profiling, leakage checksgarbage in, garbage out, at scale
Design > sample sizeBetter data beats more datamore biased data does not help
🤖
Why this matters for AI research

The notebook's blunt finding, a confounded estimate is just as wrong with 40,000 rows as with 4,000, is the deepest lesson in applied ML: garbage in, garbage out, and scaling the garbage does not help. Models exploit confounders as shortcuts (the famous case of a classifier that detected the ruler in skin-cancer photos rather than the lesion). Establishing that a feature or intervention truly causes an outcome requires a randomized A/B test, not an observational correlation. Simpson's paradox warns that headline accuracy can mask severe per-segment failures, the heart of fairness auditing. And data leakage, a validity failure where future or target information sneaks into the features, is the most common cause of models that look brilliant in development and collapse in production. Design and data quality are where real ML projects are won or lost.

🐍

See design and quality in Python

The companion notebook manufactures a spurious correlation and controls it away, contrasts a self-selected estimate with a randomized one, reproduces Simpson's paradox from the real kidney-stone data, audits a dataset across the quality dimensions, and shows confounding bias surviving a tenfold increase in data.

📓 View Notebook (code & outputs) ▶ Open in Colab ⬇ View / Download on GitHub

View opens the rendered notebook instantly (no setup). Open in Colab runs & edits it live in your browser. To run locally, install numpy, pandas, and matplotlib and launch jupyter notebook.

🎓 Key Takeaways

  • Observation shows association; experiments show causation, because a confounder can fake a correlation (r 0.58 → −0.03 once controlled).
  • Randomization balances all confounders: a self-selected estimate was −1.5, the randomized one +8.0 (the truth).
  • Simpson's paradox: A beat B in both subgroups (93/73 vs 87/69) yet lost overall (78% vs 83%), the confounder was stone size.
  • Data quality is multi-dimensional: completeness, uniqueness, validity, consistency, timeliness, accuracy, each needs its own check.
  • Design beats cleanup: a confounded estimate stayed wrong with 10× the data; in ML, garbage in is garbage out at scale.
6

Practice Challenges

Five short challenges, beginner to intermediate. Try them with NumPy and pandas before checking the solutions.

1

Spurious correlation from a confounder

Let a confounder Z drive both X and Y. Show X and Y correlate, then control for Z and watch it vanish.

Hint: correlate the residuals after regressing each on Z.
2

Randomization balances a confounder

Compare the treated/control means of a covariate under self-selection versus a coin-flip assignment.

Hint: self-selection leaves the groups imbalanced; randomization matches them.
3

Build your own Simpson's paradox

Construct two treatments where X beats Y in each subgroup but loses overall.

Hint: give X mostly hard cases and Y mostly easy ones.
4

Data quality audit

Audit a messy dataframe for completeness, uniqueness, and validity, and report each as a percentage.

Hint: isna, nunique, and between.
5

Bias is a design flaw, not a sample-size one

Show a confounded estimate stays biased as n grows from 2,000 to 50,000.

Hint: the offset from the true effect barely moves.
Check your work

A fully-worked solutions notebook walks through all five challenges, each verified in code. Try them yourself first, then compare.

📓 View Solutions ▶ Open Solutions in Colab ⬇ View / Download on GitHub
7

Quiz: Test Yourself

Eight quick questions on study design and data quality. Answer them, hit Check Answers, and keep refining until you score 100%. Your progress is saved, so you can hop back to the chapter and return anytime.

🧭
Up next

Good design assumes honest measurement, but collection itself can distort the data. Bias in Data Collection closes the Part: selection, nonresponse, response, and the survivorship bias of Wald's wartime airplanes.