oTree Forum >

Treatment assignment based on participant variables

#1 by AlejandraVG

Hello everybody,

I'm developing an experiment where I need to randomly assign 3 different treatments to 2 groups of participants based on a variable called survey2 (1=male, 2=female). I want the treatment assignment to be between-groups, meaning that in each group one third of participants should be assigned to each treatment.

I managed to randomize within-groups, but this is not what I need. I read the documentation at https://otree.readthedocs.io/en/latest/treatments.html and also at https://ibsen-otree-docs.ibsen-h2020.eu/groups.html#group-matching, but couldn't manage to randomize between-groups.

Thank you for your time. Any help would mean a lot! Here is my code where the randomization is done within-groups:

in init_.py:
from otree.api import *
c = cu
doc = ''
class C(BaseConstants):
   NAME_IN_URL = 'dictgame'
   PLAYERS_PER_GROUP = None
   NUM_ROUNDS = 2

class Subsession(BaseSubsession):
    pass

class Group(BaseGroup):
    pass
def creating_session(subsession):
    players = subsession.get_players()
    for p in players:
        import random
        import string
        treatment = random.choices([1, 2, 3], weights=[1/3, 1/3, 1/3])[0]
        p.in_round(1).treatment = treatment
        p.in_round(2).treatment = treatment
        unique_id =''.join(random.choices(string.ascii_letters + string.digits, k=8))
        p.in_round(1).unique_id = unique_id
        p.in_round(2).unique_id = unique_id

class Player(BasePlayer):
    unique_id = models.StringField()
    checkbox = models.IntegerField()
    treatment = models.IntegerField()
    survey1 = models.StringField()
    survey2 = models.IntegerField(choices=[(1, 'Hombre'), (2, 'Mujer')])
    survey3 = models.IntegerField()
    survey4 = models.IntegerField()
    survey5 = models.IntegerField()
    survey6 = models.StringField()
    survey7 = models.IntegerField()
    survey8 = models.IntegerField()
    survey9 = models.IntegerField()
    survey10 = models.IntegerField()
    survey11 = models.IntegerField()
    survey12 = models.StringField()
    workPercentage = models.IntegerField()
    householdPercentage = models.IntegerField()
    give = models.IntegerField()
    keep = models.IntegerField()
    phone = models.StringField(blank=True)
    tickets = models.LongStringField()
    coop = models.StringField(blank=True)
    municipio = models.StringField(blank=True)
    tosepan = models.StringField(blank=True)
    nombre1 = models.StringField(blank=True)
    correo1 = models.StringField(blank=True)
    telefono1 = models.StringField(blank=True)
    nombre2 = models.StringField(blank=True)
    correo2 = models.StringField(blank=True)
    telefono2 = models.StringField(blank=True)
    nombre3 = models.StringField(blank=True)
    correo3 = models.StringField(blank=True)
    telefono3 = models.StringField(blank=True)
    nombre4 = models.StringField(blank=True)
    correo4 = models.StringField(blank=True)
    telefono4 = models.StringField(blank=True)
    nombre5 = models.StringField(blank=True)
    correo5 = models.StringField(blank=True)
    telefono5 = models.StringField(blank=True)
    PersonalworkPercentage = models.IntegerField()
    PersonalhouseholdPercentage = models.IntegerField()
    FirstworkPercentage = models.IntegerField()
    FirsthouseholdPercentage = models.IntegerField()
    agreement1 = models.IntegerField()
    agreement2 = models.IntegerField()
    agreement3 = models.IntegerField()
    agreement4 = models.IntegerField()
    agreement5 = models.IntegerField()

class Introduction(Page):
   form_model = 'player'
   form_fields = ["checkbox","survey1"]
   @staticmethod
   def is_displayed(player: Player):
       return player.round_number == 1

class Survey(Page):
   form_model = 'player'
   form_fields = ["survey2","survey3","survey4",
                  "survey5","survey6","survey7","survey8",
                  "survey9","survey10","survey11","survey12","coop", "municipio", "tosepan"]
   @staticmethod
   def is_displayed(player: Player):
       return player.round_number == 1

class Introduction1(Page):
   form_model = 'player'
   @staticmethod
   def is_displayed(player: Player):
       return player.round_number == 1
class Incentivizedquestion(Page):
   form_model = 'player'
   form_fields = ["workPercentage"]
   @staticmethod
   def is_displayed(player: Player):
       return player.round_number == 1
   def vars_for_template(self):
       return dict(sexo=self.survey2)
   def js_vars(player):
       return dict(sexo=player.survey2)
   def before_next_page(player, timeout_happened):
       player.householdPercentage = 100 - player.workPercentage

class Introduction2(Page):
   form_model = 'player'
   @staticmethod
   def is_displayed(player: Player):
       return player.round_number == 1

class Round1(Page):
    form_model = 'player'
    form_fields = ["keep"]
    @staticmethod
    def is_displayed(player: Player):
        return player.round_number == 1
    def vars_for_template(self):
        return dict(sexo=self.survey2)
    def js_vars(player):
        return dict(sexo=player.survey2)
    def before_next_page(player, timeout_happened):
        player.give = 10 - player.keep


class Treatment(Page):
    form_model = 'player'
    form_fields = ["keep"]
    @staticmethod
    def is_displayed(player: Player):
        return player.round_number == 2
    def vars_for_template(self):
        if self.treatment == 1 :
            sex = self.in_round(1).survey2
            workPercentage = self.in_round(1).workPercentage
            householdPercentage = 100 - self.in_round(1).workPercentage
            return dict(treatment=self.treatment, sexo=sex,
                    workPercentage=workPercentage, householdPercentage=householdPercentage)
        elif self.treatment == 2 :
            sex = self.in_round(1).survey2
            workPercentage = self.in_round(1).workPercentage-20
            householdPercentage = 100 - self.in_round(1).workPercentage+20
            return dict(treatment=self.treatment, sexo=sex,
                    workPercentage=workPercentage, householdPercentage=householdPercentage)
        elif self.treatment == 3 :
            sex = self.in_round(1).survey2
            workPercentage = self.in_round(1).workPercentage+20
            householdPercentage = 100 - self.in_round(1).workPercentage-20
            return dict(treatment=self.treatment, sexo=sex,
                    workPercentage=workPercentage, householdPercentage=householdPercentage)

    def js_vars(player):
        if player.treatment == 1 :
            sex = player.in_round(1).survey2
            workPercentage = player.in_round(1).workPercentage
            householdPercentage = 100 - player.in_round(1).workPercentage
            return dict(treatment=player.treatment, sexo=sex,
                    workPercentage=workPercentage, householdPercentage=householdPercentage)
        elif player.treatment == 2 :
            sex = player.in_round(1).survey2
            workPercentage = player.in_round(1).workPercentage-20
            householdPercentage = 100 - player.in_round(1).workPercentage+20
            return dict(treatment=player.treatment, sexo=sex,
                    workPercentage=workPercentage, householdPercentage=householdPercentage)
        elif player.treatment == 3 :
            sex = player.in_round(1).survey2
            workPercentage = player.in_round(1).workPercentage+20
            householdPercentage = 100 - player.in_round(1).workPercentage-20
            return dict(treatment=player.treatment, sexo=sex,
                    workPercentage=workPercentage, householdPercentage=householdPercentage)

    def before_next_page(player, timeout_happened):
        player.give = 10 - player.keep

class SNQuestions(Page):
   form_model = 'player'
   form_fields = ["PersonalworkPercentage","FirstworkPercentage","agreement1","agreement2","agreement3","agreement4","agreement5"]
   @staticmethod
   def is_displayed(player: Player):
       return player.round_number == 2
   def vars_for_template(self):
       sex = self.in_round(1).survey2
       return dict(sexo=sex)
   def js_vars(player):
           sex = player.in_round(1).survey2
           return dict(sexo=sex)
   def before_next_page(player, timeout_happened):
       player.PersonalhouseholdPercentage = 100 - player.PersonalworkPercentage
       player.FirsthouseholdPercentage = 100 - player.FirstworkPercentage

class Network(Page):
   form_model = 'player'
   form_fields = ["nombre1","correo1","telefono1","nombre2",
                  "correo2","telefono2","nombre3","correo3","telefono3","nombre4","correo4","telefono4","nombre5","correo5","telefono5"]
   @staticmethod
   def is_displayed(player: Player):
       return player.round_number == 2


class Results(Page):
   form_model = 'player'
   form_fields = ["phone"]
   @staticmethod
   def is_displayed(player: Player):
       return player.round_number == 2

   def vars_for_template(self):
       sex = self.in_round(1).survey2
       round1keep = self.in_round(1).keep
       round1give = 10-self.in_round(1).keep
       round2keep = self.in_round(2).keep
       round2give = 10-self.in_round(2).keep
       total = round1keep+round2keep
       ticket_list = []
       for i in range(1, total + 1):
           ticket_list.append(f"{self.unique_id}_{i}")
       self.tickets =  ', '.join(ticket_list)
       return dict(sexo=sex, round1keep=round1keep, round1give=round1give,
                   round2keep=round2keep, round2give=round2give, playerID = self.unique_id, total = total, ticket_list=ticket_list)

   def js_vars(player):
       sex = player.in_round(1).survey2
       round1keep = player.in_round(1).keep
       round1give = 10 - player.in_round(1).keep
       round2keep = player.in_round(2).keep
       round2give = 10 - player.in_round(2).keep
       total = round1keep + round2keep
       ticket_list = []

       for i in range(1, total + 1):
           ticket_list.append(f"{player.unique_id}_{i}")
       player.tickets = ', '.join(ticket_list)
       return dict(sexo=sex, round1keep=round1keep, round1give=round1give ,
                   round2keep=round2keep, round2give=round2give, playerID = player.unique_id, total = total, ticket_list=ticket_list)


class End(Page):
   form_model = 'player'
   @staticmethod
   def is_displayed(player: Player):
       return player.round_number == 2

page_sequence =[Introduction,Survey,Introduction1,Incentivizedquestion,Introduction2,Round1,Treatment,SNQuestions,Network,Results,End]

In settings.py
PARTICIPANT_FIELDS = ['survey2', 'workPercentage']
SESSION_FIELDS = []
ROOMS = [dict(
        name='appgpk',
        display_name='Experimental Economics Lab'
    ),
    dict(
        name='appgpk',
        display_name='Experimental Economics Lab 1'
    ),
]

Write a reply

Set forum username