Pearson’s correlation

Example RQ and data

RQ: Is there an association between years of service and burnout scores among healthcare workers?

Here’s what the first six rows of the data looks like:

burnout_data |> head()
  years burnout
1    11      10
2    10      16
3    11      23
4    13       9
5    13      14
6     4      16


Here’s how we might plot this data (if you’re not sure what part of the code does, delete it or comment it out and try running what’s leftover to see what changes):

burnout_data |>
  ggplot(aes(x = years, y = burnout)) +
  geom_point(size = 3)

Define hypotheses

Our null hypothesis is that years of service and burnout scores are not correlated—in other words, that they have a Pearson’s correlation \(r_P\) of 0.

Formally:

\[ H_0: r_P = 0 \]

# H0: r_P = 0

Our alternative hypothesis is that there is a correlation between years of service and burnout scores. Formally:

\[ H_1: r_P ≠ 0 \]

# H1: r_P != 0

Run the test

To test whether Pearson’s correlation coefficient is significantly different from zero, we use the function cor.test() and give it the following arguments:

  • the first variable we want to compare
  • the second variable we want to compare

(the order that you name the variables in does not matter for the test)

cor.test(
  burnout_data$years,
  burnout_data$burnout
)

    Pearson's product-moment correlation

data:  burnout_data$years and burnout_data$burnout
t = 0.943, df = 44, p-value = 0.35
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.15598  0.41407
sample estimates:
    cor 
0.14068 

Assumptions we must make

Type of constraint What to check for Pearson’s correlation
Data requirements Both variables are continuous.
Data requirements Data contains no extreme outliers that influence the correlation.
Assumption of linearity The association between the variables is linear.
Assumption of normality Both variables are normally distributed.
Assumption of equal variance Each variable’s variance is equal across the range of the other variable.

Data requirements

First: Both variables are continuous.

Use glimpse() to check this:

glimpse(burnout_data)
Rows: 46
Columns: 2
$ years   <dbl> 11, 10, 11, 13, 13, 4, 12, 6, 14, 16, 11, 7, 6, 4, 16, 3, 12, …
$ burnout <dbl> 10, 16, 23, 9, 14, 16, 19, 14, 25, 14, 17, 20, 13, 10, 24, 3, …

Looks fine! Both are integer data: years is in units of years, burnout is in units of points.


Second: Data contains no extreme outliers that influence the correlation.

Plot the data and see if there are any points that appear far away from the others:

burnout_data |>
  ggplot(aes(x = years, y = burnout)) +
  geom_point(size = 3)

Looks OK to me! No observations appear massively outlying.

Imagine that we were concerned about the observation in the top left of the plot. We want to find out how sensitive our correlation is to that particular observation.

We can find out which row that observation appears in by filtering our dataset for observations where years is 5 and burnout is a bit above 25.

burnout_data |>
  rowid_to_column() |> # add row number as column, so we know what row this observation originally was in
  filter(
    years == 5,
    burnout > 25
  )
  rowid years burnout
1    46     5      26

OK, it’s observation number 46 that appears in the top left of the plot.

Let’s start by looking at the line that the correlation will represent by adding the line of best fit onto our scatterplot:

burnout_data |>
  ggplot(aes(x = years, y = burnout)) +
  geom_point(size = 3) +
  geom_smooth(method = 'lm', se = F)


Now, let’s drop observation number 45 from burnout_data and make the same plot again, to see how much the line changes.

burnout_data |>
  slice(-46) |> # "subtract" row 46
  ggplot(aes(x = years, y = burnout)) +
  geom_point(size = 3) +
  geom_smooth(method = 'lm', se = F)

The correlation becomes slightly more positive, but no massive change. So we don’t need to be concerned that that top-left data point is strongly influencing our correlation.

Assumption of linearity

The association between the variables is sufficiently linear.

Compare the straight line of best fit (from geom_smooth(method = 'lm')) to a wobbly line that follows the data more closely (from geom_smooth(method = 'loess')).

For a perfectly linear association, the wobbly line will perfectly match the straight line. When we have smaller samples, and when there’s more variability in the samples, the lines might diverge a bit and that’s still fine.

burnout_data |>
  ggplot(aes(x = years, y = burnout)) +
  geom_point(size = 3) +
  geom_smooth(method = 'lm', se = F) +
  geom_smooth(method = 'loess', se = F, colour = 'red')

The sample size here is fairly small, so it makes sense for the wobbly line to be pretty wobbly! But I’m still happy to assume that the association is sufficiently linear.

TipExamples of “sufficiently linear” scatterplots

All of the scatterplots below show data that is sampled from a population that contains a linear relationship between the two variables. So all the plots below should be considered linear “enough”.

Code
library(patchwork)

sim_lin <- function(){
  
  n_obs <- round(runif(1, min = 20, max = 50))
  int <- round(runif(1, min = -100, max = 100))
  sigma <- abs(int * round(runif(1, min = 1, max = 10)))  
  # ^ mult resid SD by int to get variability of the right magnitude
  slp <- round(runif(1, min = -10, max = 10))
  
  df <- tibble(
    x = 1:n_obs,
    error = rnorm(n_obs, mean = 0, sd = sigma),
    y = int + (slp * x) + error
  )
  
  return(list(df)) # return as list so we end up with a list of dfs
}

make_scatter <- function(dat){
  # dat must contain continuous variables x and y
  
  dat |>
    ggplot(aes(x = x, y = y)) +
    geom_point() +
    geom_smooth(method = 'lm', se = F) +
    geom_smooth(method = 'loess', se = F, colour = 'red') +
    theme(
      axis.ticks = element_blank(),
      axis.text = element_blank(),
      axis.title = element_blank(),
      plot.margin = margin_auto(0.5, unit = "cm"),
      )
}

set.seed(1)  # so your random numbers are the same as my random numbers

# generate set number of normally distributed datasets
dfs <- replicate(12, sim_lin())

# use patchwork's wrap_plots() to combine each dataset's qq plot into a single graphic
wrap_plots( sapply(dfs, make_scatter) )

(You don’t need to understand the code that created this graphic unless you want to.)

Assumption of normality

Both variables are normally distributed.

Check with a Q-Q plot for each variable:

burnout_data |>
  ggplot(aes(sample = burnout)) +
  geom_qq() +
  geom_qq_line() +
  labs(
    x = "Theoretical quantiles",
    y = 'Sample quantiles',
    title = 'burnout'
  ) 

burnout_data |>
  ggplot(aes(sample = years)) +
  geom_qq() +
  geom_qq_line() +
  labs(
    x = "Theoretical quantiles",
    y = 'Sample quantiles',
    title = 'years'
  ) 

Both groups’ data matches the theoretical normal distribution well enough.

TipExamples of “sufficiently normal” Q-Q plots

All of the Q-Q plots below show data that is sampled from different normal distributions. So all the plots below should be considered normal “enough”.

Code
library(patchwork)

sim_normal <- function(){
  
  # randomly sample from a uniform distribution some parameter values
  n_obs <- round(runif(1, min = 20, max = 50))
  mean <- runif(1, min = -10, max = 10)
  sd <- abs(mean * round(runif(1, min = 1, max = 10)))
  # ^ multiply SD by mean to get variability of the right magnitude
  
  # simulate the data
  df <- tibble(
    x = rnorm(n_obs, mean = mean, sd = sd)
  )
  return(list(df)) # return as list so we end up with a list of dfs
}

make_qq <- function(dat){
  # dat must contain continuous variable x
  
  dat |>
    ggplot(aes(sample = x)) +
    geom_qq() +
    geom_qq_line() +
    labs(
      x = "Theoretical quantiles",
      y = 'Sample quantiles'
    )
}


set.seed(3)  # so your random numbers are the same as my random numbers

# generate set number of normally distributed datasets
dfs <- replicate(12, sim_normal())

# use patchwork's wrap_plots() to combine each dataset's qq plot into a single graphic
wrap_plots( sapply(dfs, make_qq) ) +
  plot_layout(axes = "collect")  # use only one axis label

(You don’t need to understand the code that created this graphic unless you want to.)

Assumption of equal variance

Each variable’s variance is equal across the range of the other variable.

We want a pretty uniform and shapeless cloud of datapoints. A bad sign is a suspicious-looking fan, funnel, or cone shapes in the scatterplot that would indicate unequal variance.

burnout_data |>
  ggplot(aes(x = years, y = burnout)) +
  geom_point(size = 3)

This looks fine to me.

TipExamples of “sufficiently equal variance” in scatterplots

All of the scatterplots below show data that is sampled from a population in which both variables have equal variance across the range of the other variable. So all the plots below should be considered equal “enough” variance.

Code
library(patchwork)

sim_equalvar <- function(){
  
  cov <- 1
  n_obs <- round(runif(1, min = 20, max = 50))
  mean1 <- runif(1, -10, 10)
  mean2 <- runif(1, -10, 10)
  sd1 <- runif(1, 0, 10)
  sd2 <- runif(1, 0, 10)
  df <- MASS::mvrnorm(
    n = n_obs,
    mu = c(mean1, mean2),
    Sigma = Matrix::nearPD(matrix(c(sd1, cov, cov, sd2), nrow = 2))$mat
  )
  colnames(df) <- c('x', 'y')
  df <- as.data.frame(df)
  
  
  return(list(df)) # return as list so we end up with a list of dfs
}

make_scatter <- function(dat){
  # dat must contain continuous variables x and y
  
  dat |>
    ggplot(aes(x = x, y = y)) +
    geom_point() +
    theme(
      axis.ticks = element_blank(),
      axis.text = element_blank(),
      axis.title = element_blank(),
      plot.margin = margin_auto(0.5, unit = "cm"),
      )
}

set.seed(6)  # so your random numbers are the same as my random numbers

# generate set number of normally distributed datasets
dfs <- replicate(12, sim_equalvar())

# use patchwork's wrap_plots() to combine each dataset's qq plot into a single graphic
wrap_plots( sapply(dfs, make_scatter) ) 

(You don’t need to understand the code that created this graphic unless you want to.)

Calculate effect size

The correlation coefficient itself is the effect size measure for correlations.

Interpreting any correlation coefficients:

  • Weak correlations: between .1 and .3 (whether positive or negative)
  • Moderate correlations: between .3 and .5 (whether positive or negative)
  • Strong correlations: greater than .5 (whether positive or negative)

Example write-up

We wanted to find out whether healthcare workers’ years of service are significantly correlated with their burnout scores. In order to analyse these variables using Pearson’s correlation, we checked and accepted a range of assumptions: the data is continuous and contains no influential outliers; the association between variables is sufficiently linear; both variables are sufficiently normally distributed; and both variables have sufficiently equal variance across the range of the other.

We tested the null hypothesis that years of service is not correlated with burnout (\(\r_P = 0\)) and found a weak positive correlation (Pearson’s \(r\) = .14, t(44) = 0.94, p = .35) that was not significantly different from 0. We therefore failed to reject the H0 that healthcare workers’ years of service are not correlated with their burnout scores.

TipReporting the results of a statistical test

The most important thing: Your decision about the null hypothesis should be clear.

You can make sure your decision is clear either by

  • stating that you reject/fail to reject H0, or
  • stating that the result is significant or not.