|
z-Tree: Tips & Tricks |
|
Licencing Installation First Steps Programming Layout Running a session Printing Other Tips Future improvements |
ProgrammingProfits
Random variables
RankRepetition of a treatment
later command
Clock
Error messagesMultiphase experimentThe variable TotalProfit is not updated after the first period of a trial run, i.e. remains 0. In the following periods, this shortfall continues to miss. What is wrong?This is normal with trial runs. The TotalProfit must remain 0 since trail runs are defined thereby that subjects are not able to make profit. If you wish to make trial runs where subjects can make profit, I suggest you write a separate treatment. How is it possible that an entire vector of payments is shown to all the subjects?You put all the profits into a contracts table. To do so, you write the following program into the subjects table: contracts.new {
Who = : Subject;
Profit = :Profit;
Group = : Group;
}
Doing so, you create a new record in a contracts table for each of the subjects. Now, you can show all the profits in either a contract list box or in a contract grid box. Can the variable TotalProfit be changed?If TotalProfit is changed in a program this change is overridden by z-Tree. It set TotalProfit always to the total profit of the past periods plus the current profit in a treatment. What is the difference between the variable FinalProfit and the variable TotalProfit?FinalProfit is the sum of all profits of all treatments in money units. TotalProfit is the sum of all profits of one treatment in experimental currency units. FinalProfit is a variable of the session table, TotalProfit is in the subjects table. Payment of a Random PeriodAn experimental technique to avoid income effects consists in paying one randomly selected period. To do this with z-Tree you have to store the profits of all the periods in the summary table. You need the following program in the summary table: array Profits[]; // one array element per subject
subjects.do {
:Profits[ Subject ] = Profit;
Profit = 0; // delete Profit, otherwise it is added up in the variable TotalProfit
if ( Period == NumPeriods ) {
SelectedPeriod = RandomPeriod;
Profit = summary.find(
Period == :SelectedPeriod,
Profits[ :Subject ] );
}
}
The variable SelectedPeriod can be set at a subject's screen after a dice has determined the paid period. If you want to pay a random period out of periods not played in the same same treatment, you have to define a user defined table. See A table that survives the end of a treatment: The session table and User defined tables. Does there exist a lottery that can be programmed with z-Tree or is it only possible to execute a lottery physically?There is a random generator in z-Tree: The function random() defines a uniform random number between 0 and 1. For example you could pay a dividend depending on a random number. To do this, you write a program into a globals table: r = random (); Dividend = if ( r < 0.25 , 60 , if ( r < 0.5 , 28 , if ( r < 0.75 , 8 , 0 ))); There is no visualisation of the drawing of the random number however. Regarding the credibility, it is probably still the best to draw a random number physically, e.g. with a die. Regarding the random selection that is used automatically if I choose to have subjects begin individually, is it uniform?Each subject gets the same chance to start. If you like, you can also define your own sequence by defining the variable "Priority" in the subjects table. The subjects with the lowest value of Priority starts first. I want to generate a random variable that remains constant throughout a treatment but changes from treatment to treatment. How is this possible?To use the same random variable in all periods, you just copy it from period to period. To do so, you program the following in the globals table:
r=random();
if ( Period > 1 ) {
r=OLDglobals.find(r);
}
How do we randomly assign a number (i.e. income) to subjects? How do we choose a subject at random (i.e. for an audit)?There are in principal two ways. You either use a physical random device (e.g. a die) or you generate a random number in z-Tree. The advantage of the physical random numbers is credibility, the advantage of program generated numbers is the ease of use. If you need a lot of random numbers, you can easily use computer generated random numbers. Otherwise, I would recommend physical random numbers. We use a physical random number in: Fischbacher, Urs, Simon Gächter and Ernst Fehr, Are People Conditionally Cooperative? Evidence from a Public Goods, Experiment Economics Letters 71 (2001), p. 397-404. You find the treatment here. To answer your first question: In a subjects table, you program: Income = roundup( random*100, 1); // income =0,1,2,,...100 This way, a random number multiplied by 100 and rounded up is the randomly assigned income to subjects. To answer your second question: You choose one subject within a group. Therefore you write a program in a globals table: NumGroups = maximum (Group); // Groups 1,..., NumGroups
repeat {
subjects.do { r = random();
}
subjects.do { MaxR = maximum( same(Group), r) ; IsBest = if ( MaxR ==r,1,0); }
} while ( not( subjects.sum( IsBest )== NumGroups )); //to avoid ties
RankCalculation of RankIf you wish to determine for a record at which place the value of a variable x is situated relative to the others, you can define a formula. The record with the greatest x shall be given a 1. If all x are different or we accept that several x may have the samerank: Rank = count ( x >= :x); If x is an integer and we wish to have a different rank for each x, we add a random number to x and calculate the rank of this number. We may not thereby integrate the random function into the sum formula, as we would then get an inconsistent ranking order. x1 = x + random() / 2;//---------- in a new program Rank = count ( x1 >= :x1); Note: When we write these two lines into one program, the variable x1 of the others is not yet calculated when we use it in the rank formula. A simple trick consists of filling the random number into a field at the very beginning of the procedure. We thus set: random1 = random(); in a program of the background. Now we can define the rank: Rank = = count ( x + random1 > = :x + :random1); How do I automate the repetition of a treatment?There exists a predefined variable RepeatTreatment in the globals table. This variable is mainly defined to implement a random stopping rule. To implement this rule, you do the following: You define a one period treatment, i.e. a normal treatment with a certain number of stages. At the end of the period, you decide with what probability the treatment will be stopped or continued. To do so, you set the variable RepeatTreatment in the globals table: ProbabilityOfRepetition = 0.2; RepeatTreatment = if ( random() <= ProbabilityOfRepetition,1,0); If the treatment is repeated, the period counter is incremented as if there was one treatment. If the variable is not defined, the treatment is terminated after one period. This is the normal case; z-Tree then initialized the variable RepeatTreatment to –1. Attention: If you set the variable RepeatTreatment as 1, the treatment will be repeated forever. Is it possible to override the number of periods depending on subjects' choices?It is not possible to override the number of periods (NumPeriods) in a program. But from Version 2.1., as an experimenter you can manually end a treatment at the end of the period (Menu Run > Stop after this period). Another possibility is to use the variable RepeatTreatment in the globals table in the Background. This allows to define a treatment with a random end. (Please see instructions above.) I need a screen on which in short time intervals new information is shown about the same variable. How can I do this besides programming a new stage each time?This you do the best by using the later command. Please see here or Tutorial, pages 67/68, for further instructions. I would like to have - besides z-Leaf - another client that has predefined, fixed strategies about the next action it takes. How can I do this?It is not possible to automate z-Leaf, but you can execute automated programs in z-Tree: With the later command, you can execute a program in predefined time intervals. This way you can simulate a client. How do I show items on the screen for a limited amount of time?You can do this with the later command. You can show and hide boxes. First, you create a variable Show and initialize it to 1. Then you put the condition Show==1 into the Display Condition of teh box you want to hide. Finally, you write the following program:
show = 1;
later (30) do { show = 0; }
This means that after 30 seconds, the box and the items in the box disappear from the screen. Independently of whether the subjects already made their entries and exit the stage, the clock should continue until a certain maximal value. Is this possible?This is possible. To program this, you select in the Stage dialog for the condition "Leave stage after timeout" the button Yes. Furthermore, you write a program into the button the subjects press to confirm their entries to avoid that the stage automatically is left when the last subject has made his entry. Reduce time-out times after the experiment has startedOne defines the time-out in the stage dialog. This formula cannot be changed when the treatment has been started. Therefore, you use a variable in the globals table for the time-out. This variables can be modified in the period parameters. This is also possible this during the experiment for the periods not yet started. Can the error messages that z-Leaf shows, be changed?No, error messages are fixed. Multiphase ExperimentIf a period in an experiment consists of several phases, you define the appropriate number of stages for each phase. If a phase should be very complex, it is generally easier to define each phase as one period. You define a variable Phase. This variable is calculated at the beginning of a period. If, for example, we have 2 periods: Phase = mod( Period-1, 2) +1; calculates the phase and UserPeriod = rounddown( Period/2 ); calculates the period as it is supposed to be shown to the subjects. This variable cannot appear in a normal header box. However, you may insert it in a normal standard box (this also holds for the variable Period).
|