QScript Functions for Capping and Recoding Numeric Variables

From Q
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 capping or excluding
// high or low values. In this context, capping means
// setting all values higher (or lower) than a given
// value to that value.
// - cap: boolean. If false, high/low values will be set as missing
// - high: boolean. If true we are capping/recoding values higher
//   than given value. If false, we are capping/recoding values
//   lower than a given value.

function recodeOrCapSelectedData(cap, high) {
	includeWeb('QScript Selection Functions');
	includeWeb('QScript Functions to Generate Outputs');
	includeWeb('QScript Value Attributes Functions');

    const web_mode = inDisplayr();
    let allowed_types = ["Number", "Number - Multi", "Number - Grid"];
    let selected_questions;
    let invalid_questions;

    if (web_mode) {
        const displayr_fail_message = "Select one or more Numeric or Numeric - Multi variable sets "
            + "under Data Sets and run this option again.";
        const user_selections = getAllUserSelections();
        selected_questions = user_selections.selected_questions.concat(user_selections.questions_in_rows_of_selected_tables);
        if (selected_questions.length == 0) {
            log("No appropriate data is selected. " + displayr_fail_message);
            return false;
        }
        let sorted_selection = splitArrayIntoApplicableAndNotApplicable(selected_questions, 
            function(q) { return allowed_types.indexOf(q.questionType) > -1; });
        selected_questions = sorted_selection.applicable;
        invalid_questions = sorted_selection.notApplicable;
    } else {
        let selected_datafiles = dataFileSelection();
        let relevant_questions = getAllQuestionsByTypes(selected_datafiles, allowed_types);
        selected_questions = selectManyQuestions("Please select the questions to recode:", relevant_questions).questions;
    }




    // Recoding input
    let message = cap ? ("Recode all values " + (high ? "greater" : "less") + " than this value to this value (e.g., if you enter a "
        + "value of 10, all values " + (high ? "greater" : "less") + " than 10 will be recoded as 10):")
        : "Recode all values as missing data if they are " + (high ? "greater" : "less") + " than or equal to:";

    let value = prompt(message);

    // Recoding
    selected_questions.forEach(function (q) {
        if (cap) {
            if (high)
                recodeHighValues(q, value, value);
            else
                recodeLowValues(q, value, value);
        } else {
            if (high)
                setHighValuesToMissing(q, value);
            else
                setLowValuesToMissing(q, value);
        }
    });
        
    
    if (!web_mode) {
        generateGroupOfSummaryTables("Recoded questions", selected_questions);
        log('A group of tables showing the recoded questions has been added to your report.');
    } else if (invalid_questions.length > 0) {
        log("Some variable sets were not Numeric or Numeric - Multi and have not been changed: "
            + invalid_questions.map(q => q.name).join("\r\n"));
    }
}