0.3 Data, Measured - JulTob/Data GitHub Wiki

Data, Measured

The Map and the Territory

We study the territory (reality) by making a map (data). Every mistake in analysis is either (a) a bad map or (b) using the map for the wrong terrain.

  • Unit of analysis (u): the “who/what” you measure (a person, a transaction, a day).
  • Variable (X): a measurable attribute on units.
  • Population (𝒫): all units of interest.
  • Sample (S ⊂ 𝒫): the units observed.
  • Measurement scale: nominal / ordinal / interval / ratio.
  • Study design: observational vs experimental; cross-sectional vs longitudinal.

Before you compute anything:

  1. Name the unit, the variable, and its scale.
  2. Declare allowed operations for that scale.
  3. Check if the sample represents the population you’ll generalize to.

Variables as Functions

A variable is a function from units to values:

$$ X:\ \mathcal U \to \mathcal V,\qquad u \mapsto X(u). $$

  • Nominal: just labels (e.g., browser ∈ {Chrome, Firefox,…}).
  • Ordinal: labels with order (e.g., Likert 1–5), no guaranteed spacing.
  • Interval: equal steps, no true zero (°C). Differences make sense; ratios do not.
  • Ratio: equal steps with true zero (kg, €, time). Differences and ratios make sense.

Signal vs symbol. If you numerically encode labels, you’ve changed the symbols, not the signal. The signal lives in the scale.

Micro-lab.

Classify each:

  1. “city”
  2. satisfaction 1–5
  3. temperature °C
  4. income €.

:

Answers 1. nominal 2. ordinal 3. interval 4. ratio
---

Scale → Valid Math

You’re allowed to do only what the scale permits.

Scale Valid comparisons Legit summaries Misuse to avoid Safe visuals
Nominal = / ≠ counts, proportions, mode mean/SD, correlations on codes bar/mosaic
Ordinal order, percentiles median, IQR, rank stats (Spearman) arithmetic on codes; unqualified mean ordered bar; box/violin by group
Interval differences mean, SD, z-scores ratio talk (“twice as hot” in °C) histogram/KDE; line
Ratio differences, ratios mean, SD, geometric mean (>0) — histogram (log-x if skew), scatter

Statistic contracts (preconditions).

  • Mean( X ) requires interval or ratio.
  • Correlation(X,Y) requires both at least interval (Pearson). For ordinal, use Spearman.
  • Geometric mean requires positive ratio data.

From Study to Numbers

Sampling & Bias

Let (S\subset\mathcal P). If selection ≠ random w.r.t. (X), estimates are biased. Common gremlins: convenience samples, survivorship bias, nonresponse bias.

Reliability & Validity (fast but real)

Measured (X) = true score (T) + error (E).

$$ \text{Var}(X)=\text{Var}(T)+\text{Var}(E). $$

  • Reliability ↑ when (\text{Var}(E)) ↓.
  • Validity = measuring the right construct (map aligns with terrain).

Distributions: What “typical” even means

For a numeric variable (X) on ratio/interval scales:

Center.

$$ \text{Mean } \mu=\frac{1}{n}\sum_{i=1}^n x_i,\quad \text{Median } m=\text{Quantile}_{0.5}(X). $$

Spread.

$$ s^2=\frac{1}{n-1}\sum_{i=1}^n (x_i-\mu)^2,\quad s=\sqrt{s^2},\quad \mathrm{IQR}=\text{Q}_{0.75}-\text{Q}_{0.25}. $$

When to prefer median/IQR: heavy tails or outliers; ordinal scale.

10-second diagnostic. Plot histogram (or ECDF), eyeball skew/outliers, then choose center/spread accordingly.


Visuals that tell the truth

  • Nominal: bar/mosaic; order by frequency to reduce cognitive load.
  • Ordinal: ordered bars; box by group (on ranks).
  • Interval/Ratio: histogram/KDE; scatter; line for time; consider log-axis for multiplicative spread.

Micro-lab (5 min, code). Honor the scale in code.

import pandas as pd
from pandas.api.types import CategoricalDtype

df = pd.DataFrame({
    "browser": ["Chrome","Firefox","Safari","Chrome","Edge","Chrome"],
    "satisf":  ["bad","ok","good","ok","good","good"],
    "temp_C":  [20,22,19,23,21,26],
    "income":  [30_000,50_000,45_000,70_000,60_000,120_000],
})

# Nominal → categorical
df["browser"] = df["browser"].astype("category")

# Ordinal → ordered categorical
Likert = CategoricalDtype(["bad","ok","good"], ordered=True)
df["satisf"] = df["satisf"].astype(Likert)

# Interval / Ratio → numeric
df["temp_C"] = pd.to_numeric(df["temp_C"], errors="coerce")
df["income"] = pd.to_numeric(df["income"], errors="coerce")

# Safe summaries
browser_share = df["browser"].value_counts(normalize=True)      # nominal
satisf_median = df["satisf"].cat.codes.median()                 # ordinal (on ranks)
temp_mean, temp_sd = df["temp_C"].mean(), df["temp_C"].std()    # interval
inc_mean = df["income"].mean()                                  # ratio

Thought Experiments (Dialectics)

  • The Encoded Trap. If you relabel categories as 1,2,3, the numbers change but the facts don’t. Should correlation change? → Only if you wrongly treat labels as interval. Correct move: use counts or rank-based methods.

  • The “Twice as Hot” Illusion. 40 °C vs 20 °C: twice as large? → Not on interval scales. In Kelvin (ratio scale), 313 K vs 293 K is not 2× either.

  • Aggregation vs Truth. Two hospitals: A (small) has lower mortality than B (large) in every condition subgroup, but higher overall. → Simpson’s paradox. Condition on subgroups before global statements.


Scene 8 — Mini-Labs (10 minutes each)

Lab A — Scale-aware pipeline.

  1. Pick one variable of each scale from any dataset.
  2. Enforce dtype (category/ordered/numeric).
  3. Produce exactly one valid summary + one valid plot per variable. Deliverable: a 1-page “Data Dictionary with Contracts”.

Lab B — Outlier ethics. Compute mean/median with and without top 1% incomes. Report the decision rule for which summary you will publish and why.


Retrieval Drills (Active Recall)

  1. Why does mean require interval/ratio? — Because it assumes equal spacing; ordinal spacing is unknown.
  2. Define independence in probability and in plain language. — (P(A\cap B)=P(A)P(B)); knowing one doesn’t change odds of the other.
  3. Name three biases that break generalization. — Selection, survivorship, nonresponse (many others exist).

Cheat Cards (Paste into your wiki)

10.1 Scale → Method

  • Nominal → counts/props; bar; χ² tests; one-hot for models.
  • Ordinal → median/IQR; ordered bar; Mann–Whitney / Spearman; ordinal encodings.
  • Interval → mean/SD; hist/KDE; careful with ratios.
  • Ratio → mean/SD/geomean; log plots if skew; parametrics OK if assumptions hold.

10.2 Statistic Preconditions (as code comments)

# Mean(X): requires interval or ratio
# GeometricMean(X): requires X > 0 (ratio)
# PearsonCorr(X,Y): both interval/ratio, roughly linear, few extreme outliers
# Spearman(X,Y): ordinal or monotone relations

10.3 Minimal dtype enforcement (reusable snippet)

def EnforceScales(df, plan):
    # plan: {"var": {"scale": "nominal|ordinal|interval|ratio", "order": [...]}}
    from pandas.api.types import CategoricalDtype
    for var, spec in plan.items():
        s = spec["scale"]
        if s == "nominal":
            df[var] = df[var].astype("category")
        elif s == "ordinal":
            df[var] = df[var].astype(CategoricalDtype(spec["order"], ordered=True))
        else:
            df[var] = pd.to_numeric(df[var], errors="coerce")
    return df

Where this episode points next

  • Episode 2: Probability as the Language of Uncertainty. Events, conditional probability, total probability, Bayes—why the denominator matters.
  • Episode 3: Descriptive Statistics, Seriously. Expectation, variance, covariance; when the median outperforms the mean.
  • Episode 4: Visualization Grammar. Marks, encodings, and how to choose plots by question, not habit.
  • Episode 5: Cleaning & Types. Missingness, coercion, and making your data dictionary executable.

Appendix · Worked Micro-Examples

A. Choosing the center under skew Given (x = (10, 12, 11, 13, 500)):

$$ \mu = \frac{10+12+11+13+500}{5}=109.2,\quad m=\text{median}(x)=12. $$

Inference: publish median + IQR; show a histogram with logged x-axis if you must show 500.

B. Ordinal safely Likert scores $(L\in{1,2,3,4,5})$ represent order, not spacing. Valid: report median/IQR; use Spearman with another ordinal. If you argue equal spacing (rare but sometimes justified), document it and then treat as interval.

⚠️ **GitHub.com Fallback** ⚠️