Weighting - Save Question with Merged Categories for Weighting
This QScript creates a new copy of a Pick One question where any merged categories are hard-coded so that they can be used to set targets when creating a weight. Often, the categories shown for a demographic question in a questionnaire are not the same categories that need to be used for weighting. When the correct categories can be obtained by merging, this QScript enables those merged categories to be available when creating a weight. This is particularly helpful when a very large number of categories need to be merged.
Example
The first image below shows a SUMMARY table of the Age question from a study next to the Value Attributes window for the question. While the age groups have been merged on the table the underlying data still contains distinct values for all of the original categories. These underlying values will be used if the Age question is used to create a weight, but we would really prefer to use the two merged categories for creating the weight.
The second image shows a SUMMARY table of a new version of the Age question that has been generated by this QScript. While the table appears the same as the previous one, the Value Attributes window now contains only two distinct values, with one for each of the merged categories. This new question, Age - Merged can now be used to construct a weight based on targets for the Under 40 and 40 and over groups.
Technical details
This QScript uses a JavaScript formula to construct the merged categories. If any of the original categories from the question are not shown on the table and not included in any of the merged questions (which can happen when rows are hidden on the table), then respondents from those categories will be assigned to a category called Missing Data and given a value of -99. This will be apparent on the table after the script runs.
How to apply this QScript
- Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
- Click on the QScript when it appears in the QScripts and Rules section of the search results.
OR
- Select Automate > Browse Online Library.
- Select this QScript from the list.
Customizing the QScript
This QScript is written in JavaScript and can be customized by copying and modifying the JavaScript.
Customizing QScripts in Q4.11 and more recent versions
- Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
- Hover your mouse over the QScript when it appears in the QScripts and Rules section of the search results.
- Press Edit a Copy (bottom-left corner of the preview).
- Modify the JavaScript (see QScripts for more detail on this).
- Either:
- Run the QScript, by pressing the blue triangle button.
- Save the QScript and run it at a later time, using Automate > Run QScript (Macro) from File.
Customizing QScripts in older versions
JavaScript
// Create a new Pick One question from Consolidated categories of other pick one question
includeWeb("QScript Selection Functions");
includeWeb("QScript Functions to Generate Outputs");
includeWeb("QScript Utility Functions");
includeWeb("QScript Data Reduction Functions");
if (!main())
log("QScript Cancelled.");
else
conditionallyEmptyLog("QScript Finished.");
function main() {
var data_files = dataFileSelection();
var available_questions = [];
data_files.forEach(function (data_file) {
available_questions = available_questions.concat(data_file.questions);
});
available_questions = available_questions.filter(function (q) { return q.questionType == "Pick One"});
if (available_questions.length == 0)
log("No Pick One questions available.");
var selected_question = selectOneQuestion("Selected the question you want to use:", available_questions);
var merged_categories = getMergedCategoriesFromPickOne(selected_question);
var expression = consolidatedPickOneExpression(selected_question, merged_categories);
var q_var = selected_question.variables[0];
var data_file = selected_question.dataFile;
var new_var = data_file.newJavaScriptVariable(expression,
false,
preventDuplicateVariableName(data_file, q_var.name + "_merged"),
q_var.label + " - Merged",
q_var);
new_var.variableType = "Categorical";
// Set the value labels
var value_attributes = new_var.valueAttributes;
var unique_values = new_var.uniqueValues;
merged_categories.forEach(function (obj, ind) {
if (unique_values.indexOf(ind+1) > -1)
value_attributes.setLabel(ind+1, obj.name);
});
if (unique_values.indexOf(-99) > -1)
value_attributes.setLabel(-99, "Missing Data");
// Show the user what's been done
var new_group_name = new_var.question.name;
var new_group = project.report.appendGroup();
new_group.name = new_group_name;
var t = new_group.appendTable();
t.primary = new_var.question;
if (fileFormatVersion() > 8.65)
project.report.setSelectedRaw([new_group.subItems[0]]);
return true;
}
function consolidatedPickOneExpression(question, merge_object) {
var q_name = question.variables[0].name;
var expression = "var _res = -99;\r\n"
merge_object.forEach(function (obj, ind) {
var sub_expression = "if (";
if (ind > 0)
sub_expression = "else " + sub_expression;
var value_elements = obj.values.map(function (v) {
return "Q.Source(" + q_name + ") == " + v;
});
sub_expression += value_elements.join(" || ") + ")";
sub_expression += "_res = " + (ind+1) + ";\r\n";
expression += sub_expression;
});
expression += "_res;";
return expression;
}
See also
- QScript for more general information about QScripts.
- QScript Examples Library for other examples.
- Online JavaScript Libraries for the libraries of functions that can be used when writing QScripts.
- QScript Reference for information about how QScript can manipulate the different elements of a project.
- JavaScript for information about the JavaScript programming language.
- Table JavaScript and Plot JavaScript for tools for using JavaScript to modify the appearance of tables and charts.
Q Technical Reference
Q Technical Reference
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Updating and Automation > Automation Online Library
Q Technical Reference > Updating and Automation > JavaScript > QScript > QScript Examples Library > QScript Online Library