Modify Cell Content - Add Symbols to Significant Cells

From Q
Jump to navigation Jump to search

This rule adds symbols to the cells in the table, indicating significance levels (e.g., * for the 95% confidence level).

Example

Specify symbols corr p value.png

Technical details

  • This rule works by looking at the Corrected p values in the cells of the table and so takes into account the Multiple Comparison Correction setting for Cell Comparisons.
  • When running the rule, you will be presented with two input boxes. Completing both boxes will add a new pair of input boxes. When you have completed filling out all your p-value limits, click OK without filling out the final pair of input boxes. Ticking the box labeled Show as confidence will only change the table footer, and will show the p-values as percentage confidence levels.

Specify symbols corr p value inputs.png

  • It is not possible to align the numbers when this rule is applied (i.e., the numbers will move to the left to accommodate the symbols).
  • When exported, values with a symbol will be rounded in Excel tables. If exporting an Excel chart, the symbol will be removed, but the footer will remain.

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

// Add specified symbols to cells for specific p values.

includeWeb('Table JavaScript Utility Functions');

excludeRTables();

// Set form title
form.setHeading("Add Symbols to Significant Cells");
form.setSummary("Add symbols to significant cells");

let description = form.newLabel("Specify symbols to add to cells when the Corrected p falls below certain values");
description.lineBreakAfter = true;
// Define default values and dimension variables/arrays
let no_specified_value = 0;
let no_specified_symbol = "";
let controls = [description];
let last_index = 0;
let current_index = 0;
let specified_p = [];
let specified_s = [];

// Function to sort 2D array by value in a
// Thanks: http://stackoverflow.com/questions/16096872/how-to-sort-2-dimensional-array-by-column-value

function sortFunction(descending) {
    descending = typeof descending !== 'undefined' ? descending : true;

    return function(a, b) {
        if (a[0] === b[0]) {
            return 0;
        }
        else {
            return (a[0] < b[0]) ? (descending ? 1 : -1) : (descending ? -1 : 1);
        }
    }
}

// Add Show as confidence control
let show_as_confidence = form.newCheckBox("showasconfidence", "Show as confidence");
show_as_confidence.setDefault(false);
show_as_confidence.lineBreakAfter = true;
controls.push(show_as_confidence);
footer_show_as_confidence = show_as_confidence.getValue();

// Add controls to form
while (true) {
    // Create input field objects
    let specify_p_value = form.newNumberBox('pvalue' + current_index);
    let specify_symbol = form.newTextBox('symbol' + current_index);

    // Set input to default values
    specify_p_value.setDefault(no_specified_value);
    specify_symbol.setDefault(no_specified_symbol);
    specify_symbol.lineBreakAfter = true;

    // Add controls to form first time through
    controls.push(form.newLabel("Corrected p:"));
    controls.push(specify_p_value);
    controls.push(form.newLabel("Symbol:"));
    controls.push(specify_symbol);

    // Temporarily store entered values
    selected_p_value = specify_p_value.getValue();
    selected_symbol = specify_symbol.getValue();

    // Only add new form controls if the preceding has input
    if (selected_symbol == no_specified_symbol)
        break;
    // Check that input values are within permitted ranges
    else if (selected_p_value > 1 || selected_p_value < 0) {
		form.ruleNotApplicable("the Corrected p is greater than 0 or less than 1");
        break;
    }
    else {
        // Store input values in arrays
        specified_p.push(selected_p_value);
        specified_s.push(selected_symbol);

        // Track last index value
        last_index = current_index + 1;
    }
    // increment index
    current_index++;
}

form.setInputControls(controls);

// Only continue if inputs provided
// alert(specified_s[0]);
if (specified_s[0] == no_specified_symbol)
  form.ruleNotApplicable("you have not entered a symbol");

// ...and if there are ps to check
if (table.availableStatistics.indexOf('Corrected p') == -1)
  form.ruleNotApplicable("there are no p-values available on this table");

// Process input data
// Create 2D array of information
let merged_values_array = [];
for (j = 0; j < specified_p.length; j++)
    merged_values_array[j] = [specified_p[j],specified_s[j]];

// Sort 2D array ascending based on ps (first index position)
merged_values_array.sort(sortFunction(descending = false));

// Get array of ps from table
// Get table values for statistic and text
let table_p_values = table.get('Corrected p');
let cell_texts = table.cellText;

// For each cell...
for (rows = 0; rows < table.numberRows; rows++) {
    for (columns = 0; columns < table.numberColumns; columns++) {

        // ...and each specified p
        for (i = 0; i < specified_p.length; i++) {
            if (table_p_values[rows][columns] <= merged_values_array[i][0]) {
                cell_texts[rows][columns] = [merged_values_array[i][1]];
                break;
            }
        }
    }
}

// Show as confidence levels in footer
if (footer_show_as_confidence)
    merged_values_array.forEach(function (a) { a[0] = (1 - a[0]) * 100;});
    
merged_values_array.sort(sortFunction(descending = !footer_show_as_confidence));

// Create footer text
let final_footer_addition = " ";
let footers = table.extraFooters;
merged_values_array.forEach(function (a) {
    if (footer_show_as_confidence)
        footers.push(a[1] + " " + a[0] + "% confidence");
    else 
	   footers.push(a[1] + " p <=  " + a[0]);
});

table.extraFooters = footers;

// Update the table
table.cellText = cell_texts;

See also