Formulas for calculating respondent weightings


Formulas can be used when calculating respondent weightings to describe a continuous weighting scale based on respondents’ information.

For example:

  • If you want to weight respondents by age, you can retrieve the age information from a question using the answer(question_id, [item_id], [stimulus_id]) function, then multiply it by a scaling factor.

  • If you wanted respondents to be weighted based on whether their age is above 50 (weight of 1), exactly 50 (weight of 0.5), or below 50 (weight 0.1), the formula will be:

ifs(answer(1559304)>50,1,answer(1559304)==50,0.5,0.1)

or, almost equivalently:

(answer(1559304)>50) + (answer(1559304)==50)*0.5 + (answer(1559304)<50 & answer(1559304)>0)*0.1

Post-processing rule for respondent weightings

There is a special post-processing rule that applies to the output of formulas used in respondent weightings: The final value x from the formula language is wrapped into naTo0(toNumber(x)).

Examples:

SituationFormulaPost-processed resultComment
The GET variable gender is maleGETvariable("gender")0because male is forced into 0
The GET variable gender was not recordedGETvariable("gender")0because the empty string is forced into 0
The GET variable age is 20GETvariable("age")20because age is converted into the number 20
"3" + 58because 3 is forced into the number 3
"male" + " bird"0because both string values are forced into 0
"1"1because the string value is converted into a number
"one"0because the string value could not be converted into a number
undefined0because undefined values are converted into 0