Skip to the content.

Fisher Transform Correlation Test

R Markdown

Let’s create some fake data of two variables with a known correlation, and then test the correlation using the Pearson correlation test. We will also calculate the p-value manually using the Fisher transform.

library(MASS)
n_samples <- 25
mean_vector <- c(0, 0)
correlation <- 0.15   # Desired correlation between the variables

Next, we generate the data.

cov_matrix <- matrix(c(1, correlation, correlation, 1), nrow = 2)
sample_data <- mvrnorm(n = n_samples, mu = mean_vector, Sigma = cov_matrix)

Next, we use cor.test to get the correlation and p-value.

x <- sample_data[, 1]
y <- sample_data[, 2]

result <- cor.test(x, y, method = 'pearson', exact = TRUE)
estimate <- as.numeric(result$estimate)
result
## 
##  Pearson's product-moment correlation
## 
## data:  x and y
## t = 0.93999, df = 23, p-value = 0.357
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.2194690  0.5459781
## sample estimates:
##       cor 
## 0.1923416

We can also use the Fisher transform to get more or less the same result. This approach is more generic as it allows to test against any correlation value (not only $\rho = 0$).

Let’s first set an assumed population correlation $\rho$.

assumed_population_correlation <- 0

Now, we can test our observed correlation against the assumed population correlation using the Fisher transform.

transformed_estimate <- 0.5 * log((1 + estimate) / (1 - estimate))

# calculate the mean and standard deviation of the fisher transform
mn_z <- 0.5 * log((1 + assumed_population_correlation) / (1 - assumed_population_correlation))
sd_z <- 1 / sqrt(n_samples - 3)

# Run two sided test
z_score <- (transformed_estimate - mn_z) / sd_z
# Two-sided p-value
p_value <- 2 * (1 - pnorm(abs(z_score)))
p_value
## [1] 0.3609581