Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] cor_diff() : test for differences between correlations #338

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

DominiqueMakowski
Copy link
Member

@DominiqueMakowski DominiqueMakowski commented Jan 29, 2025

Simple function with parametric vs. bootstrapped approaches. I ran some simulations and it seems to nicely work.

correlation::cor_diff(iris, c("Sepal.Length", "Sepal.Width"), c("Sepal.Length", "Petal.Width"))
#> Correlation Difference Test
#> 
#> Method     |      t |      p
#> ----------------------------
#> parametric | -10.00 | < .001

Created on 2025-01-29 with reprex v2.1.1

@DominiqueMakowski
Copy link
Member Author

@bwiernik @mattansb do you know of any way to compute a Bayes factor for that?
It uses psych::r.test() internally for the parametric test

R/cor_diff.R Outdated
@@ -28,7 +28,7 @@
cor_diff <- function(data, x, y, x2 = NULL, y2 = NULL, method = "parametric", ...) {

# If pairs are passed
if(length(x) == 2 & length(y) == 2) {
if(length(x) == 2 && length(y) == 2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space_around

@mattansb
Copy link
Member

I'm unfamiliar with a Bayes factor for such a comparison.

@DominiqueMakowski
Copy link
Member Author

bayestestR::p_to_bf() go brrrrrr

@bwiernik
Copy link
Contributor

Didn't look into code yet.

The straightforward approach to these tests is to do a Fisher z transform and then do a z test for the difference (potentially accounting for covariance).

So BF methods analogous to a z test would work

@DominiqueMakowski
Copy link
Member Author

Test:

library(ggplot2)
library(correlation)

rez <- data.frame()
for(r in seq(0, 0.6, length.out=200)) {
  for(n in seq(20, 100, by=30)) {
    
    data <- bayestestR::simulate_correlation(
      n = n, 
      r = matrix(c(
        1.0, r, 0.0,
        r, 1.0, 0.0,
        0.0, 0.0, 1.0), nrow = 3))
    
    rez <- cor_diff(data, x="V1", y="V2", x2="V1", y2="V3", method = "parametric") |>
      datawizard::data_rename("t", "z") |> 
      rbind(cor_diff(data, x="V1", y="V2", x2="V1", y2="V3", method = "bootstrapping", iterations=2000)) |>  
      datawizard::data_modify(n = n, r = r) |> 
      rbind(rez)
  }
}

rez |> 
  datawizard::reshape_longer(select=-c("r", "n", "Method")) |>
  datawizard::data_modify(type = ifelse(name %in% c("p"), "p-value", "z-value")) |> 
  ggplot(aes(x=r, y=value)) +
  geom_smooth(aes(group=interaction(Method, n), color=n), method="loess", se=FALSE, formula = 'y ~ x') +
  geom_point(aes(color=n), alpha=0.1) +
  facet_grid(type~Method, scales="free_y", switch="y") +
  theme_minimal()

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants