In Displayr, you can create custom rules to add custom statistics or calculations to the table (using the statistics available). This article describes how to create a custom rule to show the upper and lower confidence intervals on crosstabs that use categorical variable sets (if using categorical data, click here). More specifically, this article goes from a basic crosstab with a numeric variable set...
...to a table that displays the upper and lower confidence intervals:
Requirements
- A crosstab created from variables in a data source (using drag and drop) where there is a Numeric variable set (Numeric, Numeric - Multi, or Numeric - Grid) selected in the Rows and a categorical variable set (Nominal or Binary-Multi) selected in the Columns drop-down menus.
Method
1. Select your table.
2. In Properties , go to Data > Rules > + > New Custom Rule.
3. Click Edit JavaScript and paste the code below into the editor that opens:
// Choose which stats to replace based on those available for numeric data
var upper_statistic_to_replace = "Maximum";
var lower_statistic_to_replace = "Minimum";
// Confirm table has numeric data
if (table.availableStatistics.indexOf("Average") == -1) {
form.ruleNotApplicable('it only applies to tables containing the Average in the Statistics - Cells');
}
// Confirm table has standard error statistic that is needed
if (table.availableStatistics.indexOf("Standard Error") == -1){
table.suppressOutput('Confidence intervals not computed - the table should have the Average and Standard Error statistic available.');
form.ruleNotApplicable('it only applies to tables where the Standard Error is available in the Statistics - Cells');
}
//confirm table has a categorical variable in the columns
if (["Pick One", "Pick Any"].indexOf(table.brownQuestion.questionType) == -1){
table.suppressOutput('Confidence intervals not computed - the table should have a Nominal or Binary-Multi variable set selected in columns.');
form.ruleNotApplicable('the table should have a Nominal or Binary-Multi variable set selected in columns.');
}
//add titles to rule
form.setSummary('Calculate confidence intervals on crosstabs of means');
form.setHeading('Calculate confidence intervals on crosstabs of means');
// Set up controls for user to input significance level
var label = form.newLabel('Significance level:');
var p_level = form.newNumericUpDown('Lower'); // create numeric control
p_level.setIncrement(0.01);
p_level.setDefault(0.05);
p_level.setMaximum(1);
p_level.setMinimum(0);
form.setInputControls([label, p_level]); //add controls to the settings shown
//get the value from the input form above to set as the cutoff variable
var p_cutoff = p_level.getValue();
// Get relevant table stats
var means = table.get("Average");
var standard_errors = table.get("Standard Error");
var ns = table.get('Column n');
var upper_values = table.get(upper_statistic_to_replace);
var lower_values = table.get(lower_statistic_to_replace);
// Do the calculations
// Work out quantile of Student's T Distribution
for (var row = 0; row < table.numberRows; row++) {
for (var col = 0; col < table.numberColumns; col++) {
var t_quantile = jStat.studentt.inv(p_cutoff / 2, ns[row][col]);
var increment = t_quantile * standard_errors[row][col];
lower_values[row][col] = means[row][col] - increment;
upper_values[row][col] = means[row][col] + increment;
}
}
//set the values of the statistics we're overwriting to the new values on the table
table.set(lower_statistic_to_replace, lower_values);
table.set(upper_statistic_to_replace, upper_values);
//see if statistics for upper and lower CI are showing on table and if not, add them
var stats = table.statistics;
if(stats.indexOf(upper_statistic_to_replace) == -1) stats.push(upper_statistic_to_replace);
if(stats.indexOf(lower_statistic_to_replace) == -1) stats.push(lower_statistic_to_replace);
table.statistics = stats;
//rename the statistics we're overwriting to upper and lower CI
form.setTranslation(upper_statistic_to_replace, "Upper Confidence Interval");
form.setTranslation(lower_statistic_to_replace, "Lower Confidence Interval");4. OPTIONAL: Change the statistics to replace from Minimum and Maximum at the top of the code, if desired.
5. Press the Play button.
6. Click Save.
7. Click Close.
8. Choose your Significance level. By default, this is 0.05 to match Displayr's default Overall Significance Level setting, but if you are testing at a different level, then change this number.
9. Press OK.
Additional details
- This Rule applies the formula from the Default Confidence Intervals section of Confidence Interval, which uses the quantiles of the Student's t-distribution. The rule uses the Average, Standard Error, and Column Sample Size statistics on your table.
- Numbers may differ slightly (5th decimal place) from equivalent intervals computed on SUMMARY tables in Q due to different algorithms used to compute the quantiles of the t-Distribution.
- This Rule uses the jStat JavaScript library to compute the quantiles of the Student's t-distribution.