Conduct Web experiments using PHP, Part 1
By Paul Meagher2005-03-16
Generating the design
Listing 3. Source of generate_2d.php script
<?php
/**
* @package SR
*
* Example script demonstrating how to set up factor codes
* for a two-factor experiment.
*/
require_once "config.php";
require_once PHPMATH . "/SR/StimulusResponse2D.php";
$sr = new StimulusResponse2D;
$sr->setTable("WebOffer");
// You can optionally delete all existing stimulus-response
// info before adding new stimulus codes.
$sr->emptyTable();
// Specify factors and their levels. Factor names should
// correspond to database columns and the factor levels
// should correspond to the enum values for the columns.
$factors["image"] = array("person", "product");
$factors["text"] = array("short", "long");
$sr->setFactors($factors);
// Specify number of replications of the factorial design.
$num_reps = 5;
$num_codes = $sr->insertStimulusCodes($num_reps);
if ($num_codes) {
echo "Success: Inserted $num_codes stimulus codes.";
} else {
echo "Failure: No stimulus codes were inserted.";
}
?>
The script in Listing 3 requires you to specify the factors you will use along with their levels. You also need to specify the number of design replications to use. The insertStimulusCodes() method, located in the StimulusResponse2D.php class, does the main work of populating the WebOffer table with stimulus codes, as shown in Listing 4.
Listing 4. Source of insertStimulusCodes() method
<?php
require_once "StimulusResponse.php";
class StimulusResponse2D extends StimulusResponse {
function insertStimulusCodes($num_reps) {
global $db;
list($factor1, $factor2) = array_keys($this->factors);
$num_codes=0;
for($rep=0; $rep < $num_reps; $rep++) {
foreach($this->factors[$factor1] AS $level1) {
foreach($this->factors[$factor2] AS $level2) {
$rand = mt_rand() / mt_getrandmax();
$sql = " INSERT INTO $this->table ";
$sql .= " ( $factor1, $factor2, rand ) ";
$sql .= " VALUES ";
$sql .= " ( '$level1', '$level2', $rand ) ";
$result = $db->query($sql);
if (DB::isError($result)) {
die($result->getMessage());
}
$num_codes++;
}
}
}
return $num_codes;
}
}
?>
Note that a random value between 0 and 1 is generated through this assignment:
Listing 5. Generating a unit random value
$rand = mt_rand() / mt_getrandmax();
The result of executing the insertStimulusCodes() method is that your WebOffer table contains all the stimulus codes you will need for your Web experiment. The script generates these stimulus codes so all factorial combinations are represented equally often and the order of display is randomized.
Tutorial pages:
|
First published by IBM developerWorks
|
|||||||||
You might also want to check these out:
|
Leave a Comment on "Conduct Web experiments using PHP, Part 1"
You must be logged in to post a comment.
Link to This Tutorial Page!

