forked from deniztu/minerva_al
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 18.3 KB
/
.Rhistory
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
training_array <- rbind(training_array, c(locations[[ifelse(i %% positions, i %% positions, positions)]], tail(training_array[i-1,],20) ,responses[[ifelse(i %% positions, i %% positions, positions)]]))
}
}
# random data Si//R(i-1)//R
for (i in 1:(n_trials+1)) {
if (i == 1){
random_array <- matrix(c(locations[[sample.int(positions, 1)]], zeros, responses[[sample.int(positions, 1)]]), ncol=60)
}else{
random_array <- rbind(random_array, c( locations[[sample.int(positions, 1)]], tail(random_array[i-1,],20) , responses[[sample.int(positions, 1)]]))
}
}
# practice data
for (i in 1:(practice_trials+1)) {
if (i == 1){
practice_array <- matrix(c(practice_locations[[1]], zeros, practice_responses[[1]]), ncol=60)
}else{
practice_array <- rbind(practice_array, c(practice_locations[[ifelse(i %% positions, i %% positions, positions)]], tail(practice_array[i-1,],20) ,practice_responses[[ifelse(i %% positions, i %% positions, positions)]]))
}
}
# number of replications
n_replications <- 25
# learning rates
p_encode <- c(0.33, 0.67, 1)
# specify models
model = "Minerva2"
resonance_summary_df
resonance_summary_df
tmp <- resonance_summary_df
# random test
#-----------------------100 Pretrials Condition--------------------------------#
for (m in 1:length(model)){
for (r in 1:n_replications) {
print(r)
for(i in 1:length(p_encode)) {
#To acknowledge the instructions given to the participants before starting the experiment proper, we stocked memory with one example of each S(i)//R(i21)//R(i) contingency with L ¼ 1.
# Memory is empty on first trial
normalized_echo <- probe_memory(random_array[1,], NULL, cue_features, model = model[m])
expectancy <- expect_event(random_array[1,], normalized_echo)
memory <- learn(
normalized_echo
, random_array[1,]
, 1
, NULL
, model = model[m]
)
# stock with 4 further exemplar memories
for(x in 2:5){
normalized_echo <- probe_memory(random_array[x,], memory, cue_features, model = model[m])
expectancy <- expect_event(random_array[x,], normalized_echo)
memory <- learn(
normalized_echo
, random_array[x,]
, 1
, memory
, model = model[m]
)
}
# practice blocks
for(p in 1:nrow(practice_array)){
normalized_echo <- probe_memory(practice_array[p,], memory, cue_features, model = model[m])
expectancy <- expect_event(practice_array[p,], normalized_echo)
memory <- learn(
normalized_echo
, practice_array[p,]
, 0.7
, memory
, model = model[m]
)
}
# normalized_echo <- probe_memory(test_ac_event, NULL, cue_features, model = model[m])
# expectancy <- expect_event(recall_abc_event, normalized_echo)
# sim_results_100_pretrials[[i,1,r]] <- expectancy
# normalized_echos_100_pretrials[[i,1,r]] <- normalized_echo
# Experimental trials
for(j in 2:(n_trials+1)) {
iteration <- 1
expectancy <- 0
while(expectancy<=0.99){
res <- probe_memory_resonance(random_array[j,], memory, cue_features, model = model[m], iteration)
normalized_echo <- res$normalized_echo
expectancy <- expect_event(random_array[j,], normalized_echo)
iteration <- iteration+1
#print(expectancy)
}
memory <- learn(
normalized_echo
, random_array[j,]
, p_encode[i]
, memory
, model = model[m]
)
#normalized_echo <- probe_memory(random_array[j,], memory, cue_features, model = model[m])
expectancy <- expect_event(random_array[j,], normalized_echo)
sim_resonance_100_pretrials[[i,j,r]] <- iteration
sim_results_100_pretrials[[i,j,r]] <- expectancy
normalized_echos_100_pretrials[[i,j,r]] <- normalized_echo
}
}
}
}
# random test
#-----------------------100 Pretrials Condition--------------------------------#
for (m in 1:length(model)){
for (r in 1:n_replications) {
print(r)
for(i in 1:length(p_encode)) {
#To acknowledge the instructions given to the participants before starting the experiment proper, we stocked memory with one example of each S(i)//R(i21)//R(i) contingency with L ¼ 1.
# Memory is empty on first trial
normalized_echo <- probe_memory(random_array[1,], NULL, cue_features, model = model[m])
expectancy <- expect_event(random_array[1,], normalized_echo)
memory <- learn(
normalized_echo
, random_array[1,]
, 1
, NULL
, model = model[m]
)
# stock with 4 further exemplar memories
for(x in 2:5){
normalized_echo <- probe_memory(random_array[x,], memory, cue_features, model = model[m])
expectancy <- expect_event(random_array[x,], normalized_echo)
memory <- learn(
normalized_echo
, random_array[x,]
, 1
, memory
, model = model[m]
)
}
# practice blocks
for(p in 1:nrow(practice_array)){
normalized_echo <- probe_memory(practice_array[p,], memory, cue_features, model = model[m])
expectancy <- expect_event(practice_array[p,], normalized_echo)
memory <- learn(
normalized_echo
, practice_array[p,]
, 0.7
, memory
, model = model[m]
)
}
# normalized_echo <- probe_memory(test_ac_event, NULL, cue_features, model = model[m])
# expectancy <- expect_event(recall_abc_event, normalized_echo)
# sim_results_100_pretrials[[i,1,r]] <- expectancy
# normalized_echos_100_pretrials[[i,1,r]] <- normalized_echo
# Experimental trials
for(j in 2:(n_trials+1)) {
iteration <- 1
expectancy <- 0
while(expectancy<=0.99){
res <- probe_memory_resonance(random_array[j,], memory, cue_features, model = model[m], iteration)
normalized_echo <- res$normalized_echo
expectancy <- expect_event(random_array[j,], normalized_echo)
iteration <- iteration+1
#print(expectancy)
}
memory <- learn(
normalized_echo
, random_array[j,]
, p_encode[i]
, memory
, model = model[m]
)
#normalized_echo <- probe_memory(random_array[j,], memory, cue_features, model = model[m])
expectancy <- expect_event(random_array[j,], normalized_echo)
sim_resonance_100_pretrials[[i,j,r]] <- iteration
sim_results_100_pretrials[[i,j,r]] <- expectancy
normalized_echos_100_pretrials[[i,j,r]] <- normalized_echo
}
}
}
}
expectancy
# random test
#-----------------------100 Pretrials Condition--------------------------------#
for (m in 1:length(model)){
for (r in 1:n_replications) {
print(r)
for(i in 1:length(p_encode)) {
#To acknowledge the instructions given to the participants before starting the experiment proper, we stocked memory with one example of each S(i)//R(i21)//R(i) contingency with L ¼ 1.
# Memory is empty on first trial
normalized_echo <- probe_memory(random_array[1,], NULL, cue_features, model = model[m])
expectancy <- expect_event(random_array[1,], normalized_echo)
memory <- learn(
normalized_echo
, random_array[1,]
, 1
, NULL
, model = model[m]
)
# stock with 4 further exemplar memories
for(x in 2:5){
normalized_echo <- probe_memory(random_array[x,], memory, cue_features, model = model[m])
expectancy <- expect_event(random_array[x,], normalized_echo)
memory <- learn(
normalized_echo
, random_array[x,]
, 1
, memory
, model = model[m]
)
}
# practice blocks
for(p in 1:nrow(practice_array)){
normalized_echo <- probe_memory(practice_array[p,], memory, cue_features, model = model[m])
expectancy <- expect_event(practice_array[p,], normalized_echo)
memory <- learn(
normalized_echo
, practice_array[p,]
, 0.7
, memory
, model = model[m]
)
}
# normalized_echo <- probe_memory(test_ac_event, NULL, cue_features, model = model[m])
# expectancy <- expect_event(recall_abc_event, normalized_echo)
# sim_results_100_pretrials[[i,1,r]] <- expectancy
# normalized_echos_100_pretrials[[i,1,r]] <- normalized_echo
# Experimental trials
for(j in 2:(n_trials+1)) {
iteration <- 1
expectancy <- 0
while(expectancy<=0.99){
res <- probe_memory_resonance(random_array[j,], memory, cue_features, model = model[m], iteration)
normalized_echo <- res$normalized_echo
expectancy <- expect_event(random_array[j,], normalized_echo)
iteration <- iteration+1
#print(expectancy)
}
memory <- learn(
normalized_echo
, random_array[j,]
, p_encode[i]
, memory
, model = model[m]
)
#normalized_echo <- probe_memory(random_array[j,], memory, cue_features, model = model[m])
expectancy <- expect_event(random_array[j,], normalized_echo)
sim_resonance_100_pretrials[[i,j,r]] <- iteration
sim_results_100_pretrials[[i,j,r]] <- expectancy
normalized_echos_100_pretrials[[i,j,r]] <- normalized_echo
}
}
}
}
expectancy
iteration
iteration <- 1
expectancy <- 0
iteration <- 1
m
r
i
# random test
#-----------------------100 Pretrials Condition--------------------------------#
for (m in 1:length(model)){
for (r in 1:n_replications) {
print(r)
for(i in 1:length(p_encode)) {
#To acknowledge the instructions given to the participants before starting the experiment proper, we stocked memory with one example of each S(i)//R(i21)//R(i) contingency with L ¼ 1.
# Memory is empty on first trial
normalized_echo <- probe_memory(random_array[1,], NULL, cue_features, model = model[m])
expectancy <- expect_event(random_array[1,], normalized_echo)
memory <- learn(
normalized_echo
, random_array[1,]
, 1
, NULL
, model = model[m]
)
# stock with 4 further exemplar memories
for(x in 2:5){
normalized_echo <- probe_memory(random_array[x,], memory, cue_features, model = model[m])
expectancy <- expect_event(random_array[x,], normalized_echo)
memory <- learn(
normalized_echo
, random_array[x,]
, 1
, memory
, model = model[m]
)
}
# practice blocks
for(p in 1:nrow(practice_array)){
normalized_echo <- probe_memory(practice_array[p,], memory, cue_features, model = model[m])
expectancy <- expect_event(practice_array[p,], normalized_echo)
memory <- learn(
normalized_echo
, practice_array[p,]
, 0.7
, memory
, model = model[m]
)
}
# normalized_echo <- probe_memory(test_ac_event, NULL, cue_features, model = model[m])
# expectancy <- expect_event(recall_abc_event, normalized_echo)
# sim_results_100_pretrials[[i,1,r]] <- expectancy
# normalized_echos_100_pretrials[[i,1,r]] <- normalized_echo
# Experimental trials
for(j in 2:(n_trials+1)) {
iteration <- 1
expectancy <- 0
while(expectancy<=0.99){
res <- probe_memory_resonance(random_array[j,], memory, cue_features, model = model[m], iteration)
normalized_echo <- res$normalized_echo
expectancy <- expect_event(random_array[j,], normalized_echo)
iteration <- iteration+1
#print(expectancy)
}
memory <- learn(
normalized_echo
, random_array[j,]
, p_encode[i]
, memory
, model = model[m]
)
#normalized_echo <- probe_memory(random_array[j,], memory, cue_features, model = model[m])
expectancy <- expect_event(random_array[j,], normalized_echo)
sim_resonance_100_pretrials[[i,j,r]] <- iteration
sim_results_100_pretrials[[i,j,r]] <- expectancy
normalized_echos_100_pretrials[[i,j,r]] <- normalized_echo
}
}
}
}
training_array <- rep(list(training_ab_event, training_bc_event, training_cd_event, training_de_event, training_ef_event), trials)
# setup constant variables over all simulations
n_features <- 160
cue_features <- 1:140
a <- b <- c <- d <- e <- f <- context <- outcome <- rep(0, n_features)
# setup stimuli
a[1:20] <- b [21:40] <- c[41:60] <- d[61:80] <- e[81:100] <- f[101:120] <- 1
context[121:140] <- 1
outcome[141:160] <- 0
# number of replications
n_replications <- 100
# learning rates
p_encode <- c(0.2, 0.4, 0.6, 0.8, 1)
# specify models
model = "Minerva AL"
model = "Minerva2"
# specify events
training_a_event <- a + context
training_b_event <- b + context
training_c_event <- c + context
training_d_event <- d + context
training_e_event <- e + context
training_f_event <- f + context
training_ab_event <- a+b + context
training_bc_event <- b+c + context
training_cd_event <- c+d + context
training_de_event <- d+e + context
training_ef_event <- e+f + context
test_bc_event <- b + c + context
recall_bc_event <- b + c + context
test_bd_event <- b + d + context
recall_bcd_event <- b + c + d + context
test_be_event <- b + e + context
recall_bcde_event <- b + c + d + e + context
# specify probe
probe_a <- a + context
probe_b <- b + context
probe_c <- c + context
probe_d <- d + context
probe_e <- e + context
probe_f <- f + context
trials <- 50
# setup trial number
n_trials <- 5*trials
#training array
training_array <- rep(list(training_ab_event, training_bc_event, training_cd_event, training_de_event, training_ef_event), trials)
plot(matrix(unlist(training_array), ncol=160,byrow = TRUE))
#random shuffle
training_array <- sample(training_array)
#plot(matrix(unlist(training_array), ncol=160))
# prepare results object as 3D array[p_encode, n_trials, n_replications]
sim_results_0_pretrials <- array(NA, c(length(p_encode), n_trials, n_replications))
sim_results_10_pretrials <- array(NA, c(length(p_encode), n_trials, n_replications))
sim_results_30_pretrials <- array(NA, c(length(p_encode), n_trials, n_replications))
sim_results_100_pretrials <- array(NA, c(length(p_encode), n_trials, n_replications))
sim_results_0_adjacent <- array(NA, c(length(p_encode), n_trials, n_replications))
sim_results_0_close <- array(NA, c(length(p_encode), n_trials, n_replications))
sim_results_0_distant <- array(NA, c(length(p_encode), n_trials, n_replications))
# specify object for normalized echo for every trial as 3D array[p_encode, n_trials, n_replications]
# with cells containing normalized echo vector
normalized_echos_0_pretrials <- array(list(rep(NA, n_features)), dim = c(length(p_encode), n_trials, n_replications))
normalized_echos_10_pretrials <- array(list(rep(NA, n_features)), dim = c(length(p_encode), n_trials, n_replications))
normalized_echos_30_pretrials <- array(list(rep(NA, n_features)), dim = c(length(p_encode), n_trials, n_replications))
normalized_echos_100_pretrials <- array(list(rep(NA, n_features)), dim = c(length(p_encode), n_trials, n_replications))
normalized_echos_0_adjacent <- array(list(rep(NA, n_features)), dim = c(length(p_encode), n_trials, n_replications))
normalized_echos_0_close <- array(list(rep(NA, n_features)), dim = c(length(p_encode), n_trials, n_replications))
normalized_echos_0_distant <- array(list(rep(NA, n_features)), dim = c(length(p_encode), n_trials, n_replications))
training_array <- rep(list(training_ab_event, training_bc_event, training_cd_event, training_de_event, training_ef_event), trials)
plot(matrix(unlist(training_array), ncol=160,byrow = TRUE))
plot(matrix(unlist(training_array), ncol=160, byrow = TRUE))
library("plot.matrix")
plot(matrix(unlist(training_array), ncol=160, byrow = TRUE))
library('plot.matrix')
par(mar=c(5.1, 4.1, 4.1, 4.1)) # adapt margins
plot(matrix(unlist(training_array), ncol=160, byrow = TRUE))
plot(matrix(unlist(training_array), ncol=160, byrow = TRUE))
par(mar=c(5.1, 4.1, 4.1, 4.1)) # adapt margins
plot(matrix(unlist(training_array), ncol=160, byrow = TRUE))
training_array <- sample(training_array)
training_array <- sample(training_array)
plot(matrix(unlist(training_array), ncol=160, byrow = TRUE))
p_encode
i=4
i=5
normalized_echo <- probe_memory(training_array[[1]], NULL, cue_features, model = model[m])
expectancy <- expect_event(outcome, normalized_echo)
memory <- learn(
normalized_echo
, training_array[[1]]
, p_encode[i]
, NULL
, model = model[m]
)
plot(memory)
outcome
# main test
#-----------------------Main Condition--------------------------------#
for (m in 1:length(model)){
for (r in 1:n_replications) {
training_array <- sample(training_array)
for(i in 1:length(p_encode)) {
# Memory is empty on first trial
normalized_echo <- probe_memory(training_array[[1]], NULL, cue_features, model = model[m])
expectancy <- expect_event(training_array[[1]], normalized_echo)
memory <- learn(
normalized_echo
, training_array[[1]]
, p_encode[i]
, NULL
, model = model[m]
)
normalized_echo <- probe_memory(test_bd_event, NULL, cue_features, model = model[m])
expectancy <- expect_event(recall_bcd_event, normalized_echo)
sim_results_0_close[[i,1,r]] <- expectancy
normalized_echos_0_close[[i,1,r]] <- normalized_echo
normalized_echo <- probe_memory(test_be_event, NULL, cue_features, model = model[m])
expectancy <- expect_event(recall_bcde_event, normalized_echo)
sim_results_0_distant[[i,1,r]] <- expectancy
normalized_echos_0_distant[[i,1,r]] <- normalized_echo
normalized_echo <- probe_memory(test_bc_event, NULL, cue_features, model = model[m])
expectancy <- expect_event(recall_bc_event, normalized_echo)
sim_results_0_adjacent[[i,1,r]] <- expectancy
normalized_echos_0_adjacent[[i,1,r]] <- normalized_echo
# A trials
for(j in 2:n_trials) {
normalized_echo <- probe_memory(training_array[[j]], memory, cue_features, model = model[m])
expectancy <- expect_event(outcome, normalized_echo)
memory <- learn(
normalized_echo
, training_array[[j]]
, p_encode[i]
, memory
, model = model[m]
)
normalized_echo <- probe_memory(test_bd_event, memory, cue_features, model = model[m])
expectancy <- expect_event(recall_bcd_event, normalized_echo)
sim_results_0_close[[i,j,r]] <- expectancy
normalized_echos_0_close[[i,j,r]] <- normalized_echo
normalized_echo <- probe_memory(test_be_event, memory, cue_features, model = model[m])
expectancy <- expect_event(recall_bcde_event, normalized_echo)
sim_results_0_distant[[i,j,r]] <- expectancy
normalized_echos_0_distant[[i,j,r]] <- normalized_echo
normalized_echo <- probe_memory(test_bc_event, memory, cue_features, model = model[m])
expectancy <- expect_event(recall_bc_event, normalized_echo)
sim_results_0_adjacent[[i,j,r]] <- expectancy
normalized_echos_0_adjacent[[i,j,r]] <- normalized_echo
}
}
}
}
#-----calculate mean and standard error of results objects-----
divide_by_n_rep <- function(x, n_rep = n_replications){x/n_rep}
# Sum of expectancies for each trial over all replications
sum_sim_results_0_adjacent <- apply(sim_results_0_adjacent, c(1,2), FUN = sum)
# Mean of expectancies for each trial over all replications
mean_sim_results_0_adjacent <- apply(sum_sim_results_0_adjacent, c(1,2), FUN = divide_by_n_rep)
# Sum of expectancies for each trial over all replications
sum_sim_results_0_close <- apply(sim_results_0_close, c(1,2), FUN = sum)
# Mean of expectancies for each trial over all replications
mean_sim_results_0_close <- apply(sum_sim_results_0_close, c(1,2), FUN = divide_by_n_rep)
# Sum of expectancies for each trial over all replications
sum_sim_results_0_distant <- apply(sim_results_0_distant, c(1,2), FUN = sum)
# Mean of expectancies for each trial over all replications
mean_sim_results_0_distant <- apply(sum_sim_results_0_distant, c(1,2), FUN = divide_by_n_rep)
# critical trial until x|cue >= 0.95 in each replication for every learning rate
# crit_trials_100_pretrials <- apply(sim_results_100_pretrials, c(1,3), FUN = find_crit_trial)
# se of critical trials until x|cue >= 0.95 in each replication for every learning rate
# std_error_100_pretrials <- apply(crit_trials_100_pretrials, 1, FUN = std_err)
mean_sim_results_0_adjacent
mean_sim_results_0_adjacent
adjacent_df <- as.data.frame(mean_sim_results_0_adjacent)
adjacent_df
p_encode
str(p_encode)
adjacent_df <- as.data.frame(mean_sim_results_0_adjacent, row.names = p_encode)
adjacent_df
library(tidyr)
install.packages("tidyr")
library(tidyr)
library("tidyr")
library("tidyverse")
install.packages("tidyverse")
library("tidyverse")
library(ggplot)
library(ggplot2)
library(gather)
library(tidyr)