-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2_Crash_Course.qmd
1204 lines (919 loc) · 37.5 KB
/
2_Crash_Course.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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Handling Uncertainty Crash Course"
subtitle: "Extracted from Workshop: \"Handling Uncertainty in your Data\""
author: "Dr. Mario Reutter"
format:
revealjs:
smaller: true
scrollable: true
slide-number: true
theme: serif
chalkboard: true
width: 1280
height: 720
from: markdown+emoji
---
# Agenda
1. Measurement Precision
2. Confidence Intervals
3. Visualizing Uncertainty
# Measurement Precision
## Group-Level Precision
![<https://bookdown.org/MathiasHarrer/Doing_Meta_Analysis_in_R/forest.html#forest-R>](images/precision_group_forest.png)
→ Effects in relation to their *group-level* precision
## Subject-Level Precision
Is there a meaningful correlation?
![](images/precision_subject_cor1.png)
## Subject-Level Precision
Is there a meaningful correlation?
![](images/precision_subject_cor2.png)
## Subject-Level Precision
Is there a meaningful correlation?
![](images/precision_subject_cor3.png)
:::notes
Simulated data with added noise. True score correlation *r* = 1
⇒ We want to enable you to create plots like this one
Side note: \
Individual errorbars = subject-level precision of each individual (across trials)\
Confidence band of regression slope = group-level precision of the regression estimate (cf. reporting CI around Pearson's r)
:::
## Trial-Level Precision
![[Holmqvist et al. (2023, retracted)](https://doi.org/10.3758/s13428-021-01762-8)](images/precision_trial.png)
Only relevant for time series data (i.e., several "measurements" per trial)
Note: Calculating standard deviation / error is not trivial here because of auto-correlation
## What is Precision?
1. Precision is indicated by errorbars / confidence intervals
2. Whenever you `summarize` across a variable, you can calculate the \
**precision of the aggregation**
3. Precision exists on different levels: group, subject, trial (and more)
4. Closely linked to statistical power and reliability
## How Can We Enhance Precision?
1. Shield the measurement from random noise → precise equipment / paradigm\
⇒ trial-level precision
2. Identify the **aggregation level of interest**:
i) sample differences → optimize group-level precision: \
many subjects that respond homogenously
ii) correlational hypotheses / application → optimize subject-level precision: \
systematic differences between individuals but little variability within subjects across many trials (mind "sequence effects"; cf. [Nebe, Reutter, et al., 2023](https://doi.org/10.7554/eLife.85980))
⇒ “two disciplines of scientific psychology” ([Cronbach, 1957](https://psycnet.apa.org/doi/10.1037/h0043943))
## Group- vs. Subject-Level Precision
:::columns
:::column
**Group-Level Precision**
- Group differences (t-tests, ANOVA)
- Many subjects (independent observations)
- Homogenous sample (e.g., psychology students?)
:::
:::column
**Subject-Level Precision**
- Correlations (e.g., Reliability)
- Many trials (careful: sequence effects!)
- Heterogenous/diverse sample
- Low variability within subjects across trials (i.e., SD~within~)
:::
:::
## Summary: Precision
- We are in a replication crisis
- Increasing the number of subjects is not the only way to get out
- Sample size benefits basic research on groups only
⇒ Increase precision on the aggregate level of interest!
![](images/precision_vs_sample_size.png)
# Precision in `R`!
```{css}
code.sourceCode {
font-size: 1.4em;
}
div.cell-output-stdout {
font-size: 1.4em;
}
```
## The Standard Error (SE)
Calculates the (lack of) precision of a mean based on the \
standard deviation ($SD$) of the individual observations and their number ($n$)
$$SE = \frac{SD}{\sqrt{n}}$$
## The Standard Error (SE) in `R`
No base `R` function available 💩
1. Use `confintr::se_mean` (not part of the tidyverse)
2. Use a custom function
```{r}
#| echo: true
se <- function(x, na.rm = TRUE) {
sd(x, na.rm) / sqrt(if(!na.rm) length(x) else sum(!is.na(x)))
}
```
:::aside
Careful with custom functions! I often see people using `n()` but this does not work correctly with missing values (`NA` / `na.rm = TRUE`)
:::
::: notes
Thanks to [Juli Nagel](https://juli-nagel.de/) for suggesting the `confintr` package!
Thanks to [Daniel Gromer](https://www.psychologie.uni-wuerzburg.de/bioklin/team/dr-daniel-gromer/) for sharing his custom function!
:::
## The Standard Error (SE) in `R` 2
Whenever you use `mean`, also calculate the SE:
```{r}
#| echo: true
library(tidyverse)
iris %>% #helpful data set for illustration
summarize( #aggregation => precision
across(.cols = starts_with("Sepal"), #everything(), #output too wide
.fns = list(mean = mean, se = confintr::se_mean)),
.by = Species)
```
\
→ subject-level (species-level) precision
## Standard Error Pitfalls in `R`
What is the group-level precision of `Sepal.Length`?
Note: Treat `Species` as subjects (`N = 3`) and rows within `Species` as trials.
```{r}
#| echo: true
data <-
iris %>%
rename(subject = Species, measure = Sepal.Length) %>%
mutate(trial = 1:n(), .by = subject) %>%
select(subject, trial, measure) %>% arrange(trial, subject) %>% tibble() #neater output
print(data)
```
## Standard Error Pitfalls in `R`
What is the group-level precision of `Sepal.Length`?
```{r}
#| echo: true
data %>%
summarize(m = mean(measure),
se = confintr::se_mean(measure))
```
. . .
\
```{r}
#| echo: true
#| code-line-numbers: "4"
data %>%
summarize(m = mean(measure),
se = confintr::se_mean(measure),
n = n())
```
## Standard Error Pitfalls in `R` 2
What is the group-level precision of `Sepal.Length`?
```{r}
#| echo: true
#| code-line-numbers: "2"
data %>%
summarize(measure.subject = mean(measure), .by = subject) %>% #subject-level averages
summarize(m = mean(measure.subject),
se = confintr::se_mean(measure.subject),
n = n())
```
. . .
⇒ When using trial-level data to calculate group-level precision, **summarize twice!**
⇒ Every summarize brings you up exactly *one* level - don't try to skip!\
trial-level → subject-level → group-level
:::aside
Linear Mixed Models (LMMs) implicitly take this into account when specifying *random intercepts for subjects* `(1|subject)`.
:::
## Summary: Precision in `R`
```{r}
#| echo: true
#| code-line-numbers: "2-5|6-10"
data %>%
# from trial- to subject-level
summarize(.by = subject, #trial- to subject-level
measure.subject = mean(measure), #subject-level averages
se.subject = confintr::se_mean(measure)) %>% #subject-level precision
# from subject- to group-level
summarize(m = mean(measure.subject), #group-level mean ("grand average")
se = confintr::se_mean(measure.subject), #group-level precision
se.subject = mean(se.subject), #average subject-level precision (note: pooling should be used)
n = n())
```
# Confidence Intervals (CIs)
1. CIs around means
2. CIs around effect sizes
# CIs around means
## CIs in `R`
Easiest way is to use calls to `t.test`:
```{r}
#| echo: true
#| code-line-numbers: "5-7|3,7"
iris %>%
mutate(subject = 1:n(), .by = Species) %>%
pivot_wider(names_from = Species, values_from = Petal.Width) %>%
summarize(
setosa.ci.size = t.test(setosa)$conf.int %>% diff(), # or confintr::ci_mean(setosa)$interval %>% diff()
versicolor.ci.size = t.test(versicolor)$conf.int %>% diff(), # or confintr::ci_mean(versicolor)$interval %>% diff()
diff.ci.size = t.test(setosa, versicolor)$conf.int %>% diff() # or confintr::ci_mean_diff(setosa, versicolor)$interval %>% diff()
)
```
## CIs in `R`: Within-Subjects Design
It works similarly with paired t-tests (but the pivoting is slightly different):
```{r}
#| echo: true
#| code-line-numbers: "3,7"
iris %>%
mutate(subject = 1:n(), .by = Species) %>%
pivot_wider(names_from = Species, values_from = Petal.Width, id_cols = subject) %>%
summarize(
setosa.ci.size = t.test(setosa)$conf.int %>% diff(),
versicolor.ci.size = t.test(versicolor)$conf.int %>% diff(),
diff.ci.size = t.test(setosa, versicolor, paired = TRUE)$conf.int %>% diff() #or confintr::ci_mean(setosa - versicolor)$interval %>% diff()
)
```
## Summary: CIs around means
::: incremental
- CIs around means are different for one sample vs. independent samples vs. paired samples
- Mixed designs: Difficult! You will probably need to choose between between-subject or within-subject CIs (depending on your comparisons of interest; see later part!)\
⇒ Be transparent! Describe how you calculated your CIs! (between vs. within, SE vs. CI)
- Assumption: Normally distributed means \
⇒ Might want to plot raw data (see last part!)
:::
# CIs around effect sizes
## Cohen's d
```{r}
#| echo: true
#| code-line-numbers: "5-7"
iris %>%
mutate(subject = 1:n(), .by = Species) %>%
pivot_wider(names_from = Species, values_from = Petal.Width) %>%
summarize(
setosa.test = t.test(setosa) %>% apa::t_apa(es_ci = TRUE, print = FALSE),
versicolor.test = t.test(versicolor) %>% apa::t_apa(es_ci = TRUE, print = FALSE),
diff.test = t.test(setosa, versicolor, var.equal = TRUE) %>% apa::t_apa(es_ci = TRUE, print = FALSE)
) %>%
pivot_longer(everything(), names_to = "test", values_to = "output") # nicer output
```
## Correlations
```{r}
#| echo: true
iris %>%
summarize(
cortest = cor.test(Petal.Width, Petal.Length) %>%
apa::cor_apa(r_ci = TRUE, print = FALSE),
.by = Species
)
```
## ANOVAs: Preparation
The fhch2010 data set:
- 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; within subjects).
```{r}
library(afex)
head(fhch2010)
```
. . .
\
```{r}
#| echo: true
# Aggregate to get a word/nonword reaction time per participant
fhch2010_summary <-
fhch2010 %>%
summarize(rt = mean(rt), .by = c(id, task, stimulus))
```
## ANOVAs
```{r}
#| echo: true
aov_words <-
afex::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 %>% apa::anova_apa() #optional: slightly different (APA-conform) output
```
## CI around partial eta²
In principle, `apaTables` offers a (non-vectorized) function for CIs around $\eta_p^2$ ...
```{r}
#| echo: true
# 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}
#| echo: true
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}
#| echo: true
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$.
::: notes
We cross-checked the results between `apaTables` and Daniel Lakens's blog post: they are identical!
:::
# Visualizing CIs
In the previous part, you learned about CIs around *means* and *effect sizes*.
CIs around means are for Figures.\
CIs around effect sizes are for the statistical reporting section.
## Visualizing CIs around means
In this part, we will visualize CIs around means for:
- t-tests (one sample, independent samples, & paired samples),
- ANOVAs (a simple $2 \times 2$ interaction),
- and correlations.
You have already learned how to calculate CIs around their effect sizes (Cohen's *d*, $\eta_p^2$, and Pearson's *r*)
. . .
\
For all examples, we will use the `fhch2010` data set of the `afex` package that we have encountered before. Make sure that you have the subject-level aggregates ready:
```{r}
#| echo: true
fhch2010_summary <-
afex::fhch2010 %>%
summarize(rt = mean(rt), .by = c(id, task, stimulus))
```
## GgThemes
The default options in `ggplot` have some problems: Most importantly, text is too small. The easiest solution is to create your own theme that you apply to your plots.
I am currently using this theme adapted from an old script of [Lara Rösler](https://lararoesler.nl/).
```{r}
#| echo: true
theme_set( # theme_set has to be executed every session; cf. library(tidyverse)
myGgTheme <- # you can save your theme in a local variable instead, add it to every plot, and save your environment across sessions
theme_bw() + # start with the black-and-white theme
theme(
#aspect.ratio = 1,
plot.title = element_text(hjust = 0.5),
panel.background = element_rect(fill = "white", color = "white"),
legend.background = element_rect(fill = "white", color = "grey"),
legend.key = element_rect(fill = "white"),
strip.background = element_rect(fill = "white"),
axis.ticks.x = element_line(color = "black"),
axis.line.x = element_line(color = "black"),
axis.line.y = element_line(color = "black"),
axis.text = element_text(color = "black"),
axis.text.x = element_text(size = 16, color = "black"),
axis.text.y = element_text(size = 16, color = "black"),
axis.title = element_text(size = 16, color = "black"),
legend.text = element_text(size = 14, color = "black"),
legend.title = element_text(size = 14, color = "black"),
strip.text = element_text(size = 12, color = "black"))
)
```
## One Sample t-test
Are the mean reaction times **for naming words** faster than 1 sec?
. . .
```{r}
#| echo: true
fhch2010_summary_wordnaming <-
fhch2010_summary %>%
filter(task == "naming", stimulus == "word")
fhch2010_summary_wordnaming %>%
pull(rt) %>%
t.test(mu = 1, alternative = "less") %>%
apa::t_apa(es_ci = TRUE) # output to APA format
```
## One Sample t-test: Visualization Code
```{r}
#| echo: true
#| code-line-numbers: "2-6|4|4,8|5|5,10"
ostt <-
fhch2010_summary_wordnaming %>%
summarize(
rt.m = mean(rt), #careful! if you do rt = mean(rt), you cannot calculate t.test(rt) afterwards
rt.ci.length = t.test(rt)$conf.int %>% diff()
) %>%
ggplot(aes(y = rt.m, x = "naming words")) +
geom_point() + #plot the mean
geom_errorbar(aes(ymin = rt.m - rt.ci.length/2, ymax = rt.m + rt.ci.length/2)) + #plot the CI
geom_hline(yintercept = 1, linetype = "dashed") #plot the population mean to test against
```
::: notes
"Beautiful" alternative:
```{r}
fhch2010_summary_wordnaming %>%
pull(rt) %>%
confintr::ci_mean() %>%
lapply(function(x) {
attr(x, "class") <- NULL; #delete class attribute which kills bind_cols()
return(x)}) %>% #return altered object for pipe-friendliness
bind_cols()
```
:::
## One Sample t-test: Figure
```{r}
#| echo: false
ostt
```
## Independent Samples t-test
Are the mean reaction times for **naming words** different from **lexically identifying words**?
. . .
```{r}
#| echo: true
#| results: hold
fhch2010_summary_words <-
fhch2010_summary %>%
filter(stimulus == "word")
with(fhch2010_summary_words,
t.test(rt ~ task) #Welch test
#t.test(rt ~ task, var.equal = TRUE) #assume equal variances
) #%>% apa::t_apa(es_ci = TRUE) #does not work for Welch test :(
```
:::notes
You can also `pivot_wider` instead of using the formula notation:
```{r}
with(fhch2010_summary_words %>%
pivot_wider(names_from = task, values_from = rt), #ignore the NAs
t.test(naming, lexdec) #Welch test
#t.test(rt ~ task, var.equal = TRUE) #assume equal variances
) #%>% apa::t_apa(es_ci = TRUE) #does not work for Welch test :(
```
:::
## Independent Samples t-test: Viz Code
```{r}
#| echo: true
#| code-line-numbers: "3|3,5|3,5,6"
istt2 <-
fhch2010_summary_words %>%
pivot_wider(names_from = task, values_from = rt) %>%
summarize(
rt.m = mean(naming, na.rm = TRUE) - mean(lexdec, na.rm = TRUE), #difference of the means == mean difference
rt.ci.length = t.test(naming, lexdec)$conf.int %>% diff()
) %>%
ggplot(aes(y = rt.m, x = "difference")) +
geom_point() + #plot the mean difference
geom_errorbar(aes(ymin = rt.m - rt.ci.length/2, ymax = rt.m + rt.ci.length/2)) + #plot the difference CI
geom_hline(yintercept = 0, linetype = "dashed") #plot the population mean to test against
```
## Independent Samples t-test: CI of the difference: Figure
```{r}
#| echo: false
istt2
```
<!-- TODO: Option to plot this CI on both individual mean RTs (cf. ANOVA Viz Code 2) instead of plotting the mean difference -->
## Paired Samples t-test
Are the mean reaction times for **naming words** different from **naming non-words**?
. . .
```{r}
#| echo: true
#| results: hold
fhch2010_summary_naming <-
fhch2010_summary %>%
filter(task == "naming")
# with(fhch2010_summary_naming, t.test(rt ~ stimulus, paired = TRUE)) #not allowed anymore :(
# fhch2010_summary_naming %>% rstatix::t_test(rt ~ stimulus, paired = TRUE, detailed = TRUE) # alternative!
with(fhch2010_summary_naming %>%
pivot_wider(names_from = stimulus, values_from = rt, id_cols = id), #make pairing by id explicit
t.test(word, nonword, paired = TRUE)) %>%
apa::t_apa(es_ci = TRUE) # output to APA format
```
## Paired Samples t-test: Viz Code?
```{r}
#| echo: true
#| code-line-numbers: "1,3,8"
#| output-location: column-fragment
fhch2010_summary_naming %>%
summarize(
.by = stimulus,
rt.m = mean(rt),
rt.ci.length = t.test(rt)$conf.int %>% diff()
) %>%
ggplot(aes(y = rt.m, x = stimulus)) +
geom_point() +
geom_errorbar(aes(ymin = rt.m - rt.ci.length/2, ymax = rt.m + rt.ci.length/2))
```
. . .
\
There is a difference between the precision of the means (aggregated **across subjects**) and the \
precision of the paired differences (paired **within the same subjects** and then aggregated across).
## Between- vs. Within-Subject Error
Between- and within-subject variance can be (partially) independent.
:::notes
Use `Notes Canvas` (paint brush icon on bottom left) to draw worst possibility of paired differences in "Raw Data" and resulting increase in within-subjects error variance while between-subjects variance remains fixed.
:::
<!-- DOI not working: https://doi.org/10.2478/v10053-008-0133-x -->
![[Pfister & Janczyk (2013)](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3699740/), Fig. 1](images/PfisterJanczyk2013.png)
## Paired Samples t-test: Insight
The paired samples t-test of two factor levels
```{r}
#| echo: true
#| code-line-numbers: "3"
with(fhch2010_summary_naming %>%
pivot_wider(names_from = stimulus, values_from = rt, id_cols = id), #make pairing by id explicit
t.test(word, nonword, paired = TRUE)) %>%
apa::t_apa(es_ci = TRUE) # output to APA format
```
... is the **one sample t-test** of the paired differences (i.e., slopes between the factor levels).
```{r}
#| echo: true
#| code-line-numbers: "3"
with(fhch2010_summary_naming %>%
pivot_wider(names_from = stimulus, values_from = rt, id_cols = id), #make pairing by id explicit
t.test(word - nonword)) %>% # one sample t-test of paired differences
apa::t_apa(es_ci = TRUE) # output to APA format
```
## Paired Samples t-test: Viz Code
```{r}
#| echo: true
#| code-line-numbers: 3-5
pstt <-
fhch2010_summary_naming %>%
pivot_wider(names_from = stimulus, values_from = rt, id_cols = id) %>% #make pairing by id explicit
mutate(diff = word - nonword) %>%
#almost identical to one sample t-test from here
summarize(diff.m = mean(diff),
diff.ci.length = t.test(word, nonword, paired = TRUE)$conf.int %>% diff()
) %>%
ggplot(aes(y = diff.m, x = "naming words vs. non-words")) +
geom_point() + #plot the mean
geom_errorbar(aes(ymin = diff.m - diff.ci.length/2, ymax = diff.m + diff.ci.length/2)) + #plot the CI
geom_hline(yintercept = 0, linetype = "dashed") #plot the population mean to test against
```
## Paired Samples t-test: Figure
```{r}
#| echo: false
pstt
```
## ANOVA
Is the reaction time difference between words and non-words different for naming vs. lexical decision?
:::aside
The question above is equivalent to: "Is the reaction time difference between naming and lexical decision different for words vs. non-words?"
:::
. . .
```{r}
#| echo: true
aov_words <-
afex::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 %>% apa::anova_apa() #optional: slightly different (APA-conform) output
```
<!--
## ANOVA Insights
The main effect of the **between-subjects** factor `task`
```{r}
aov_words$anova_table %>%
rownames_to_column("effect") %>% #output looks weird but works!
filter(effect == "task")
```
... is the **independent samples** t-test for `task` collapsed across `stimulus`
```{r}
with(fhch2010_summary %>%
summarize(rt = mean(rt), .by = c(id, task)), #collapse across stimulus (words vs. non-words)
t.test(rt ~ task, var.equal = TRUE)$p.value) #just check p value
```
## ANOVA Insights 2
The main effect of the **within-subjects** factor `stimulus`
```{r}
#| code-line-numbers: "3"
aov_words$anova_table %>%
rownames_to_column("effect") %>% #output looks weird but works!
filter(effect == "stimulus")
```
... is **NOT?** the **paired samples** t-test for `stimulus` (ignoring `task`)
```{r}
fhch2010_summary %>%
summarize(rt = mean(rt), .by = c(id, stimulus)) %>% #collapse across task (naming vs. lexical decision)
rstatix::t_test(rt ~ stimulus, paired = TRUE) %>% pull(p) #just check p value
```
-->
## ANOVA interaction: Viz Code 1
*Within-effect* modulated by *between-variable*:\
Is the reaction time difference between *words and non-words* different for *naming vs. lexical decision*?
. . .
```{r}
#| echo: true
#| code-line-numbers: "3|5-7|10,12"
aov1 <-
fhch2010_summary %>%
pivot_wider(names_from = stimulus, values_from = rt, id_cols = c(id, task)) %>%
summarize(
.by = task, #for each condition combination
diff.m = mean(word - nonword),
diff.ci.length = t.test(word, nonword, paired = TRUE)$conf.int %>% diff()
) %>%
ggplot(aes(y = diff.m, x = task)) +
geom_point() + #plot the mean
geom_errorbar(aes(ymin = diff.m - diff.ci.length/2, ymax = diff.m + diff.ci.length/2)) + #plot the CI
geom_hline(yintercept = 0, linetype = "dashed") #plot the population mean to test against
```
## ANOVA interaction: Viz Code 2
*Between-effect* modulated by *within-variable*:\
Is the reaction time difference between *naming and lexical decision* different for *words vs. non-words*?
```{r}
#| echo: true
#| code-line-numbers: "3,5,12,13|3,6-8,10|13-16"
aov2 <-
fhch2010_summary %>%
pivot_wider(names_from = task, values_from = rt, id_cols = c(id, stimulus)) %>%
summarize(
.by = stimulus, # task is implicitly kept due to pivot_wider
ci.length = t.test(naming, lexdec)$conf.int %>% diff(), # do this first so we can overwrite naming & lexdec
naming = mean(naming, na.rm = TRUE), # we want to put CIs on RT this time, not on RT difference
lexdec = mean(lexdec, na.rm = TRUE)
) %>%
pivot_longer(cols = c(naming, lexdec), names_to = "task", values_to = "rt.m") %>%
ggplot(aes(y = rt.m, x = task, color = stimulus)) +
facet_wrap(vars(stimulus), labeller = label_both) +
geom_point(position = position_dodge(.9)) + # explicitly specify default width = .9
geom_errorbar(aes(ymin = rt.m - ci.length/2, ymax = rt.m + ci.length/2),
position = position_dodge(.9)) + # explicitly specify default width = .9
theme(legend.position = "top")
```
## ANOVA interaction: Viz Code 2.2
*Between-effect* modulated by *within-variable*:\
Is the reaction time difference between *naming and lexical decision* different for *words vs. non-words*?
```{r}
#| echo: true
#| code-line-numbers: "3,6-8"
aov2 <-
fhch2010_summary %>%
# alternative: spare the first pivot by using base R indexing (only safe for between-subjects variables)
summarize(
.by = stimulus,
ci.length = t.test(rt[task == "naming"], rt[task == "lexdec"])$conf.int %>% diff(),
naming = mean(rt[task == "naming"]), # we want to put CIs on RT this time, not on RT difference
lexdec = mean(rt[task == "lexdec"])
) %>%
pivot_longer(cols = c(naming, lexdec), names_to = "task", values_to = "rt.m") %>%
ggplot(aes(y = rt.m, x = task, color = stimulus)) +
facet_wrap(vars(stimulus), labeller = label_both) +
geom_point(position = position_dodge(.9)) + # explicitly specify default width = .9
geom_errorbar(aes(ymin = rt.m - ci.length/2, ymax = rt.m + ci.length/2),
position = position_dodge(.9)) + # explicitly specify default width = .9
theme(legend.position = "top")
```
## ANOVA interaction: Figures
:::notes
If you are asking: Can I do the plot on the right but ordered by stimulus first and with within-errors to show the within effect modulated by the between factor?
→ next slide :)
:::
```{r}
#| echo: false
cowplot::plot_grid(aov1, aov2, nrow = 1, labels = "AUTO")
```
## Within-Errors on Marginal Means
There are methods to draw within-subject errors directly on marginal means instead of on paired differences (e.g., [Morey, 2008](https://doi.org/10.20982/tqmp.01.1.p042)).
While this is okay for the simplest design including **one within-variable with just two factor levels**, this already becomes problematic at 3 levels because sphericity may not hold, i.e., the variance may be heterogenous across paired differences (level 1-2 vs. 2-3).
→ If the CI for 1-2 is small but CI for 2-3 is large. What do you plot on factor level 2?
. . .
⇒ The within-subjects standard error is a characteristic of paired differences and should thus not be plotted on factor levels.
## Correlations
What is the correlation between reactions to words and non-words in the naming task?
. . .
We want to include subject-level CIs, so we need to start with the trial-level data!
```{r}
#| echo: true
#| code-line-numbers: "2,6-7"
fhch2010_summary2 <-
afex::fhch2010 %>% #trial-level data!
filter(task == "naming") %>%
summarize(.by = c(id, task, stimulus), #retain task column for future reference
rt.m = mean(rt),
rt.m.low = t.test(rt)$conf.int[[1]], #subject-level CIs!
rt.m.high = t.test(rt)$conf.int[[2]]) #subject-level CIs!
correl <-
with(fhch2010_summary2 %>%
pivot_wider(names_from = stimulus, values_from = rt.m, id_cols = id),
cor.test(word, nonword)) %>% apa::cor_apa(r_ci = TRUE, print = FALSE)
correl
```
## Correlations: Viz Code
```{r}
#| echo: true
#| code-line-numbers: "3|4,6|7-8|5|9-11"
correlplot <-
fhch2010_summary2 %>%
pivot_wider(names_from = stimulus, values_from = starts_with("rt.m")) %>% #also rt.m.low & rt.m.high
ggplot(aes(x = rt.m_nonword, y = rt.m_word)) + #non-words vs. words
stat_smooth(method = "lm", se = TRUE) + #linear regression line with confidence bands
geom_point() + #rt means of individual subjects
geom_errorbarh(aes(xmin = rt.m.low_nonword, xmax = rt.m.high_nonword)) + #horizontal CIs: non-words
geom_errorbar (aes(ymin = rt.m.low_word, ymax = rt.m.high_word)) + #vertical CIs: words
geom_label(aes(x = min(rt.m.low_nonword), y = max(rt.m.high_word)), #statistics to show off
hjust = "inward",
label = correl) +
coord_equal() #same scaling on both axes (even though break tics are different)
```
## Correlations: Figure
```{r}
#| echo: false
correlplot
```
. . .
Error bars for non-words are bigger! \
⇒ More non-word trials to increase precision and stabilize correlation estimation?
::: notes
```{r}
#| echo: true
fhch2010_summary2 %>%
summarize(.by = stimulus,
precision = mean(rt.m.high - rt.m.low))
```
:::
# Visualizing "Raw Data"
As we see from the previous scatter plot, it is informative to see individual "raw data" points. Wouldn't this be nice to have for the group-differences plots (t-tests + ANOVA), too?
With `ggplot`, we can simply add a new layer on our previous plots `aov1` and `aov2`.
\
Quick technical note: Usually, we do not visualize the "raw" (trial-level) data but the subject-level *aggregates* (→ we could always visualize subject-level precision!). \
<!--
But, hey! It's at least one additional level of information! 😅
-->
:::notes
We will not include subject-level precision in the next plots because it is irrelevant for group-level significance (except for its contribution to increasing group-level variance, which is already depicted in the group-level errorbars). Thus, the additional informational value is usually extremely limited while strongly decreasing readability of the plots (a lot of busy errorbars!).
:::
## Stacked Points
```{r}
#| echo: true
#| code-line-numbers: "2|3-5|6"
#| output-location: slide
aov1 +
geom_dotplot( # experts can try ggbeeswarm::geom_beeswarm()
data = fhch2010_summary %>% #I should have saved this calculation step...
pivot_wider(names_from = "stimulus", values_from = "rt", id_cols = c(id, task)) %>%
mutate(diff.m = word - nonword), #if we call this diff.m, it conforms to aov1
#aes(y = diff.m, x = task), #inherited from aov1
binaxis = "y",
stackdir = "center",