oTree Forum >

Randomize app order

#1 by Askhat

Dear Chris and forum members,

I have some experience in building oTree experiments, however I have never used its advanced features. I need to randomize the order of my 3 applications in my experiment. Previously I was duplicating the apps and simply used app_after_this_page() function, however I don't like that code is duplicated and that data file then have empty columns for some participants. Recently found one of Chris's answers that alternative way is to create several session configs. Doesn't several session configs result in several experiment links? Or am I missing something? I would prefer to have single link for all participants that will randomly send them to one of the sessions.

I understand that I can deploy on Heroku separate experiment link which will be given to participants and where common apps will reside in (e.g. cognitive reflection test, demographic survey, etc.). After starting this link participants will be randomly redirected to one of the links with certain session configuration in second deployed experiment. After completing main apps in the second experiment they will be redirected back to the first one and complete remaining survey apps. I've seen similar implementation in this old thread: https://groups.google.com/g/otree/c/0tSeU1mOy-k . However what bothers me is that I can't know in advance the link for either of deployed experiments. Can this idea be implemented via REST API or is there a smarter way to do it?

Thanks in advance!

#2 by swordchen

Hi Askhat,

I randomize my app order in settings.py, using python's random library. I don't know whether it meets your need. 

My experiment contains 18 games and 3 games are one group, I need to shuffle the game sequence in every group. I accomplish it by the code below. I think similar way can be used to meet your randomize needs. But this method may need every game to be a single app.

With the code in the head of settings.py:

import random
app_sequence = [
    'general_instruction',
    'ug1', 'tg1', 'pd1',
    'ug2', 'tg2', 'pd2',
    'ug3', 'tg3', 'pd3',
    'ug4', 'tg4', 'pd4',
    'ug5', 'tg5', 'pd5',
    'ug6', 'tg6', 'pd6',
    'after_survey'
]

start = ['general_instruction']
end = ['after_survey']
middle_blocks = [app_sequence[i:i+3] for i in range(1, len(app_sequence)-1, 3)]
for block in middle_blocks:
    random.shuffle(block)
shuffled_sequence = start + [item for block in middle_blocks for item in block] + end

then SESSION_CONFIGS = [dict(name='games', num_demo_participants=4, app_sequence=shuffled_sequence)]

Hope it can be useful!
Chen

#3 by Askhat

Thank you for your reply, Chen. I believe your solution randomizes order of apps for the whole session, so it still will require creating several sessions in order to control for effect of app order. What I had in mind was to randomize app order for each participant in the single session. From what I understand recommended approach is to duplicate apps (counting all permutations) and randomly send each participant to the required app through app_after_this_page() function.

I was wondering if there is another way to randomize the sequence?

#4 by swordchen

Maybe this will be helpful:https://github.com/Socrats/randomize-apps-otree 

I never use it before and not the developer as well , but it looks like it can meet your need for randomize app sequence for different participants because in the code doc it says "Randomize the sequence of apps per participant". If you have spare time, maybe it is valuable to take a look at it. 

Apologize for the misunderstanding about the first look, I neglect the key needs, sorry! Hope the new solution can be helpful!

#5 by Askhat

Wow! It indeed looks promising. Appreciate your help, Chen.

Write a reply

Set forum username