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

From Q
Jump to: navigation, search

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

When you use this rule you can choose:

Minium Sample Size The cut-off value when determining whether a row or column will be hidden. All cells in the row must fall below this value.

Hide Rows/Columns Whether you want to hide rows, columns, or both.

Statistic for rows/columns Which table statistics to check when determining whether a row or column should be hidden.

Example

The table below shows a cross tab between an Aided awareness question and Gender, and the rule has been applied to remove rows and columns with sample size less than 40. In this case, many respondents were not show the brand Optus due to the questionnaire logic, resulting in small sample sizes (i.e. Column n values of 38 and 39 respectively). Hence Optus has been removed. In many cases, small sample sizes in the rows or columns will lead to small sample sizes for the NET, and in this example the NET has also been removed. The rule has an option to prevent the removal of the main NET row or column for cases where this is not desirable.

HideSmallSample.png

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");
form.setHeading("Hide Rows and Columns with Small Sample Sizes");

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);
let description = form.newLabel("Hide any rows and columns whose sample size is too small");
description.lineBreakAfter = true;

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 columns_tickbox = form.newCheckBox('dc', 'Hide Columns');
columns_tickbox.setDefault(true);
columns_tickbox.lineBreakAfter = true;
let hide_columns = columns_tickbox.getValue();
controls.push(columns_tickbox);

let column_label = form.newLabel("Statistic for columns:");
let stat_for_columns_box = form.newComboBox('cs', translated_sample_stats);
stat_for_columns_box.lineBreakAfter = true;
stat_for_columns_box.setDefault(translated_sample_stats[0]);


if (hide_columns) {
    controls.push(column_label);
    controls.push(stat_for_columns_box);
}

let rows_tickbox = form.newCheckBox('dr', 'Hide Rows')
rows_tickbox.setDefault(true);
rows_tickbox.lineBreakAfter = true;
let hide_rows = rows_tickbox.getValue();
controls.push(rows_tickbox);

let row_label = form.newLabel("Statisic 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]);

if (hide_rows) {
    controls.push(row_label);
    controls.push(stat_for_rows_box);
}


form.setInputControls(controls);

if (!hide_rows && !hide_columns) 
    form.ruleNotApplicable('you must hide either rows, columns, or both');

let summary_text = 'Remove '
+ (hide_rows && hide_columns ? 'rows and columns' : (hide_rows ? 'rows' : 'columns'))
+ ' where the sample size of all cells is less than ' + min_sample; 

form.setSummary(summary_text);


let removed_columns, removed_rows;
let footers = table.extraFooters;

if (hide_rows) {
    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);
    }

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

    // Remove empty rows
    if (rows_to_remove.length > 0) {
        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")
    }
}

if (hide_columns) {
    let col_stat = stat_for_columns_box.getValue();
    col_stat = sample_stats[translated_sample_stats.indexOf(col_stat)];
    let col_stat_values = table.get(col_stat);

    let columns_to_remove = [];

    for (let col = 0; col < table.numberColumns; col++) {
           if (allValuesBelowMinimum(col_stat_values.map(function (row_array) { return row_array[col]; }), min_sample))
                columns_to_remove.push(col);
    }

    removed_columns = table.columnLabels == null ? [] : columns_to_remove.map(function (x) { return table.columnLabels[x]; });

    // Remove empty columns
    if (columns_to_remove.length > 0) {
        for (let j = columns_to_remove.length - 1; j > -1; j--)
            deleteColumnComplete(columns_to_remove[j]);
        footers.push("Some columns with sample sizes less than " + min_sample + " have been removed")
    }
}

table.extraFooters = footers;

function allValuesBelowMinimum(array, minimum_value) {
    return array.every(function (x) { return x < minimum_value; });
}

See also