Computations - The Weighted Average of Each Column

From Q
Jump to: navigation, search

This rule adds an extra row to the bottom of the table which shows the weighted average of the statistics that are shown in the columns, where they are weighted by a user-selectable statistic. In a previous version of this rule it was fixed to Column n. Rows which are duplicates of other rows, for example the NET row, are not included in the computation of the average. If there is more than one statistic in the table, the user may select the statistic to use, which defaults to the primary statistic. The user must also select the weighting statistic. It is up to the user to ensure that weighting by the chosen statistic makes sense for their analysis.

Example

ColumnWeightedAverage.png

Technical details

Statistical tests are not performed on these cells. Averaging is not conducted on the statistics shown in Statistics - Right.

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

table.requireNumericTable();
form.setSummary('Computing the Weighted Average for each Column');
includeWeb('Table JavaScript Utility Functions');
 
// Set up controls for user input.
form.setHeading('Computing the Weighted Average for each Column');
var label_statistic = form.newLabel('Statistic to average:');
var valid_statistics = table.statistics.filter(function (i) {return i != "Column Names" && i != "Columns Compared" & i != "Column Comparisons";});
var combo_box = form.newComboBox('statistic', valid_statistics);
combo_box.setDefault(valid_statistics[0]);
combo_box.lineBreakAfter = true;
var label_available = form.newLabel('Statistic to weight by:');
var available_statistics = form.newComboBox('availablestatistics', table.availableStatistics);
form.setInputControls([label_statistic, combo_box, label_available, available_statistics]);
 
var statistic = combo_box.getValue();
var weight_statistic = available_statistics.requireValue();
 
// Insert a new row to store the numbers.
var last_row = table.numberRows - 1;
insertRowAfterComplete(last_row, 'Column Average weighted by ' + weight_statistic);
 
// Get the values for statistic
var values = table.get(statistic);
var weight = table.get(weight_statistic);
 
// Get the not duplicate markers for each cell. This
// indicates whether the cell has been copied from
// another cell, such as when NETs are created.
var not_duplicates = table.get('Not Duplicate');
var pick_one_multi_vars_across_rows = table.blueQuestion.questionType == "Pick One - Multi" && table.blueQuestion.dataReduction.transposed;
 
// For each column...
for (var column = 0; column < table.numberColumns; column++) {
    // Keep track of the sum of averages, and the number of
    // non-duplicate rows so far.
    var sum = 0;
    var base = 0;
    // For each row...
    for (var row = 0; row < table.numberRows; row++)
        // If this cell is not a duplicate...
        if (!isNaN(not_duplicates[row][column]) && (not_duplicates[row][column] || pick_one_multi_vars_across_rows) && !isNaN(values[row][column]) && weight[row][column] != 0&& !isNaN(weight[row][column])) {
            // Add its average value to this column's sum.
            sum  += values[row][column] * weight[row][column];
            base += weight[row][column];
        }
 
    // We now have a sum of all the averages in this column.
    // Compute the average.
    var average = sum / base;
 
    // Store the average in the new 'Column Average' row
    // we added above.
    values[table.numberRows - 1][column] = average;
}
 
// Use our new average values.
table.set(statistic, values);
 
// 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;
}

See also