Weighting - Apply a Weight to a Column

From Q
Jump to: navigation, 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.

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

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

table.requireNumericTable();
table.requireOriginalRowsColumns();
includeWeb('Table JavaScript Utility Functions');
includeWeb('JavaScript Array Functions');
 
// Specify the weights for individual columns of a table
// Note that the first column is column 0, the second 1, etc.
var data_file = table.brownQuestion.dataFile
var 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, '(', ')');
var variables = data_file.variables;
var weight_variables = variables.filter(function(v) { return v.question.isWeight; });
var weight_names = weight_variables.map(function(v) { return v.name; });
var 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('Weight a Column');
var label_column = form.newLabel('Column to weight:');
var combobox_column = form.newComboBox('column', col_labels);
combobox_column.setDefault(col_labels[0]); 
var label_weight = form.newLabel('Weight:');
var combobox_weight = form.newComboBox('weight', weight_labels);
combobox_weight.lineBreakAfter = true;
form.setInputControls([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.");
 
var column = combobox_column.requireValue();
var selected_weight_label = combobox_weight.requireValue();
var weight = weight_names[weight_labels.indexOf(selected_weight_label)];
 
var rule_name = 'Weight "' + column + '" by "' + selected_weight_label + '"';
form.setSummary(rule_name);
var weighted_table = calculateTable(table.blue, table.brown, ['!UseQFilters'], weight);
 
// Copy the weighted statistics for the column to the current table
prepareAllTableStats(table);
var column_index = col_labels.indexOf(column);
var stats = table.availableStatistics;
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_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
var cell_arrows = table.cellArrows;
var cell_font_colors = table.cellFontColors;
var cell_significance = table.cellSignificance;
var weight_cell_arrows = weighted_table.cellArrows;
var weight_cell_font_colors = weighted_table.cellFontColors;
var weight_cell_significance = weighted_table.cellSignificance;
for (var 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()) {
    var weighted_marginal_table = Q.calculateTable(table.blue, table.brown, ['!UseQFilters'], weight, 'Below');
    
    // Prepare marginal statistics so that they are all available
    prepareAllTableStats(below_table);
    weighted_marginal_table.availableStatistics.forEach(function (stat) {
        var temp_stats = weighted_marginal_table.get(stat);
        var 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
    var weight_cell_arrows = weighted_marginal_table.cellArrows;
    var weight_cell_font_colors = weighted_marginal_table.cellFontColors;
    var weight_cell_significance = weighted_marginal_table.cellSignificance;
 
    var cell_arrows = below_table.cellArrows;
    var cell_font_colors = below_table.cellFontColors;
    var 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;
 
}
 
 
var 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() {
    var exists = true;
    try { 
        right_table.statistics;
    } catch (e) {
        exists = false;
    }
    return exists;
}
 
function getWeightedStatisticsBelow() {
    if (fileFormatVersion() > 8.41) {
        var weighted_marginal_table = Q.calculateTable(blue_q, brown_q, filter_array, '!UseQWeight', margin);
        weighted_marginal_table.availableStatistics.forEach(function (stat) {
            var 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]; });
        });
    }
}

See also