--- title: "CorSym: Correlation Estimation For Exchangeable/Symmetrical Variables" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{CorSym: Correlation Estimation For Exchangeable/Symmetrical Variables} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ``` # When to use CorSym CorSym is a correlation estimator designed for exchangeable pair data, where the two values you want the correlation of come in an arbitrary/random order. This occurs frequently in the study of assortative pairing, where we wish to desire if individuals that are paired (meaning dating, co-habitation, marriage, or sharing children) have more similar characteristics (for example, socioeconomics or genetics) than random pairs in the population. More broadly, the exchangeable variables that CorSym requires are essentially separate measures of the same variable (with the same distribution) in two cases (like two people, in assortative pairing). Two variables $X$ and $Y$ are exchangeable if the joint probability distribution of $(X,Y)$ and $(Y,X)$ are the same. We developed this estimator because we found that the obvious way to estimate correlations, using the Pearson estimator, is not only sensitive to the order (the estimates change if order is changed) but also statistically biased if the order is biased, meaning that the first value tends to be smaller than the second, or viceversa. CorSym estimates correlations for data like this without bias, and the answer does not depend on the order of the data. However, most applications requiring correlation estimates do not satisfy these assumptions! For example, if you want to estimate the correlation between age and smoking, you cannot randomly switch the age and smoking status of one observation without doing it to all other observations. In such a case, CorSym is not the right tool and its estimates will be nonsense. # Simulated example: extreme ordering Realistic data ideally is truly correlated, but here for simplicity we only simulate uncorrelated data. You can easily generate correlated data with the `mvtnorm` and other packages, as we do in our paper, but here we don't use those to reduce package dependencies. Nevertheless, you will see how data with zero correlation will have large Pearson estimates when the order of the data is biased. In general, order biases result in upward Pearson correlation biases. Let's simulate simple independent (uncorrelated) data for these examples. To be exchangeable, it is vital that the marginal distributions of both columns be the same! (Here both are Standard Normal.) Let's simulate a relatively large number of pairs so estimates don't have much noise, and the biases are evident. ```{r} # number of pairs n <- 1000 # matrix with different pairs in rows X <- matrix( rnorm( n * 2 ), nrow = n, ncol = 2 ) ``` The above data is in "random" order: although the proportion of pairs where the value in the first column is less than that of the second column is rarely exactly 0.5, it will differ from it by small random amounts, without a preference (bias) for lower or higher values than 0.5. To contrast, we create a version of this data with "extreme" order, where every pair lists the minimum value first, and maximum value last. ```{r} library(corsym) Xe <- partial_order( X ) ``` We can also take biased data and "randomize" it, for further comparison: ```{r} Xr <- randomize_order( Xe ) ``` Let's quantify the order biases by counting the number of times a given order was observed (say `x 0.5$ cases are more likely to be significantly non-zero and quickly increase. ```{r} ggplot( data, aes( x = q, y = r ) ) + geom_point() + theme_classic() + geom_errorbar( aes( ymin = CIL, ymax = CIU ) ) + geom_hline( yintercept = 0, linetype = 'dashed', color = 'gray' ) ``` Lastly, we can look directly at significance of the order bias. We have a lot of power to detect differences, so even at $q=0.1$ we tend to get significant results. Comparing to the previous figure, we see that a significant order bias does not directly result in significantly biased Pearson estimates. ```{r} # Bonferroni threshold pcut <- 0.05 / nrow( data ) pcut ggplot( data, aes( x = q, y = -log10( pval ) ) ) + geom_point() + theme_classic() + geom_hline( yintercept = -log10( pcut ), linetype = 'dashed', color = 'gray' ) ```