-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiment.js
653 lines (517 loc) · 17.8 KB
/
experiment.js
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
//---------------------------------------//
// Define symbol assignment.
//---------------------------------------//
const jsPsych = initJsPsych({
on_trial_finish: function (data) {
console.log(data)
}
});
// Define unique symbols.
var symbol_array = ['c','d','e','f','j','k','m','o','s','t','y','C','N','O','L','T']
// Shuffle symbols.
symbol_array = jsPsych.randomization.repeat(symbol_array, 1);
// var context_array = ['forrest_1', 'forrest_2', 'forrest_3', 'desert_1', 'desert_2', 'desert_3']
var practice_array = ['forrest_1', 'desert_1']
var learn_1_array = ['forrest_2', 'desert_4']
var learn_2_array = ['forrest_4', 'desert_3']
// var context_array_forrest = ['forrest_1', 'forrest_2', 'forrest_3']
// var context_array_desert = ['desert_1', 'desert_2', 'desert_3']
context_array = jsPsych.randomization.repeat(practice_array, 1);
context_array = context_array.concat(jsPsych.randomization.repeat(learn_1_array, 1));
context_array = context_array.concat(jsPsych.randomization.repeat(learn_2_array, 1));
// Define comprehension threshold.
var max_errors = 0;
var max_loops = 3;
var num_loops = 0;
// Define missed repsonses count.
var missed_threshold = 10;
var missed_responses = 0;
//---------------------------------------//
// Define learning phase instructions.
//---------------------------------------//
var instructions_01 = {
type: jsPsychInstructions,
pages: [
"We are now starting the experiment.<br><br>Use the buttons below (or the left/right arrow keys) to navigate the instructions.",
"In this task, you are picking a team of knights.<br>The knights will look like the ones below.",
"Each knight will have a <b>unique symbol</b> on its chestplate.<br>This symbol will help you identify each knight.",
"You'll also pick your team of knights from different places. <br>Some of places will have knights that are good and will give you more points and some places will have knights are bad and will take away points.",
"On every turn, you will choose a knight for your team.<br>When you select a knight, it may give you:<br><b><font color=#01579b>+10 points, </font><font color=#303030>+0 points</font></b>, or <b><font color=#A41919>-10 points</font></b>.",
"To help you learn, we will also show you the points you<br><i>could have earned</i> if you had chosen the other knight.<br><b>NOTE:</b> You will earn points only for the knight you choose.",
"Some knights are better than others. Some will give you more points than others and some will lose you less points that others.",
"Sometimes you'll know the points from the unchosen knights, but sometimes you won't know.",
"Now let's practice with the knights below. Using the left/right<br>arrow keys, select the knights for testing and try to learn<br>which will give you more points.",
"<b>HINT:</b> Pay attention to the symbols and the results of each test."
],
symbol_L: "V",
symbol_R: "U",
}
var practice_block_01 = {
type: jsPsychPractice,
symbol_L: "V",
symbol_R: "U",
outcome_L: "zero",
outcome_R: "win",
context:context_array[0],
choices: ['arrowleft','arrowright'],
correct: 'arrowright',
feedback_duration: 2000
}
const instructions_02 = {
type: jsPsychMyInstructions,
pages: [
"Great job! Now let's try for one more set of knights."
],
symbol_L: "W",
symbol_R: "R",
}
var practice_block_02 = {
type: jsPsychPractice,
symbol_L: "W",
symbol_R: "R",
outcome_L: "lose",
outcome_R: "zero",
context:context_array[1],
choices: ['arrowleft','arrowright'],
correct: 'arrowright',
feedback_duration: 2000
}
const instructions_03 = {
type: jsPsychMyInstructions,
pages: [
"During the task, there will be many knights to test.<br>Remember to pay close attention to their symbols.",
"Your job is to try to select the best knight on each trial.<br>Even though you will learn the test results for both knights,<br>you will only earn points for the knight you test.",
"<b>HINT:</b> The knights may not always give you points, but some knights will give you points and others will lose you points more often than others.",
"You should try to earn as many points as you can, even if it's not possible to win points or avoid losing points on every round.",
"At the end of the task, the total number of points you've earned will be converted into a performance bonus.",
"Next, we will ask you some questions about the task.<br>You must answer all the questions correctly to continue."],
symbol_L: " ",
symbol_R: " "
}
var comprehension = {
type: jsPsychComprehension
}
var instructions = {
timeline: [
instructions_01,
practice_block_01,
instructions_02,
practice_block_02,
instructions_03,
// comprehension
],
// }
loop_function: function(data) {
// Extract number of errors.
const num_errors = data.values().slice(-1)[0].num_errors;
// Check if instructions should repeat.
if (num_errors > max_errors) {
num_loops++;
if (num_loops >= max_loops) {
low_quality = true;
return false;
} else {
return true;
}
} else {
return false;
}
}
}
var comprehension_check = {
type: jsPsychCallFunction,
func: function(){},
on_finish: function(trial) {
if (low_quality) { jsPsych.endExperiment(); }
}
}
var ready = {
type: jsPsychInstructions,
pages: [
"Great job! You've passed the comprehension check.",
"Get ready to begin the experiment.<br>Press next when you're ready to start.",
],
}
// //---------------------------------------//
// // Define probe phase instructions.
// //---------------------------------------//
var instructions_05 = {
type: jsPsychInstructions,
pages: [
"That's the end of the learning phase. Great job!",
"In this next part, you will select which knights you would like to join your team.",
"As you make your choices, you will not receive any feedback after your choice.",
"You should still choose the knight you think is better on each trial.<br>Your choices will still contribute to your performance bonus.",
"Get ready to make your selections.<br><br>Choose wisely!"
],
}
var instructions_06 = {
type: jsPsychInstructions,
pages: [
"That's the end of the selection phase. Great job!",
"Take a break for a few moments and<br>click next when you are ready to continue.",
"Great! You are now going to <b>test</b> a new set of knights.<br>The task is the same as before.",
"Remember to pay close attention to the symbol on each knight<br>and try to earn as many points as you can.",
],
}
//---------------------------------------//
// Define learning phase 1.
//---------------------------------------//
// Learning blocks are comprised of
// 24 presentations of 4 unique stimulus
// pairs (96 total trials). With left/right
// side counterbalancing, this is 12
// presentations per unique pair / side.
// Initialize phase array.
var learning_phase_1 = [];
// Iteratively define trials
// for (i = 0; i < 12; i++) {
for (i = 0; i < 1; i++) {
// Initialize (temporary) trial array.
const trials = [];
// Iterate over unique pairs.
for (j = 0; j < 4; j++) {
// Define metadata.
if (j % 2 == 0) {
var val = 'win';
var color = context_array[2];
}
else {
var val = 'lose';
var color = context_array[3];
}
// if (val = 'win') { var color = 'reward'; }
// else { var color = 'punish'; }
// If you want to take away counterfactuals half time
// if (j == 1) { var cf = false;}
// else if (j == 3) { var cf = false;}
// else { var cf = true; }
var cf = true;
// Append trial (LR).
var LR = {
type: jsPsychLearning,
symbol_L: symbol_array[2*j+0],
symbol_R: symbol_array[2*j+1],
outcome_L: jsPsych.randomization.sampleWithoutReplacement([val,val,val,'zero'],1)[0],
outcome_R: jsPsych.randomization.sampleWithoutReplacement(['zero','zero','zero',val],1)[0],
counterfactual: cf,
context:color,
choices: ['arrowleft','arrowright'],
correct: ((val == 'win') ? 'arrowleft' : 'arrowright'),
data: {block: 1},
on_finish: function(data) {
// Evaluate missing data
if ( data.rt == null ) {
// Set missing data to true.
data.missing = true;
// Increment counter. Check if experiment should end.
missed_responses++;
if (missed_responses >= missed_threshold) {
low_quality = true;
jsPsych.endExperiment();
}
} else {
// Set missing data to false.
data.missing = false;
}
}
}
// Define looping node.
const LR_node = {
timeline: [LR],
loop_function: function(data) {
return data.values()[0].missing;
}
}
trials.push(LR_node);
// Append trial (RL).
var RL = {
type: jsPsychLearning,
symbol_L: symbol_array[2*j+1],
symbol_R: symbol_array[2*j+0],
outcome_L: jsPsych.randomization.sampleWithoutReplacement(['zero','zero','zero',val],1)[0],
outcome_R: jsPsych.randomization.sampleWithoutReplacement([val,val,val,'zero'],1)[0],
counterfactual: cf,
context:color,
choices: ['arrowleft','arrowright'],
correct: ((val == 'win') ? 'arrowright' : 'arrowleft'),
data: {block: 1},
on_finish: function(data) {
// Evaluate missing data
if ( data.rt == null ) {
// Set missing data to true.
data.missing = true;
// Increment counter. Check if experiment should end.
missed_responses++;
if (missed_responses >= missed_threshold) {
low_quality = true;
jsPsych.endExperiment();
}
} else {
// Set missing data to false.
data.missing = false;
}
}
}
// Define looping node.
const RL_node = {
timeline: [RL],
loop_function: function(data) {
return data.values()[0].missing;
}
}
trials.push(RL_node);
}
// Shuffle trials. Append.
learning_phase_1 = learning_phase_1.concat( jsPsych.randomization.repeat(trials, 1) );
}
//------------------------------------//
// Define probe phase 1.
//------------------------------------//
// Probe phases are comprised of
// every possible pair combination
// (28 in total) presented 4 times
// (112 total trials).
// Initialize phase array.
probe_phase_1 = [];
// Iteratively define trials
// for (i = 0; i < 8; i++) {
for (i = 0; i < 2; i++) {
// for (j = 0; j < 8; j++) {
for (j = 0; j < 2; j++) {
if (i != j) {
// Append trial.
var probe = {
type: jsPsychProbe,
symbol_L: symbol_array[i],
symbol_R: symbol_array[j],
choices: ['arrowleft','arrowright'],
data: {block: 1},
on_finish: function(data) {
// Evaluate missing data
if ( data.rt == null ) {
// Set missing data to true.
data.missing = true;
// Increment counter. Check if experiment should end.
missed_responses++;
if (missed_responses >= missed_threshold) {
low_quality = true;
jsPsych.endExperiment();
}
} else {
// Set missing data to false.
data.missing = false;
}
}
}
// Define looping node.
const probe_node = {
timeline: [probe],
loop_function: function(data) {
return data.values()[0].missing;
}
}
// Add trials twice.
probe_phase_1.push(probe_node);
probe_phase_1.push(probe_node);
}
}
};
// Shuffle trials.
probe_phase_1 = jsPsych.randomization.repeat(probe_phase_1, 1);
// //---------------------------------------//
// // Define learning phase 2.
// //---------------------------------------//
// // Learning blocks are comprised of
// // 24 presentations of 4 unique stimulus
// // pairs (96 total trials). With left/right
// // side counterbalancing, this is 12
// // presentations per unique pair / side.
// Initialize phase array.
var learning_phase_2 = [];
// Iteratively define trials
// for (i = 0; i < 12; i++) {
for (i = 0; i < 2; i++) {
// Initialize (temporary) trial array.
const trials = [];
// Iterate over unique pairs.
for (j = 0; j < 4; j++) {
// for (j = 4; j < 8; j++) {
// Define metadata.
if (j % 2 == 0) {
var val = 'win';
var color = context_array[4];
}
else {
var val = 'lose';
var color = context_array[5];
}
// // Define metadata.
// if (j % 2 == 0) { var val = 'win'; }
// else { var val = 'lose'; }
// if (val = 'lose') { var color = 'reward'; }
// else { var color = 'punish'; }
// If you want to take away counterfactuals half time
// if (j == 1) { var cf = false;}
// else if (j == 3) { var cf = false;}
// else { var cf = true; }
var cf = true;
// Append trial (LR).
var LR = {
type: jsPsychLearning,
symbol_L: symbol_array[2*j+0],
symbol_R: symbol_array[2*j+1],
outcome_L: jsPsych.randomization.sampleWithoutReplacement([val,val,val,'zero'],1)[0],
outcome_R: jsPsych.randomization.sampleWithoutReplacement(['zero','zero','zero',val],1)[0],
counterfactual: cf,
context:color,
choices: ['arrowleft','arrowright'],
correct: ((val == 'win') ? 'arrowleft' : 'arrowright'),
data: {block: 2},
on_finish: function(data) {
// Evaluate missing data
if ( data.rt == null ) {
// Set missing data to true.
data.missing = true;
// Increment counter. Check if experiment should end.
missed_responses++;
if (missed_responses >= missed_threshold) {
low_quality = true;
jsPsych.endExperiment();
}
} else {
// Set missing data to false.
data.missing = false;
}
}
}
// Define looping node.
const LR_node = {
timeline: [LR],
loop_function: function(data) {
return data.values()[0].missing;
}
}
trials.push(LR_node);
// Append trial (RL).
var RL = {
type: jsPsychLearning,
symbol_L: symbol_array[2*j+1],
symbol_R: symbol_array[2*j+0],
outcome_L: jsPsych.randomization.sampleWithoutReplacement(['zero','zero','zero',val],1)[0],
outcome_R: jsPsych.randomization.sampleWithoutReplacement([val,val,val,'zero'],1)[0],
counterfactual: cf,
context:color,
choices: ['arrowleft','arrowright'],
correct: ((val == 'win') ? 'arrowright' : 'arrowleft'),
data: {block: 2},
on_finish: function(data) {
// Evaluate missing data
if ( data.rt == null ) {
// Set missing data to true.
data.missing = true;
// Increment counter. Check if experiment should end.
missed_responses++;
if (missed_responses >= missed_threshold) {
low_quality = true;
jsPsych.endExperiment();
}
} else {
// Set missing data to false.
data.missing = false;
}
}
}
// Define looping node.
const RL_node = {
timeline: [RL],
loop_function: function(data) {
return data.values()[0].missing;
}
}
trials.push(RL_node);
}
// Shuffle trials. Append.
learning_phase_2 = learning_phase_2.concat( jsPsych.randomization.repeat(trials, 1) );
}
// //------------------------------------//
// // Define probe phase 2.
// //------------------------------------//
// // Probe phases are comprised of
// // every possible pair combination
// // (28 in total) presented 4 times
// // (112 total trials).
// Initialize phase array.
probe_phase_2 = [];
// Iteratively define trials
// for (i = 0; i < 8; i++) {
for (i = 0; i < 2; i++) {
// for (j = 0; j < 8; j++) {
for (j = 0; j < 2; j++) {
if (i != j) {
// Append trial.
var probe = {
type: jsPsychProbe,
symbol_L: symbol_array[i],
symbol_R: symbol_array[j],
choices: ['arrowleft','arrowright'],
data: {block: 2},
on_finish: function(data) {
// Evaluate missing data
if ( data.rt == null ) {
// Set missing data to true.
data.missing = true;
// Increment counter. Check if experiment should end.
missed_responses++;
if (missed_responses >= missed_threshold) {
low_quality = true;
jsPsych.endExperiment();
}
} else {
// Set missing data to false.
data.missing = false;
}
}
}
// Define looping node.
const probe_node = {
timeline: [probe],
loop_function: function(data) {
return data.values()[0].missing;
}
}
// Add trials twice.
probe_phase_2.push(probe_node);
probe_phase_2.push(probe_node);
}
}
};
// Shuffle trials.
probe_phase_2 = jsPsych.randomization.repeat(probe_phase_2, 1);
// Complete screen
var complete = {
type: jsPsychInstructions,
pages: [
"Great job! You have completed the experiment."
],
show_clickable_nav: true,
button_label_previous: 'Prev',
button_label_next: 'Next',
}
const fullscreen = {
type: jsPsychFullscreen
}
var timeline = [];
timeline = timeline.concat(fullscreen);
timeline = timeline.concat(instructions);
// timeline = timeline.concat(comprehension_check); // Comprehension check (implicit)
timeline = timeline.concat(ready);
timeline = timeline.concat(learning_phase_1);
timeline = timeline.concat(instructions_05);
timeline = timeline.concat(probe_phase_1);
// timeline = timeline.concat(instructions_06);
// timeline = timeline.concat(learning_phase_2);
// timeline = timeline.concat(instructions_05);
// timeline = timeline.concat(probe_phase_2);
timeline = timeline.concat(complete);
jsPsych.run(timeline);