Create New Variables - Rebase Multiple Response Data in Variable(s) to NET

From Q
Jump to navigation Jump to search

This tool is used to rebase Pick Any and Pick Any - Grid questionsBinary - Multi and Binary - Grid variable sets which do not have missing values recorded for respondents who skipped the question in the questionnaire, leading to a NET which is less than 100%. Any respondents who do not have at least one selection in the questionvariable set will be removed from the base, and so the NET will be 100%.

The tool makes a new copy of any questionsvariable sets that you choose to rebase, and you have the option of hiding the original questionsvariable sets if you no longer want to see them in your project.

Example

This example shows a SUMMARY table of an Unaided awareness questionvariable set. As indicated by the NET, some respondents have not given any answers, but they have also not been recorded as missing data. If those respondents skipped the question in the questionnaire then we may wish to rebase the questionvariable set to only respondents with at least one answer. The table to the right shows the result of rebasing using this QScript.

RebasePickAny1.PNG

Technical details

This tool is intended to be used for multiple response data that has no Missing Data code. This is to correct for data files which have been created poorly, and it is usually better to ask the data provider to make sure that respondents who skip a question are marked as missing, according to the recommended specifications for SPSS data files, linked here: SPSS Data File Specifications.

If the script is applied to questionsvariable sets which already have missing data then respondents with no responses will still be removed from the base, but the base will not necessarily be the same as the original NET.

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

  • Copy the JavaScript shown on this page.
  • Create a new text file, giving it a file extension of .QScript. See here for more information about how to do this.
  • Modify the JavaScript (see QScripts for more detail on this).
  • Run the file using Automate > Run QScript (Macro) from File.

JavaScript

includeWeb('QScript Functions to Generate Outputs');
includeWeb('QScript Selection Functions');
includeWeb('QScript Utility Functions');

rebaseMultipleResponse()

function rebaseMultipleResponse() {
    const allowed_types = ["Binary - Multi", "Binary - Grid"];
    const web_mode = inDisplayr();
    let selected_questions = selectInputQuestions(allowed_types);
    if (!selected_questions)
        return false;

    // Only prompt and hide in Q
    let hide_inputs = false;
    if (!web_mode)
        hide_inputs = askYesNo("Do you want to hide the original questions?");

    // Make new questions and show them in table
    let new_group;
    if (!web_mode) {
        new_group = project.report.appendGroup();
        new_group.name = "Rebased Questions";    
    }
    
    let new_questions = [];
    selected_questions.forEach(function (q) {
    	let new_question = rebasePickAny(q, hide_inputs);
        new_questions.push(new_question);
        if (!web_mode) {
            let t = new_group.appendTable();
            t.primary = new_question;
            t.cellStatistics = ['%', 'n', 'Base n'];
        }
    });

    moveQuestionsToHoverButtonIfShown(new_questions);

    // More recent Q versions can point the user to the new items.
    if (!web_mode && fileFormatVersion() > 8.65)
        project.report.setSelectedRaw([new_group.subItems[0]]);
    return true;
}

// Create a new question where each of the new variables is missing for
// respondents that made no selections in the input question.
function rebasePickAny(question, hide_inputs) {
    let variables = question.variables;
    let num_vars = variables.length;
	let last = variables[num_vars-1];
    let data_file = question.dataFile;

    // Formula for respondents with at least one answer
    let chosen_formula = "(" + variables.map(function (v) { return v.name; }).join(" == 1) || (") + " == 1)";

    // For each variable, create the appropriate rebased variable with a new JavaScript variable
    let rebased_vars = [];
    try {
        variables.forEach(function (variable) {
            let new_name = preventDuplicateVariableName(data_file, variable.name + "_r");
            let rebased_var = data_file.newJavaScriptVariable("if (" + chosen_formula + ") " + variable.name + "; \r\nelse NaN;", false, new_name, variable.label, last, true);
            rebased_var.variableType = "Categorical";
            rebased_vars.push(rebased_var);
            last = rebased_var;
        });
    } catch(e)
    {
        log("Could not create variable: " + e);
        return false;
    }
        
    // Rename and hide the original question
    if (hide_inputs)
    	question.isHidden = true;

    // Set the question
    let new_q = data_file.setQuestion(
		preventDuplicateQuestionName(question.dataFile, question.name + " - rebased to NET"),
		question.questionType, // Pick Any or Pick Any - Grid
		rebased_vars);

    // Set the Value Attributes
    setCountThisValueForVariablesInQuestion(new_q, 1, true);
    setLabelForVariablesInQuestion(new_q, 1, 'Selected')
    setLabelForVariablesInQuestion(new_q, 0, 'Not Selected')
    new_q.needsCheckValuesToCount = false;
    return new_q;
}

See also