Weight - Tables with Weights for Each Time Period

From Q
Jump to navigation Jump to search

This QScript generates crosstabs where each column represents a different time period and each time period has a separate weight. It is best not to view these tables as charts - to make charts please see Weighting - Create Plots with Different Weights for Each Time Period.

Technical details

When you run the script you will be asked to specify:

  1. The weight variables. These must be contained within a Number - Multi question.
  2. The Significance level (alpha) for the significance testing for these tables. Results will be shown as significant when their p-values are less than or equal to this value. Note that this testing will NOT use any settings from the Statistical Assumptions tab in the Project Options or Table Options.
  3. The questions to show in the rows of the tables. Any Pick One - Multi, Pick Any - Grid, and Number - Grid questions will be flattened into a single column for the crosstab.

Table Creation Process

To generate a crosstab, the QScript makes a new Pick Any question in your project which has one category for each weight variable. This question is used in the Brown Drop-down Menu so that the categories appear as columns and there is one column for each weight. The name of the new question will be Time periods for: followed by the name of the question that contains the weights.

The table itself is constructed using Table JavaScript. For each column the script does the following:

  1. Generates a new table.
  2. Applies the weight for that column.
  3. Copies the relevant statistics from that table into the main crosstab.

Statistics Used

The JavaScript copies statistics from a SUMMARY table into a crosstab. As such, some statistics that are available in the SUMMARY are not available in the crosstab.

The statistics that are copied across from the SUMMARY tables which have different names on the crosstab are:

Name on Crosstab Name on SUMMARY
Column % %
Column n Base n
Column Population Population
% Column Share % Share

The Standard Error statistic from the SUMMARY table is copied into the Standard Error on the crosstab when viewing a Number or Number - Multi question, and it is copied into the Column Standard Error when viewing a Pick One or Pick Any question.

Statistics that retain their name from the SUMMARY tables (that is, are copied across using the same name) are:

If a statistic does not make sense for this table then you will be warned if you try to apply it to the table.

Statistics - Right and Statistics - Below are not available for tables constructed by this script.

Significance Testing

In the crosstabs that are generated by this QScript the significance test compares each cell with the cell in the previous column. In the typical case, where each column represents the average of two or more time periods, this means that each cell is compared with the previous rolling time period. The tests assume that the sample for each column is independent of the other columns.

Results that are significantly high according to the test used are marked with a '+' and results that are significantly low are marked with a '-'.

IMPORTANT: The testing used on these generated tables does NOT employ any of the settings that have been set in the Statistical Assumptions. Consequently, changing these settings will have no impact on the results displayed in the table. The significance level that is used in the tests is specified when the script is run, and if you want to use a different level then you should run the script again.


When the table shows proportions (that is, when the question selected for the Blue drop-down is a Pick One or Pick Any question) then the test used to compare between cells is the Independent Complex Samples Z-Test - Comparing Two Proportions. This test uses the proportions shown in the Column % on the crosstab and the Column Standard Error shown in the crosstab. These in turn correspond to results shown in the % and Standard Error on the SUMMARY tables that were used to construct the columns.


When the table shows averages (that is, when the question selected for the Blue drop-down is a Number or Number - Multi question) then the cells are compared using Independent Complex Samples t-Test - Comparing Two Means with the Bessel's correction. This test uses the Average, Standard Error, and Column n statistics that are displayed in the crosstab.

How to apply this QScript

  • Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
  • Click on the QScript when it appears in the QScripts and Rules section of the search results.


  • Select Automate > Browse Online Library.
  • Select this QScript from the list.

Customizing the QScript

This QScript is written in JavaScript and can be customized by copying and modifying the JavaScript.

Customizing QScripts in Q4.11 and more recent versions

  • Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
  • Hover your mouse over the QScript when it appears in the QScripts and Rules section of the search results.
  • Press Edit a Copy (bottom-left corner of the preview).
  • Modify the JavaScript (see QScripts for more detail on this).
  • Either:
    • Run the QScript, by pressing the blue triangle button.
    • Save the QScript and run it at a later time, using Automate > Run QScript (Macro) from File.

Customizing QScripts in older versions

  • Copy the JavaScript shown on this page.
  • Create a new text file, giving it a file extension of .QScript. See here for more information about how to do this.
  • Modify the JavaScript (see QScripts for more detail on this).
  • Run the file using Automate > Run QScript (Macro) from File.


// Create Tables – Different Weight for Each Time Period

// Please see this wiki article for an explanation of this script: http://wiki.q-researchsoftware.com/wiki/Create_Tables_%E2%80%93_Different_Weight_for_Each_Time_Period#

includeWeb('QScript Selection Functions');
includeWeb('QScript Utility Functions');
includeWeb('Table JavaScript Functions for Building New Tables');
includeWeb('QScript Table Functions');  // for addTableJavaScriptToItem

// Get data files
var selected_datafiles = dataFileSelection();

// Find all Number - Multi questions
var number_multi = getAllQuestionsByTypes(selected_datafiles, ["Number - Multi"]);

// Throw a message if there are no Number - Multi questions to use for weighting.
if (number_multi.length == 0)
    throw("No Number - Multi question could be found for the weighting. Please ensure the weight variables have been combined into a Number - Multi question and run the script again.")

// Ask the user to select the Number - Multi question that contains their weights
var weight_question = selectOneQuestion("Please select the data that contains the weight variables:", number_multi);

// Ask the user for the p-value threshold to set for the significance 
var p_cutoff = prompt("Enter the Significance level (i.e., alpha; results will be shown as significant when their p-values are less than or equal to this value):", 0.05);

// Find all questions that are candidates for the rows.
var relevant_questions = getAllQuestionsByTypes(selected_datafiles, ["Number", "Number - Multi", "Pick One", "Pick Any", "Pick One - Multi", "Pick Any - Grid", "Number - Grid"]);

// Ask the user to select the questions for their crosstabs
var selected_questions = selectManyQuestions("Select the data to place in the rows of your crosstabs:", relevant_questions, false).questions;

// Make flattened versions of any two-dimensional questions

// Generate a new dummy question for the crosstab
var dummy = generateDummyPickAnyQuestionForTimePeriods("Time periods for: " + weight_question.name, weight_question);

// Get the list of names of the weight variables
var weight_names = weight_question.variables.map(function (v) { return v.name; });

// Build the JavaScript expression for constructing the table
// Write the array of weight variable names
var weight_name_string = "var weight_names = [" +
    weight_names.map(function (v) { return '"' + v + '"'; }).join(", ") + "];\r\n";

// Main expression
var expression = "var buildTableWithDifferentWeightsForDifferentPeriods = " + buildTableWithDifferentWeightsForDifferentPeriods + "\r\n"
                     + weight_name_string
                     + "var p_cutoff = " + p_cutoff + ";\r\n"
                     + "var use_bessel_correction = true;\r\n"
                     + "var weight_q_name = \"" + weight_question.name + "\";\r\n"
                     + "var columns_q_name = \"" + dummy.name + "\";\r\n"
                     + "var high_symbol = \" + \";\r\n"
                     + "var low_symbol = \" - \";\r\n"
                     + "var for_plot = false;\r\n"                      
                     + "buildTableWithDifferentWeightsForDifferentPeriods(for_plot, p_cutoff, use_bessel_correction, weight_names, weight_q_name, columns_q_name, high_symbol, low_symbol);"

// Build the crosstabs
var new_group = project.report.appendGroup();
new_group.name = "Weighted Time Series Tables";
selected_questions.forEach(function (question) {
    var t = new_group.appendTable();
    t.primary = question;
    t.secondary = dummy;
    addTableJavaScriptToItem(t, expression);

log('Crosstabs with different weights for each period have been added to your report.');

// Generates a Pick Any question from the input weight question (which should be a Number - Multi)
// All values that are not 0, NaN or set as missing will be set to 'Selected';
function generateDummyPickAnyQuestionForTimePeriods(new_question_name, weight_question) {
    checkQuestionType(weight_question, "Number - Multi");
    // Make the new question
    var new_question = weight_question.duplicate(preventDuplicateQuestionName(weight_question.dataFile, new_question_name));
    new_question.questionType = "Pick Any";
    var v = new_question.variables;
    if (v.length === 1)
        v[0].label = new_question.name;

    // Ensure all values with non-zero values and non-missing values are "Selected";
    var values = new_question.uniqueValues;
    var attributes = new_question.valueAttributes;
    values.forEach(function (value) {
        if (value > 0 && !attributes.getIsMissingData(value))
            setCountThisValueForVariablesInQuestion(new_question, value, true);
    new_question.needsCheckValuesToCount = false;
    var net_rows = new_question.dataReduction.netRows;
    if (net_rows)
        net_rows.forEach(function (row, index) {
            new_question.dataReduction.hide(row - index);

    return new_question;

Prior to the 15th of December, 2015, this page was known as Tables - Different Weight for Each Time Period

See also