-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrorbars.R
77 lines (61 loc) · 3.08 KB
/
errorbars.R
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
library(tidyverse)
library(afex)
library(apa)
se <- function(x, na.rm = TRUE) {
sd(x, na.rm) / sqrt(if(!na.rm) length(x) else sum(!is.na(x)))
}
CI <- .95 #we want 95% CIs
z.CI <- qnorm(1-(1-CI)/2) #two-sided CI
# Load Data ---------------------------------------------------------------
data <- read_rds("eye.rds") %>% tibble() %>%
filter(phase == "Gen") %>%
select(subject, trial, dwell, dwell.non, threat, diagnostic) %>%
pivot_longer(cols=contains("dwell"), names_to = "diagnosticity", values_to = "dwell") %>%
mutate(diagnosticity = if_else(diagnosticity %>% grepl("non", .), "Non-Diagnostic", "Diagnostic") %>% as_factor()) %>%
summarize(.by = c(subject, diagnostic, diagnosticity),
dwell = mean(dwell))
# ANOVA -------------------------------------------------------------------
afex::aov_ez(data = data,
dv = "dwell",
id = "subject",
within = c("diagnostic", "diagnosticity"),
include_aov = TRUE) %>% apa::anova_apa(force_sph_corr = TRUE)
# Compute Between Standard Error ------------------------------------------
#wrong
se.wrong <- data %>% summarize(.by = "diagnosticity", dwell.m = mean(dwell), dwell.se = se(dwell))
data %>% summarize(.by = "diagnosticity",
subjects = subject %>% unique %>% length(),
rows = n(),
ratio = paste((rows / subjects), "1", sep = ":"))
#correct
data.se <- data %>% summarize(.by = c("subject", "diagnosticity"),
dwell = mean(dwell)) %>%
summarize(.by = "diagnosticity", dwell.m = mean(dwell), dwell.se = se(dwell))
full_join(data.se, se.wrong %>% rename(dwell.se.wrong = dwell.se))
#plot
plot.between <- data.se %>% ggplot(aes(y = dwell.m, x = diagnosticity, fill = diagnosticity)) +
geom_col(color = "black") +
geom_errorbar(aes(ymin = dwell.m - dwell.se*z.CI, ymax = dwell.m + dwell.se*z.CI), width = .5) +
scale_fill_viridis_d() + theme_bw()
plot.between
# Compute Within Standard Error -------------------------------------------
data.se.within <- data %>% summarize(.by = c("subject", "diagnosticity"),
dwell = mean(dwell)) %>%
pivot_wider(names_from = diagnosticity, values_from = dwell) %>%
mutate(diff = Diagnostic - `Non-Diagnostic`) %>%
summarize(dwell.m = mean(diff), dwell.se = se(diff)) %>% mutate(diagnosticity = "Difference")
#plot
# plot.within <- data.se.within %>% ggplot(aes(y = dwell.m, x = diagnosticity)) +
# geom_col(color = "black") +
# geom_errorbar(aes(ymin = dwell.m - dwell.se*z.CI, ymax = dwell.m + dwell.se*z.CI), width = .5) +
# theme_bw()
# plot.within
#
# library(patchwork)
# plot.between + plot.within + plot_annotation(tag_levels = 'a') + plot_layout(axes = "collect")
plot.within <- data.se %>% bind_rows(data.se.within) %>% mutate(diagnosticity = diagnosticity %>% as_factor()) %>%
ggplot(aes(y = dwell.m, x = diagnosticity, fill = diagnosticity)) +
geom_col(color = "black") +
geom_errorbar(aes(ymin = dwell.m - dwell.se*z.CI, ymax = dwell.m + dwell.se*z.CI), width = .5) +
scale_fill_viridis_d() + theme_bw()
plot.within