Table Computations - Weighted Average of Each Column
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
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('Compute the weighted average of each column');
includeWeb('Table JavaScript Utility Functions');
excludeRTables();
// Set up controls for user input.
form.setHeading('Weighted Average of Each Column');
let label_statistic = form.newLabel('Statistic to average:');
let valid_statistics = table.statistics.filter(function (i) {return !isTextStatistic(i); });
let all_table_valid_stats = table.availableStatistics.filter(function (i) {return !isTextStatistic(i); });
let combo_box = form.newComboBox('statistic', valid_statistics);
combo_box.setDefault(valid_statistics[0]);
combo_box.lineBreakAfter = true;
let label_available = form.newLabel('Statistic to weight by:');
let valid_stats_translated = all_table_valid_stats.map(function(s) {
try {
return table.getTranslation(s);
} catch(e) {
return s;
}
});
let available_statistics = form.newComboBox('availablestatistics', valid_stats_translated);
let desc = form.newLabel('Adds a row to the bottom of the table showing the weighted average of the selected column statistic');
desc.lineBreakAfter = true;
form.setInputControls([desc, label_statistic, combo_box, label_available, available_statistics]);
let statistic = combo_box.getValue();
let weight_statistic = available_statistics.requireValue();
let untranslated_weight_statistic = all_table_valid_stats[valid_stats_translated.indexOf(weight_statistic)];
// Insert a new row to store the numbers.
let last_row = table.numberRows - 1;
insertRowAfterComplete(last_row, 'Column Average weighted by ' + weight_statistic);
// Get the values for statistic
let values = table.get(statistic);
let weight = table.get(untranslated_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.
let not_duplicates = table.get('Not Duplicate');
let pick_one_multi_vars_across_rows = table.blueQuestion.questionType == 'Pick One - Multi' && table.blueQuestion.dataReduction.transposed;
// For each column...
for (let column = 0; column < table.numberColumns; column++) {
// Keep track of the sum of averages, and the number of
// non-duplicate rows so far.
let sum = 0;
let base = 0;
// For each row...
for (let 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.
let 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);
See also
- User Input for Rules for technical information on Rules.
- Rule Online Library for other examples of Rules.
- Table JavaScript and Plot JavaScript for the JavaScript that is used to write custom rules.
- JavaScript for information about the JavaScript programming language.
Q Technical Reference
Q Technical Reference
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Updating and Automation > Automation Online Library
Q Technical Reference > Updating and Automation > JavaScript > Table JavaScript and Plot JavaScript
Rule Online Library