This is an old revision of the document!
Randomization is used to assign a participant at random to an experimental group (random sampling).
This is different to rotation, in which the order of questions or items is varied in order to reduce bias that might be introduced by the order in which questions/choices are listed on the page.
Tip: Read the chapters Introduction to PHP and Introduction to Arrays for a better understanding of the complex example codes shown below.
The random event – drawing a random number – lies at the core of every randomization. Generally, there are two possibilities when randomly selecting a stimulus, question, sequence etc: drawing “with” or “without” replacement.
Once a random number has been drawn for the first time, the continued use is independent of “with” or “without”. The random number is selected from the data record through value()
and used, for example, in a filter.
random()
. Note, that the unrestricted random drawing does not ensure equal groups.Particularly in experiments, participants often have to be allocated at random to trial and control groups. Put another way: the participant who is to be shown multiple stimuli must be selected at random.
The follow example shows how one of 4 pictures can be shown by using PHP code. The prerequisite, as described above, is that you drew a random number between 1 and 4 and saved this in the variable RG01
. Furthermore, you have to have uploaded the four pictures beforehand in Image and Media Files into the survey project
$number = value('RG01'); // select random number drawn // display graphic depending on the number // (HTML code used for this) if ($number == 1) { html('<p><img src="stimulus1.png"></p>'); } elseif ($number == 2) { html('<p><img src="stimulus2.png"></p>'); } elseif ($number == 3) { html('<p><img src="stimulus3.png"></p>'); } elseif ($number == 4) { html('<p><img src="stimulus4.png"></p>'); }
This example shows graphic “stimulus1.png”, “stimulus2.png”, “stimulus3.png” or “stimulus4.png” at random. As an example, a question evaluating the picture could be displayed below this.
Instead of using html()
to display text or a graphic, text()
can also be used to show a longer text, integrate a video or show a question with question()
.
In this chapter, several example codes describe how pictures can be rotated or randomized. A HTML tag <img>
is integrated into each code, for example:
// enter complete HTML code every time if ($number == 1) { html('<img src="imageA.jpg" alt="election poster candidate A">'); } elseif ($number == 2) { html('<img src="imageB.jpg" alt="election poster candidate B">'); }
// only determine the image's name in the filter ... if ($number == 1) { $image = 'imageA.jpg'; } elseif ($number == 2) { $image = 'imageB.jpg'; } // ... and only after this generate HTML code html('<img src="'.$image.'" alt="election poster">');
Other content – e.g. films, stimulus texts etc. – can be integrated as HTML code in exactly the same way. In addition, see Media Files in Questionnaires for more information.
It is best to enter HTML codes to integrate film and audio files (stimulus texts) as text elements in Text Elements and Labels (Texts in Questionnaire). These text elements can be integrated using the function text()
. The equivalent to the code excerpt above would then look as follows:
if ($number == 1) { text('stimulusA'); } elseif ($number == 2) { text('stimulusB'); }
Note: The examples above only show excerpts of PHP code that are required to show the stimulus. A random number still has to be drawn and sorted beforehand. (see above).
Tip: You can indeed combine multiple elements in a text element – for instance, a picture with text next to it.
<table cellspacing="0" cellpadding="0" border="0"> <colgroup> <col width="400"> <!-- width of the picture in pixels --> <col> <colgroup> <tr> <td><img src="imageA.jpg" alt="election poster"></td> <td style="padding-left: 20px"> <p>Max Müller supports the citizens' rights. He would like to achieve greater citizen participation and more efficient decision-making with a sustainable concept and substantiated ideas. </td> </tr> </table>
Do you require one question for one part of the stimuli and another question for the rest?
if ($number == 1) { text('stimulusA'); question('AB01'); } elseif ($number == 2) { text('stimulusB'); question('AB01'); } elseif ($number == 3) { text('stimulusC'); question('AB02'); } elseif ($number == 4) { text('stimulusD'); question('AB02'); }
Entire questions are randomized primarily in method experiments — this is also easy to do with filters. In principle, the process is the same as when a stimulus is randomized:
if ($number == 1) { question('AB01'); } elseif ($number == 2) { question('AB02'); }
It gets a little more tricky if the questions should appear on different pages in the questionnaire. If this is the case, you have to filter in two places:
// show the one or the other question $number = value('RG01'); // Retrieve the random number drawn by RG01 if ($number == 1) { question('AB01'); // assessment of politician } else { question('AB02'); // assessment of party }
$number = value('RG01'); if ($number == 1) { question('AB02'); // assessment of party } else { question('AB01'); // assessment of politician }
In this example, either question AB01 will be shown first and then AB02 later at random – or the other way round.
If the page order should be more varied more complexly, then filters can quickly become confusing – as shown in the example above. The PHP function setPageOrder()
can help in this case. This function varies the sequence of pages in the questionnaire.
$number = value('RG01'); // Must have drawn a random number with RG01 if ($number == 1) { setPageOrder('q1','q2','mainStart-mainEnd','q3'); } elseif ($number == 2) { setPageOrder('q1','mainStart-mainEnd','q2','q3'); } else { setPageOrder('q2','q1','mainStart-mainEnd','q3'); }
You can find further explanations and examples in the section setPageOrder()
.
Do you want to show 3 questions out of 10 at random, in order to distribute your stimulus across several participants? There are two ways to do so for random draws with and without replacement.
If it does not matter which combination the questions are shown in and if there the number of cases per question is big enough to cope with statistical fluctuations, then create a list (array) with all questions IDs, shuffle the list and then show the questions with the first three IDs.
// make sure page will not be redrawn when reloaded if (!isset($questions)) { // list of relevant questions $questions = array( 'AB01', 'AB02', 'AB03', 'AB04', 'AB05', 'BB01', 'BB02', 'BB03', 'CC01', 'CC02' ); // shuffle list shuffle($questions); // and save variable for a new display of the page registerVariable('questions'); } // show question, which ranks first after shuffling question($questions[0]); // and the questions in second and third place question($questions[1]); question($questions[2]);
Tip: If questions should be shown on separate pages, simply place the lines with question()
on separate pages in the questionnaire.
Take care: If you use several types of these selections in the questionnaire, then the line with isset()
can cause trouble. In this instance, use different variables for $questions
, for example, $questions1
and $questions2
(of course, this must also be adjusted in registerVariable()
).
If you have to make sure that all questions are shown equally often, you need an urn that contains the possible question IDs in combinations of three. You have to create these combinations beforehand, e.g. in Excel.
AB01, AB02, AB03 AB01, AB02, AB04 AB01, AB02, AB05 ... AB05, AB03, AB04 AB05, AB04, AB03
Indeed, there are 60 combinations possible with just 5 questions. If you have distinctly more combinations than participants then you have might have to remove a portion of the combinations systematically – otherwise it is not guaranteed that the questions will be shown equally often.
In the questionnaire you now draw one of the combinations out of the urn and use the IDs drawn as they are in the question()
function. Please note, in this instance you need 3 variables in the question IV01
using the type “Internal Variables”.
urnDraw('questions', 'IV01'); question(value('IV01_01')); question(value('IV01_02')); question(value('IV01_03'));
You can also distribute the question()
functions here on different pages in the questionnaire.
If possible, you would like a greater number of objects to be evaluated. Instead of using an if
construction, the switch
construction is somewhat more convenient. Both the following codes have exactly the same effect:
$number = value('RG01'); // Random number drawn by RG01 if ($number == 1) { $src = 'monkey.jpg'; } elseif ($number == 2) { $src = 'brontosaurus.jpg'; } elseif ($number == 3) { $src = 'chinchilla.jpg'; } elseif ($number == 4) { $src = 'dachshund.jpg'; } elseif ($number == 5) { $src = 'elephant.jpg'; } elseif ($number == 6) { $src = 'ferret.jpg'; } elseif ($number == 7) { $src = 'cheetah.jpg'; } elseif ($number == 8) { $src = 'hawk.jpg'; } elseif ($number == 9) { $src = 'hedgehog.jpg'; } elseif ($number == 10) { $src = 'jaguar.jpg'; } html(' <div style="margin: 30px 0px; text-align: center"> <img src="'.$src.'" alt="animal photo"> </div> ');
urnDraw('images', 'AX01'); switch (value('AX01_01')) { case 1 : $src = 'monkey.jpg'; break; case 2 : $src = 'brontosaurus.jpg'; break; case 3 : $src = 'chinchilla.jpg'; break; case 4 : $src = 'dachshund.jpg'; break; case 5 : $src = 'elephant.jpg'; break; case 6 : $src = 'ferret.jpg'; break; case 7 : $src = 'cheetah.jpg'; break; case 8 : $src = 'hawk.jpg'; break; case 9 : $src = 'hedgehog.jpg'; break; case 10 : $src = 'jaguar.jpg'; break; } html(' <div style="margin: 30px 0px; text-align: center"> <img src="'.$src.'" alt="animal photo"> </div> ');
You are also able to draw an entire combination with an urn, not just a random number. This means it is easy to implement multi-factorial experiments or conjoint analyses.
This example uses 3 internal variables (IV01_01
to IV01_03
, labeled as “price”, “quality” and “delivery time”) and a combination urn with the content below:
1, 1, 1 1, 1, 2 1, 2, 1 1, 2, 2 2, 1, 1 2, 1, 2 2, 2, 1 2, 2, 2
In this example, a combination of three is always drawn. Each of the three variables is denoted as either 1 or 2. By using an array, text is assigned to the numbers. In doing so, the value drawn (1 or 2) serves as a key – this is indicated in square brackets, in order to select the respective element of the array. See Arrays.
urnDraw('combi', 'IV01'); // the numbers program three features - text is assigned to them here $price = array( 1 => '5 euros', 2 => '10 euros', ); $quality = array( 1 => 'normal', 2 => 'premium' ); $deliverytime = array( 1 => 'quick', 2 => 'slow' ); html(' <h1>Imagine the following basket:</h1> <p>Price: '.$price[value('IV01_01')].'</p> <p>Quality: '.$quality[value('IV01_02')].'</p> <p>Delivery Time: '.$deliverytime[value('IV01_03')].'</p> ');
If you want to draw from different urns you also need several questions with the type “Internal Variables” – in this example there are three of these types of question “IV01”, “IV02” und “IV03”.
urnDraw('people', 'IV01', 'end'); // draw out first ballot from "people" urnDraw('people', 'IV02', 'end'); // draw out second ballot from "people" urnDraw('relationship','IV03', 'end'); // draw out third ballot from "relationship" // ballots will not stored until the participant // reaches the end of the questionnaire - and so drop outs will not be counted in the draw // now there is age, first name and sex in the "people" ballot // this is merged as "age (first name, sex)" $description1 = value('IV01_01').' ('.value('IV01_02').', '.value('IV01_03').')'; $description2 = value('IV02_01').' ('.value('IV02_02').', '.value('IV02_03').')'; // now in the description there is // e.g. "Andreas (25, m)" and "Berta (22, w)" // now it states in the third ballot, what the relationship is // between the two of them, and a description of the situation // is thus created like so: $complete = $description1.' '.value('IV03_01').' '.$description2; // e.g. "Andreas (25, m) lives with Berta (22, w)"; // of course, the text could also be integrated in the question via placeholder html($complete);
Now onto an even more complicated example which shows how random numbers can be transformed in text elements. In the following example a 6×4 design was assumed – correspondingly, the “award” urn contains 24 ballots:
11, 21 11, 22 11, 23 11, 24 12, 21 12, 22 ... 16, 23 16, 24
// draw a combination out of the "award" urn and save as "A101", // do not store ballot yet urnDraw('Award','A101','man'); // values now saved under A101_01 and A101_02 // the first value can be 11-16, the second 21-24 // saves texts in variables $text1 and $text2 switch (value('A101_01')) { case 11: $text1 = '500 euros'; break; case 12: $text1 = '1000 euros'; break; case 13: $text1 = '1500 euros'; break; case 14: $text1 = '2000 euros'; break; case 15: $text1 = '2500 euros'; break; case 16: $text1 = '3000 euros'; break; default: $text1 = 'invalid code: '.value('A101_01'); } switch (value('A101_02')) { case 21: $text2 = 'a friend'; break; case 22: $text2 = 'a relative'; break; case 23: $text2 = 'a colleague'; break; case 24: $text2 = 'your boss'; break; default: $text1 = 'invalid code: '.value('A101_02'); } $situation = $text2.' wants to from you '.$text1.' borrow'; // save in placeholder %situation% replace('%situation%', $situation); // question regarding situation (with placeholder %situation%) question('AL07');
When all relevant information has been requested the ballot will be stored on a later page:
urnPutAway('A101'); // or in this case also simply just urnPutAway();
Note: When drawing out of multiple urns, an equal distribution of the individual drawings is indeed guaranteed, but an equal distribution of the combinations is not. Several combinations may appear considerably more often than others. Use one Urn with Multiple Values per Ballot to evenly distribute the combinations as well.
Note: If you draw out of the same urn twice, then you can get two of the same ballots in the process of doing so. To avoid this, only work with one of the urns that contains all the necessary combinations.
It may be the case that you need to ensure equal distribution of experimental groups in two subgroups – for instance, if you have 3 stimuli and both the women in the study, as well as the men, should be equally divided between all three conditions.
To do so, you require an internal variable and two urns (with the same content). The following example assumes that the sex was queried on an earlier page in the multiple-choice question SD02. Also, that you have set up two urns with the IDS “women” and “men”, both of which contain the numbers 1 to 2 (each one on a separate line).
if (value('SD02') == 1) { urnDraw('women', 'IV01', 'end'); } else { urnDraw('men', 'IV01', 'end'); } // show stimulus $z = value('IV01_01'); if ($z == 1) { text('stimulus1'); } elseif ($z == 2) { text('stimulus2'); } elseif ($z == 3) { text('stimulus3'); }
If possible, the different subpopulations (e.g. men and women) should also get given different stimuli. A simple solution would be to put different numbers in the urns.
For the following example, you need an “internal variables” question IVO1, with one item and two urns with the IDs “women” (numbers 1 to 3, with each one in a separate line) and “men” (number 4 to 6). Additionally, the sex must have been prompted beforehand in the multiple-choice question SD02.
Apart from that, the PHP code is largely identical with the previous example.
if (value('SD02') == 1) { urnDraw('women', 'IV01', 'end'); } else { urnDraw('men', 'IV01', 'end'); } // show stimulus $z = value('IV01_01'); if ($z == 1) { text('stimulusF1'); } elseif ($z == 2) { text('stimulusF2'); } elseif ($z == 3) { text('stimulusF3'); } elseif ($z == 4) { text('stimulusM1'); } elseif ($z == 5) { text('stimulusM2'); } elseif ($z == 6) { text('stimulusM3'); }