Introduction
This article describes how to go from a crosstab which includes a numeric question...
...to a table that displays the upper and lower confidence intervals:
Requirements
This example may be used to compute confidence intervals for crosstabs where there is a numeric question (Number, Number - Multi, or Number - Grid) selected in the Blue drop-down menu, and a Pick One or Pick Any question selected in the Brown drop-down menu. The rule borrows statistics from the table (by default it takes over the Maximum and Minimum statistics), and renames them to Upper Confidence Interval and Lower Confidence Interval respectively.
Method
1. Select your table.
2. Go to Automate > Custom Rule.
3. Paste the below into the dialog:
// Choose which stats to replace.
var upper_statistic_to_replace;
var lower_statistic_to_replace;
// Numeric results
if (table.availableStatistics.indexOf("Average") > -1) {
var upper_statistic_to_replace = "Maximum";
var lower_statistic_to_replace = "Minimum";
} else {
form.ruleNotApplicable('it only applies to tables containing the Average in the Statistics - Cells');
}
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');
}
if (["Pick One", "Pick Any"].indexOf(table.brownQuestion.questionType) == -1){
table.suppressOutput('Confidence intervals not computed - the table should have a Pick One or Pick Any question selected in the Brown drop-down menu.');
form.ruleNotApplicable('the table should have a Pick One or Pick Any question selected in the Brown drop-down menu.');
}
// Set up controls for user input.
form.setSummary('Calculate confidence intervals on crosstabs of means');
form.setHeading('Calculate confidence intervals on crosstabs of means');
var label = form.newLabel('Significance level:');
var p_level = form.newNumericUpDown('Lower');
p_level.setIncrement(0.01);
p_level.setDefault(0.05);
p_level.setMaximum(1);
p_level.setMinimum(0);
form.setInputControls([label, p_level]);
// Work out quantile of Student's T Distribution
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
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;
}
}
table.set(lower_statistic_to_replace, lower_values);
table.set(upper_statistic_to_replace, upper_values);
form.setTranslation("Maximum", "Upper Confidence Interval");
form.setTranslation("Minimum", "Lower Confidence Interval");
3. OPTIONAL: Change the statistics to replace from Minimum and Maximum, if desired.
4. Press the blue Play button > Close.
5. Choose your Significance level. By default this is 0.05 to match Q's default Overall Significance Level setting, but if you are testing at a different level then change this number.
6. Press OK > OK.
Note the following:
- 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 n 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 quatiles of the Student's t-distribution.
Next
How to Calculate Confidence Intervals for Crosstabs with Categorical Questions