Weight - Save Question with Merged Categories for Weighting
This tool 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 tool 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 underlying values. 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 data that has been generated by this tool. While the table appears the same as the previous one, the underlying values now contains only two distinct values. This new merged , 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 tool uses a JavaScript formula to construct the merged categories. If any of the original categories from the data are not shown on the table and not included in any of the merged categories (which can happen when rows are hideen on the table), then respondents from those categories will be assigned to a category called Missing Data and given a value of -99.
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");
main();
function main() {
let selected_questions = selectInputQuestions(["Nominal", "Ordinal"], false, false, false);
if (!selected_questions || selected_questions.length == 0) {
log(correctTerminology("Please select one or more Pick One questions."));
return false;
}
let new_questions = selected_questions.map(createPickOneQuestionWithMergesHardCoded);
// Show the user what's been done
if (inDisplayr()) {
project.report.setSelectedRaw([new_questions[0]]);
return;
}
let new_group = project.report.appendGroup();
new_group.name = "Merged Categories for Weighting";
new_questions.forEach(function (q) {
let t = new_group.appendTable();
t.primary = q;
});
if (fileFormatVersion() > 8.65)
project.report.setSelectedRaw([new_group.subItems[0]]);
return;
}
function consolidatedPickOneExpression(question, merge_object) {
let q_name = question.variables[0].name;
let expression = "var _res = -99;\r\n"
merge_object.forEach(function (obj, ind) {
let sub_expression = "if (";
if (ind > 0)
sub_expression = "else " + sub_expression;
let 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;
}
function createPickOneQuestionWithMergesHardCoded(question) {
let merged_categories = getMergedCategoriesFromPickOne(question);
let expression = consolidatedPickOneExpression(question, merged_categories);
let q_var = question.variables[0];
let data_file = question.dataFile;
let 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
let value_attributes = new_var.valueAttributes;
let 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");
return new_var.question;
}
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