Correlation vs Causation (Pearson, Spearman)
Correlation measures association
Section titled “Correlation measures association”Correlation answers:
- “Do variables move together?”
It does not answer:
- “Does X cause Y?”
Pearson correlation
Section titled “Pearson correlation”- Measures linear relationship
- Sensitive to outliers
import numpy as np
from scipy import stats
x = np.array([1, 2, 3, 4, 5, 6])
y = np.array([2, 4, 5, 4, 5, 7])
r, p = stats.pearsonr(x, y)
print("r:", r)
print("p:", p)Spearman correlation
Section titled “Spearman correlation”- Uses ranks
- Captures monotonic relationships
- More robust to outliers and non-linearity
import numpy as np
from scipy import stats
x = np.array([1, 2, 3, 4, 5, 6])
y = np.array([10, 9, 7, 6, 3, 1])
rho, p = stats.spearmanr(x, y)
print("rho:", rho)
print("p:", p)Visualize it
Section titled “Visualize it”Pearson’s r measures how tightly two variables move together: near +1 the points hug
a rising line, near 0 there’s no pattern, near −1 they hug a falling line. Watch the
cloud tighten and flip as r changes:
Practical guidance
Section titled “Practical guidance”- Plot scatter first.
- Consider transformations (log) if scales vary.
- Be cautious: confounders can create spurious correlation.
Why correlation isn’t causation
Section titled “Why correlation isn’t causation”flowchart LR Z["Confounder
(e.g. summer heat)"] --> X["Ice cream sales"] Z --> Y["Drowning incidents"] X -.->|"correlated, not causal"| Y
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Pearson correlation
Section titled “Exercise 1 – Pearson correlation”Exercise 2 – Spearman for a monotonic (non-linear) trend
Section titled “Exercise 2 – Spearman for a monotonic (non-linear) trend”Exercise 3 – Spotting a possible confounder
Section titled “Exercise 3 – Spotting a possible confounder”Continue to Non-Parametric Tests for association and comparison tests that don’t assume normal, linear relationships.
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading