# Correlation Method Card: [Your study question or course]

> A method card for measuring and testing the association between two variables: Pearson's r, Spearman's rho,
> Kendall's tau, partial correlation, confidence intervals through Fisher's z, and the comparison of two correlations.
> It covers the assumptions, the influence of single points, restriction of range, and why a correlation is not a
> causal effect. It is for students preparing for an exam, master's and PhD students exploring thesis data, and
> analysts who need a quick, honest measure of association. It speeds up learning by putting the decisions in order
> (which coefficient, which checks, which interval, which sentence to report), by showing one example fully solved,
> one half solved and one left for you, and by ending with self-test questions whose answers sit in a separate key. The
> worked examples use fictional data; replace them with your own.
> Source basis: Pearson (1896), Spearman (1904), Kendall (1938), Fisher (1915, 1921), Anscombe (1973), the
> NIST/SEMATECH e-Handbook of Statistical Methods, and the R and SciPy documentation, all written up in original words
> and examples.

<!--
Guidance: How to use this template
- Draw the scatter plot before you compute anything. A correlation coefficient summarises a picture; without the
  picture it can mislead (see the influence example).
- Read "When to use it" and pick the coefficient from the measurement level and the shape of the relation.
- Run Worked example A in R or Python exactly as written and check you get the same output. Then fill every [ ] in
  Worked example B before you look at the answer key. Do Exercise C by hand first, then with code.
- Always report the interval, not only r and p: with small samples the interval is wide.
- Answer the self-test questions from memory, then scroll to the answer key. Come back after two days and after a week.
- Related templates: academic/stats-eda-report, academic/stats-linear-regression, academic/stats-effect-sizes,
  academic/stats-power-sample-size, academic/stats-confidence-intervals, academic/stats-hypothesis-testing,
  academic/stats-nonparametric-tests, academic/stats-data-transformations, academic/stats-test-selector,
  academic/stats-results-reporting, academic/stats-error-log, science/observational-study.
-->

## Document control

<!-- Guidance: Record what the card is for and which software produced the outputs, so that every number can be reproduced. Update the version when you replace the example data. -->

| Field | Value |
|---|---|
| Course or project | [STAT 101 / thesis chapter 4 / client project] |
| Learner or analyst | [Your name] |
| Data set | [Lakemoor University sleep and quiz study, 15 students (fictional) / your data set and source] |
| Software and versions | R 4.5.1 (stats); Python 3.14 with numpy 2.5.4, pandas 3.1.0, scipy 1.18.1, statsmodels 0.15.0 |
| Date | YYYY-MM-DD |
| Version | [0.1] |

## When to use it

<!-- Guidance: Start with the one-sentence rule and the table. If you want to predict one variable from another, or you have a causal question, follow the alternatives column. -->

**One-sentence rule:** use a correlation coefficient when you want one number for the strength and direction of the
association between two variables measured on the same units, with neither variable treated as the outcome.

There are three common coefficients, and the data decide which one you use:

- **Pearson's r:** the strength of the **linear** relation between two quantitative variables. It is the most
  efficient choice when the relation is linear and there are no extreme points, and it is the one most readers expect.
- **Spearman's rho:** Pearson's r computed on the **ranks**. It measures **monotonic** association (as one goes up,
  the other tends to go up, not necessarily in a straight line), and is resistant to outliers. Use it for ordinal data
  or curved but monotonic relations.
- **Kendall's tau:** the difference between the proportions of concordant and discordant pairs. Also rank-based; it
  has a direct probability reading and behaves well with small samples and many ties. Its values are usually smaller
  than rho for the same data, so do not compare tau with rho or r on the same scale.

**Partial correlation** measures the association between two variables after removing the linear effect of one or
more other variables from both.

| Use when | Do not use when |
|---|---|
| Two quantitative or ordinal variables measured on the same units | One variable is nominal with more than two categories (use group comparisons or Cramér's V) |
| You want a symmetric measure of association | You want to predict Y from X or estimate a slope in units (use regression) |
| The relation is linear (Pearson) or monotonic (Spearman, Kendall) | The relation is U-shaped or otherwise non-monotonic (r near 0 hides a strong pattern) |
| Observations are independent pairs | Repeated measures on the same people are pooled (use repeated-measures correlation or a mixed model) |
| You want to describe, then perhaps test, an association | You want to claim that X causes Y (needs a design, not a coefficient) |

**Alternatives:** simple linear regression when one variable is the outcome (`academic/stats-linear-regression`);
Cramér's V or phi for two categorical variables (`academic/stats-contingency-tables`); point-biserial correlation (it
is Pearson's r with a 0/1 variable) or a t-test for a binary and a quantitative variable (`academic/stats-t-tests`);
intraclass correlation for agreement between raters (`academic/stats-reliability-factor-analysis`); bootstrap CIs
for any coefficient (`academic/stats-resampling-methods`).

## The question and the data

<!-- Guidance: Name both variables, their measurement levels and the unit of observation. Note any third variable that could drive both. Type the example data as a table so a reader can check it by eye. -->

**Question (example).** Among Lakemoor University STAT 101 students (fictional), is average nightly sleep associated
with the week 6 quiz score, and does the association remain once weekly study hours are taken into account?
**Unit:** one student. **Variables:** sleep (hours per night, average over two weeks, ratio scale), study (hours per
week, ratio), quiz (score out of 20, treated as interval). **Design:** observational, one measurement per student,
n = 15.

| Student | Sleep (h) | Study (h/week) | Quiz (/20) | | Student | Sleep (h) | Study (h/week) | Quiz (/20) |
|---|---|---|---|---|---|---|---|---|
| 1 | 5.5 | 3 | 11 | | 9 | 8.4 | 7 | 17 |
| 2 | 6.0 | 6 | 13 | | 10 | 5.1 | 3 | 12 |
| 3 | 6.5 | 4 | 12 | | 11 | 6.8 | 4 | 14 |
| 4 | 7.0 | 5 | 15 | | 12 | 6.6 | 6 | 13 |
| 5 | 7.5 | 7 | 16 | | 13 | 7.7 | 5 | 14 |
| 6 | 8.0 | 6 | 14 | | 14 | 7.9 | 8 | 18 |
| 7 | 6.2 | 2 | 10 | | 15 | 5.8 | 4 | 11 |
| 8 | 7.1 | 8 | 15 | | | | | |

Correlation matrix (Pearson): sleep and quiz 0.813; sleep and study 0.711; study and quiz 0.856. Study hours are
strongly related to both sleep and quiz, so they are a candidate **confounder**: part of the sleep-quiz association
may exist because students who study more also sleep more (a more organised week) and score higher.

## Assumptions and how to check each

<!-- Guidance: Check each row before you trust the coefficient or its p-value. The scatter plot covers the first three rows at once. -->

| Assumption | Check | R | Python | If violated |
|---|---|---|---|---|
| Independent pairs (one row per unit) | Design: no repeated measures, no clusters | n/a | n/a | Repeated-measures correlation, mixed model, or one value per unit |
| Linear relation (Pearson) | Scatter plot; add a smoother | `plot(x, y); lines(lowess(x, y))` | `plt.scatter(x, y)` | Spearman or Kendall for monotonic; transform; regression with a curve |
| No single point dominating | Scatter; r with and without each point | `cor(x[-i], y[-i])` | `np.corrcoef(np.delete(x, i), np.delete(y, i))` | Check the point; report both; use Spearman |
| Bivariate normality, only for the Pearson test and Fisher CI | Q-Q plots of each variable; look for skew and outliers | `qqnorm(x)` | `stats.probplot(x, plot=plt)` | Spearman or Kendall; bootstrap CI |
| Full range of both variables sampled | Compare observed range with the population range | `range(x)` | `x.min(), x.max()` | Report the restriction; do not generalise beyond the range |
| Ties (rank methods) | Count repeated values | `sum(duplicated(x))` | `x.duplicated().sum()` | Kendall's tau-b; asymptotic p-values (both programs warn or switch) |

## How it works

<!-- Guidance: Read the plain-language idea first, then the formulas. The notation table is the reference for the rest of the card. -->

Pearson's r asks: when a student is above average on X, are they also above average on Y, and by how much in standard
units? It multiplies each student's two z-scores, adds the products and averages them. Points in the upper-right and
lower-left quadrants (both above or both below the means) add positive products; points in the other two quadrants add
negative ones. The result lies between -1 and +1. $r^2$ is the proportion of the variance of Y that a straight line in X
accounts for (and vice versa).

$$
r = \frac{\sum_{i=1}^{n}(x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum_{i=1}^{n}(x_i - \bar{x})^2}\sqrt{\sum_{i=1}^{n}(y_i - \bar{y})^2}} = \frac{1}{n-1}\sum_{i=1}^{n} z_{x,i}\, z_{y,i}
$$

Test of $H_0: \rho = 0$, with df $= n - 2$:

$$
t = r\sqrt{\frac{n-2}{1-r^2}}
$$

Spearman's rho is Pearson's formula applied to the ranks $R(x_i)$ and $R(y_i)$. Without ties it simplifies to

$$
r_s = 1 - \frac{6\sum d_i^2}{n(n^2 - 1)}, \qquad d_i = R(x_i) - R(y_i)
$$

Kendall's tau compares every pair of units. A pair is **concordant** if the unit higher on X is also higher on Y, and
**discordant** if it is lower. With $C$ concordant and $D$ discordant pairs and no ties,
$\tau = (C - D)/\binom{n}{2}$. Tau-b (the version R and SciPy report) adjusts the denominator for ties.

**Fisher's z and the confidence interval.** The sampling distribution of r is skewed when $\rho \neq 0$, so the CI is
built on a transformed scale where it is close to normal, then transformed back:

$$
z' = \operatorname{artanh}(r) = \frac{1}{2}\ln\frac{1 + r}{1 - r}, \qquad SE_{z'} = \frac{1}{\sqrt{n - 3}}, \qquad \text{CI} = \tanh\left(z' \pm 1.96\, SE_{z'}\right)
$$

The interval is not symmetric around r on the original scale; it is longer on the side towards zero.

**Comparing two independent correlations** (two separate samples):

$$
z = \frac{z'_1 - z'_2}{\sqrt{\dfrac{1}{n_1 - 3} + \dfrac{1}{n_2 - 3}}}
$$

Correlations from the **same** sample that share a variable (for example $r_{XY}$ and $r_{XZ}$) are dependent, and need
a different test (Steiger 1980), not this formula.

**Partial correlation** of X and Y given Z: regress X on Z and Y on Z, and correlate the two sets of residuals. With
one control variable,

$$
r_{XY \cdot Z} = \frac{r_{XY} - r_{XZ}\, r_{YZ}}{\sqrt{(1 - r_{XZ}^2)(1 - r_{YZ}^2)}}, \qquad t = r_{XY \cdot Z}\sqrt{\frac{n - 3}{1 - r_{XY \cdot Z}^2}}, \quad \text{df} = n - 3
$$

Each extra control variable removes one more degree of freedom.

| Symbol | Meaning |
|---|---|
| $n$ | Sample size (number of pairs) |
| $r$, $\rho$ | Sample and population Pearson correlation |
| $r_s$ | Spearman's rank correlation (rho) |
| $\tau$ | Kendall's tau (tau-b with ties) |
| $z_{x,i}$ | z-score of $x_i$, using $s$ with divisor $n - 1$ |
| $z'$ | Fisher-transformed correlation |
| $SE_{z'}$ | Standard error on the Fisher scale |
| $r_{XY \cdot Z}$ | Partial correlation of X and Y controlling for Z |
| $C, D$ | Numbers of concordant and discordant pairs |
| df | Degrees of freedom |

Choosing the coefficient:

```mermaid
flowchart TD
  A["Two variables, same units"] --> B{"Measurement level?"}
  B -- "Both nominal" --> C["Cramer's V or phi"]
  B -- "One binary, one quantitative" --> D["Point-biserial r or t-test"]
  B -- "Ordinal or quantitative" --> E["Draw the scatter plot"]
  E --> F{"Shape of the relation?"}
  F -- "Linear, no dominant points" --> G["Pearson r with Fisher CI"]
  F -- "Monotonic but curved, or outliers" --> H["Spearman rho"]
  F -- "Small n or many ties" --> I["Kendall tau-b"]
  F -- "U-shape or other" --> J["Do not summarise with r: model the curve"]
  G --> K{"Third variable could drive both?"}
  H --> K
  I --> K
  K -- "Yes" --> L["Partial correlation or regression with controls"]
  K -- "No" --> M["Report coefficient, CI, n"]
  L --> M
```

### Influence: one point can make the correlation

The scatter below shows 11 Lakemoor students' daily coffee cups and quiz scores (fictional). Ten students show a weak
**negative** pattern (r = -0.293). One student drank 9 cups and scored 88. With that one student included, Pearson's
r jumps to **0.801** (p = .003); Spearman's rho, which only sees that student as "most cups, highest score", is 0.023.
The red line is the least-squares fit with all 11 students (score = 58.08 + 2.76 × cups); the grey line excludes
student 11 (score = 63.06 - 0.90 × cups).

```vega-lite
{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "title": "One point drives r: coffee cups vs quiz score (fictional)",
  "width": 380,
  "height": 240,
  "data": {"values": [
    {"cups": 1, "score": 62, "who": "Students 1-10"}, {"cups": 2, "score": 58, "who": "Students 1-10"},
    {"cups": 1, "score": 66, "who": "Students 1-10"}, {"cups": 3, "score": 60, "who": "Students 1-10"},
    {"cups": 2, "score": 64, "who": "Students 1-10"}, {"cups": 0, "score": 61, "who": "Students 1-10"},
    {"cups": 3, "score": 57, "who": "Students 1-10"}, {"cups": 1, "score": 59, "who": "Students 1-10"},
    {"cups": 2, "score": 67, "who": "Students 1-10"}, {"cups": 0, "score": 63, "who": "Students 1-10"},
    {"cups": 9, "score": 88, "who": "Student 11"}
  ]},
  "layer": [
    {
      "mark": {"type": "point", "filled": true, "size": 70},
      "encoding": {
        "x": {"field": "cups", "type": "quantitative", "title": "Cups of coffee per day"},
        "y": {"field": "score", "type": "quantitative", "title": "Quiz score", "scale": {"zero": false}},
        "color": {"field": "who", "type": "nominal", "title": null}
      }
    },
    {
      "transform": [{"regression": "score", "on": "cups"}],
      "mark": {"type": "line", "color": "#c0392b"},
      "encoding": {"x": {"field": "cups", "type": "quantitative"}, "y": {"field": "score", "type": "quantitative"}}
    },
    {
      "transform": [{"filter": "datum.who === 'Students 1-10'"}, {"regression": "score", "on": "cups"}],
      "mark": {"type": "line", "color": "#7f8c8d", "strokeDash": [4, 3]},
      "encoding": {"x": {"field": "cups", "type": "quantitative"}, "y": {"field": "score", "type": "quantitative"}}
    }
  ]
}
```

```text
all 11            Pearson r = 0.801 (p = 0.003)  Spearman rho = 0.023 (p = 0.946)
without point 11  Pearson r = -0.293 (p = 0.411)  Spearman rho = -0.313 (p = 0.379)
```

A point far from the others in **X** has high leverage: it acts like a lever on the line and on r. The lesson is not
"delete it", but "look at it": check whether it is an error, report r with and without it, and consider a rank-based
coefficient. Anscombe (1973) built four data sets with the same r (0.816) and very different pictures to make the same
point.

### Restriction of range

If you sample only part of the range of X, the correlation usually shrinks. In the sleep data, r = 0.813 for all 15
students but only 0.369 for the 7 students who sleep 7 hours or more. Nothing about the relationship changed; the
smaller spread of X leaves less signal relative to the scatter. Typical cases: admission test scores correlated with
grades among admitted students only, or a study recruited only from high performers. Say so in the limitations and do
not generalise beyond the observed range.

### Correlation, causation and confounding

A correlation between X and Y can arise because X affects Y, Y affects X, a third variable affects both (confounding),
or because of how units were selected (selection bias), or by chance. The diagram shows the confounding story for the
example.

```mermaid
flowchart TD
  Z["Study organisation: weekly study hours"] --> X["Sleep hours"]
  Z --> Y["Quiz score"]
  X -. "association seen: r = 0.81" .-> Y
  X -- "possible direct effect?" --> Y
  S["Selection: only students who attended week 6"] --> X
  S --> Y
```

Partial correlation removes the **linear** effect of the measured confounder. It cannot remove confounders you did not
measure, so it never turns an observational correlation into a causal estimate. For causal questions, randomise
(`academic/stats-experiment-design`) or use a design for observational data with stated assumptions
(`science/observational-study`).

## Worked example A (fully solved): sleep and quiz score

<!-- Guidance: Follow every step: plot, hypotheses, checks, code, hand calculation of t and the Fisher CI, rank coefficients, partial correlation, interpretation and the report sentence. Compare your output line by line. -->

**Step 1. Plot.** The scatter of quiz against sleep (not shown; draw it with `plot(lab$sleep, lab$quiz)`) rises
roughly linearly with no isolated points. Pearson is appropriate; the rank coefficients are reported as a check.

**Step 2. Hypotheses.** $H_0: \rho = 0$; $H_1: \rho \neq 0$; $\alpha = 0.05$, two-sided.

**Step 3. Checks.** 15 students, one row each (independent pairs). Sleep ranges 5.1 to 8.4 hours, a reasonable spread.
No point dominates: dropping any one student leaves r between 0.773 and 0.850. Ties: quiz has repeated values (11,
12, 13, 14, 15 occur more than once), so rank methods use tie corrections and asymptotic p-values.

**R code:**

```r
lab <- data.frame(
  sleep = c(5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 6.2, 7.1, 8.4, 5.1, 6.8, 6.6, 7.7, 7.9, 5.8),
  study = c(3, 6, 4, 5, 7, 6, 2, 8, 7, 3, 4, 6, 5, 8, 4),
  quiz  = c(11, 13, 12, 15, 16, 14, 10, 15, 17, 12, 14, 13, 14, 18, 11)
)
cor.test(lab$sleep, lab$quiz)                       # Pearson (default)
cor.test(lab$sleep, lab$quiz, method = "spearman")
cor.test(lab$sleep, lab$quiz, method = "kendall")
# Partial correlation by residuals
ex <- resid(lm(sleep ~ study, data = lab))
ey <- resid(lm(quiz ~ study, data = lab))
pr <- cor(ex, ey); n <- nrow(lab)
t <- pr * sqrt((n - 3) / (1 - pr^2))
round(c(partial_r = pr, t = t, df = n - 3, p = 2 * pt(-abs(t), n - 3)), 4)
round(cor(lab), 3)
```

```text
	Pearson's product-moment correlation

data:  lab$sleep and lab$quiz
t = 5.032, df = 13, p-value = 0.0002296
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.5151007 0.9355631
sample estimates:
      cor
0.8128732

	Spearman's rank correlation rho

data:  lab$sleep and lab$quiz
S = 90.635, p-value = 9.537e-05
alternative hypothesis: true rho is not equal to 0
sample estimates:
      rho
0.8381512

Warning message:
In cor.test.default(lab$sleep, lab$quiz, method = "spearman") :
  Cannot compute exact p-value with ties

	Kendall's rank correlation tau

data:  lab$sleep and lab$quiz
z = 3.3972, p-value = 0.0006809
alternative hypothesis: true tau is not equal to 0
sample estimates:
      tau
0.6703494

Warning message:
In cor.test.default(lab$sleep, lab$quiz, method = "kendall") :
  Cannot compute exact p-value with ties
partial_r         t        df         p
   0.5614    2.3501   12.0000    0.0367
      sleep study  quiz
sleep 1.000 0.711 0.813
study 0.711 1.000 0.856
quiz  0.813 0.856 1.000
```

**Python code:**

```python
import numpy as np, pandas as pd
from scipy import stats
import statsmodels.formula.api as smf

lab = pd.DataFrame({
    "sleep": [5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 6.2, 7.1, 8.4, 5.1, 6.8, 6.6, 7.7, 7.9, 5.8],
    "study": [3, 6, 4, 5, 7, 6, 2, 8, 7, 3, 4, 6, 5, 8, 4],
    "quiz":  [11, 13, 12, 15, 16, 14, 10, 15, 17, 12, 14, 13, 14, 18, 11],
})
n = len(lab)
x, y = lab["sleep"], lab["quiz"]

r = stats.pearsonr(x, y)
print(f"Pearson r = {r.statistic:.4f}, p = {r.pvalue:.6f}, 95% CI {np.round(r.confidence_interval(0.95), 4)}")
rho = stats.spearmanr(x, y)
print(f"Spearman rho = {rho.statistic:.4f}, p = {rho.pvalue:.6f}")
tau = stats.kendalltau(x, y)
print(f"Kendall tau-b = {tau.statistic:.4f}, p = {tau.pvalue:.6f}")

# Fisher z confidence interval by hand
z = np.arctanh(r.statistic); se = 1 / np.sqrt(n - 3)
print(f"z = {z:.4f}, SE = {se:.4f}, CI = {np.round(np.tanh([z - 1.959964*se, z + 1.959964*se]), 4)}")

# Partial correlation of sleep and quiz, controlling for study (residual method)
ex = smf.ols("sleep ~ study", data=lab).fit().resid
ey = smf.ols("quiz ~ study", data=lab).fit().resid
pr = np.corrcoef(ex, ey)[0, 1]
t = pr * np.sqrt((n - 3) / (1 - pr**2))
print(f"partial r = {pr:.4f}, t({n-3}) = {t:.3f}, p = {2*stats.t.sf(abs(t), n-3):.4f}")
print(lab.corr().round(3))

# Restriction of range: only students sleeping 7 h or more
sub = lab[lab["sleep"] >= 7]
print(f"n = {len(sub)}, r (sleep >= 7) = {sub['sleep'].corr(sub['quiz']):.3f}")

# Comparing two independent correlations (Fisher z test)
r1, n1, r2, n2 = 0.813, 15, 0.35, 40
zd = (np.arctanh(r1) - np.arctanh(r2)) / np.sqrt(1/(n1 - 3) + 1/(n2 - 3))
print(f"z = {zd:.3f}, p = {2*stats.norm.sf(abs(zd)):.4f}")
```

```text
Pearson r = 0.8129, p = 0.000230, 95% CI [0.5151 0.9356]
Spearman rho = 0.8382, p = 0.000095
Kendall tau-b = 0.6703, p = 0.000681
z = 1.1354, SE = 0.2887, CI = [0.5151 0.9356]
partial r = 0.5614, t(12) = 2.350, p = 0.0367
       sleep  study   quiz
sleep  1.000  0.711  0.813
study  0.711  1.000  0.856
quiz   0.813  0.856  1.000
n = 7, r (sleep >= 7) = 0.369
z = 2.319, p = 0.0204
```

**Step 4. By hand: the t statistic.** $t = 0.8129\sqrt{13/(1 - 0.8129^2)} = 0.8129\sqrt{13/0.3392} = 0.8129 \times 6.191
= 5.03$, df = 13, matching R's t = 5.032.

**Step 5. By hand: the Fisher CI.** $z' = \operatorname{artanh}(0.8129) = 1.1354$; $SE = 1/\sqrt{12} = 0.2887$;
$1.1354 \pm 1.96 \times 0.2887 = [0.5696, 1.7012]$; back-transformed with tanh: [0.515, 0.936]. R and SciPy give the
same interval because both use Fisher's z.

**Step 6. Effect size.** r itself is the effect size: 0.81 is large by Cohen's (1988) rough benchmarks (0.1 small, 0.3
medium, 0.5 large), but benchmarks are a last resort; compare with correlations usually found in your field.
$r^2 = 0.66$: a straight line in sleep accounts for about two thirds of the variance in quiz scores in this sample.

**Step 7. Partial correlation.** Controlling for study hours, the sleep-quiz correlation falls from 0.813 to 0.561,
*t*(12) = 2.35, *p* = .037. The association is weaker but still present once study time is held constant. Check with
the formula: $(0.813 - 0.711 \times 0.856)/\sqrt{(1 - 0.711^2)(1 - 0.856^2)} = 0.2044/0.3635 = 0.56$.

**Step 8. Interpretation.** Students who slept more tended to score higher on the quiz. Part of that association is
shared with study hours. The design is observational, so the result does not show that more sleep raises scores.

**APA-style report sentence.** "Average nightly sleep was positively correlated with quiz score, *r*(13) = .81,
*p* < .001, 95% CI [.52, .94], *n* = 15 (Spearman's $r_s$ = .84). Controlling for weekly study hours, the partial
correlation was smaller, $r_{\text{sleep,quiz} \cdot \text{study}}$ = .56, *t*(12) = 2.35, *p* = .037."

**Where R and Python differ by design.**

| Point | R `cor.test` | SciPy | Note |
|---|---|---|---|
| Pearson CI | Fisher z, shown by default | `pearsonr(...).confidence_interval()` (Fisher z) | Same numbers |
| Spearman p with ties | Asymptotic t approximation, warning "Cannot compute exact p-value with ties" | `spearmanr` always uses the t approximation | Same here (p = 0.000095) |
| Spearman p without ties | Algorithm AS 89 by default (n up to about 1290): exact permutation p for n < 10, an Edgeworth series approximation for larger n | `spearmanr` has no exact option: always the t approximation (use `stats.permutation_test` for an exact p) | Differ for small n: in Exercise C (n = 8), R gives the exact p = .0004 and SciPy p = .00003 |
| Kendall | tau-b; exact p when no ties and n < 50, else normal approximation | tau-b; `method="auto"` picks exact when there are no ties and n is 33 or less | Same here (normal approximation because of ties) |
| Partial correlation | No base function; residual method (or the ppcor package) | No function in scipy or statsmodels; residual method | Same numbers |

## Worked example B (half solved): another cohort and a comparison

<!-- Guidance: Fill every [ ] before you look at the answer key. Show each step of the Fisher transformation. -->

A second Lakemoor cohort of n = 40 students gives r = 0.35 between sleep and quiz.

1. Fisher transform: $z' = \operatorname{artanh}(0.35) =$ [ ].
2. Standard error: $SE = 1/\sqrt{40 - 3} =$ [ ].
3. CI on the z scale: $z' \pm 1.96 \times SE =$ [ , ].
4. Back-transform with tanh: 95% CI for $\rho$ = [ , ].
5. Test of $\rho = 0$: $t = 0.35\sqrt{38/(1 - 0.35^2)} =$ [ ], df = [ ], *p* = .027.
6. Compare with the first cohort (r = 0.813, n = 15): $z = (1.1354 - 0.3654)/\sqrt{1/12 + 1/37} = 2.319$, *p* = .020.
   What does this comparison tell you, and what should you be cautious about? [ ]

## Exercise C (unsolved): Harbour City Library visitors and loans

<!-- Guidance: Do steps 1 to 3 by hand or with a calculator, then check with code. Write the report sentence last. -->

Harbour City Library (fictional) records daily visitors and books loaned on 8 days:

| Day | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Visitors | 120 | 135 | 150 | 160 | 180 | 200 | 210 | 240 |
| Loans | 48 | 50 | 61 | 58 | 70 | 75 | 83 | 90 |

1. Draw the scatter plot. Is Pearson appropriate?
2. Compute r (use software for the sums) and the t statistic with its df.
3. Compute the 95% CI with Fisher's z.
4. Compute Spearman's rho from the rank differences by hand (there are no ties).
5. Run `cor.test(..., method = "spearman")` in R and `stats.spearmanr` in Python. Why are the p-values different?
6. Write an APA-style sentence, and one sentence on what the correlation does and does not tell the library manager.

## Reading software output

<!-- Guidance: Each row says what a line of R or Python output means and whether it goes into your report. -->

| Output line | Meaning | Report it? |
|---|---|---|
| `t = 5.032, df = 13` | Test statistic for $\rho = 0$, df $= n - 2$ | Report df in brackets: *r*(13) |
| `p-value = 0.0002296` | Two-sided p-value | Yes, as *p* < .001 |
| `95 percent confidence interval: 0.515 0.936` | Fisher z interval for $\rho$ | Yes, always |
| `cor 0.8128732` | Sample Pearson r | Yes, two decimals, no leading zero in APA style |
| `S = 90.635` (Spearman) | Sum of squared rank differences statistic | No; report rho and p |
| `z = 3.3972` (Kendall) | Normal approximation statistic | Optional |
| `Cannot compute exact p-value with ties` | Ties present, so an approximation is used | Mention ties if p is borderline |
| `partial_r 0.5614, df 12` | Partial correlation and its df $= n - 2 - k$ | Yes, with the control variables named |

## Reporting template

<!-- Guidance: Fill the sentence and the table, then tick the checklist. For many correlations, a matrix table with n and CIs is clearer than many sentences. -->

**Fill-in sentence.** "[X] was [positively / negatively] correlated with [Y], *r*([n - 2]) = [.xx], *p* = [.xxx],
95% CI [[.xx], [.xx]], *n* = [ ]. [Spearman's $r_s$ = [.xx] gave the same conclusion.] [Controlling for [Z], the
partial correlation was [.xx], *t*([n - 3]) = [ ], *p* = [ ].]"

| Pair | n | r | 95% CI | p | Spearman $r_s$ | Note |
|---|---|---|---|---|---|---|
| *Example:* sleep, quiz | 15 | .81 | [.52, .94] | < .001 | .84 | Partial r given study = .56 |
| *Example:* sleep, study | 15 | .71 | [ ] | [ ] | [ ] | |
| [pair] | [ ] | [ ] | [ ] | [ ] | [ ] | [ ] |

**Results checklist.**

- [ ] Scatter plot drawn (and shown for the key pairs).
- [ ] Coefficient chosen for a stated reason (linear vs monotonic, outliers, ties).
- [ ] r, df (or n), p and 95% CI reported.
- [ ] Influential points checked; r with and without them if they matter.
- [ ] Range of each variable reported; restriction of range discussed.
- [ ] Confounders named; partial correlation or regression used where measured.
- [ ] No causal wording ("led to", "improves") for observational data.
- [ ] Multiple correlations: number of tests stated and multiplicity handled (`academic/stats-hypothesis-testing`).

## Common mistakes and misconceptions

<!-- Guidance: Read these before you write up. Copy any you have made into your error log with the correct version. -->

| Wrong statement | Correct statement |
|---|---|
| "r = 0 means the variables are unrelated." | r = 0 means no **linear** relation; a perfect U-shape can give r = 0 |
| "r = 0.80 means 80% of Y is explained by X." | $r^2$ = 0.64 is the share of variance a straight line accounts for, in this sample |
| "A significant correlation is a strong one." | With n = 1,000, r = 0.07 is significant at .05; significance depends on n, strength does not |
| "r = 0.81 shows that sleep improves scores." | Observational correlation is not causation; confounding (study hours) and reverse paths are possible |
| "The partial correlation removes all confounding." | It removes only the linear effect of the measured control variables |
| "The CI is r ± 1.96 SE." | Use Fisher's z; the CI is asymmetric on the r scale |
| "Spearman and Kendall measure the same thing on the same scale." | Both are rank-based, but tau is usually smaller than rho for the same data |
| "Removing the outlier gives the true r." | Report both; delete only proven errors |
| "A low r in my selected sample means the relation is weak in the population." | Restriction of range lowers r; the full population may show a stronger relation |
| "Pooling repeated measures from the same people gives more power." | Pairs are not independent; use a repeated-measures method |
| "Two correlations differ because one is significant and the other is not." | Test the difference directly (Fisher z for independent samples) |

## Self-test questions

<!-- Guidance: Answer from memory, then check the answer key. Repeat after two days and after a week. -->

1. Ten points give r = -0.29; adding one point far to the right and high gives r = 0.80. What is this property called,
   and what should you report?
2. Why is the 95% CI for r = 0.81 with n = 15 not symmetric around 0.81?
3. You correlate an admission test with first-year grades among admitted students only and find r = 0.15. Why might
   the true relation among all applicants be stronger?
4. A study reports r = 0.45 (p = .03) in women and r = 0.20 (p = .31) in men and concludes that the relation differs by
   gender. What is the correct way to check that claim?
5. For ordinal data (a 5-point satisfaction item) and a small sample with many ties, which coefficient would you
   choose, and why?
6. The correlation between sleep and quiz drops from 0.81 to 0.56 after controlling for study hours. Give two
   different causal stories consistent with this result.

## My notes and next steps

<!-- Guidance: This section is yours. Rate your confidence honestly; it tells you what to review next. -->

| Item | Entry |
|---|---|
| Confidence choosing a coefficient (1-5) | [ ] |
| Confidence with Fisher z CIs (1-5) | [ ] |
| Confidence explaining confounding (1-5) | [ ] |
| Open questions | [What I am still unsure about] |
| Mistakes to log | [Link to `academic/stats-error-log` entry] |
| Next review dates | YYYY-MM-DD (2 days), YYYY-MM-DD (1 week), YYYY-MM-DD (1 month) |
| Next template | [`academic/stats-linear-regression` / `academic/stats-effect-sizes`] |

## Sources and further reading

<!-- Guidance: Cite the method papers you rely on. The formulas are standard; the wording and examples here are original. -->

Based on the original method papers listed below and the NIST/SEMATECH e-Handbook of Statistical Methods; all text,
data and examples are original.

- Pearson, K. (1896). Mathematical contributions to the theory of evolution. III. Regression, heredity, and panmixia.
  *Philosophical Transactions of the Royal Society of London, Series A*, 187, 253–318.
- Spearman, C. (1904). The proof and measurement of association between two things. *American Journal of Psychology*,
  15(1), 72–101.
- Kendall, M. G. (1938). A new measure of rank correlation. *Biometrika*, 30(1–2), 81–93.
- Fisher, R. A. (1915). Frequency distribution of the values of the correlation coefficient in samples from an
  indefinitely large population. *Biometrika*, 10(4), 507–521.
- Fisher, R. A. (1921). On the "probable error" of a coefficient of correlation deduced from a small sample. *Metron*,
  1(4), 3–32.
- Anscombe, F. J. (1973). Graphs in statistical analysis. *The American Statistician*, 27(1), 17–21.
- Steiger, J. H. (1980). Tests for comparing elements of a correlation matrix. *Psychological Bulletin*, 87(2),
  245–251.
- Cohen, J. (1988). *Statistical Power Analysis for the Behavioral Sciences* (2nd ed.). Lawrence Erlbaum Associates.
- NIST/SEMATECH e-Handbook of Statistical Methods, section 1.3.3.26 Scatter Plot,
  https://www.itl.nist.gov/div898/handbook/ (accessed 2026-10-25). Public domain.
- Further reading (by name only): de Smith, M. J. (2024). *Statistical Analysis Handbook* (2024 edition, StatsRef);
  Diez, D. M., Çetinkaya-Rundel, M., and Barr, C. D., *OpenIntro Statistics* (CC BY-SA); American Psychological
  Association, *Publication Manual* (7th ed.) and JARS-Quant.
- Software documentation: R `cor()`, `cor.test()`; SciPy `stats.pearsonr()`, `stats.spearmanr()`,
  `stats.kendalltau()`.

## Answer key

<!-- Guidance: Check only after you have written your own answers. Log every miss in the error log with the reason. -->

**Worked example B.** (1) $z' = 0.3654$. (2) $SE = 1/\sqrt{37} = 0.1644$. (3) $0.3654 \pm 0.3222 = [0.0432, 0.6877]$.
(4) $\tanh$ gives 95% CI [0.04, 0.60]. (5) $t = 0.35\sqrt{38/0.8775} = 0.35 \times 6.581 = 2.30$, df = 38, *p* = .027.
(6) The two cohorts' correlations differ more than sampling error alone would usually produce (*z* = 2.32,
*p* = .020): the association looks weaker in the second cohort. Cautions: the first cohort is small (its CI is
[.52, .94]), the cohorts may differ in range of sleep (restriction of range) or measurement, and the comparison was
not planned in advance.

**Exercise C.** (1) The points rise almost on a straight line with no isolated point; Pearson is appropriate.
(2) r = 0.987, $t = 0.987\sqrt{6/(1 - 0.987^2)} = 14.98$, df = 6, *p* < .001. (3) $z' = \operatorname{artanh}(0.987) =
2.51$, $SE = 1/\sqrt{5} = 0.447$, CI on the z scale [1.63, 3.39], back-transformed [0.93, 1.00] (R: [0.927, 0.998]).
(4) Visitor ranks 1 to 8; loan ranks 1, 2, 4, 3, 5, 6, 7, 8; $d$ = 0, 0, -1, 1, 0, 0, 0, 0; $\sum d^2 = 2$;
$r_s = 1 - 6 \times 2/(8 \times 63) = 1 - 12/504 = 0.976$. (5) Both give rho = 0.976. R reports an **exact**
permutation p-value (p = .0004, statistic S = 2) because there are no ties and n is below 10; SciPy's `spearmanr` uses a
t approximation (p = .00003). For small n, prefer the exact value. Kendall's tau is 0.929 with exact p = .0004 in both
programs. (6) "Daily visitors and loans were strongly correlated, *r*(6) = .99, *p* < .001, 95% CI [.93, 1.00], *n* = 8
days." The correlation says busy days are high-loan days; it does not say how many extra loans one extra visitor
brings (that needs a regression slope), and with 8 days it says nothing about unusual days outside the observed range.

**Self-test.**

1. Influence (a high-leverage point). Check the point against the source, report r with and without it, show the
   scatter, and consider Spearman (here 0.02 with the point).
2. The sampling distribution of r is bounded by 1 and skewed when $\rho$ is far from 0, so the CI is computed on the
   Fisher z scale (where it is symmetric) and transformed back, which stretches the side towards 0: [0.52, 0.94].
3. Restriction of range: admitted students all scored high on the test, so the spread of X is small and r shrinks.
4. Test the difference between the two correlations directly with Fisher's z test for independent samples (or an
   interaction term in a regression). "Significant versus not significant" is not itself a significant difference.
5. Kendall's tau-b: it handles ties well, is designed for ordinal data and has good small-sample behaviour. Spearman is
   also acceptable; Pearson is not, because the item is ordinal.
6. (a) Study organisation affects both sleep and scores (confounding), and sleep also has a smaller direct effect on
   scores. (b) Sleep improves concentration, which allows more effective study, which raises scores: then study hours
   are partly a **mediator**, and controlling for them removes part of a real effect of sleep. The data alone cannot
   tell these stories apart; a design or prior knowledge must.

## Revision history

<!-- Guidance: Add a row each time you change the data, the analysis or the conclusions. -->

| Version | Date | Author | Change summary |
|---|---|---|---|
| 0.1 | YYYY-MM-DD | [Your name] | First draft from the template |
| 0.2 | YYYY-MM-DD | [Your name] | *Example:* Replaced Lakemoor example with thesis questionnaire data; partial correlation added |
