QScript Functions for Removing Dont Know Categories
Jump to navigation
Jump to search
This page is currently under construction, or it refers to features which are under development and not yet available for use.
This page is under construction. Its contents are only visible to developers!
// General purpose function for setting Dont Know categories as missing
// remove: boolean. If true, set DK categories as Missing Data
// If false, just recode DK categories to NaN.
// In Q, prompt the user to enter additional labels to identify as
// 'Don't know'. In Displayr do not prompt. (May be revised base on user
// feedback, but for now we want to avoid prompts.)
// If questions are identified to have more thab one 'Don't Know'
// category, the user will be alerted
function removeDKInSelection(remove) {
includeWeb('QScript Utility Functions');
includeWeb('QScript Value Attributes Functions');
includeWeb('QScript Questionnaire Functions');
includeWeb('QScript Selection Functions');
includeWeb('JavaScript Array Functions');
includeWeb('QScript Functions to Generate Outputs'); // For logQuestionList, generateGroupOfSummaryTables
const web_mode = inDisplayr();
const valid_types = ["Pick One", "Pick One - Multi", "Number", "Number - Multi"];
let selected_questions;
let extra_dk_strings;
let wrong_type, not_applicable_questions;
if (web_mode) { // On the web just take from what is selected.
const user_selections = getAllUserSelections();
selected_questions = user_selections.selected_questions.concat(user_selections.questions_in_rows_of_selected_tables);
// Split out any selected questions that are of the wrong type
let question_type_selection = splitArrayIntoApplicableAndNotApplicable(selected_questions, function (q) { return valid_types.indexOf(q.questionType) > -1; });
wrong_type = question_type_selection.notApplicable;
selected_questions = question_type_selection.applicable;
// Split out questions that don't have a 'Dont Know' option
let sorted_selection = splitArrayIntoApplicableAndNotApplicable(selected_questions, questionHasNonMissingDontKnowOption)
selected_questions = sorted_selection.applicable;
not_applicable_questions = sorted_selection.notApplicable;
extra_dk_strings = [];
} else {
// Prompt the user to enter additional labels for DK options
extra_dk_strings = promptForDKLabels();
let selected_datafiles = dataFileSelection();
// Get all Pick One and Pick One - Multi questions that look to have 'Dont Know' options
let relevant_questions = getDKQuestionsWithUserEnteredLabels(
(remove ? ["Pick One", "Pick One - Multi"] : valid_types), extra_dk_strings);
if (relevant_questions.length == 0) {
log("No appropriate questions found.");
return false;
}
// Ask the user which questions they want to change
selected_questions = selectManyQuestions("Select the questions to modify:", relevant_questions).questions;
}
// Set 'Dont Know' options in the selected questions as missing
let questions_to_check = []; // Questions which may potentially need to be checked.
let non_check_questions = [];
selected_questions.forEach(function (current_question) {
let current_value_attributes = current_question.valueAttributes;
let current_unique_values = current_question.uniqueValues;
let num_vals = current_unique_values.length;
let labels = valueLabels(current_question);
let change_counter = 0;
for (var k = 0; k < num_vals; k++) {
let v = current_unique_values[k];
let label = current_value_attributes.getLabel(v);
if (isDontKnow(label) || containsSubstring(label, extra_dk_strings)) {
change_counter++;
if (remove)
current_value_attributes.setIsMissingData(current_unique_values[k], true);
else
setValueForVariablesInQuestion(current_question, v, NaN);
}
}
if (change_counter > 1)
questions_to_check.push(current_question);
else
non_check_questions.push(current_question);
});
if (web_mode) {
if (questions_to_check.length > 0)
logQuestionList("Some variable sets were identified to have more than one Don't Know category and should be checked:", questions_to_check);
if (wrong_type.length > 0) {
if (selected_questions.length > 0)
log("\r\n")
logQuestionList("Could not remove \"Don't Know\" categories because data is the wrong type:", wrong_type);
}
if (not_applicable_questions.length > 0) {
if (selected_questions.length > 0 || wrong_type.length > 0)
log("\r\n");
logQuestionList("Could not find a \"Don't Know\" option:", not_applicable_questions);
}
} else {
let new_group_name = remove ? "Questions with 'Don't Know' options removed" : "Recoded Questions";
let new_group = generateGroupOfSummaryTables(new_group_name, non_check_questions);
log("Recoded questions have been added to a group in your Report: " + new_group_name);
if (questions_to_check.length > 0) {
let check_group_name = "Questions to check";
log("Some questions appear to have more than one Don't Know category and should be checked. These have been placed in the group: " + check_group_name);
generateSubgroupOfSummaryTables(check_group_name, new_group, questions_to_check);
}
}
return true;
}