-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcorrelation.qmd
501 lines (434 loc) · 18.9 KB
/
correlation.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# Correlation Analysis {#sec-correlation}
## Getting Started {#sec-correlationGettingStarted}
### Load Packages {#sec-correlationLoadPackages}
```{r}
library("petersenlab")
library("XICOR")
library("tidyverse")
```
## Overview of Correlation {#sec-correlationOverview}
Correlation is an index of the association between variables.
Covariance is the association between variables and in an unstandardized metric that differs for variables with different scales.
By contrast, correlation is in a standarized metric that does not differ for variables with different scales.
When examining the association between variables that are [interval](#sec-interval) or [ratio](#sec-ratio) levels of measurement, Pearson correlation is used.
When examining the association between variables that are [ordinal](#sec-ordinal) in level of measurement, Spearman correlation is used.
Pearson correlation is an index of the *linear* association between variables.
If a nonlinear association is present, other indices like xi [$\xi$; @Chatterjee2021] and distance correlation coefficients are better suited to detect the association.
## The Correlation Coefficient ($r$)
The formula for the correlation coefficient\index{correlation} is in @eq-pearsonCorrelation.
The correlation coefficient ranges from −1.0 to +1.0.\index{correlation}
The correlation coefficient ($r$) tells you two things: (1) the direction (sign) of the association (positive or negative) and (2) the magnitude of the association.\index{correlation}
If the correlation coefficient is positive, the association is positive.\index{correlation}
If the correlation coefficient is negative, the association is negative.\index{correlation}
If the association is positive, as `X` increases, `Y` increases (or conversely, as `X` decreases, `Y` decreases).\index{correlation}
If the association is negative, as `X` increases, `Y` decreases (or conversely, as `X` decreases, `Y` increases).\index{correlation}
The smaller the absolute value of the correlation coefficient (i.e., the closer the $r$ value is to zero), the weaker the association and the flatter the slope of the best-fit line in a scatterplot.\index{correlation}
The larger the absolute value of the correlation coefficient (i.e., the closer the absolute value of the $r$ value is to one), the stronger the association and the steeper the slope of the best-fit line in a scatterplot.\index{correlation}
See @fig-rangeOfCorrelations for a range of different correlation coefficients and what some example data may look like for each direction and strength of association.\index{correlation}
```{r}
#| label: fig-rangeOfCorrelations
#| fig-cap: "Correlation Coefficients."
#| fig-alt: "Correlation Coefficients."
#| fig-height: 12
#| code-fold: true
#| message: false
set.seed(52242)
correlations <- data.frame(criterion = rnorm(1000))
correlations$v1 <- complement(correlations$criterion, -1)
correlations$v2 <- complement(correlations$criterion, -.9)
correlations$v3 <- complement(correlations$criterion, -.8)
correlations$v4 <- complement(correlations$criterion, -.7)
correlations$v5 <- complement(correlations$criterion, -.6)
correlations$v6 <- complement(correlations$criterion, -.5)
correlations$v7 <- complement(correlations$criterion, -.4)
correlations$v8 <- complement(correlations$criterion, -.3)
correlations$v9 <- complement(correlations$criterion, -.2)
correlations$v10 <-complement(correlations$criterion, -.1)
correlations$v11 <-complement(correlations$criterion, 0)
correlations$v12 <-complement(correlations$criterion, .1)
correlations$v13 <-complement(correlations$criterion, .2)
correlations$v14 <-complement(correlations$criterion, .3)
correlations$v15 <-complement(correlations$criterion, .4)
correlations$v16 <-complement(correlations$criterion, .5)
correlations$v17 <-complement(correlations$criterion, .6)
correlations$v18 <-complement(correlations$criterion, .7)
correlations$v19 <-complement(correlations$criterion, .8)
correlations$v20 <-complement(correlations$criterion, .9)
correlations$v21 <-complement(correlations$criterion, 1)
par(mfrow = c(7,3), mar = c(1, 0, 1, 0))
# -1.0
plot(correlations$criterion, correlations$v1, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v1)$estimate, 2))))
abline(lm(v1 ~ criterion, data = correlations), col = "black")
# -.9
plot(correlations$criterion, correlations$v2, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v2)$estimate, 2))))
abline(lm(v2 ~ criterion, data = correlations), col = "black")
# -.8
plot(correlations$criterion, correlations$v3, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v3)$estimate, 2))))
abline(lm(v3 ~ criterion, data = correlations), col = "black")
# -.7
plot(correlations$criterion, correlations$v4, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v4)$estimate, 2))))
abline(lm(v4 ~ criterion, data = correlations), col = "black")
# -.6
plot(correlations$criterion, correlations$v5, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v5)$estimate, 2))))
abline(lm(v5 ~ criterion, data = correlations), col = "black")
# -.5
plot(correlations$criterion, correlations$v6, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v6)$estimate, 2))))
abline(lm(v6 ~ criterion, data = correlations), col = "black")
# -.4
plot(correlations$criterion, correlations$v7, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v7)$estimate, 2))))
abline(lm(v7 ~ criterion, data = correlations), col = "black")
# -.3
plot(correlations$criterion, correlations$v8, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v8)$estimate, 2))))
abline(lm(v8 ~ criterion, data = correlations), col = "black")
# -.2
plot(correlations$criterion, correlations$v9, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v9)$estimate, 2))))
abline(lm(v9 ~ criterion, data = correlations), col = "black")
# -.1
plot(correlations$criterion, correlations$v10, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v10)$estimate, 2))))
abline(lm(v10 ~ criterion, data = correlations), col = "black")
# 0.0
plot(correlations$criterion, correlations$v11, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v11)$estimate, 2))))
abline(lm(v11 ~ criterion, data = correlations), col = "black")
# 0.1
plot(correlations$criterion, correlations$v12, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v12)$estimate, 2))))
abline(lm(v12 ~ criterion, data = correlations), col = "black")
# 0.2
plot(correlations$criterion, correlations$v13, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v13)$estimate, 2))))
abline(lm(v13 ~ criterion, data = correlations), col = "black")
# 0.3
plot(correlations$criterion, correlations$v14, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v14)$estimate, 2))))
abline(lm(v14 ~ criterion, data = correlations), col = "black")
# 0.4
plot(correlations$criterion, correlations$v15, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v15)$estimate, 2))))
abline(lm(v15 ~ criterion, data = correlations), col = "black")
# 0.5
plot(correlations$criterion, correlations$v16, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v16)$estimate, 2))))
abline(lm(v16 ~ criterion, data = correlations), col = "black")
# 0.6
plot(correlations$criterion, correlations$v17, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v17)$estimate, 2))))
abline(lm(v17 ~ criterion, data = correlations), col = "black")
# 0.7
plot(correlations$criterion, correlations$v18, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v18)$estimate, 2))))
abline(lm(v18 ~ criterion, data = correlations), col = "black")
# 0.8
plot(correlations$criterion, correlations$v19, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v19)$estimate, 2))))
abline(lm(v19 ~ criterion, data = correlations), col = "black")
# 0.9
plot(correlations$criterion, correlations$v20, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v20)$estimate, 2))))
abline(lm(v20 ~ criterion, data = correlations), col = "black")
# 1.0
plot(correlations$criterion, correlations$v21, xaxt = "n", yaxt = "n", xlab = "" , ylab = "",
main = substitute(paste(italic(r), " = ", x, sep = ""), list(x = round(cor.test(x = correlations$criterion, y = correlations$v21)$estimate, 2))))
abline(lm(v21 ~ criterion, data = correlations), col = "black")
invisible(dev.off()) #par(mfrow = c(1,1))
```
See @fig-interepretingCorrelationCoefficients for the interpretation of the magnitude and direction (sign) of various correlation coefficients.
```{r}
#| label: fig-interepretingCorrelationCoefficients
#| fig-cap: "Interpretation of the Magnitude and Direction (Sign) of Correlation Coefficients."
#| fig-alt: "Interpretation of the Magnitude and Direction (Sign) of Correlation Coefficients."
#| fig-height: 9
#| fig-width: 9
#| code-fold: true
#| message: false
library("patchwork")
set.seed(52242)
correlations2 <- data.frame(criterion = rnorm(15))
correlations2$v1 <- complement(correlations2$criterion, -1)
correlations2$v2 <- complement(correlations2$criterion, -.9)
correlations2$v3 <- complement(correlations2$criterion, -.8)
correlations2$v4 <- complement(correlations2$criterion, -.7)
correlations2$v5 <- complement(correlations2$criterion, -.6)
correlations2$v6 <- complement(correlations2$criterion, -.5)
correlations2$v7 <- complement(correlations2$criterion, -.4)
correlations2$v8 <- complement(correlations2$criterion, -.3)
correlations2$v9 <- complement(correlations2$criterion, -.2)
correlations2$v10 <-complement(correlations2$criterion, -.1)
correlations2$v11 <-complement(correlations2$criterion, 0)
correlations2$v12 <-complement(correlations2$criterion, .1)
correlations2$v13 <-complement(correlations2$criterion, .2)
correlations2$v14 <-complement(correlations2$criterion, .3)
correlations2$v15 <-complement(correlations2$criterion, .4)
correlations2$v16 <-complement(correlations2$criterion, .5)
correlations2$v17 <-complement(correlations2$criterion, .6)
correlations2$v18 <-complement(correlations2$criterion, .7)
correlations2$v19 <-complement(correlations2$criterion, .8)
correlations2$v20 <-complement(correlations2$criterion, .9)
correlations2$v21 <-complement(correlations2$criterion, 1)
# -1.0
p1 <- ggplot(
data = correlations2,
mapping = aes(
x = criterion,
y = v1
)
) +
geom_point() +
geom_smooth(
method = "lm",
se = FALSE) +
labs(
title = "Perfect Negative Association",
subtitle = expression(paste(italic("r"), " = ", "−1.0"))
) +
theme_classic(
base_size = 12) +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
# -0.9
p2 <- ggplot(
data = correlations2,
mapping = aes(
x = criterion,
y = v2
)
) +
geom_point() +
geom_smooth(
method = "lm",
se = FALSE) +
labs(
title = "Strong Negative Association",
subtitle = expression(paste(italic("r"), " = ", "−.9"))
) +
theme_classic(
base_size = 12) +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
# -0.5
p3 <- ggplot(
data = correlations2,
mapping = aes(
x = criterion,
y = v6
)
) +
geom_point() +
geom_smooth(
method = "lm",
se = FALSE) +
labs(
title = "Moderate Negative Association",
subtitle = expression(paste(italic("r"), " = ", "−.5"))
) +
theme_classic(
base_size = 12) +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
# -0.2
p4 <- ggplot(
data = correlations2,
mapping = aes(
x = criterion,
y = v9
)
) +
geom_point() +
geom_smooth(
method = "lm",
se = FALSE) +
labs(
title = "Weak Negative Association",
subtitle = expression(paste(italic("r"), " = ", "−.2"))
) +
theme_classic(
base_size = 12) +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
# 0.0
p5 <- ggplot(
data = correlations2,
mapping = aes(
x = criterion,
y = v11
)
) +
geom_point() +
geom_smooth(
method = "lm",
se = FALSE) +
labs(
title = "No Association",
subtitle = expression(paste(italic("r"), " = ", ".0"))
) +
theme_classic(
base_size = 12) +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
# 0.2
p6 <- ggplot(
data = correlations2,
mapping = aes(
x = criterion,
y = v13
)
) +
geom_point() +
geom_smooth(
method = "lm",
se = FALSE) +
labs(
title = "Weak Positive Association",
subtitle = expression(paste(italic("r"), " = ", ".2"))
) +
theme_classic(
base_size = 12) +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
# 0.5
p7 <- ggplot(
data = correlations2,
mapping = aes(
x = criterion,
y = v16
)
) +
geom_point() +
geom_smooth(
method = "lm",
se = FALSE) +
labs(
title = "Moderate Positive Association",
subtitle = expression(paste(italic("r"), " = ", ".5"))
) +
theme_classic(
base_size = 12) +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
# 0.9
p8 <- ggplot(
data = correlations2,
mapping = aes(
x = criterion,
y = v20
)
) +
geom_point() +
geom_smooth(
method = "lm",
se = FALSE) +
labs(
title = "Strong Positive Association",
subtitle = expression(paste(italic("r"), " = ", ".9"))
) +
theme_classic(
base_size = 12) +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
# 1.0
p9 <- ggplot(
data = correlations2,
mapping = aes(
x = criterion,
y = v21
)
) +
geom_point() +
geom_smooth(
method = "lm",
se = FALSE) +
labs(
title = "Perfect Positive Association",
subtitle = expression(paste(italic("r"), " = ", "1.0"))
) +
theme_classic(
base_size = 12) +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 +
plot_layout(
ncol = 3,
heights = 1,
widths = 1)
```
Interactive visualizations by Kristoffer Magnusson on *p*-values and null-hypothesis significance testing are below:
- <https://rpsychologist.com/correlation/> (archived at <https://perma.cc/G8YR-VCM4>)
## Examples {#sec-correlationExamples}
### Covariance {#sec-correlationExamplesCovariance}
### Pearson Correlation {#sec-correlationExamplesPearson}
### Spearman Correlation {#sec-correlationExamplesSpearman}
### Nonlinear Correlation {#sec-correlationExamplesNonlinear}
### Correlation Matrix {#sec-correlationExamplesMatrix}
### Correlogram {#sec-correlationExamplesCorrelogram}
## Impact of Outliers {#sec-correlationOutliers}
## Correlation Does Not Imply Causation {#sec-correlation-correlationAndCausation}
As described in @sec-correlationCausation, correlation does not imply causation.
There are several reasons (described in @sec-correlationCausation) that, just because `X` is correlated with `Y` does not necessarily mean that `X` causes `Y`.
However, correlation can still be useful.
In order for two processes to be causally related, they must be associated.
That is, association is necessary but insufficient for causality.
## Conclusion {#sec-correlationConclusion}
Correlation is an index of the association between variables.
The correlation coefficient ($r$) ranges from −1 to +1, and indicates the sign and magnitude of the association.
Although correlation does not imply causation, identifying associations between variables can still be useful because association is a necessary (but insufficient) condition for causality.
## Session Info {#sec-correlationSessionInfo}
::: {.content-visible when-format="html"}
```{r}
sessionInfo()
```
:::