Create New Variables - Recode Net Promoter Score (NPS) Variable(s)

From Q
Jump to navigation Jump to search

This tool creates new copies of questionsvariable sets which contain likelihood to recommend scales, and recodes the data to display the Net Promoter Score (NPS). Likelihood to recommend survey questions are those which contain the words likely or likelihood in the label, or whose category labels contain the word likely, and which are 11-point scales. 10-point scales are also recoded, however NPS scores strictly only apply for 11-point scales.

Technical details

Recoding is conducted based on numbers found in the category labels. Those categories whose labels contain numbers less than or equal to 6 (detractors) will be recoded with a value of -100, those categories whose labels contain a 7 or 8 (neutral) will be recoded to a value of 0, and categories with labels 9 or 10 (promoters) will be recoded to a value of 100. This produces an Average that is the NPS score.

Categories which look like standard Don't Know questionnaire options will be set as missing and will not be included in the calculation of the NPS.

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 Questionnaire Functions');
includeWeb('QScript Value Attributes Functions');
includeWeb('QScript Utility Functions');
includeWeb('QScript Selection Functions');
includeWeb('QScript Functions to Generate Outputs');

recodeNPS()

function recodeNPS() {
    const allowed_types = ["Pick One", "Pick One - Multi"];
    const is_displayr = inDisplayr();
    
    const user_selections = getAllUserSelections();
    let selected_questions = user_selections.selected_questions;
    if (selected_questions.length > 0) {
        let sorted_selection = splitArrayIntoApplicableAndNotApplicable(selected_questions, 
            function (q) {
                return allowed_types.indexOf(q.questionType) != -1 && isLikelihoodScale(q, false);
            });
        selected_questions = sorted_selection.applicable;
        let not_applicable_questions = sorted_selection.notApplicable;

        if (not_applicable_questions.length > 0) {
            log(correctTerminology("The following variable sets can not be used because they do not have the appropriate format (e.g. contain an 11-point likelihood to recommend scale):"));
            log(not_applicable_questions.map(function (q) { return q.name; }).join("\r\n"));
            return false;
        }
    } else if (!is_displayr) { //Only prompt in Q
        let selected_datafiles = dataFileSelection();
        let candidate_questions = getAllQuestionsByTypes(selected_datafiles, allowed_types);
        candidate_questions = candidate_questions.filter(function (q) { return isLikelihoodScale(q, true); } );
     
        if (candidate_questions.length == 0) {
            log("No 'likelihood to recommend' questions found.")
            return false;
        }

        selected_questions = selectManyQuestions("The following questions look like 'likelihood to recommend' questions. Please select the ones you want to create new NPS variables for.", candidate_questions).questions;
     
        if (selected_questions.length == 0) {
            log("No questions selected.")
            return false;
        }
    } else { //In Displayr, present log with instructions
        log("Please select one or more Nominal/Ordinal or Nominal/Ordinal - Multi variable sets with 11-point 'likelihood to recommend' scales.");
        return false;
    }
    if (!areQuestionsValidAndNonEmpty(selected_questions))
        return false;

    let nps_questions = [];
    for (let j = 0; j < selected_questions.length; j++) {
        let question = selected_questions[j];
        let new_q_type;
        if (question.questionType == "Pick One")
            new_q_type = "Number";
        else
            new_q_type = "Number - Multi";

        let new_q = question.duplicate(preventDuplicateQuestionName(question.dataFile, question.name + " - NPS"));
        new_q.questionType = new_q_type;
        npsRecode(new_q);
        nps_questions.push(new_q);
        let v = new_q.variables;
        if (v.length === 1)
            v[0].label = new_q.name;
    }
    moveQuestionsToHoverButtonIfShown(nps_questions);

    if (!is_displayr) {
        let new_group = generateGroupOfSummaryTables("NPS Variables", nps_questions);
        new_group.subItems.forEach(function (item) {
            if (item.type == "Table") {
                let trans = item.translations;
                trans.set("Average", "NPS");
            }
        });
    }
    return true;
}

function npsRecode(question) {

    // check for DK labels and set as missing
    let unique_values = question.uniqueValues;
    let value_attributes = question.valueAttributes;
    unique_values.forEach(function (x) { 
        if (isDontKnow(value_attributes.getLabel(x)))
            setIsMissingDataForVariablesInQuestion(question, x, true);
    });

    let non_missing_labels = nonMissingValueLabels(question);
    let quantified_labels = non_missing_labels.map(quantify);
 
    // If the first or last label can't be quantified by the usual means
    // (ie because it doesn't have a number and is just something like
    // "Not very likely") then work out the correct value by looking at the
    // adjacent value.
    if (isNaN(quantified_labels[0]))
        quantified_labels[0] = quantified_labels[1] - 1;
    if (isNaN(quantified_labels[quantified_labels.length - 1]))
        quantified_labels[quantified_labels.length -1 ] = quantified_labels[quantified_labels.length - 2] + 1;
 
    let non_missing_source_values = nonMissingSourceValues(question);
    
    let source_value;
    let new_value;
    for (let j = 0; j < non_missing_labels.length; j++) {
        source_value = non_missing_source_values[j];
        if (quantified_labels[j] <= 6)
            new_value = -100;
        else if (quantified_labels[j] > 8)
            new_value = 100;
        else
            new_value = 0;
        setValueForVariablesInQuestion(question, source_value, new_value);
    }
}
 
function nonMissingSourceValues(question) {
    let unique_values = question.uniqueValues;
    let value_attributes = question.valueAttributes;
    return unique_values.filter(function (v) {return !value_attributes.getIsMissingData(v); });
}

See also