Create New Variables - Rebase Multiple Response Data to NET

From Q
Jump to: navigation, search

This QScript is used to rebase Pick Any and Pick Any - Grid questions 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 question will be removed from the base, and so the NET will be 100%.

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

Example

This example shows a SUMMARY table of an Unaided awareness question. 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 question 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 QScript 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 questions 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");

if (!main())
	log("QScript Cancelled.");
else
	conditionallyEmptyLog("QScript finished.");

function main() {
	includeWeb('QScript Selection Functions');

    var web_mode = (!!Q.isOnTheWeb && Q.isOnTheWeb());    
    
    if (!web_mode) {
        // User selections of questions
        var data_file = requestOneDataFileFromProject();
        var candidate_questions = getAllQuestionsByTypes([data_file], ["Pick Any", "Pick Any - Grid"]);

        if (candidate_questions.length == 0) {
            log("No Pick Any or Pick Any - Grid questions found.");
            return false;
        }

        var selected_questions = selectManyQuestions("Select the questions to rebase:", candidate_questions, true).questions;

        if (selected_questions.length == 0) {
            log("No questions selected.");
            return false;
        }

        var hide_inputs = askYesNo("Do you want to hide the original questions?");
    }else {
        var hide_inputs = false;
    	var allowed_types = ["Binary - Multi", "Binary - Grid"];
	    var selected_questions = project.report.selectedQuestions();
        if (selected_questions.length == 0) {
            log("To use this feature, you must select at least one variable set from Data Sets with one of the following structures: " + allowed_types.join(", "));
            return false;
        }
	    var sorted_selection = splitArrayIntoApplicableAndNotApplicable(selected_questions, function (q) { return allowed_types.indexOf(q.variableSetStructure) != -1 && !q.isBanner; });
	    var questions = sorted_selection.applicable;

        if (sorted_selection.notApplicable.length != 0){
            log("The structure for each of the selected variable sets must be one of: " + allowed_types.join(", "));
            return false;
        }    
    }
    // Make new questions and show them in tables
    var new_group = project.report.appendGroup();
    new_group.name = web_mode ? "Rebased Variable Sets" : "Rebased Questions";
    var new_var_names = [];
    selected_questions.forEach(function (q) {
    	var new_question = rebasePickAny(q, hide_inputs);
        new_var_names.push(new_question.name);
        if (!web_mode) {
            var t = new_group.appendTable();
            t.primary = new_question;
            t.cellStatistics = ['%', 'n', 'Base n'];
        }else { 
            var page = new_group.appendPage('Blank');
            page.name = new_question.name;
            var t = page.appendTable();
            t.primary = new_question;
            t.cellStatistics = ['%', 'n', 'Base n'];
        }
    });

    // More recent Q versions can point the user to the new items.
    if (fileFormatVersion() > 8.65)
        project.report.setSelectedRaw([new_group.subItems[0]]);
    
    if (web_mode)
        log(makeWordList(new_var_names) 
              + "Tables showing the rebased variable sets have been added to the bottom of the document.");

    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) {
    var variables = question.variables;
    var num_vars = variables.length;
	var last = variables[num_vars-1];
    var data_file = question.dataFile;

    // Formula for respondents with at least one answer
    var 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
    var rebased_vars = [];
    variables.forEach(function (variable) {
        var new_name = preventDuplicateVariableName(data_file, variable.name + "_r");
        var 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;
    });
    
    // Rename and hide the original question
    if (hide_inputs)
    	question.isHidden = true;

    // Set the question
    var 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')

    return new_q;
}


See also