You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to set up a probability weighting task to find the certainty equivalent range for a number of values. The procedure entails asking the participant whether they prefer to take the bet or the secure amount across a range of values, with the specific values changing adaptively based on the participant's responses.
The issues is that I am struggling to access the data generated in the previous timeline.
Below is my code, I basically would like to retrieve the same two values that I am accessing inside the loop_node section after that trial has ended in order to set up for the next rounds (the loops is there to prevent participants from making illogical selections).
I don't understand why I cannot index them and I keep getting undefined error (despite being able to retrieve them when inside the loop_node section). Is it because they are part of a loop_function?
I attach a screenshot of the console output when displaying the data.
<!DOCTYPE html>
<html>
<head>
<title>My experiment</title>
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/@jspsych/[email protected]"></script>
<script src="https://unpkg.com/@jspsych/[email protected]"></script>
<script src="https://unpkg.com/@jspsych/[email protected]"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/jspsych.js"></script>
<link href="https://unpkg.com/[email protected]/css/jspsych.css" rel="stylesheet" type="text/css" />
</head>
<body></body>
<script>
/* initialize jsPsych */
var jsPsych = initJsPsych({
on_finish: function() {
jsPsych.data.displayData();
}
});
var timeline = [];
var preload = {
type: jsPsychPreload,
images: ['figs/p01.png', 'figs/p03.png', 'figs/p05.png', 'figs/p07.png', 'figs/p09.png', 'figs/p1.png']
};
timeline.push(preload);
let probabilities = [0.1, 0.3, 0.5, 0.7, 0.9];
let EV = [10, 50, 100, 500];
let V = [];
for (let i = 0; i < probabilities.length; i++) {
V.push(EV.map(ev => ev / probabilities[i]));
}
var df = [];
for (let i = 0; i < probabilities.length; i++) {
for (let j = 0; j < EV.length; j++) {
df.push({
probability: probabilities[i],
ev: EV[j],
v: V[i][j],
ce_range: [],
ce_value: 0
});
}
}
// Shuffle df (like df.sample(frac=1).reset_index(drop=True) in pandas)
df = jsPsych.randomization.shuffle(df);
var prob_figs = {
0.1: "01",
0.3: "03",
0.5: "05",
0.7: "07",
0.9: "09"
};
var welcome = {
type: jsPsychHtmlKeyboardResponse,
stimulus: "Welcome to the experiment. Press any key to begin."
};
timeline.push(welcome);
var instructions = {
type: jsPsychHtmlKeyboardResponse,
stimulus: `
<p>In this experiment, you will decide whether you want
to take a bet or a secure amount.</p>
<p>One the left you will see a pie chart representing the
probability of winning that sum. If you decide to <strong>bet</strong>,
press the letter F on the keyboard as fast as you can.</p>
<p>On the right a secure amount will be shown.
If you take the <strong>secure</strong> amount, press the letter J
as fast as you can.</p>
<div align='left'; style='position:absolute; top: 60%; left: 10%;'><img src='figs/p01.png' height="500px"></img></div>
<div align='left'; style='position:absolute; top: 130%; left: 25%;'><p class="ex1"><strong>Press the F key</strong></p></div>
<div align='left'; style='position:absolute; top: 60%; left: 53%;'><img src='figs/p1.png' height="500px"></img></div>
<div align='left'; style='position:absolute; top: 130%; left: 68%;'><p class='small'><strong>Press the J key</strong></p></div>
</div>
<p>Press any key to begin.</p>
`,
post_trial_gap: 2000
};
timeline.push(instructions);
function win_disp(t_bet, t_secure, t_p, callback) {
t_bet = t_bet.toFixed(2);
t_secure = t_secure.toFixed(2);
let p_fig = prob_figs[t_p]
var stim = [`
<div align='left'; style='position:absolute; top: 15%; left: 10%;'><img src='figs/p${p_fig}.png' height="500px"></img></div>
<div align='left'; style='position:absolute; top: 50%; left: 25%;'><p style="font-size: 30px"><strong>${t_bet}</strong></p></div>
<div align='left'; style='position:absolute; top: 15%; left: 53%;'><img src='figs/p1.png' height="500px"></img></div>
<div align='left'; style='position:absolute; top: 50%; left: 68%;'><p style="font-size: 30px"><strong>${t_secure}</strong></p></div>
`];
return stim;
}
let i = 0
let row = df[i];
let t_1 = row.v / 3;
let t_2 = 2 * row.v / 3;
let t_v = row.v
let t_p = row.probability
var first_round = {
type: jsPsychHtmlKeyboardResponse,
choices: ['f', 'j'],
timeline: [
{stimulus: win_disp(t_v, t_1, t_p),
data: {
task: 'resp_r1_t1',
}
},
{stimulus: win_disp(t_v, t_2, t_p),
data: {
task: 'resp_r1_t2'
}
}
]
};
var loop_node = {
timeline: [first_round],
loop_function: function(){
// get the data from the previous trial,
// and check which key was pressed
data_t1 = jsPsych.data.get().last(2).filter({task: 'resp_r1_t1'}).values()[0];
data_t2 = jsPsych.data.get().last(2).filter({task: 'resp_r1_t2'}).values()[0];
if((jsPsych.pluginAPI.compareKeys(data_t1.response, 'j')) & (jsPsych.pluginAPI.compareKeys(data_t2.response, 'f'))){
return true;
} else {
return false;
}
}
}
timeline.push(loop_node);
var lasttrialdata = jsPsych.data.getLastTrialData();
console.log(lasttrialdata);
var dar = {
type: jsPsychHtmlKeyboardResponse,
stimulus: `<p>${lasttrialdata}</p>`
};
timeline.push(dar);
jsPsych.run(timeline);
</script>
</html>
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello,
I am trying to set up a probability weighting task to find the certainty equivalent range for a number of values. The procedure entails asking the participant whether they prefer to take the bet or the secure amount across a range of values, with the specific values changing adaptively based on the participant's responses.
The issues is that I am struggling to access the data generated in the previous timeline.
Below is my code, I basically would like to retrieve the same two values that I am accessing inside the
loop_node
section after that trial has ended in order to set up for the next rounds (the loops is there to prevent participants from making illogical selections).I don't understand why I cannot index them and I keep getting undefined error (despite being able to retrieve them when inside the
loop_node
section). Is it because they are part of aloop_function
?I attach a screenshot of the console output when displaying the data.
Thanks,
Federica
Beta Was this translation helpful? Give feedback.
All reactions