Preliminary Project Setup - Identify Questions with Straight-lining/Flat-lining

From Q
Jump to: navigation, search

This QScript looks through a data file and identifies questions that contain straight-lining (also known as flat-lining, where a respondent chooses the same option for all items in a question).

Technical details

The Pick One - Multi questions in a project are examined, and new questions are created that show the extent of straight-lining. It assumes that there are no missing values (i.e., if a respondent has missing data, then they will not be classified as having flatlined, even if they gave the same answers to all the questions that they answered).

Three new questions are added to the project:

  • Straight Lining by Question - Shows the proportion of straight-lining respondents in each question.
  • Number of Questions with Straight Lining - Counts the number of questions that have been straight-lined by each respondent.
  • Always Straight Lines - A filter to select those respondents who have straight-lined all considered questions.

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 Utility Functions');
includeWeb('QScript Selection Functions');
 
if (!main()) {
    log("QScript cancelled.");
} else {
    log("QScript finished.");
}
 
function main() { 
    var flatline_count_expression = "";
    var flat_lining_variables = [];
 
var web_mode = (!!Q.isOnTheWeb && Q.isOnTheWeb());

    if (!web_mode){
        var data_file = requestOneDataFileFromProject(false, true);
        if (data_file == null)
            throw new Error("There are not data sets in your project.");
    }else 
        var data_file = selectOneDataFileFromSelectedQuestions();
 
    var candidate_questions = getAllQuestionsByTypes([data_file], ["Pick One - Multi"]);
    var num_questions = candidate_questions.length;
 
    if (num_questions == 0) {
        log("No Pick One - Multi questions found to check.");
        return false;
    }
 
    flatline_count_expression = ""; // Expression to count the number of questions straigh-lined by each respondent
    flat_lining_variables = [];
 
    var new_var;
    var question;
    var count = 1;
    for (var j = 0; j < num_questions; j++) {
        question = candidate_questions[j];
        if (question.variables.length > 1) {
            new_var = generateFlatlinerVariableForQuestion(candidate_questions[j], count);
            count ++;
        }
        else
            continue;
 
        if (flatline_count_expression.length == 0)
            flatline_count_expression += new_var.name;
        else
            flatline_count_expression +=  " + " + new_var.name;
        flat_lining_variables.push(new_var);
    }
 
    if (flat_lining_variables.length > 0) {
        // Question to show the number of straigh-liners by question
        var straight_lining_by_question = data_file.setQuestion(preventDuplicateQuestionName(data_file, "Straight Lining by Question"), "Pick Any", flat_lining_variables);
        straight_lining_by_question.isFilter = true;
     
        // Question to count the number of questions flat-lined by each respondent
        new_var = data_file.newJavaScriptVariable(flatline_count_expression , false, preventDuplicateVariableName(data_file, "nflatline"), "Number of Questions with Straight Lining" , null);
        new_var.variableType = "Categorical";
        var number_questions_straight_lining = new_var.question;
     
        // Filter to identify respondents who always flat-line
        var new_filter = data_file.newJavaScriptVariable(new_var.name + "  == " + flat_lining_variables.length, false, preventDuplicateVariableName(data_file, "flatliner"), "Always Straight Lines" , null);
        new_filter.variableType = "Categorical";
        new_filter.question.isFilter = true;
     
        log("QScript finished!\r\n");
        log("The following questions have been added to your project:\r\n");
        log(straight_lining_by_question.name + " - Shows the proportion of straight-lining respondents in each question.");
        log(new_var.question.name + " - Counts the number of questions that have been straight-lined by each respondent.");
        log(new_filter.question.name + " - A filter to select those respondents who have straight-lined all considered questions.\r\n");
        log("These questions have been added to the folder called 'Tables showing degree of straight-lining.'");
     
        var new_group = project.report.appendGroup();
        new_group.name = "Tables showing degree of straight-lining";
        if (web_mode) {
            var page = new_group.appendPage("Blank");
            page.name = straight_lining_by_question.name;
            var t = page.appendTable();
            var page = new_group.appendPage("Blank");
            page.name = number_questions_straight_lining.name;            
            var t2 = page.appendTable();
        }else {
            var t = new_group.appendTable();
            var t2 = new_group.appendTable();
        }
        t.primary = straight_lining_by_question;
        t2.primary = number_questions_straight_lining;
    } else {
        log("No straight-lining has been identified.");
    }

 
    return true;
}
 
function generateFlatlinerVariableForQuestion(question, count) {
    var variables = question.variables;
    var n_variables = variables.length;
    var data_file = question.dataFile;
    var expression = variables[0].name + " == " + variables[1].name;
    for (var v = 2; v < n_variables; v++)
        expression += " && " + variables[v - 1].name + " == " + variables[v].name;
    var new_var = data_file.newJavaScriptVariable(expression, false, preventDuplicateVariableName(data_file, "straightline"+count), "Straight Liners " + question.name, null);
    new_var.variableType = "Categorical";
    return new_var;
}


See also