-
Notifications
You must be signed in to change notification settings - Fork 0
/
English past priming_bayesian.R
231 lines (182 loc) · 6.6 KB
/
English past priming_bayesian.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
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
###BAYESIAN####
setwd('/Users/jobrenn/Documents/Projects/cho-priming/')
library(brms)
library(tidyverse)
library(bayesplot)
library(tidybayes)
library(emmeans)
data2 <- read_csv("English_past_trimmed.csv")
# merge with word frequencies
prime <- read.csv("prime_char.csv")
target <- read.csv("target_char.csv")
data2<-merge(data2, prime, by="prime")
data2<-merge(data2, target, by="target")
# prep condition info
data2 <- data2 %>%
mutate(type = if_else(condition %in% c("11", "12", "13"),
'Morphological',
'Orthographical')) %>%
mutate(cond = case_when(
condition %in% c("11", "14") ~ "Identity",
condition %in% c("12", "15") ~ "Test",
TRUE ~ "Control" ) ) %>%
mutate(cond = factor(cond,
levels=c(levels=c("Identity", "Test", "Control"))))
# sum coding for categorical effects?
#contrasts(data2$condition2) <- cbind(C1 = c(1, 0, 0), C2 = c(0, 1, 0))
#data2$type2<-ifelse((data2$type=="Orthographical"), -0.5, 0.5)
#data2$SOA2<-ifelse((data2$SOA=="masked"), -0.5, 0.5)
# center continuous covariates
data2 <- data2 %>%
mutate(primelength = str_length(prime)) %>%
mutate(primelength.c = scale(primelength, center=TRUE, scale=FALSE),
targetlength.c = scale(wordlength, center=TRUE, scale=FALSE),
prime_freq.c = scale(prime_freq, center=TRUE, scale=FALSE),
target_freq.c = scale(target_freq, center=TRUE, scale=FALSE))
# to set reasonably weakly-informative priors
mu=mean(log(data2$rt))
sigma=sd(log(data2$rt))
mu2=exp(mu+sigma^2/2)
sigma2=mu2*sqrt(exp(sigma^2)-1)
#sigma: exponential distribution -exp(1)
# set priors
# see: https://vasishth.github.io/bayescogsci/book/sec-trial.html
# and: https://vasishth.github.io/bayescogsci/book/modeling-a-lexical-decision-task.html
my_priors <- c(
prior(normal(6, 1), class = 'Intercept'),
prior(normal(0, 1), class = 'b'),
prior(normal(0, 1), class = 'sigma'),
prior(normal(0, 1), class = 'sd')
)
### SANDBOX
subjects <- unique(data2$participant)
# test
data2 %>%
filter(participant %in% subjects[1:80]) %>%
brm(rt ~ 1 + targetlength.c + (1 + targetlength.c | participant),
data = .,
prior = my_priors,
cores = 4,
iter=8000,
family= lognormal() ) -> m_test
m_test
# pairs(m_test)
## NOTES
# <https://discourse.mc-stan.org/t/low-ess-and-high-rhat-for-random-intercept-slope-simulation-rstan-and-rstanarm/9985/2>
# - could the model be too good? no
# - I think the problem is non-independence between intercept and participant sd... and I think it can be addressed by increasing iterations;??
# - IF increased iterations keeps RHat low... it does!?
# - ELSE there is something wrong with the model...
#
# - model: rt ~ 1, subjects[1:20]: looks good!
#
# - model: rt ~ 1 + (1|participant)
# - subjects[1:5]: ESS ~ 1000
# - subjects[1:20]: ESS < 300 :( :(
# - no problem with (1|item)!
# - population priors too loose?
#
# - model: rt ~ intercept + targetlength.c + (1|participant)
# - 20 - 40 participants (about 1 min to fit)
# - lognormal() returns more accurate fit than shifted_lognormal(),
# intercept = 6.5 rather than 6.0... effect of shift param??
# - regardless Intercept is poorly explored (ESS < 400; rhat >1.01)
# - ESS *lower* with more participants
summary(m_test)
plot(m_test)
pp_check(m_test)
mcmc_plot(m_test)
mcmc_plot(m_test, "^b_[^I]")
### END SANDBOX
##################
# simple version #
##################
# fit model
f2<-brm(rt ~ cond * type * SOA + primeLength.c + targetLength.c +
(1 | participant) +
(1 | item),
data=data2,
prior = my_priors,
cores=4,
iter = 8000,
family=lognormal())
# fit model (frequency included)
f2<-brm(rt ~ cond * type * SOA * prime_freq.c * target_freq.c + primeLength.c + targetLength.c +
(1 | participant) +
(1 | item),
data=data2,
prior = my_priors,
cores=4,
iter = 8000,
family=lognormal())
summary(f2)
plot(f2)
pp_check(f2)
mcmc_plot(f2)
mcmc_plot(f2, "^b_[^I]")
save(f2, file='model-simple.Rda')
#load(file='model-simple.Rda')
# plot estimated effects
color_scheme_set('red')
mcmc_areas_ridges(f2, regex_pars="^b_[^I]")
# Many ways to plot expected values across conditions!
# (1) with conditional_effects
c <- make_conditions(data2, vars=c('SOA'))
conditional_effects(f2,
effects='type:cond',
conditions = c)
# (2) - with emmeans...
# same as 1!
my_emm <- f2 %>% emmeans(~ cond + type + SOA, epred=TRUE)
my_emm %>%
summary() %>%
# gather_emmeans_draws(ndraws=100)
ggplot(aes(x=type, y=emmean, ymin=lower.HPD, ymax=upper.HPD, col=cond)) +
geom_pointinterval(position='dodge') +
facet_wrap(~SOA) # why do these have less var than conditional_effects?
# (3) - emmeans but now with draws
# (same again!)
my_emm %>% gather_emmeans_draws(ndraws=500) %>%
ggplot(aes(x=type, y=.value, col=cond)) +
stat_pointinterval(position='dodge') +
facet_wrap(~SOA) # why do these have less var than conditional_effects?
# (4) - fitted values for every data point DON'T DO THIS
# less var! prog not marginalizing correctly!!
my_draws <- data2 %>% add_epred_draws(f2, ndraws=100, re_formula)
my_draws %>%
group_by(type, cond, SOA, .draw) %>%
summarize(yhat = mean(.epred)) %>%
ggplot(aes(x=type, y=yhat, col=cond)) +
stat_pointinterval(position='dodge') +
facet_wrap(~SOA) # why do these have less var than conditional_effects?
# (5) - fitted values for target conditions all else NA
# same as conditional_effects!
my_draws_2 <-
expand_grid(
type = unique(data2$type),
cond = unique(data2$cond),
SOA = unique(data2$SOA),
targetLength.c = 0,
primeLength.c = 0,
) %>%
add_epred_draws(f2, ndraws=500, re_formula = NA)
my_draws_2 %>%
ggplot(aes(x=type, y=.epred, col=cond)) +
stat_pointinterval(position='dodge') +
facet_wrap(~SOA) # why do these have MORE var than conditional_effects?
# TAKE-AWAYS: conditional_effects (1), emmeans (2), emmeans+tidybayes (3) and just tidybayes with covariates set to zero/NA (5) are all equivalent and look good; DO NOT average tidybayes draws from every datapoint!!! (4)
#########################
#full version (updated) #
#########################
f3 <- brm(rt ~ cond * type * SOA +
primeLength.c + targetLength.c +
(1 + cond * type *SOA || participant) +
(1 + cond * type *SOA || item),
data=data2,
prior = my_priors,
iter = 8000,
family=lognormal())
summary(f3)
plot(f3)
pp_check(f3)
save(f3, file='model-complex.Rda')