forked from spressi/precision_workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.2_CIs.qmd
527 lines (385 loc) · 12.7 KB
/
2.2_CIs.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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
---
title: "2.2 Confidence Intervals"
subtitle: "Workshop: \"Handling Uncertainty in your Data\""
author: "Dr. Mario Reutter & Juli Nagel"
format:
revealjs:
smaller: true
scrollable: true
slide-number: true
theme: serif
chalkboard: true
width: 1280
height: 720
from: markdown+emoji
execute:
echo: true
---
# CIs around means
```{css}
#| echo: false
code.sourceCode {
font-size: 1.4em;
}
div.cell-output-stdout {
font-size: 1.4em;
}
```
```{r}
#| warning: false
#| message: false
#| echo: false
library(tidyverse)
```
## Data preparation
::: {.incremental}
- If you haven't done so yet: Create an `R` project for the precision workshop.
- Create a script for this part of the workshop (e.g., `cis.R`).
- Load the `tidyverse` (`library(tidyverse)`) at the beginning of your script.
:::
. . .
<span style="font-size: 18px">Note: We show different packages that help us with confidence interval calculations, some of which may have conflicting functions. This is why in this presentation, we call each package explicitly, e.g., `confintr::ci_mean()`.</span>
## Why confidence intervals?
::: {.incremental}
- Goal: indicate precision of a point estimate (e.g., the mean).
- If visualized, it's often supposed to help with "inference by eye".
- However, confidence intervals are often misinterpreted (e.g., [Hoekstra et al., 2014](https://doi.org/10.3758/s13423-013-0572-3))
:::
## What is a confidence interval?
::: {.incremental}
- Assume we use a 95% CI.
- In the long run, in 95% of our replications, the interval we calculate will contain the true population mean.
- As with any statistical inference: no guarantee! Errors are possible.
- "If a given value is within the interval of a result, the two can be informally assimilated as being comparable." ([Cousineau, 2017](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5511611/))
:::
## Confidence interval of the mean
<br>
<center>
$M - t_{\gamma} \times SE_{M}, M + t_{\gamma} \times SE_{M}$
</center>
<br>
::: {.incremental}
- $M$ = mean of a set of observations
- $SE_{M}$ = standard error of the mean
- $SE_{M} = s/\sqrt n$
- $t_{\gamma}$ = multiplier from a Student $t$ distribution, with d.f. $n - 1$ and coverage level $\gamma$ (often 95%)
:::
## Briefly: The iris data
Data about the size of three different species of iris flowers.
```{r}
head(iris)
```
## In R: Confidence interval of the mean
The package `confintr` offers a variety of functions around confidence intervals. Default here: Student's $t$ method.
```{r}
iris %>%
filter(Species == "versicolor") %>%
pull(Petal.Width) %>%
confintr::ci_mean()
```
. . .
Also available: Wald and bootstrap CIs.
## Confidence interval of the mean
<br>
<center>
$M - t_{\gamma} \times SE_{M}, M + t_{\gamma} \times SE_{M}$
</center>
<br>
::: {.incremental}
- However: very "limited" CI!
- Comparison to a fixed value (e.g., population mean).
- Cannot easily be used to compare different means.
- Useless to compare means of repeated-measures designs.
:::
## CIs: comparing groups
::: {.incremental}
- Position of each group mean as well as the relative position of the means are uncertain.
- I.e., SE for the comparison between two means is larger than for the comparison of a mean with a fixed value.
- How much the CI needs to be expanded depends on the variance of each group.
- When the variance is homogeneous across conditions, there is a shortcut: The sum of two equal variances can be calculated by multiplying the common variance by two.
- I.e., the CI must be $\sqrt2$ ($\approx$ 1.41) times wider ($SE_{M} = \sqrt{Var_{SEM}}$).
:::
. . .
<br>
<center>
$M - t_{\gamma} \times \sqrt2 \times SE_{M}, M + t_{\gamma} \times \sqrt2 \times SE_{M}$
</center>
## CIs: potential problems when comparing groups
See [Cousineau, 2017](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5511611/) - more about data viz later!
![](images/cousineau_fig1.png){fig-align="center"}
## In R: custom function
- Sometimes, writing your own custom function gives you the control you need over certain calculations.
. . .
<br>
```{r}
# You've seen this in the previous part :-)
se <- function(x, na.rm = TRUE) {
sd(x, na.rm) / sqrt(if(!na.rm) length(x) else sum(!is.na(x)))
}
```
. . .
<br>
```{r}
# CI based on t distribution
z_CI_t <- function(x, CI, na.rm = FALSE) {
if (na.rm) x <- x[!is.na(x)]
ci <- qt(1 - (1 - CI) / 2, length(x) - 1) # two-sided CI
return(ci)
}
```
## In R: custom function - applied
<br>
```{r}
iris %>%
filter(Species %in% c("setosa", "versicolor")) %>%
summarise(
ci_lower = mean(Petal.Width) - se(Petal.Width) * z_CI_t(Petal.Width, .95),
ci_upper = mean(Petal.Width) + se(Petal.Width) * z_CI_t(Petal.Width, .95),
.by = Species
)
```
## CIs: comparing groups
- Wider CIs for group comparisons, as e.g. suggested by [Cousineau (2017)](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5511611/).
```{r}
iris %>%
filter(Species %in% c("setosa", "versicolor")) %>%
summarise(
ci_lower = mean(Petal.Width) - se(Petal.Width) * sqrt(2) * z_CI_t(Petal.Width, .95),
ci_upper = mean(Petal.Width) + se(Petal.Width) * sqrt(2) * z_CI_t(Petal.Width, .95),
.by = Species
)
```
. . .
<br>
- Attention! Assumes equal variance!
- Even though here, we use the mean and SD/SE of each group (*not* e.g. the pooled SD).
## CIs: difference of means
- If possible, the most elegant way is to report/plot the CI for the difference in means.
```{r}
with(
iris,
confintr::ci_mean_diff(
Petal.Width[Species == "versicolor"],
Petal.Width[Species == "setosa"]
)
)
```
. . .
- Not pipe-friendly, if that's important for you.
- Careful! Not for within-subjects comparisons!
## CIs: difference of means
- Looks familiar? Part of the `t.test()` output by default!
```{r}
with(
iris,
t.test(
Petal.Width[Species == "versicolor"],
Petal.Width[Species == "setosa"]
)
)
```
## CIs: difference of means
- Looks familiar? Part of the `t.test()` output by default!
- Can be accessed like this:
```{r}
ttest_between <-
with(
iris,
t.test(
Petal.Width[Species == "versicolor"],
Petal.Width[Species == "setosa"]
)
)
ttest_between$conf.int[1:2]
```
## What about within-subject designs?
::: {.incremental}
- Repeated measures are typically correlated, which makes it possible to evaluate the difference between means more precisely.
- I.e., we adjust the width of the CI based on the correlation between measures.
- The difference between the two means is estimated as if we had a larger (independent) sample.
:::
. . .
<br>
<center>
$M - t_{\gamma} \times \sqrt{1-r} \times \sqrt2 \times \frac{s}{\sqrt{n}}, M + t_{\gamma} \times \sqrt{1-r} \times \sqrt2 \times \frac{s}{\sqrt{n}}$
</center>
. . .
<span style="font-size: 18px">Note that we still are comparing two means, so as before, the CI is "difference adjusted" using $\sqrt2$.</span>
## Briefly: the fhch2010 data
- Data from Freeman, Heathcote, Chalmers, & Hockley (2010), included in `afex`.
- Lexical decision and word naming latencies for 300 words and 300 nonwords.
- For simplicity, we're only interested in the task (word naming or lexical decision; between subjects) and the stimulus (word or nonword).
```{r}
library(afex)
head(fhch2010)
```
. . .
```{r}
# Aggregate to get a word/nonword reaction time per participant
fhch2010_summary <-
fhch2010 %>%
summarise(rt = mean(rt), .by = c(id, task, stimulus))
```
## CIs for within-subject designs
The measures are highly correlated :-)
```{r}
fhch2010_summary_wf <-
fhch2010_summary %>%
pivot_wider(
id_cols = id,
names_from = stimulus,
values_from = rt
)
fhch2010_summary_wf %>%
rstatix::cor_test(word, nonword)
```
## CIs for within-subject designs
```{r}
# without adjusting
fhch2010_summary %>%
summarise(
ci_lower = mean(rt) - se(rt) * sqrt(2) * z_CI_t(rt, .95),
ci_upper = mean(rt) + se(rt) * sqrt(2) * z_CI_t(rt, .95),
.by = stimulus
)
```
. . .
```{r}
# adjust for correlation
word_cor <- cor.test(fhch2010_summary_wf$word, fhch2010_summary_wf$nonword)
fhch2010_summary %>%
summarise(
ci_lower = mean(rt) - se(rt) * sqrt(1 - word_cor$estimate) * sqrt(2) * z_CI_t(rt, .95),
ci_upper = mean(rt) + se(rt) * sqrt(1 - word_cor$estimate) * sqrt(2) * z_CI_t(rt, .95),
.by = stimulus
)
```
## CIs for differences in means in within-subject designs
As before, the $t$-test will give us a CI for the difference in means:
```{r}
with(
fhch2010_summary,
t.test(
rt[stimulus == "word"],
rt[stimulus == "nonword"],
paired = TRUE
)
)
# can be accessed using $conf.int[1:2]!
```
## CIs: caveats
::: {.incremental}
- Assumptions: normally distributed means, homogeneity of variances, sphericity (repeated measures)
- Mixed designs: Difficult! You will probably need to choose between between-subject or within-subject CIs (depending on your comparisons of interest); see upcoming data viz part!
- Most importantly: Be transparent! Describe how you calculated your CIs and what they are for!
:::
# CIs around effect sizes
## Cohen's d
- Also possible to report CIs on effect size estimates; different methods of calculating the desired CIs for different measures exist (beyond the scope of this workshop).
- E.g., `cohens_d()` from the package `effectsize`: "CIs are estimated using the noncentrality parameter method (also called the 'pivot method')"; see further details at \
`?effectsize::cohens_d()`.
```{r}
with(
iris,
effectsize::cohens_d(
Petal.Width[Species == "versicolor"],
Petal.Width[Species == "setosa"]
)
)
```
## rstatix: pipe-friendly t-test
- `rstatix` offers pipe-friendly statistical tests, and also calculates CIs.
- `detailed = TRUE` will give us confidence estimates for our $t$-test.
. . .
<br>
```{r}
iris %>%
filter(Species %in% c("setosa", "versicolor")) %>%
rstatix::t_test(Petal.Width ~ Species, detailed = TRUE) %>%
select(contains("estimate"), statistic:conf.high) #reduce output for slide
```
## rstatix: pipe-friendly Cohen's d
- Also includes a pipe-friendly version of Cohen's d.
- Careful: CIs are bootstrapped (hence the small discrepancy to the output of `effectsize` earlier)
. . .
<br>
```{r}
iris %>%
filter(Species %in% c("setosa", "versicolor")) %>%
rstatix::cohens_d(Petal.Width ~ Species, ci = TRUE)
```
## Correlations: apa
Option 1: Using the `apa` package.
<br>
```{r}
iris %>%
summarise(
cortest = cor.test(Petal.Width, Petal.Length) %>%
apa::cor_apa(r_ci = TRUE, print = FALSE),
.by = Species
)
```
## Correlations: rstatix
Option 2: Using the `rstatix` package.
<br>
```{r}
iris %>%
group_by(Species) %>%
rstatix::cor_test(Petal.Width, Petal.Length) #implicitly ungroups the data
```
. . .
Note: Only gives rounded values ...
## ANOVAs
```{r}
aov_words <-
aov_ez(
id = "id",
dv = "rt",
data = fhch2010_summary,
between = "task",
within = "stimulus",
# we want to report partial eta² ("pes"), and include the intercept in the output table ...
anova_table = list(es = "pes", intercept = TRUE)
)
aov_words
```
## CI around partial eta²
In principle, `apaTables` offers a function for CIs around $\eta_p^2$ ...
```{r}
# e.g., for our task effect
apaTables::get.ci.partial.eta.squared(
F.value = 15.76, df1 = 1, df2 = 43, conf.level = .95
)
```
. . .
... but it would be tedious to copy these values.
## CI around partial eta²
A little clunky function that can be applied to `afex` tables:
```{r}
peta.ci <-
function(anova_table, conf.level = .9) { # 90% CIs are recommended for partial eta² (https://daniellakens.blogspot.com/2014/06/calculating-confidence-intervals-for.html#:~:text=Why%20should%20you%20report%2090%25%20CI%20for%20eta%2Dsquared%3F)
result <-
apply(anova_table, 1, function(x) {
ci <-
apaTables::get.ci.partial.eta.squared(
F.value = x["F"], df1 = x["num Df"], df2 = x["den Df"], conf.level = conf.level
)
return(setNames(c(ci$LL, ci$UL), c("LL", "UL")))
}) %>%
t() %>%
as.data.frame()
result$conf.level <- conf.level
return(result)
}
```
## CI around partial eta²
The result of custom function that applies the `apaTables` function to our entire ANOVA table:
```{r}
peta.ci(aov_words$anova_table)
```
Also see this [blogpost by Daniel Lakens from 2014](https://daniellakens.blogspot.com/2014/06/calculating-confidence-intervals-for.html) about CIs for $\eta_p^2$.
# Thanks!
Learning objectives:
- Understand what different versions of CIs exist, and which one you need for your data.
- Apply different functions in `R` (and write custom ones if needed) to report CIs around means, and common effect size estimates.
Next: Visualizing uncertainty