-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTestScript.R
417 lines (344 loc) · 12.8 KB
/
TestScript.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
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
################################################################################
######### ANALYZE R-PACKAGE STATCONFR ##########################################
################################################################################
# Manuel Rausch, 15.04.2023
# To demonstrate that the package is working, we first fitted all models to the data of a
# masked orientation discrimination task, and then use the obtained parameter sets
# for a parameter recovery analysis. Unfortunately, running the complete script takes several hours
# on a PC with 15 cores. We upload the script anyway to document how we have varified the
# the functionality of our package.
# For those who want to test whether the package is working in principle, we
# provide a shorter script "QuickTests.R".
# We also uploaded the results of our own run of "TestScripts.R" to show the results of our
# parameter recovery analysis.
# 0) Preparations
# 1) Fit all models to the dataset from Hellmann et al. (2023) Exp. 1
# 2) For each model: Parameter recovery based on simulated data using the fitted parameter sets
# 2.1) SDT
# 2.2) GN
# 2.3) PDA
# 2.4) IG
# 2.5) WEV
# 2.6) ITGc
# 2.7) ITGcm
# 2.8) logN
# 2.9) logWEV
# 3) meta-d′/d′
# 3.1) meta-d′/d′ using Maniscalco and Lau (2012)'s model specification
# 3.2) meta-d′/d′ using Fleming (2017)'s model specification
# 4. meta-I and co
# 5. Visualize Model fit
# 6) Bayesian model selection
# 0) Preparations
rm(list=ls())
setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) # if you are not using R-Studio, specify here a working directory.
library(tidyverse)
library(statConfR)
if (file.exists("TestResults.RData")) load("TestResults.RData")
# 1) Fit all models to the dataset from Hellmann et al. (2023) Exp. 1
fitted_pars <-
MaskOri %>%
# filter(participant < 3) %>% #uncomment this line if you don't have so much time for testing the package"
fitConfModels(models = "all",
.parallel = TRUE)
PlotFitsBICWeights <-
fitted_pars %>% #group_by(participant) %>%
ggplot(aes(x=participant, y=wBIC, fill = model)) +
geom_bar(stat="identity", color="black") +
scale_x_continuous(breaks=unique(fitted_pars$participant)) +
labs(fill = "Model")+
ylab("Schwarz Weights") +
theme_minimal()
PlotFitsBICWeights
# 2) Parameter recovery based on simulated data using the fitted parameter sets
# 2.1) SDT
recov_pars_SDT <-
fitted_pars %>%
filter(model=="SDT") %>%
group_by(participant) %>%
simConf(model="SDT") %>%
fitConfModels(models = "SDT", .parallel = TRUE)
Plot_recov_SDT <-
merge(recov_pars_SDT %>%
select(-model, -c(negLogLik:AIC)) %>%
pivot_longer(cols = d_1:theta_plus.4),
fitted_pars %>%
filter(model=="SDT") %>%
select(participant, d_1:theta_plus.4) %>%
pivot_longer(cols = d_1:theta_plus.4,
values_to = "true")) %>%
ggplot(aes(x=true, y=value)) +
facet_wrap(~ name, nrow=4, scales="free") + xlab("true parameter") + ylab("estimated parameter") +
geom_point(color="purple") +
geom_abline(slope = 1, intercept = 0, linetype = "dashed") +
theme_minimal()
Plot_recov_SDT
# 2.2) GN
recov_pars_GN <-
fitted_pars %>%
filter(model=="GN") %>%
group_by(participant) %>%
simConf(model="GN") %>%
fitConfModels(models = "GN",
.parallel = TRUE)
Plot_recov_GN <-
merge(recov_pars_GN %>%
select(-model, -c(negLogLik:AIC)) %>%
pivot_longer(cols = d_1:sigma),
fitted_pars %>%
filter(model=="GN") %>%
select(participant, d_1:sigma) %>%
pivot_longer(cols = d_1:sigma,
values_to = "true")) %>%
ggplot(aes(x=true, y=value)) +
facet_wrap(~ name, nrow=4, scales="free") + xlab("true parameter") + ylab("estimated parameter") +
geom_point(color="purple") +
geom_abline(slope = 1, intercept = 0, linetype = "dashed") +
theme_minimal()
Plot_recov_GN
# (iii) PDA
recov_pars_PDA <-
fitted_pars %>%
filter(model=="PDA") %>%
group_by(participant) %>%
simConf(model="PDA") %>%
fitConfModels(models = "PDA",
.parallel = TRUE)
Plot_recov_PDA <-
merge(recov_pars_PDA %>%
select(-model, -c(negLogLik:AIC)) %>%
pivot_longer(cols = d_1:b),
fitted_pars %>%
filter(model=="PDA") %>%
select(participant, d_1:b) %>%
pivot_longer(cols = d_1:b,
values_to = "true")) %>%
ggplot(aes(x=true, y=value)) +
facet_wrap(~ name, nrow=4, scales="free") + xlab("true parameter") + ylab("estimated parameter") +
geom_point(color="purple") +
geom_abline(slope = 1, intercept = 0, linetype = "dashed") +
theme_minimal()
Plot_recov_PDA
# (iv) IG
recov_pars_IG <-
fitted_pars %>%
filter(model=="IG") %>%
group_by(participant) %>%
simConf(model="IG") %>%
fitConfModels(models = "IG",
.parallel = TRUE)
Plot_recov_IG <-
merge(recov_pars_IG %>%
select(-model, -c(negLogLik:AIC)) %>%
pivot_longer(cols = d_1:m),
fitted_pars %>%
filter(model=="IG") %>%
select(participant, d_1:m) %>%
pivot_longer(cols = d_1:m,
values_to = "true")) %>%
ggplot(aes(x=true, y=value)) +
facet_wrap(~ name, nrow=4, scales="free") + xlab("true parameter") + ylab("estimated parameter") +
geom_point(color="purple") +
geom_abline(slope = 1, intercept = 0, linetype = "dashed") +
theme_minimal()
Plot_recov_IG
# (v) WEV
recov_pars_WEV <-
fitted_pars %>%
filter(model=="WEV") %>%
group_by(participant) %>%
simConf(model="WEV") %>%
fitConfModels(models = "WEV",
.parallel = TRUE)
Plot_recov_WEV <-
merge(recov_pars_WEV %>%
select(-model, -c(negLogLik:AIC)) %>%
pivot_longer(cols = d_1:w),
fitted_pars %>%
filter(model=="WEV") %>%
select(participant, d_1:w) %>%
pivot_longer(cols = d_1:w,
values_to = "true")) %>%
ggplot(aes(x=true, y=value)) +
facet_wrap(~ name, nrow=4, scales="free") + xlab("true parameter") + ylab("estimated parameter") +
geom_point(color="purple") +
geom_abline(slope = 1, intercept = 0, linetype = "dashed") +
theme_minimal()
Plot_recov_WEV
# (vi) ITGc
recov_pars_ITGc <-
fitted_pars %>%
filter(model=="ITGc") %>%
group_by(participant) %>%
simConf(model="ITGc") %>%
fitConfModels(models = "ITGc", .parallel = TRUE)
Plot_recov_ITGc <-
merge(recov_pars_ITGc %>%
select(-model, -c(negLogLik:AIC)) %>%
pivot_longer(cols = d_1:m),
fitted_pars %>%
filter(model=="ITGc") %>%
select(participant, d_1:m) %>%
pivot_longer(cols = d_1:m,
values_to = "true")) %>%
ggplot(aes(x=true, y=value)) +
facet_wrap(~ name, nrow=4, scales="free") + xlab("true parameter") + ylab("estimated parameter") +
geom_point(color="purple") +
geom_abline(slope = 1, intercept = 0, linetype = "dashed") +
theme_minimal()
Plot_recov_ITGc
# (vii) ITGcm
recov_pars_ITGcm <-
fitted_pars %>%
filter(model=="ITGcm") %>%
group_by(participant) %>%
simConf(model="ITGcm") %>%
fitConfModels(models = "ITGcm", .parallel = TRUE)
Plot_recov_ITGcm <-
merge(recov_pars_ITGc %>%
select(-model, -c(negLogLik:AIC)) %>%
pivot_longer(cols = d_1:m),
fitted_pars %>%
filter(model=="ITGcm") %>%
select(participant, d_1:m) %>%
pivot_longer(cols = d_1:m,
values_to = "true")) %>%
ggplot(aes(x=true, y=value)) +
facet_wrap(~ name, nrow=4, scales="free") + xlab("true parameter") + ylab("estimated parameter") +
geom_point(color="purple") +
geom_abline(slope = 1, intercept = 0, linetype = "dashed") +
theme_minimal()
Plot_recov_ITGcm
# (viii) logN
recov_pars_logN <-
fitted_pars %>%
filter(model=="logN") %>%
group_by(participant) %>%
simConf(model="logN") %>%
fitConfModels(models = "logN", .parallel = TRUE)
Plot_recov_logN <-
merge(recov_pars_logN %>%
select(-model, -c(negLogLik:AIC)) %>%
pivot_longer(cols = c(d_1:c, M_theta_minus.4:M_theta_plus.4, sigma)),
fitted_pars %>%
filter(model=="logN") %>%
select(participant, d_1:sigma) %>%
pivot_longer(cols = d_1:sigma,
values_to = "true")) %>%
ggplot(aes(x=true, y=value)) +
facet_wrap(~ name, nrow=4, scales="free") + xlab("true parameter") + ylab("estimated parameter") +
geom_point(color="purple") +
geom_abline(slope = 1, intercept = 0, linetype = "dashed") +
theme_minimal()
Plot_recov_logN
# (ix) logWEV
recov_pars_logWEV <-
fitted_pars %>%
filter(model=="logWEV") %>%
group_by(participant) %>%
simConf(model="logWEV") %>%
fitConfModels( models = "logWEV", .parallel = TRUE)
Plot_recov_logWEV <-
merge(recov_pars_logWEV %>%
select(-model, -c(negLogLik:AIC)) %>%
pivot_longer(cols = d_1:w),
fitted_pars %>%
filter(model=="logWEV") %>%
select(participant, d_1:w) %>%
pivot_longer(cols = d_1:w,
values_to = "true")) %>%
ggplot(aes(x=true, y=value)) +
facet_wrap(~ name, nrow=4, scales="free") + xlab("true parameter") + ylab("estimated parameter") +
geom_point(color="purple") +
geom_abline(slope = 1, intercept = 0, linetype = "dashed") +
theme_minimal()
Plot_recov_logWEV
# 3) meta-d′/d′
# 3.1) meta-d′/d′ using Maniscalco and Lau (2012)'s model specification
recov_metaDprime_ML <-
fitted_pars %>%
filter(model=="ITGcm") %>%
filter(participant!=11) %>% # subject 11 performed very low.
select(participant, d_3, c:theta_plus.4, m) %>%
rename(d_1 = d_3) %>%
mutate(N = 10000) %>% #
group_by(participant) %>%
simConf(model="ITGcm") %>%
fitMetaDprime(model="ML", .parallel = TRUE)
Plot_recov_metaDprime_ML <-
merge(recov_metaDprime_ML %>%
select(participant, Ratio),
fitted_pars %>%
filter(model=="ITGcm") %>%
select(participant, m)) %>%
ggplot(aes(x=m, y=Ratio)) + #scale_x_log10() + scale_y_log10() +
xlab("m-parameter") + ylab("meta-d′/d′") +
geom_point(color="purple") +
geom_abline(slope = 1, intercept = 0, linetype = "dashed") +
theme_minimal()
Plot_recov_metaDprime_ML
# 3.2) meta-d′/d′ using Fleming (2017)'s model specification
recov_metaDprime_F <-
fitted_pars %>%
filter(model=="ITGc") %>%
filter(participant!=11) %>% # subject 11 performed very low, so meta-d'/d' will be unstable no matter whether the code works or not.
select(participant, d_3, c:theta_plus.4, m) %>%
rename(d_1 = d_3) %>%
mutate(N = 10000) %>% # simulate 400 trials because 400 trials considered to be required to estimate meta-d′/d′
group_by(participant) %>%
simConf(model="ITGc") %>%
fitMetaDprime(model="F", .parallel = TRUE)
Plot_recov_metaDprime_F <-
merge(recov_metaDprime_F %>%
select(participant, Ratio),
fitted_pars %>%
filter(model=="ITGc" ) %>%
select(participant, m)) %>%
ggplot(aes(x=m, y=Ratio)) + #scale_x_log10() + scale_y_log10() +
xlab("m-parameter") + ylab("meta-d′/d′") +
geom_point(color="purple") +
geom_abline(slope = 1, intercept = 0, linetype = "dashed") +
theme_minimal()
Plot_recov_metaDprime_F
# 4. meta-I and co
MetaDs <- fitMetaDprime(data = MaskOri, model="ML", .parallel = TRUE)
MetaInfoMeasures <- estimateMetaI(data = MaskOri, bias_reduction = F)
merge(MetaDs %>% select(participant, Ratio),
MetaInfoMeasures %>% select(participant, meta_Ir1)) %>%
ggplot(aes(x=Ratio, y=meta_Ir1)) +
geom_point() + geom_smooth(method="lm", se=F)+
theme_minimal()
# 5) Plotting fits
PlotFitSDT <- plotConfModelFit(data=MaskOri, fitted_pars=fitted_pars, model="SDT")
PlotFitGN <- plotConfModelFit(MaskOri, fitted_pars, model="GN")
PlotFitLogN <- plotConfModelFit(MaskOri, fitted_pars, model="logN")
PlotFitWEV <- plotConfModelFit(MaskOri, fitted_pars, model="WEV")
PlotFitLogWEV <- plotConfModelFit(MaskOri, fitted_pars, model="logWEV")
PlotFitITGcm <- plotConfModelFit(MaskOri, fitted_pars, model="ITGcm")
PlotFitITGc <- plotConfModelFit(MaskOri, fitted_pars, model="ITGc")
PlotFitIG <- plotConfModelFit(MaskOri, fitted_pars, model="IG")
PlotFitPDA <- plotConfModelFit(MaskOri, fitted_pars, model="PDA")
# 6) Bayesian model selection
save(fitted_pars, PlotFitsBICWeights,
recov_pars_SDT, Plot_recov_SDT,
recov_pars_GN, Plot_recov_GN,
recov_pars_logN, Plot_recov_logN,
recov_pars_WEV, Plot_recov_WEV,
recov_pars_logWEV, Plot_recov_logWEV,
recov_pars_IG, Plot_recov_IG, # re-test that
recov_pars_ITGc, Plot_recov_ITGc,
recov_pars_ITGcm, Plot_recov_ITGcm,
recov_pars_PDA, Plot_recov_PDA,
recov_metaDprime_ML, Plot_recov_metaDprime_ML,
recov_metaDprime_F, Plot_recov_metaDprime_F,
MetaInfoMeasures,
PlotFitSDT,
PlotFitGN ,
PlotFitLogN ,
PlotFitWEV,
PlotFitLogWEV ,
PlotFitITGcm,
PlotFitITGc,
PlotFitIG,
PlotFitPDA ,
file = "TestResults.RData")