-
-
Notifications
You must be signed in to change notification settings - Fork 57
[Feature] cor_diff() : test for differences between correlations #338
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
base: main
Are you sure you want to change the base?
Conversation
@bwiernik @mattansb do you know of any way to compute a Bayes factor for that? |
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unfamiliar with a Bayes factor for such a comparison. |
|
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 |
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() |
Simple function with parametric vs. bootstrapped approaches. I ran some simulations and it seems to nicely work.
Created on 2025-01-29 with reprex v2.1.1