PERL script to check for straightlining behavior in grid questions

Hello everyone,

I am currently creating a survey including grid questions (Likert items with scales ranging from 1 to 7, which will later be used to create scale variables). I would like to check - depending on the number of items - for straightlining behavior within a grid question (in case of many items per grid question) as well as across several grid questions (in case of only a few items per grid question). For this purpose, I wrote a generic Perl script and put it into a Free Format Question:


<input type="hidden" name="SLValueScale_Dummy" id="SLValueScale_Dummy" value="[% Begin Unverified Perl

# Exercises
my @exernames = ('ValueScale_r');
my @exernums = (16);
my $nScales = scalar @exernames;


# Write answers in array
my @answers = ();
my $j=0;
for (my $k = 0; $k < $nScales; $k++) {
for (my $i = 1; $i <= $exernums[$k]; $i++) {
$answers[$j] = GETVALUE($exernames[$k] . $i);
$j = $j+1;
}
}

# Calculate mean
my $answers_mean = 0;
foreach my $answer (@answers) {
$answers_mean += $answer;
}
my $answers_mean = $answers_mean / ( scalar @answers );


# Calculate variance and standard deviation
my $straightliner = 0;
my $sqtotal = 0;

foreach my $answer (@answers) {
$sqtotal += ($answer - $answers_mean) ** 2;
}
my $variance = $sqtotal / ( scalar @answers );
my $sd = sqrt($variance);


# Check for straightlining behavior
if ( $sd<0.1 ) {
$straightliner = 1;
}

# Return function value
return $straightliner;

End Unverified %]"/>


This code is only for one grid question called "ValueScale", which contains 16 item questions. In this Free Format Question, I have defined a hidden variable "SLValueScale_Dummy" of type "Whole number". Unfortunately, something seems to go wrong, because the hidden variable - regardless of the answers to the item questions - always has the value 1.
I have also tried setting a Pass-In variable after the return argument to check if the error is with the hidden variable. More specifically, I added the following code:


[…]
# Set values
SETVALUE("PassinSLValueScaleDummy", $straightliner);
End Unverified %]"/>


I have had data generated (using the built-in function of Lighthouse Studio), but the values of the pass-in variable do not make any sense as they are six-digit numbers ( and not a dummy).

I am a bit lost right now and would appreciate any help.

Resolved
4 replies