0.2 Choosing Summaries & Visuals - JulTob/Data GitHub Wiki
Statistical hygiene
Once you’ve declared a variable’s measurement scale (nominal, ordinal, interval, ratio), the next step is: only use statistic or plot types valid for that scale. Below is a compact guide.
| Scale | Allowed summaries / metrics | Allowed visualizations | Not allowed / misuse to avoid |
|---|---|---|---|
| Nominal | counts, proportions, mode | bar chart, pie (small categories) | mean, standard deviation, histogram (meaningless) |
| Ordinal | median, rank correlations (e.g. Spearman) | bar chart (ordered), boxplot of ranks | arithmetic mean (unless evenly spaced), variance with caution |
| Interval | mean, standard deviation, differences | histogram, density plot, boxplot | true “zero‑based” ratios (only differences meaningful) |
| Ratio | all interval metrics + meaningful ratios | same as interval + log scales if needed | none in the standard sense (all metrics valid) |
Procedure:
- For each variable, annotate its scale (in data dictionary or schema).
- When choosing a summary or statistic, refer back to this table.
- For visualizations, match type to permissible plots above.
- If you ever see code taking a mean of a nominal code (e.g. 0/1 categories), flag for correction.
# Example: binary treatment (nominal) → use proportion & bar, not mean code:
import pandas as pd
df = pd.DataFrame({"group": ["A","B","A","B","A","A","B"], "outcome": [0,1,0,1,0,1,1]})
counts = df.groupby("group")["outcome"].value_counts(normalize=True).unstack(fill_value=0)
print(counts)
counts.plot(kind="bar", stacked=True)
You can also encode nominal codes as pd.Categorical (unordered) so that accidental numeric ops error out.
3) Where it goes
Page: 0.1 Measurement Scales & Variable Types
Section: After the definitions / examples of nominal, ordinal, interval, ratio — insert a new subheading “0.2 From Scales → Choosing Summaries & Visuals (Statistical hygiene)”
4) Micro‑exercises (with brief answers)
-
Variable “color of car” (red/blue/green) → what summary metric and plot do you choose?
Answer: counts / proportions; bar chart (categorical); not mean or histogram. -
Variable “customer satisfaction” rated 1–5, with ordinal scale → is taking the mean valid?
Answer: only if you assume equal spacing; safer to use median, rank correlation, or treat it as ordinal in box/rank plots. -
You see code
df["gender_code"].mean()wheregender_codeis encoded 0 = male, 1 = female. What’s wrong and what to replace?
Answer: Taking a mean of nominal binary is meaningless — instead compute proportiondf["gender_code"].mean()is okay but only if clearly documented and interpreted as proportion; better: treat as nominal and show counts/proportions with bar chart.
5) Expand Path (optional)
Next pages / sections to build:
- 1. Probability → as earlier patch suggested: events, conditional, Bayes, total probability.
- 2. Descriptive Statistics → deeper: variance, covariance, quantiles; tie to scale table above.
- 3. Visualization → mapping plot types to data shapes and objectives (e.g. scatter, line, faceting).
- 4. Data Cleaning / Type Enforcement → enforcing categorical dtype, safe coercion, missingness policies.
- 5. Inference / Hypothesis Testing → building on probability + descriptive foundations.
That single inserted “scale → summary/plot” guide will reduce many future misapplications, and anchor all downstream choices to sound foundations.
Micro‑exercises:
- Q1: A variable “browser type” (Chrome, Firefox, Safari) — what summary metric and plot should you use?
A1: counts or proportions; bar chart. (Not mean or histogram of codes.) - Q2: A satisfaction rating 1‑5 (ordinal) — is it always valid to compute the mean?
A2: No — only if spacing is truly equal and you treat it as interval; safer: median or percentiles. - Q3: A variable “time since signup (in days)” (ratio) — what summaries/plots are valid?
A3: mean, sd, ratios (e.g., twice as long), histogram, density plot; log scale if heavy tail.