-
Notifications
You must be signed in to change notification settings - Fork 0
/
Correlations.qmd
215 lines (162 loc) · 7.16 KB
/
Correlations.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Correlation between Two Continuous Variables
To quantify the relationship between two continuous variables, the most common method is to use a Pearson correlation coefficient (denoted with the letter $r$). The pearson correlation takes the covariance between a continuous independent ($X$) and dependent ($Y$) variable and standardizes it by the standard deviations of $X$ and $Y$,
$$
r = \frac{\text{Cov}(X,Y)}{S_{X} S_{Y}}.
$$
We can visualize what a correlation between two variables looks like with scatter plots. @fig-cor-example shows scatter plots with differing levels of correlation.
```{r, echo =FALSE, message=FALSE,warning=FALSE, fig.height=5}
#| label: fig-cor-example
#| fig-cap: Simulated data from a bivariate normal distribution displaying 6 different correlations, r = 0, .20, .40, .60, .80, and 1.00.
library(ggplot2)
library(MASS)
library(ggdist)
library(patchwork)
n = 1000
r = .0
data1<- mvrnorm(n = n,
mu = c(0,0),
Sigma = data.frame(x = c(1,r),
y = c(r,1)),
empirical=TRUE)
h1 <- ggplot(data=NULL,aes(x=data1[,1],y=data1[,2])) +
geom_point(color = 'skyblue4',alpha=.7,size=.6) +
theme_ggdist() +
theme(aspect.ratio = 1,
axis.text = element_text(size=13),
axis.title = element_text(size=14),
title = element_text(size=14)) +
ggtitle("r = 0") +
theme(axis.title.y = element_text(angle=360,vjust = .5)) +
ylab('Y') +
xlab('X')+
scale_x_continuous(breaks=c(-2,0,2),limits = c(-3.5,3.5))+
scale_y_continuous(breaks=c(-2,0,2),limits = c(-3.5,3.5))
r = .20
data2<- mvrnorm(n = n,
mu = c(0,0),
Sigma = data.frame(x = c(1,r),
y = c(r,1)),
empirical=TRUE)
h2 <- ggplot(data=NULL,aes(x=data2[,1],y=data2[,2])) +
geom_point(color = 'skyblue4',alpha=.7,size=.6) +
theme_ggdist() +
theme(aspect.ratio = 1,
axis.text = element_text(size=13),
axis.title = element_text(size=14),
title = element_text(size=14)) +
ggtitle("r = .20") +
ylab('') +
xlab('')+
scale_x_continuous(breaks=c(-2,0,2),limits = c(-3.5,3.5))+
scale_y_continuous(breaks=c(-2,0,2),limits = c(-3.5,3.5))
r = .4
data3<- mvrnorm(n = n,
mu = c(0,0),
Sigma = data.frame(x = c(1,r),
y = c(r,1)),
empirical=TRUE)
h3 <- ggplot(data=NULL,aes(x=data3[,1],y=data3[,2])) +
geom_point(color = 'skyblue4',alpha=.7,size=.6) +
theme_ggdist() +
theme(aspect.ratio = 1,
axis.text = element_text(size=13),
axis.title = element_text(size=14),
title = element_text(size=14)) +
ggtitle("r = .40") +
ylab('') +
xlab('') +
scale_x_continuous(breaks=c(-2,0,2),limits = c(-3.5,3.5))+
scale_y_continuous(breaks=c(-2,0,2),limits = c(-3.5,3.5))
r = .6
data4<- mvrnorm(n = n,
mu = c(0,0),
Sigma = data.frame(x = c(1,r),
y = c(r,1)),
empirical=TRUE)
h4 <- ggplot(data=NULL,aes(x=data4[,1],y=data4[,2])) +
geom_point(color = 'skyblue4',alpha=.7,size=.6) +
theme_ggdist() +
theme(aspect.ratio = 1,
axis.text = element_text(size=13),
axis.title = element_text(size=14),
title = element_text(size=14)) +
ggtitle("r = .60") +
theme(axis.title.y = element_text(angle=360,vjust = .5)) +
ylab('') +
xlab('')+
scale_x_continuous(breaks=c(-2,0,2),limits = c(-3.5,3.5))+
scale_y_continuous(breaks=c(-2,0,2),limits = c(-3.5,3.5))
r = .8
data5<- mvrnorm(n = n,
mu = c(0,0),
Sigma = data.frame(x = c(1,r),
y = c(r,1)),
empirical=TRUE)
h5 <- ggplot(data=NULL,aes(x=data5[,1],y=data5[,2])) +
geom_point(color = 'skyblue4',alpha=.7,size=.6) +
theme_ggdist() +
theme(aspect.ratio = 1,
axis.text = element_text(size=13),
axis.title = element_text(size=14),
title = element_text(size=14)) +
ggtitle("r = .80") +
theme(axis.title.y = element_text(angle=360,vjust = .5)) +
ylab('') +
xlab('')+
scale_x_continuous(breaks=c(-2,0,2),limits = c(-3.5,3.5))+
scale_y_continuous(breaks=c(-2,0,2),limits = c(-3.5,3.5))
r = 1
data6<- mvrnorm(n = n,
mu = c(0,0),
Sigma = data.frame(x = c(1,r),
y = c(r,1)),
empirical=TRUE)
h6 <- ggplot(data=NULL,aes(x=data6[,1],y=data6[,2])) +
geom_point(color = 'skyblue4',alpha=.7,size=.6) +
theme_ggdist() +
theme(aspect.ratio = 1,
axis.text = element_text(size=13),
axis.title = element_text(size=14),
title = element_text(size=14)) +
ggtitle("r = 1.00") +
theme(axis.title.y = element_text(angle=360,vjust = .5)) +
ylab('') +
xlab('')+
scale_x_continuous(breaks=c(-2,0,2),limits = c(-3.5,3.5))+
scale_y_continuous(breaks=c(-2,0,2),limits = c(-3.5,3.5))
(h1 + h2 + h3) / (h4 + h5 + h6)
```
The standard error of the Pearson correlation coefficient is,
$$
SE_r = \sqrt{\frac{\left(1-r^2\right)^2}{n-1}}
$$
Unlike Cohen's $d$ and other effect size measures, The correlation coefficient is bounded by -1 and positive 1, with positive 1 being a perfectly positive correlation, -1 being a perfectly negative correlation, and zero indicating no correlation between the two variables. The bounding has the consequence of making the confidence interval asymmetric around $r$ (e.g., if the correlation is positive, the lower bound is farther away from $r$ than the upper bound is). It is important to note that with a correlation of zero, the confidence interval is symmetric and approximately normal. Instead, to obtain the confidence intervals of $r$, we first need to apply a Fisher's Z transformation. A Fisher's Z transformation is a hyperbolic arctangent transformation of a Pearson correlation coefficient and can be computed as,
$$
Z_r = \text{arctanh}(r)
$$
The Fisher Z transformation ensures $Z_r$ has a symmetric and approximately normal sampling distribution. This then allows us to calculate the confidence interval from the standard error of $Z_r$ ($SE_{Z_r} = \frac{1}{\sqrt{n-3}}$). We can also back-transform the confidence into a Pearson correlation scale,
$$
CI_{r} = \text{tanh}(Z_r \pm 1.96\times SE_{Z_r})
$$
We can then back-transform the upper bound and lower bound into the upper and lower bound of $r$ by taking the hyperbolic tangent (the inverse of the arctangent).
In R, the full process of obtaining confidence intervals can be done quite easily. Note if you have raw data for $X$ and $Y$, then you can compute the correlation with base R, `cor(X,Y)`.
```{r, echo = TRUE}
# example: r = .50, n = 50
r <- .50
n <- 50
# compute Zr
Zr <- atanh(r)
# calculate standard error of Zr
SE_Zr <- 1/sqrt(n-3)
# compute confidence interval of Zr
Zlow <- Zr - 1.96 * SE_Zr
Zhigh <- Zr + 1.96 * SE_Zr
# backtransform CI of Z to CI of Pearson correlation
rlow <- tanh(Zlow)
rhigh <- tanh(Zhigh)
# print pearson correlation and confidence intervals
data.frame(r = MOTE::apa(r),
rlow = MOTE::apa(rlow),
rhigh = MOTE::apa(rhigh))
```
The output shows that the correlation and its confidence intervals are $r$ = 0.50, 95% CI \[0.26, 0.68\].