Delete Tables and Plots - Delete If Not Significant at Specified Level

From Q
Jump to navigation Jump to search

Deletes any of the selected tables and charts if they do not contain results that are significant at a specified significance level. This QScript deletes any of the selected tables and charts if they do not contain results that are significant at a specified significance level.

Technical details

  • Tables are deleted if they contain no cells with Corrected p values less than or equal to the specified value, excluding cells containing NET or SUM.
  • For versions of Q before Q 4.8, the user selects tables and charts from a listbox.

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 Selection Functions');

var stat_name = 'Corrected p';
var deleted_tables_names = [];
var deleted_plots_names = [];

try {
    var selected_items = selectedTablesAndPlots(project.report);
    var p_threshold = getThreshold();
    var is_cancelled = false;
}
catch(e) {
    var selected_items = [];
    var is_cancelled = true;
}

if (is_cancelled) {
    log('QScript cancelled.');
    log('');
}
else {
    for (var i = 0; i < selected_items.length; i++) {
        item = selected_items[i];
        var row_indices_without_net;
        // treat plots and tables separately
        if (item.type == 'Plot') {
            if (item.tertiary != null) {
                continue;
            }

            var table = project.report.appendTable();
            table.primary = item.primary;
            if (table.primary.dataReduction.rowLabels.length == 0) { // Question contains no data
                deleted_plots_names.push(item.name);
                item.deleteItem();
                table.deleteItem();
                continue;
            }
            if (item.secondary != null) {
                table.secondary = item.secondary;
                if (table.secondary.dataReduction != null && table.secondary.dataReduction.rowLabels.length == 0) { // Question contains no data
                    deleted_plots_names.push(item.name);
                    item.deleteItem();
                    table.deleteItem();
                    continue;
                }
            }
            if (item.weight != null) {
                table.weight = item.weight;
            }
            if (item.filters != null) {
                table.filters = item.filters;
            }
            
            var table_output = table.calculateOutput();
            row_indices_without_net = table_output.rowIndices(false);
            var stats = getStatisticsFromTable(table, [stat_name]);
            table.deleteItem();
        }
        else{
            var table = item;
            if (table.primary.dataReduction.rowLabels.length == 0) { // Question contains no data
                deleted_tables_names.push(item.name);
                item.deleteItem();
                continue;
            }
            if (item.secondary != null) {
                if (table.secondary.dataReduction != null && table.secondary.dataReduction.rowLabels.length == 0) { // Question contains no data
                    deleted_tables_names.push(item.name);
                    item.deleteItem();
                    continue;
                }
            }
            var table_output = table.calculateOutput();
            row_indices_without_net = table_output.rowIndices(false);
            var stats = getStatisticsFromTable(table, [stat_name]);
        }

        if (stats[stat_name] == null) {
            if (item.type == 'Table')
                deleted_tables_names.push(item.name);
            else
                deleted_plots_names.push(item.name);
            item.deleteItem();
            continue;
        }

        // remove NET and SUM rows
        var stats_without_net = [];
        row_indices_without_net.forEach(function (index) {
            stats_without_net.push(stats[stat_name][index]);
        });

        var min_value = minWithReplacedNaN(stats_without_net, Number.POSITIVE_INFINITY);

        if (min_value > p_threshold) {
            if (item.type == 'Table') {
                deleted_tables_names.push(item.name);
            }
            else {
                deleted_plots_names.push(item.name);
            }
            item.deleteItem();
        }
    }

    if (deleted_tables_names.length > 0) {
        log('The following tables have been deleted:');
        for (i = 0; i < deleted_tables_names.length; i++) {
            log(deleted_tables_names[i]);
        }
    }
    else {
        log('No tables have been deleted.');
    }
    log('');

    if (deleted_plots_names.length > 0) {
        log('The following plots have been deleted:');
        for (i = 0; i < deleted_plots_names.length; i++) {
            log(deleted_plots_names[i]);
        }
    }
    else {
        log('No plots have been deleted.');
    }
}

function getThreshold()
{
    var sig_level = -1;
    while (sig_level <= 0 || sig_level >= 100) {
        sig_level = prompt("Please enter the desired significance level (e.g. 95 for significance at 95%):", 95);
    }
    return 1 - sig_level/100;
}

See also