Weight - Apply Weight to Column

From Q
Jump to navigation Jump to search

This rule applies a weight to a single column of a table. This is achieved by generating a new table, applying the selected weight to that table, and then copying the column of weighted numbers into the original table.

Technical details

The significance testing results for the weighted columns are taken from the weighted table and no attempt is made to reconcile tests between the weighted and unweighted portions of the table.

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.


  • 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).


You can find a simpler version of this code, which does not contain the controls,here.

includeWeb('Table JavaScript Utility Functions');
includeWeb('JavaScript Array Functions');
includeWeb('QScript Utility Functions');


// Specify the weights for individual columns of a table
// Note that the first column is column 0, the second 1, etc.
let data_file = table.blueQuestion.dataFile
let col_labels = table.columnLabels;
if (col_labels == null)
    form.ruleNotApplicable('table has a single column. Use the Weight drop-down below the table');
if (arrayHasDuplicateElements(col_labels))
    col_labels = enumerateDuplicatesInStringArray(col_labels, '(', ')');
let variables = data_file.variables;
let weight_variables = variables.filter(function(v) { return v.question.isWeight; });
let weight_names = weight_variables.map(function(v) { return v.name; });
let weight_labels = weight_variables.map(function(v) { return v.label; });

if (weight_variables.length < 1)
    form.ruleNotApplicable('no weight variables have been found');
if (!col_labels)
    form.ruleNotApplicable('the table only has a single column');

// Set up controls for user input.
form.setHeading('Apply Weight to Column');
let label_column = form.newLabel('Column to weight:');
let combobox_column = form.newComboBox('column', col_labels);
let label_weight = form.newLabel('Weight:');
let combobox_weight = form.newComboBox('weight', weight_labels);
combobox_weight.lineBreakAfter = true;
let description = form.newLabel('This rule applies a weight to all the specified columns within a column span of a table.');
description.lineBreakAfter = true;
form.setInputControls([description, label_column, combobox_column, label_weight, combobox_weight]);

// Prevent Statistics - Right (all versions of Q) and Statistics - Below (in older versions where the weighted stats can't be computed)
if (belowTableExists())
    if (fileFormatVersion() <= 8.41 && below_table.statistics.length > 0)
        table.suppressOutput('This table cannot be displayed because Statistics - Below have been selected for this ' +
                             'table, and this is not compatible with this Rule.  Either remove the Statistics - ' +
                             'Below selection (right-click on the table) or remove the Rule.');
if (rightTableExists())
    if (right_table.statistics.length > 0)
        table.suppressOutput('This table cannot be displayed because Statistics - Right have been selected for this ' +
                             'table, and this is not compatible with this Rule.  Either remove the Statistics - ' +
                             'Right selection (right-click on the table) or remove the Rule.');

let column = combobox_column.requireValue();
let selected_weight_label = combobox_weight.requireValue();
let weight = weight_names[weight_labels.indexOf(selected_weight_label)];

let rule_name = 'Weight column "' + column + '" by "' + selected_weight_label + '"';
let weighted_table = calculateTable(table.blue, table.brown, ['!UseQFilters'], weight);

// Copy the weighted statistics for the column to the current table
let column_index = col_labels.indexOf(column);
let stats = table.availableStatistics;
for (let stat = 0; stat < stats.length; stat++) {
    let weighted_values = weighted_table.get(stats[stat]);
    let main_values = table.get(stats[stat]);
    for (let row = 0; row < table.numberRows; row++)
        main_values[row][column_index] = weighted_values[row][column_index];
    // Set the altered values to the main table.
    table.set(stats[stat], main_values);

// Copy the Significance Testing from the weighted table
let cell_arrows = table.cellArrows;
let cell_font_colors = table.cellFontColors;
let cell_significance = table.cellSignificance;
let weight_cell_arrows = weighted_table.cellArrows;
let weight_cell_font_colors = weighted_table.cellFontColors;
let weight_cell_significance = weighted_table.cellSignificance;
for (let row = 0; row < table.numberRows; row++) {
    cell_arrows[row][column_index] = weight_cell_arrows[row][column_index];
    cell_font_colors[row][column_index] = weight_cell_font_colors[row][column_index];
    cell_significance[row][column_index] = weight_cell_significance[row][column_index];
table.cellArrows = cell_arrows;
table.cellFontColors = cell_font_colors;
table.cellSignificance = cell_significance;

// In newer versions of Q, also copy Statistics - Below
if (fileFormatVersion() > 8.41 && belowTableExists()) {
    let weighted_marginal_table = Q.calculateTable(table.blue, table.brown, ['!UseQFilters'], weight, 'Below');

    // Prepare marginal statistics so that they are all available
    weighted_marginal_table.availableStatistics.forEach(function (stat) {
        let temp_stats = weighted_marginal_table.get(stat);
        let current_stats = below_table.get(stat);
        // Copy stats depending on the orientation of the table that has been returned by table.get
        if (temp_stats.length == 1) {
            current_stats[0][column_index] = temp_stats[0][column_index];
        } else {
            current_stats[column_index][0] = temp_stats[column_index][0];
        below_table.set(stat, current_stats);

    // Significance testing for statistics - below
    // Copy the Significance Testing from the weighted table
    let weight_cell_arrows = weighted_marginal_table.cellArrows;
    let weight_cell_font_colors = weighted_marginal_table.cellFontColors;
    let weight_cell_significance = weighted_marginal_table.cellSignificance;

    let cell_arrows = below_table.cellArrows;
    let cell_font_colors = below_table.cellFontColors;
    let cell_significance = below_table.cellSignificance;

    if (cell_arrows.length == 1) {
        cell_arrows[0][column_index] = weight_cell_arrows[0][column_index];
        cell_font_colors[0][column_index] = weight_cell_font_colors[0][column_index];
        cell_significance[0][column_index] = weight_cell_significance[0][column_index];
    } else {
        cell_arrows[column_index] = weight_cell_arrows[column_index];
        cell_font_colors[column_index] = weight_cell_font_colors[column_index];
        cell_significance[column_index] = weight_cell_significance[column_index];

    below_table.cellArrows = cell_arrows;
    below_table.cellFontColors = cell_font_colors;
    below_table.cellSignificance = cell_significance;


let footers = table.extraFooters;
footers.push(column + ' weighted by ' + selected_weight_label);
table.extraFooters = footers;

// 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 {
    } catch (e) {
        exists = false;
    return exists;

function getWeightedStatisticsBelow() {
    if (fileFormatVersion() > 8.41) {
        let weighted_marginal_table = Q.calculateTable(blue_q, brown_q, filter_array, '!UseQWeight', margin);
        weighted_marginal_table.availableStatistics.forEach(function (stat) {
            let temp_stats = weighted_marginal_table.get(stat);
            filtered_stats[stat] = temp_stats.length == 1 ? temp_stats[0] : temp_stats.map(function (arr) { return arr[0]; });

// Force Q to calculate all of the available statistics in the table before continuing
function prepareAllTableStats(table) {
    if (table.availableStatistics.indexOf('Minimum') >= 0)

See also