| Reference | Downloads | Github

Counterbalancing two variables from excel file in an online experiment

Hey, I have a question about counterbalance.
I have two variables in my experiment (1- images, 2- texts) and I want to make sure each of them is randomized.
Defining randomization for the entire excel file, unfortunately doesn’t do the trick:

var trials;
var currentLoop;
function trialsLoopBegin(thisScheduler) {
  // set up handler to look after randomisation of conditions etc
  trials = new TrialHandler({
    psychoJS: psychoJS,
    nReps: 1, method: TrialHandler.Method.RANDOM,
    extraInfo: expInfo, originPath: undefined,
    trialList: 'GIRLSstimuli-nocross.xlsx',
    seed: undefined, name: 'trials'});
  psychoJS.experiment.addLoop(trials); // add the loop to the experiment
  currentLoop = trials;  // we're now the current loop`

It only randomizes the appearance of the rows (a row includes an image + a text in the excel file), but not the appearance of the columns (the image column and the text column) in each row.

When I tried to use a shuffle function, like:

function shuffle(a) {
    for (let i = a.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [a[i], a[j]] = [a[j], a[i]];
    return a;

It doesn’t work.
My guess is that I need to define my variables as an array? But how can I do so if I am using the excel file for the variables?

Any suggestions?

I imagine there is a way to solve this if you really need to, but I have to ask. Is it really going to be easier or necessary to try to create all this custom Python code instead of just rebuilding your conditions file to be counterbalanced? Typically the condition file is built where each row is a “trial” or iteration through a loop, so if you are trying to counter balance across trials then doing it in the conditions file is what I normally do.

1 Like

If I understood you correctly, then you are talking about arranging the rows beforehand (as if there are supposed to be two specific variables in a row). However, the rows in my experiment have no meaning. I just have two lists (one list of images and one list of texts) written one next to the other in an excel file. So I wish to write a JS code that will automatically mix up the orders of the lists (=variables), so each participant will randomly see an image and a text, that aren’t necessarily written in the same row in the excel file.
Is there anyway to do it?

Right, I understand that, I was just suggesting that unless you have good reason for wanting every subject to see a randomized combination of the stimuli, it would be easier to just do the randomization and counterbalancing before hand.
I can’t give a specific code solution since I’m unclear on the details of what you are trying to do (is this written in the coder or builder, and do you have the python version of this already working or no?)
But I feel it would be easier to split your conditions file into 2 files, 1 for each variable. Then just create two random vectors of numbers for however many trials you will have, and use the first vector to select from variable one on each trial, and the other vector to select from variable 2 on each trial.
If you are using builder for this experiment there are probably easier ways using the loops in builder as well.