How do you test if two correlation coefficients are signficantly different - in GNU R?
That is, if the effect between the same variables (e.g., age and income) is different in two different populations (subsamples).
For background information see How do I compare correlation coefficients of the same variables across different groups and Significance test on the difference of Spearman's correlation coefficient (both at CrossValidated).
Here is a ready-to-use function for GNU R if you want to compare multiple pairs of coefficients (based on Significance of the difference between two correlation coefficients and Quantitative Analysis and Politics, PDF):
cor.diff.test = function(r1, r2, n1, n2, alternative = c("two.sided", "less", "greater")) {
Z1 = 0.5 * log( (1+r1)/(1-r1) )
Z2 = 0.5 * log( (1+r2)/(1-r2) )
diff = Z1 - Z2
SEdiff = sqrt( 1 / (n1 - 3) + 1 / (n2 - 3))
diff.Z = diff / SEdiff
if (alternative == "less") {
return(pnorm(diff.Z, lower.tail=F))
} else if (alternative == "greater") {
return(pnorm(-diff.Z, lower.tail=F))
} else if (alternative == "two.sided") {
return(2 * pnorm( abs(diff.Z), lower.tail=F))
} else {
warning(paste("Invalid alterantive", alternative), domain=NA)
return(NA)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With