oTree Forum >

Randomly assigning participants to different apps

#1 by vedube (edited )

Hi! 
I would like to assign randomly the participants I have in a session into the 2 apps I have in my code. The apps I have are "control" and "treatment" and the difference between them are that the first one has a time limit of 15 minutes and the second one a time limit of 5 minutes. After the main questions that have time limit, there are some final questions that are the same for everyone. I've been reading the oTree documentation about Treatment but I don't see the explanation on how to assign the participants into different apps, so I would like to know if it's possible or if it's not, then how could I implement different time limits depending of the assignment of the participant into control or treatment in one app? I didn't see either how to assign different time limits depending the treatment assigned to the participant.

Thank you very much!

#2 by Daniel_Frey

Hi there!

Is the only difference of the treatments the different countdown?

If yes, you can show the participants the same page with different timeouts using get_timeout_seconds: 

class YourPage(Page):
    @staticmethod
    def get_timeout_seconds(player):
        # I assume you have saved the assigned treatment in a player-field
        if player.treatment == 'control':
            timeout_seconds = 5 * 60
        if player.treatment == 'treatment':
            timeout_seconds = 15 * 60
            
        return timeout_seconds
        
If you want to send the players to different apps, you can use app_after_this_page: https://otree.readthedocs.io/en/latest/pages.html?highlight=app_after_this_page#app-after-this-page

#3 by vedube

Thank you so much for your help!
As I am modifying an existing code, I am thinking of implementing the second method. In that case, should I hardcode the exact page each player will go?
Again, I appreciate the help

#4 by vedube

I created this new app in order to assign each participant randomly to either the treat app or the control app but I don't know what error I'am making:

import random

from otree.api import *


class C(BaseConstants):
    NAME_IN_URL = 'gbat_treatments'
    PLAYERS_PER_GROUP = None  # You can set this to a specific number if needed
    NUM_ROUNDS = 1


class Subsession(BaseSubsession):
    pass


class Group(BaseGroup):
    pass

class Player(BasePlayer):
    treatment = models.BooleanField()

class MyWaitPage(WaitPage):
    def creating_session(subsession):
        if subsession.round_number == 1:
            for player in subsession.get_players():
                participant = player.participant
                participant.treatment = random.choice([True, False])

class MyPage(Page):
    def app_after_this_page(self, player):
        participant = player.participant
        if participant.treatment:
            return "treat"
        else:
            return "control"

page_sequence = [MyWaitPage]

#5 by Daniel_Frey (edited )

creating_session should be on the top level of your code (usually placed after the class Subsession), not on the WaitPage:

...
class Subsession(BaseSubsession):
    pass


def creating_session(subsession):
    if subsession.round_number == 1:
        # save the players in a local list, so the function get_players() is only executed once
        all_players = subsession.get_players()
        for player in all_players:
            participant = player.participant
            participant.treatment = random.choice([True, False])
            
            
class Group(BaseGroup):
    pass


class Player(BasePlayer):
    treatment = models.BooleanField()
    

class MyWaitPage(WaitPage):
    pass
    
    
class MyPage(Page):
    @staticmethod
    def app_after_this_page(player, upcoming_apps):
        participant = player.participant
        if participant.treatment:
            return "treat"
        else:
            return "control"
            

# you need to add MyPage to the page_sequence, otherwise you cannot sort the players into the different apps
page_sequence = [MyWaitPage, MyPage]

#6 by Daniel_Frey

Furthermore, player.treatment and player.participant.treatment are two different variables: you initiate player.treatment in the Player class, but use player.participant.treatment for the treatment-assignment.

Hope that helps!

Best,
Daniel

Write a reply

Set forum username