-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel-accuracy-posthoc.R
151 lines (141 loc) · 6.75 KB
/
model-accuracy-posthoc.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
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
#!/usr/bin/env Rscript
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(afex))
## load data
load(file.path("models", "accuracy-model-nobias-final.RData")) # provides `mod`
emm_options(pbkrtest.limit=10000, lmerTest.limit=10000)
outdir <- "posthocs"
dir.create(outdir, showWarnings=FALSE)
# print(mod$anova_table)
# print(summary(mod$full_model))
stars <- function(pval) {
ifelse(pval < 0.001, "***", ifelse(pval < 0.01, "**",
ifelse(pval < 0.05, "*", "")))
}
replace_names <- function(x) {
x <- replace(x, x=="non-spatial", "non-\nspatial")
x <- replace(x, x=="ldiff", "listening\ndifficulty")
x <- replace(x, x=="ctrl", "control")
}
## MAIN EFFECTS
maineffs <- c("ldiff", "attn", "space")
for (maineff in maineffs) {
## make emmeans objects
contr <- switch(maineff, attn="trt.vs.ctrlk", ldiff="trt.vs.ctrlk",
space="revpairwise")
posthoc <- emmeans(mod, spec=maineff, by="truth", type="response",
contr=contr)
assign(maineff, posthoc, envir=.GlobalEnv)
## make dataframe for plotting
name <- paste0(maineff, "_emmeans")
as.data.frame(get(maineff)$emmeans) %>%
rename(level=maineff, token=truth) %>%
mutate(variable=maineff,
level=as.character(level),
level=replace(level, level=="non-spatial", "non-\nspatial"),
level=replace(level, level=="ldiff", "listening\ndifficulty"),
level=replace(level, level=="ctrl", "control")) %>%
select(variable, level, token, everything()) ->
this_df
assign(name, this_df, envir=.GlobalEnv)
## make dataframe for post-hocs
name <- paste0(maineff, "_posthocs")
as.data.frame(get(maineff)$contrasts) %>%
rename(token=truth) %>%
mutate(variable=maineff, signif=stars(p.value)) %>%
select(variable, contrast, token, everything()) ->
this_df
assign(name, this_df, envir=.GlobalEnv)
}
## aggregate
main_effects <- bind_rows(ldiff_emmeans, attn_emmeans, space_emmeans)
maineff_posthocs <- bind_rows(ldiff_posthocs, attn_posthocs, space_posthocs)
## INTERACTIONS
interacts <- list(c("ldiff", "space"), c("attn", "ldiff"), c("space", "attn"))
for (interact in interacts) {
## make emmeans objects
spec <- interact[1]
grby <- interact[2]
contr <- switch(spec, attn="trt.vs.ctrlk", ldiff="trt.vs.ctrlk",
space="revpairwise")
posthoc <- emmeans(mod, spec=spec, by=c(grby, "truth"),
type="response", contr=contr)
name <- paste(interact, collapse="_")
assign(name, posthoc, envir=.GlobalEnv)
## make dataframe for plotting
name_emm <- paste0(name, "_emmeans")
as.data.frame(get(name)$emmeans) %>%
rename(level=spec, group=grby, token=truth) %>%
mutate(grouping=grby, variable=spec,
level=as.character(level),
level=replace_names(level)) %>%
select(grouping, group, variable, level, token, everything()) ->
this_df
assign(name_emm, this_df, envir=.GlobalEnv)
## make dataframe for post-hocs
name_ph <- paste0(name, "_posthocs")
as.data.frame(get(name)$contrasts) %>%
rename(group=grby, token=truth) %>%
mutate(grouping=grby, variable=spec, signif=stars(p.value),
group=as.character(group),
group=replace_names(group)) %>%
select(grouping, group, variable, contrast, token, everything()) ->
this_df
assign(name_ph, this_df, envir=.GlobalEnv)
}
## aggregate
interactions <- bind_rows(ldiff_space_emmeans, attn_ldiff_emmeans,
space_attn_emmeans)
interact_posthocs <- bind_rows(ldiff_space_posthocs, attn_ldiff_posthocs,
space_attn_posthocs)
## SAVE
write.csv(main_effects, file=file.path(outdir, "main-effects-estimates.csv"),
row.names=FALSE)
write.csv(interactions, file=file.path(outdir, "interaction-estimates.csv"),
row.names=FALSE)
write.csv(maineff_posthocs, file=file.path(outdir, "main-effects-posthocs.csv"),
row.names=FALSE)
write.csv(interact_posthocs, file=file.path(outdir, "interaction-posthocs.csv"),
row.names=FALSE)
## MODEL SUMMARY
modsum <- summary(mod$full_model)$coefficients
renamer <- c(`(Intercept)`="neither:::",
`truth_target`="target:::",
`truth_foil`="foil:::",
`truthneither:ldiff_ldiff`="neither:ldiff::",
`truthtarget:ldiff_ldiff`="target:ldiff::",
`truthfoil:ldiff_ldiff`="foil:ldiff::",
`truthneither:attn_switch`="neither::switch:",
`truthtarget:attn_switch`="target::switch:",
`truthfoil:attn_switch`="foil::switch:",
`truthneither:space_non-spatial`="neither:::non-spatial",
`truthtarget:space_non-spatial`="target:::non-spatial",
`truthfoil:space_non-spatial`="foil:::non-spatial",
`truthneither:space_spatial`="neither:::spatial",
`truthtarget:space_spatial`="target:::spatial",
`truthfoil:space_spatial`="foil:::spatial",
`truthneither:ldiff_ldiff:attn_switch`="neither:ldiff:switch:",
`truthtarget:ldiff_ldiff:attn_switch`="target:ldiff:switch:",
`truthfoil:ldiff_ldiff:attn_switch`="foil:ldiff:switch:",
`truthneither:ldiff_ldiff:space_non-spatial`="neither:ldiff::non-spatial",
`truthtarget:ldiff_ldiff:space_non-spatial`="target:ldiff::non-spatial",
`truthfoil:ldiff_ldiff:space_non-spatial`="foil:ldiff::non-spatial",
`truthneither:ldiff_ldiff:space_spatial`="neither:ldiff::spatial",
`truthtarget:ldiff_ldiff:space_spatial`="target:ldiff::spatial",
`truthfoil:ldiff_ldiff:space_spatial`="foil:ldiff::spatial",
`truthneither:attn_switch:space_non-spatial`="neither::switch:non-spatial",
`truthtarget:attn_switch:space_non-spatial`="target::switch:non-spatial",
`truthfoil:attn_switch:space_non-spatial`="foil::switch:non-spatial",
`truthneither:attn_switch:space_spatial`="neither::switch:spatial",
`truthtarget:attn_switch:space_spatial`="target::switch:spatial",
`truthfoil:attn_switch:space_spatial`="foil::switch:spatial")
col_renamer <- c(Estimate="coef", `Std. Error`="stderr", `z value`="z", `Pr(>|z|)`="p")
rownames(modsum) <- renamer[rownames(modsum)]
colnames(modsum) <- col_renamer[colnames(modsum)]
modsum %>%
as.data.frame() %>%
mutate(effect=rownames(.), signif=stars(p)) %>%
tidyr::separate(effect, into=c("truth", "ldiff", "attn", "space"), sep=":") %>%
select(truth, ldiff, attn, space, everything()) ->
modsum
write.csv(modsum, file=file.path(outdir, "model-summary.csv"), row.names=FALSE)