Assigning Subject Numbers in Web Experiments

Subject numbers play a number of important roles in experiments. In longitudinal studies, they can be used to correlate data gathered from a subject at different times. For experiments with between-subject variables, subject numbers play a critical role in assigning participants into particular conditions. By using identification numbers such as student ids or telephone numbers, the subject number can also allow researchers to identify participants in cases where the research makes that necessary.

For traditional lab research, managing subject numbers with Inquisit is straightforward. The experimenter starts the Inquisit script and either enters the subject number herself, or she instructs the participant to enter the number. With web research, participants may be located anywhere in the world, in which case there is no experimenter overseeing the data collection session who can assign the appropriate subject number. In these cases, it is necessary to devise a system in which either participants can specify the number themselves, or the number is automatically assigned by the web site. This article will discuss several strategies for assigning subject numbers to web participants.

Subject numbers for between-subject variables

One of the most common uses of subject numbers by Inquisit is to assign subjects into a particular cell of a between-subjects variable. Subject numbers are mapped to conditions within the Inquisit script itself, either by the expt or the variables element. For example, in Figure 1 below the script uses the expt element to counterbalance the order of two blocks across even and odd numbered subjects:

Figure 1.

<expt>
/ subjects = (1 of 2)
/ blocks = [1=conditiona; 2=conditionb]
</expt>

<expt>
/ subjects = (2 of 2)
/ blocks = [1=conditionb; 2=conditiona]
</expt>

In most cases, the researcher wants to ensure that subjects are randomly assigned into the even or odd numbered group, and they want equal numbers of subjects in each condition.

Subject number assignment for scripts hosted on millisecond.com

If you choose to host your script on the millisecond.com server, there are several options for generating subject numbers that you can choose from: random generation, random generation without replacement, sequential, user entered, and user entered with confirmation prompt. You simply choose which option you want when running the Script Registration Wizard. There's no need to understand the technical details of how these work.

Random Assignment of Subject Numbers

The first sample page shows random generation of subject nunbers, which is the simplest way to achieve a random, approximately even distribution of subjects into different conditions. Let's take a look at how the reference page randomly assigns subject numbers. (You can see this first hand by browsing to the page and viewing the underlying source.) The first thing to note is the following section of JavaScript at the top of the page source:

In the source code for the page is a javascript method called "GetSubjectNumber" that is responsible for generating the subject number. For random generation, the method looks like this:

Figure 2.
function GetSubjectNumber() 
{ 
   return (Math.floor(Math.random() * 1000000000)); 
} 

The method contains a single line of code does the work of randomly generating the subject number. The function uses the javascrip method , Math.random(), to generate a random number from 0 to 1, then multiples that value by 1,000,000,000 and rounds it down to the nearest integer so that the final result is a random number between 1 and 1,000,000,000. Note that we are selecting numbers with replacement, so it is theoretically possible that two participants might be assigned the same subject number. However, the chances are slim indeed, and if it does happen, you can use the time of the session as logged in the "date" and "time" data columns to distinguish the subjects' data.

Prompting participants to enter a subject number

For some experiments, it may be necessary or convenient to use personal identification such as a telephone number, social security number, or student id as the subject number rather than an arbitrary, randomly generated number. To administer this kind of experiment over the web, the participant must be allowed to input their id number so that it can be recorded along with the data. The second option allow you to prompt the participant to enter an id number. The third option has the subject enter the number twice to avoid keying errors.

Let's take a look at the javascript for the third option, which is the more complicated method. In this case, the GetSubjectNumber() method contains the following javascript code, which prompts the subject to enter a 5 digit number, then prompts again to confirm the number, and provides error feedback if the number is invalid or they don't match.

Figure 3.

function GetSubjectNumber() 
{ 
// This method prompts the subject for an id number and then prompts
    again to // confirm in order avoid mistyped numbers. 

var intRegExp = /(^\d{5,5}$)/;
var promptMsg = "Please enter a 5 digit id number."; 
var confirmMsg = "Please confirm the number you entered."; 
var invalidMsg = "The number was invalid. Please enter a valid number.";
var matchMsg = "The numbers you entered did not match. Please enter the number again."; 

var snum = window.prompt(promptMsg, ""); 

while ( snum != null && intRegExp.test(snum) == false ) 
{ 
// if the input was invalid, prompt again 
    window.alert(invalidMsg); 
    snum = window.prompt(promptMsg, ""); 
} 

if ( snum == null ) 
{ 
    return null; 
} 

var sconfirm = window.prompt(confirmMsg, ""); 
while ( sconfirm != null && (sconfirm != snum || intRegExp.test(sconfirm) == false) ) 
{ 
    // if the input was invalid, prompt again 
    if ( intRegExp.test(sconfirm) == false) 
    { 
        window.alert(invalidMsg); 
    } 
    
    // if the numbers do not match, alert the user and bail out so
        they can start over 
    else if ( sconfirm != snum ) 
    { 
        window.alert(matchMsg); 
        return null; 
    } 
    
    sconfirm = window.prompt(confirmMsg, ""); 
} 

return sconfirm; 

}

The validation is done through the regular expression in the first line of code. You can change the validation by replacing the regular expression (which only verifies that the input is numeric and 5 digits long) with your own. If you don't understand regular expressions, don't worry, the web is abundant with ready made regular expressions for validating all kinds of input (integers, zip codes, telephone numbers, etc.). I found this one in seconds through Google.

Customized subject number generation

If you wish to use your own method for generating subject numbers, you can do so simply by editing the GetSubjectNumber() method in the source code. Remember, this javascript code runs on the participant's machine, not on the server, so it has no way to keep track of which subject numbers have already been assigned. For that, you would have to program some server code that tracks subject numbers in a database, then dynamically injects the subject number into this web page, and in particular, into the GetSubjectNumber(). If you host your scripts on millisecond.com, this is how the hosting service is able to generate sequential subject numbers and random selection without replacement.

These are just a few of the strategies for assigning subject numbers on the web. They are by no means the only techniques possible. Other strategies include deriving subject numbers from the date and time, the ip address of the client computer, or the session id. For those with some familiarity with web development, the web is an flexible and open programming environment that makes any number of schemes possible.