Skip to main content

Correlation

Pearson and Spearman correlation

Pearson correlation and its rank-based partner, Spearman’s rho, on the booby and rope-trick data from lecture.
Author

Bill Perry

📥 Download the R script 📊 booby_aggression.csv 📊 rope_trick.csv

What correlation does

Correlation measures how strongly two continuous variables move together in a straight line. Neither variable is the “response”; both are just measured. The correlation coefficient r runs from −1 (perfect negative) through 0 (none) to +1 (perfect positive).

\(H_0: \rho = 0\) (no linear association) vs. \(H_A: \rho \neq 0\)

Version Use when R call
Pearson’s r Both variables roughly normal, relationship linear cor.test(x, y)
Spearman’s rho Non-normal, outliers, or monotonic but curved cor.test(x, y, method = "spearman")

Correlation is not causation, and r only detects linear patterns. Always plot first.

Step 1: Load packages and data

library(tidyverse)

booby_df <- read_csv("data/booby_aggression.csv")
rope_df  <- read_csv("data/rope_trick.csv")

booby_df
# A tibble: 24 × 2
   visits_as_nestling future_aggression
                <dbl>             <dbl>
 1                  1             -0.8 
 2                  7             -0.92
 3                 15             -0.8 
 4                  4             -0.46
 5                 11             -0.47
 6                 14             -0.46
 7                 23             -0.23
 8                 14             -0.16
 9                  9             -0.23
10                  5             -0.23
# ℹ 14 more rows

Booby data: how often a Nazca booby was visited by unrelated adults as a nestling, and how aggressive it was as an adult.

Step 2: Plot the relationship

booby_df %>%
  ggplot(aes(visits_as_nestling, future_aggression)) +
  geom_point(size = 2) +
  labs(x = "Visits as a nestling", y = "Adult aggression score") +
  theme_bw()

Look for: a roughly straight trend, no strong curve, no single point driving the pattern.

Step 3: Check normality

Pearson’s test assumes both variables are (bivariate) normal.

shapiro.test(booby_df$visits_as_nestling)

    Shapiro-Wilk normality test

data:  booby_df$visits_as_nestling
W = 0.95783, p-value = 0.3965
shapiro.test(booby_df$future_aggression)

    Shapiro-Wilk normality test

data:  booby_df$future_aggression
W = 0.91575, p-value = 0.04709
booby_df %>%
  pivot_longer(everything()) %>%
  ggplot(aes(sample = value)) +
  geom_qq() +
  geom_qq_line() +
  facet_wrap(~ name, scales = "free") +
  theme_bw()

Step 4: Pearson correlation

booby_test <- cor.test(booby_df$visits_as_nestling,
                       booby_df$future_aggression)
booby_test

    Pearson's product-moment correlation

data:  booby_df$visits_as_nestling and booby_df$future_aggression
t = 2.9603, df = 22, p-value = 0.007229
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.1660840 0.7710999
sample estimates:
      cor 
0.5337225 

Reading the output:

  • cor: r = 0.53, a moderate positive association.
  • t, df: the test of \(H_0: \rho = 0\); df = n − 2.
  • p-value: 0.0072, so we reject \(H_0\).
  • 95% CI: the plausible range for ρ. Wide CIs are normal with small n.
  • r² = 0.28 is the share of variation the two variables have in common.

Step 5: When normality fails — Spearman’s rho

Rope trick data: years since a person saw the Indian rope trick, and how impressive they remember it being.

shapiro.test(rope_df$impressiveness)

    Shapiro-Wilk normality test

data:  rope_df$impressiveness
W = 0.82319, p-value = 0.001517
rope_df %>%
  ggplot(aes(years_since_seen, impressiveness)) +
  geom_point(size = 2) +
  labs(x = "Years since seen", y = "Impressiveness score") +
  theme_bw()

Impressiveness is a 1–5 score and is not normal, so use Spearman. It correlates the ranks, not the raw values.

rope_test <- cor.test(rope_df$years_since_seen,
                      rope_df$impressiveness,
                      method = "spearman")
rope_test

    Spearman's rank correlation rho

data:  rope_df$years_since_seen and rope_df$impressiveness
S = 332.12, p-value = 2.571e-05
alternative hypothesis: true rho is not equal to 0
sample estimates:
      rho 
0.7843363 

The warning about ties is expected with a 1–5 score; R uses an approximate p-value.

Step 6: Write it up

Adult aggression increased with the number of visits a booby received as a nestling (Pearson’s r = 0.53, t(22) = 2.96, p = 0.0072, n = 24).

Remembered impressiveness of the rope trick increased with years since it was seen (Spearman’s ρ = 0.78, p < 0.001, n = 21).

Report r (or ρ), the test statistic and df for Pearson, p, and n. Show the scatterplot.