There are several layers here to unpack.
An attribute level may have
- a categorical order 1,2,3 etc
- a label e.g. (“$23.34 including VAT”)
- and optionally a numerical value (e.g 23.34)
Each of these fully describes the attribute level and is used in various places.
Although it sometimes looks possible, It is not generall feasible to simply transliterate textual numeric values into numeric ones.
Some pathological cases, are
- 1.203,46 Euros (yes thats how they do it
- 25K (thousand)
- 12lb 6 oz
- 30 minutes, 1 hour, 1 day
There are many ways to go wrong, so labels and numeric values are ‘double input’.
Where are each used
categorical order - in decising which level to present from an experimental design
label - what the respondent sees
numeric value - in experimental design optimisation and in modelling
So in your case - if this is too complex, just make it simpler and just don’t bother with numeric values unless you need to do some fancy design with priors, or you want the modelling to work automatically - you can always recode categorical levels later.
To render a level from a variable -
Firtly lets claify your example which doesn’t make that much sense as stated.
You create a derived value tt_neg50 some where in the survey that is 50% of the respondents travel time as an absolute value.
Say they enter travel time into a question labelled travel_time
When accessing this in a followup derived value this is the PERL value $travel_time
Then the expression tt_neg50 is something like
OK
int(0.5 *$travel_time)
But this will need to be displayed to a human so you might want to up your game and make this a much more readable label with hours and minutes. Here is a nice little Perl snippet
BETTER
my $travel_time = 154; # example value in minutes
my $hours = int($travel_time / 60);
my $minutes = $travel_time % 60;
my $readable;
if ($hours > 0) {
$readable = “$hours hour” . ($hours == 1 ? “” : “s”);
$readable .= " $minutes minute" . ($minutes == 1 ? “” : “s”) if $minutes > 0;
} else {
$readable = “$minutes minute” . ($minutes == 1 ? “” : “s”);
}
print “$readable\n”;
# in the derived expression we would use
# return $readable to place this into tt_neg50
DISPLAYING
Now you have your value - you want to display it in the experiment.
In SurveyEngine you can ‘pipe’ labels anywhere using a custom html class alled ‘expression’
So in the experiment click the </> code view and enter
<span class=’expression’>$tt_neg50</span>
What this does is whenever an html element is tagged ‘expression’ the bit between the tags - is evaluated and the whole html tage replaced with the result.
REFERENCES
Tools used
ChatGPT prompt used
I want a short PERL snippet that takes a variable called travel_time which is in minutes and converts this to a more human readable value like 2 hours 34 minutes. round down to the nearest minute and ignore hours if less than 1