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

Read through a data file and identifies variable sets that contain 'straight-lining' (also known as 'flat-lining', where a respondent chooses the same option for all items in a variable set)

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.

• 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

• 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 Utility Functions");
includeWeb("QScript Selection Functions");

if (!main()) {
conditionallyEmptyLog("QScript cancelled.");
} else {
conditionallyEmptyLog("QScript finished.");
}

function main() {
var flatline_count_expression = "";
var flat_lining_variables = [];

const is_displayr = inDisplayr();
const data_file = requestOneDataFileFromProject(true);

var candidate_questions = getAllQuestionsByTypes([data_file], ["Pick One - Multi"]);
var num_questions = candidate_questions.length;

if (num_questions == 0) {
log(correctTerminology("No Pick One - Multi questions were found to check for straight-lining/flat-lining."));
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;
straight_lining_by_question.needsCheckValuesToCount = false;

// 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;

// In Q, the explanation of new data will be presented as a log.
// In Displayr, we will add Text to the pages to explain the new data.
var new_group = project.report.appendGroup();
new_group.name = "Tables showing degree of straight-lining";
if (is_displayr) {
var page = new_group.appendPage("ItemAndText");
page.name = straight_lining_by_question.name;
var t = page.appendTable();
t.primary = straight_lining_by_question;
project.report.setSelectedRaw([page]);
var title_slot = page.subItems.filter(function (x) { return x.name == "Title placeholder"; })[0];
var text_slot = page.subItems.filter(function (x) { return x.name == "Text placeholder"; })[0];
//var page_text = page.subItems.filter(function (item) { return item.type == "Text"; });
title_slot.text = page.name;
text_slot.text = "This table shows the proportion of straight-lining respondents for each Nominal - Multi or Ordinal - Multi variable set.";
text_slot.width = page.width/2 - 2* padding;
var page = new_group.appendPage("ItemAndText");
page.name = number_questions_straight_lining.name;
var t2 = page.appendTable();
t2.primary = number_questions_straight_lining;
//var page_text = page.subItems.filter(function (item) { return item.type == "Text"; });
var title_slot = page.subItems.filter(function (x) { return x.name == "Title placeholder"; })[0];
var text_slot = page.subItems.filter(function (x) { return x.name == "Text placeholder"; })[0];
title_slot.text = page.name;
text_slot.text = "This table shows the proportion of respondents who have not straight-lined (0), straight-lined once (1), straight-lined twice (2), and so on.";
text_slot.width = page.width/2 - 2* padding;
insertGroupAtHoverButtonIfUsed(new_group);
log("Tables have been added to show the degree of straight-lining.\r\n\r\nA filter called '" + new_filter.question.name
+ "'' has been added to select respondents who have straight-lined all considered variable sets. The filter"
+ " is available in the Data Editor where it can be used to identify and delete cases.");
} else {
log("The following data has been added:\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"
+ "The filter is available in the Data tab where it can be used to identify and delete cases.\r\n");
log("These questions have been added to the folder called 'Tables showing degree of straight-lining.'");
var t = new_group.appendTable();
t.primary = straight_lining_by_question;
var t2 = new_group.appendTable();
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;
}