Applying Different Weights to Columns

From Q
Jump to navigation Jump to search

This example applies no weight to the first three columns of a table and then a weight to the final three.

This script will cause significance results to be displayed incorrectly. Please read Caveats for Table JavaScript before using this script.

Note: This code is outdated has been replaced by more advanced and maintained code in these two rules: Weight - Apply Weight to Column and Weight - Apply Weight to Column Span. Please use those rules instead. This code is intended only as a reference.

includeWeb('Table JavaScript Utility Functions');
// Specify the weights for individual columns of a table
// Note that the first column is column 0, the second 1, etc.
var weights_by_column = {
    0: null,
    1: null,
    2: null,
    3: 'ageWeight',
    4: 'ageWeight',
    5: 'ageWeight'};

// Get the table dimensions and data type
var num_rows = table.numberRows;
var num_cols = table.numberColumns;
var is_numeric = table.availableStatistics.indexOf("Average") > -1;
var is_categorical = table.availableStatistics.indexOf("Column %") > -1;

if (is_numeric) 
    prepareAllTableStats(table); // If table contains any numeric statistics, make sure all numeric stats are computed up front

// Prevent the user from displaying statistics that don't make sense on this table
var stats_to_prevent = ["p", "t-Statistic", "z-Statistic", "Column Comparisons", "Column Names", 
                                               "Columns Compared", "Index", "Total %", "% Row Responses", 
                                                "% Column Responses", "% Total Responses", ,
                                                "Not Duplicate", "Base Population", "Corrected p", "Multiple Comparison Adjustment",
                                                "Effective Base n", "Base n",
                                                "Row %", "Row n", "Row Population"];

if (is_categorical)
    stats_to_prevent.push("Standard Error"); // Don't show Standard error for categorical data as the 'Column Standard Error' is used instead

table.statistics.forEach(function (stat) {
    if (stats_to_prevent.indexOf(stat) > -1) {
            table.suppressOutput("It is not appropriate to show the " + stat + " on this constructed table. Remove it from the Statistics - Cells menu.");

// Prevent marginal statistics
if (belowTableExists())
    if (below_table.statistics.length > 0) {
        table.suppressOutput("Statistics - Below are not available for this constructed table. Remove any selections from the Statistics - Below menu.");

if (rightTableExists())
    if (right_table.statistics.length > 0) {
        table.suppressOutput("Statistics - Right are not available for this constructed table. Remove any selections from the Statistics - Right menu.");

// For each column calculate the weighted data and copy statistics into table
for (var column = 0; column < table.numberColumns; column++) {
    // Run the current table, with a different weight
    var weight = weights_by_column[column];
    var weighted_table = calculateTable(, table.brown,['!UseQFilters'], weight);
    // Copy the weighted statistics for the column to the current table .
    var stats = table.statistics;
    for (var stat = 0; stat < stats.length; stat++) {
        var weighted_values = weighted_table.get(stats[stat]);
        var main_values = table.get(stats[stat]);
        for (var row = 0; row < table.numberRows; row++)
            main_values[row][column] = weighted_values[row][column];
        // Set the altered values to the main table.
        table.set(stats[stat], main_values);

// Turn off stat testing
if (table.cellSignificance) {
    table.cellSignificance = Q.matrix(false, num_rows, num_cols);
    table.hypothesisTestingEnabled = false;

See also