Modify Whole Table or Plot - Hide Rows with Small Sample Sizes

From Q
Jump to navigation Jump to search


This rule hides rows in the table in which all of the cells have a sample size that is smaller than a certain value.

See more details in [Whole Table or Plot - Hide Rows and Columns with Small Sample Sizes]

How to apply this rule

For the first time in a project

  • Select the table(s)/chart(s) that you wish to apply the rule to.
  • Start typing the name of the Rule into the Search features and data box in the top right of the Q window.
  • Click on the Rule when it appears in the QScripts and Rules section of the search results.

OR

  • Select Automate > Browse Online Library.
  • Choose this rule from the list.

Additional applications of the rule

  • Select a table or chart that has the rule and any table(s)/chart(s) that you wish to apply the rule to.
  • Click on the Rules tab (bottom-left of the table/chart).
  • Select the rule that you wish to apply.
  • Click on the Apply drop-down and choose your desired option.
  • Check New items to have it automatically applied to new items that you create. Use Edit > Project Options > Save as Template to create a new project template that automatically uses this rule.

Removing the rule

  • Select the table(s)/chart(s) that you wish to remove the rule from.
  • Press the Rules tab (bottom-right corner).
  • Press Apply next to the rule you wish to remove and choose the appropriate option.

How to modify the rule

  • Click on the Rules tab (bottom-left of the table/chart).
  • Select the rule that you wish to modify.
  • Click Edit Rule and make the desired changes. Alternatively, you can use the JavaScript below to make your own rule (see Customizing Rules).

JavaScript

includeWeb("Table JavaScript Utility Functions");
includeWeb("JavaScript Array Functions");

form.setHeading("Hide Rows with Small Sample Sizes");

let description = form.newLabel("Hide any rows whose sample size is too small");
description.lineBreakAfter = true;

let sample_stats = ["Column n", "Base n", "Row n",
    "Column Population", "Base Population",
    "Row Population"].filter(function (stat) { return table.availableStatistics.indexOf(stat) > -1; });

let translated_sample_stats = translateStats(sample_stats);

if (sample_stats.length < 1) {
    form.ruleNotApplicable('table does not have any sample size statistics available');
}

// Create the form
let min_size_label = form.newLabel('Minimum Sample Size:');
let numeric_up_down = form.newNumericUpDown('threshold');
numeric_up_down.setDefault(30);
numeric_up_down.setIncrement(1);
numeric_up_down.setMaximum(999999999);
numeric_up_down.lineBreakAfter = true;
let min_sample = numeric_up_down.requireValue();

let controls = [description, min_size_label, numeric_up_down];

let row_label = form.newLabel("Statistic for rows:");
let stat_for_rows_box = form.newComboBox('rs', translated_sample_stats);
stat_for_rows_box.lineBreakAfter = true;
stat_for_rows_box.setDefault(translated_sample_stats[0]);

controls.push(row_label);
controls.push(stat_for_rows_box);

form.setInputControls(controls);

let summary_text = 'Remove rows where the sample size of all cells is less than ' + min_sample;

form.setSummary(summary_text);

let removed_rows;
let footers = table.extraFooters;

let row_stat = stat_for_rows_box.getValue();
row_stat = sample_stats[translated_sample_stats.indexOf(row_stat)];
let row_stat_values = table.get(row_stat);

let rows_to_remove = [];

for (let row = 0; row < table.numberRows; row++) {
    if (allValuesBelowMinimum(row_stat_values[row], min_sample))
        rows_to_remove.push(row);
}

let row_labels = table.rowLabels;
removed_rows = rows_to_remove.map(function (x) { return row_labels[x]; });

// Remove empty rows
if (rows_to_remove.length > 0) {
    if (table.deleteRows !== undefined) {
        table.deleteRows(rows_to_remove);
    }
    else {
        for (let j = rows_to_remove.length - 1; j > -1; j--)
            deleteRowComplete(rows_to_remove[j]);
    }
    footers.push("Some rows with sample sizes less than " + min_sample + " have been removed");
}

table.extraFooters = footers;

See also