Assigning participants randomly to a condition

@dvbridges thank you for your suggestion! I had really similar question to @Nessimon and your suggestion worked perfectly for me on my laptop! However, I couldn’t run the experiment on Pavlovia. I may be doing something wrong about Javascript.

Windows 10
v2020.1.3.
Chrome

I have 4 different condition file and all of them have different response keys combination, therefore, I have 4 different instructions. Since I will do the experiment on Pavlovia participants need to be assigned to one of the four conditions randomly.

The following is the flow of my experiment;

.
After participants are welcomed I want to present them one of the 4 different instructions. To do this I added the code you provided as following to the instruction routine and then added image component (named instructionss).

Image name is $'Order' + condition + '.png' and set to set every repeat.
Loop name is $'Order' + condition + '.xlsx'

Although my Exp. works perfectly on my desktop I couldn’t manage to run it online. I have following error (I interpreted as I had a problem with my Javascript) However, I couldn’t solve my problem.

hata

Is it possible to add a code component out of flow just like I did?

Also I have a feedback component. Is it possible that codes overlap? Following is my feedback code:


Thanks in advance!
Emre

@emregurbuz, the error suggests your code has not compiled correctly. You should have had an error message output into the Runner window when you tried to sync your experiment, that will point to where the error has occured.

@dvbridges thank you for the super fast reply!

Following is the error message output I received when I tried to sync.

realerror

I guess the problem is with the feedback code. Following is the HTML file that I opened on github:

Also the first lines;

firstlines

Many thanks,
Emre

@emregurbuz, its probably because you need to be in a loop to use continue - it moves on to the next loop iteration. In this specific code, you do not need the else continue statement.

@dvbridges
I want to give feedback after each trial to participants by showing a red X if they make a mistake. Since I do have my feedback code component in the loop, I thought I had to use “continue” (or equivalent in JS as well). How can I do this in Javascript?

Please excuse me if I am asking elementary questions, I am a newbie in coding.

I see, you could try something like this, putting the code in the relevant tabs:

// Begin Experiment
red = new util.Color("red");  // create new colors
green = new util.Color("green");

// Begin Routine
if (resp.corr === 0) {
  msg = 'X';
  text_5.color = red;
} else {
  msg = 'Correct';
  text_5.color = green;
}

Ive used text_5 as the text component, since in your example it looks like that is the feedback component.

@dvbridges yes I see! So the problem is that I cannot use continue in JS as I use in Python!

Thank you very much!

I am curious about one last thing. Is it possible to give feedback only on erroneous trials (i.e., “mistake” when person makes an error and “No response” when one does not give any response" but not providing any feedback (i.e., correct), and instead simply continue to routine?

Sure, try the following

if (resp.corr === 0) {
  msg = 'X';
  text_5.color = red;
} else {
  msg = '';
}

@dvbridges That worked perfectly! Thank you so much!

I have just realized something important. The code that I wrote from your example had just condition A and B. However, I have 4 different conditions. Is the following correct code for 4 different conditions? I couldn’t be sure about JavaScript translation (about the brackets and numbers).

 *# Begin Experiment*
import random
condition = random.choice(('A','B','C','D'))
// Begin Experiment
function random_character() {
    var chars = "ABCD";
    return chars.substr((( Math.floor(Math.random() * 4), 3), 2), 1);
}

condition = random_character();  // the condition variable will be used in your loop, as with the Python code

@emregurbuz, try:

function random_character() {
    var chars = "ABCD";
    return chars.substr( Math.floor(Math.random() * 4), 1);
}

@dvbridges thank you so much for the help!

I am replying late because I couldn’t pilot the final version of the experiment online due to the Pilot token missing error. I will try to fix that problem and use the Javascript code that you provided!

All best,

Emre

Hi @dvbridges where should I put this code component? in a new routine at the beginning of the whole experiment? or in the first routine of my block?

You only want it to run once so it should be before your loop. It doesn’t need to be in a new routine if you already have one available (e.g. instructions).

I have 3 instructions screens (one after the other one) followed by one loop (three routines in this loop). The conditions file that I want to have pulled at random is in the loop. All instructions would be the same for participants.

Should I then put it inside the first routine inside my loop? or in the last set of instructions?

Since it’s in a Begin Experiment tab of a code component it shouldn’t matter, but I quite often put everything like this into the very first routine so it’s easy to find.

1 Like

Is there a possibility to randomize people to a condition based on their response on a slider or something else? so based on a response, that they gave before the specific instructions for each condition?

Many thanks for your solution! The code works well for my study as well. But I have a further issue which I guess a lot of people might also have: how to ensure that an equal number of participants are randomly assigned to each of the two versions? Is this code alone able to do that?

To my knowledge there isn’t a way to make sure # ptps is equal across conditions when using code to randomly select a condition.

One option that I’ve yet to try, but others seem to have had success, is using @wakecarter’s tool here to assign consecutive ptp ID #s, and then within the url that you send to pavlovia, use “%x” where x is the number of conditions. I believe it works by taking the number of the current ptp, e.g. “11”, and dividing by the # of conditions and returning the remainder as the condition assignment, so that if you had 3 conditions, ptp ID #11 would be assigned to cond 2.

You could also just use random as you’re currently doing and tally on your end, but of course a bit more burdensome.

1 Like

Thank you very much for the suggestion! I’ll try the code and see.

@dvbridges I have a question. I have 4 conditions: ABCD. I want the participants assigned randomly to one of the four conditions. According to conditions, most of the parts of the experiment will be changed such as instructions, emotional text, or some rating sentences.

I used this code and I create 4 different condition files: OrderA.xlsx, OrderB.xlsx, OrderC.xlsx, or OrderD.xlsx. I indicated the part of the changed with condition in both condition file (OrderA.xlsx etc.) and and routine (like this $instructions).

Actually, the code perfectly worked first. Then, PsychoPy has begun showing both one experimental condition (for example OrderA) and None (twice or three times). I haven’t any “None” condition, but it shows an empty condition/screen with constant properties in the experiment for twice or three times.

I am not sure where I have a mistake.
Thanks in advance.

Windows 10
PsychoPy 2022.1.3
Online Experiment