Modify Whole Table or Plot - Always Show Sample Size

From Q
Jump to navigation Jump to search


This rule always displays whichever is appropriate of Base n, Column n and Row n on tables. Where these statistics are common within rows or columns, the statistics in the row or column headers. Otherwise, they are selected using Statistics - Cells. The statistics can only be taken off by removing the Rule.

Example

AlwaysShowSampleSize.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'); 

excludeRTables();

let n_rows = table.numberRows;
let n_columns = table.numberColumns;

form.setHeading("Always Show Sample Size"); 
form.setSummary("Always show sample size");
 
let description = form.newLabel("Display the relevant sample size depending on the type of data in the table.");
form.setInputControls([description]);
// Checking to see where column n is available
let use_column_n = false, use_row_n = false, use_base_n = false;
let blue_type = table.blueQuestion.questionType;
let blue_numeric_1d = blue_type == "Number" || blue_type == "Number - Multi";
let blue_numeric = blue_numeric_1d ||   blue_type  ==  "Number - Grid"  ;
let blue_2d = blue_type == "Pick One - Multi" || blue_type == "Pick Any - Grid" || blue_type  ==  "Number - Grid";
let blue_2d_crosstab = blue_2d && table.blue != "SUMMARY";
let brown_type = table.brownQuestion.questionType;
let brown_numeric_1d = brown_type == "Number" || brown_type == "Number - Multi" ;
let brown_2d = brown_type == "Pick One - Multi" || brown_type == "Pick Any - Grid" || brown_type  ==  "Number - Grid";

if (table.brown == "RAW DATA")
    form.ruleNotApplicable("this rule does not apply to RAW DATA tables"); 

if (n_rows < 2 || n_columns < 2)
    form.ruleNotApplicable("this rule is only applied on tables with 2 or more columns and rows");    
 
if ((blue_type ==  "Number - Grid" || blue_type ==  "Pick Any - Grid") && table.brownQuestion == "SUMMARY")
    use_base_n = true;
else if (blue_numeric_1d && !brown_numeric_1d)
    use_column_n = true;
else if (!blue_numeric_1d && brown_numeric_1d)
    use_row_n = true;
else{
    try {
        use_column_n = below_table.availableStatistics.indexOf('Column n') != -1;
        if (n_columns < 2)
            form.ruleNotApplicable("this rule is only applied on tables with 2 or more columns");    
    } catch (e) {
        use_column_n = false;
    }
    if (!use_column_n){  // Checking to see of Row n can be used.
        try {
            use_row_n = right_table.availableStatistics.indexOf('Row n') != -1;
        } catch (e) {
            use_row_n = !blue_numeric && brown_2d;  // As with numeric in both, row n may not appear+
        }
        if (!use_row_n){  // Checking to see of Row n can be used.
            try {
                use_base_n = table.availableStatistics.indexOf('Base n') != -1;
            } catch (e) {
                form.ruleNotApplicable("this rule requires that the data selected has sample size information (e.g., Row n, Column n or Base n)");    
            }
        }
    }
}


// Placing base in column labels
if (use_column_n || use_base_n) {
    let cell_stats =  table.availableStatistics.indexOf('Column n') != -1 ? table.get('Column n') : table.get('Base n');    
    let marginal_stats = use_column_n ? below_table.get('Column n') : cell_stats;    
    let column_labels = table.columnLabels;// Get the standard column labels.
    let marginal_transposed = marginal_stats[0].length == 1;
    let non_constant_base = false;
    for (let c = 0; c < n_columns; c++){
        let value = marginal_transposed ? marginal_stats[c][0] : marginal_stats[0][c];
        for (let r = 0; r < n_rows; r++) {
            if (cell_stats[r][c] != value) {
                non_constant_base = true;
                break;
            }
        }
        column_labels[c] = column_labels[c] + '\r\n' + (use_column_n ? 'Column n = ' : 'Base n = ') +value; // Write the results.
    }
    if (non_constant_base)
        addStatsWhereAvailable(["Column n"], table);
    else
        table.columnLabels = column_labels; 
} else if (use_row_n) { // Placing base in row labels.
    let cell_stats =  table.availableStatistics.indexOf('Row n') != -1 ? table.get('Row n') : table.get('Base n');
    let marginal_stats;
    if (brown_2d || blue_2d_crosstab)
        marginal_stats = cell_stats;
    else 
         marginal_stats = right_table.availableStatistics.indexOf('Row n') != -1  ? right_table.get('Row n') : cell_stats;    
    let marginal_transposed = marginal_stats.length == 1;
    let row_labels = table.rowLabels;
    let non_constant_base = false;
    for (let r = 0; r < n_rows; r++) {
        let value = marginal_transposed ? marginal_stats[0][r] : marginal_stats[r][0] ;
        for (let c = 0; c < n_columns; c++){
            if (cell_stats[r][c] != value) {
                non_constant_base = true;
                break;
            }
        }
        row_labels[r] = row_labels[r] +' Row n = ' + value;
    }
    if (non_constant_base)
         addStatsWhereAvailable(["Row n"], table)
    else
        table.rowLabels = row_labels; // Set the adjusted column labels.
}
 
 
 
// Adds the statistics named in stat_array to the table_object,
// which can be table, right_table, or below_table.
function addStatsWhereAvailable(stats_array, table_object) {
    let availables = table_object.availableStatistics;
    let currents = table_object.statistics;
    stats_array.forEach(function (stat) {
        if (availables.indexOf(stat) > -1 && currents.indexOf(stat) == -1)
            currents.push(stat);
    });
    table_object.statistics = currents;
}
 
// Returns true if Statistics - Below are available for this table.
// This function needs to be kept in the main body of the rule to ensure
// that below_table is included properly before the rule code is
// executed.
function belowTableExists() {
    let exists = true;
    try { 
        below_table.statistics;
    } catch (e) {
        exists = false;
    }
    return exists;
}
 
// Returns true if Statistics - Right are available for this table.
// This function needs to be kept in the main body of the rule to ensure
// that right_table is included properly before the rule code is
// executed.
function rightTableExists() {
    let exists = true;
    try { 
        right_table.statistics;
    } catch (e) {
        exists = false;
    }
    return exists;
}

See also