Weight - Plots with Weights for Each Time Period

This QScript generates charts where each different time period (i.e., column) has a separate weight.

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 charts. Any Pick One - Multi, Pick Any - Grid, and Number - Grid questions will be flattened into a single column the charts.

If using Q 4.7, the charts should be changed to Line charts in the Show Data as menu once the script is complete. It is best to avoid converting the charts created by this script into tables - to view tables use Weighting - Create Tables with Different Weights for Each Time Period instead.

Chart Creation Process

To generate a line chart, this script generates a new Pick Any question with one category for each time period in the weight question. The script first builds a table with the selected question in the Brown drop-down and the time-periods in the Blue drop-down. The statistics in each row of the table (i.e. for each time period) are obtained by

1. Generating a SUMMARY table for the question selected in the Brown drop-down.
2. Applying the appropriate weight for that period.
3. Copying the relevant statistics to the main table.

It is necessary for the time periods to be rows in the table, so they are shown on the X axis when the table is converted to a chart. Once the table is constructed it is converted into a chart, and the type of chart can be selected from the Show Data as drop-down menu.

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 chart are:

Name on Chart Name on SUMMARY
Row % %
Row n Base n
Row Population Population
% Row Share % Share

The Standard Error statistic from the SUMMARY table is copied into the Standard Error on the chart 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 chart then you will be warned if you try to select it from the Statistics menu.

Significance Testing

In the charts that are generated by this QScript the significance test compares each cell with the cell in the previous time period. The tests assume that the sample for each time period is independent of the other time periods. When viewed as a line chart each data point is compared with the data point to the left. If viewed as a table each cell is compared with the cell in the row above. Due to the way the table is converted into a chart, these are equivalent.

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.

Proportions

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 Row % on the chart and the Column Standard Error shown in the chart. These in turn correspond to results shown in the % and Standard Error on the SUMMARY tables that were used to construct the columns.

Averages

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 Row n statistics that are displayed on the chart.

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.

OR

• 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.

• 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

• 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.

JavaScript

// Create Plots – Different Weight for Each Time Period

// Please see this wiki article for an explanation of this script: http://wiki.q-researchsoftware.com/wiki/Create_Plots_%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 your plots:", relevant_questions, false).questions;

// Make flattened versions of any two-dimensional questions
flattenSelectedQuestions(selected_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 = true;\r\n"
+ "buildTableWithDifferentWeightsForDifferentPeriods(for_plot, p_cutoff, use_bessel_correction, weight_names, weight_q_name, columns_q_name, high_symbol, low_symbol);"

// Build the plots
var does_support_templates = fileFormatVersion() > 8.12;
var new_group = project.report.appendGroup();
new_group.name = "Weighted Time Series Plots";
selected_questions.forEach(function (question) {
var p;
if (does_support_templates)
p = new_group.appendPlot("Line plot", [dummy, question]);
else {
p = new_group.appendPlot("Line plot");
p.primary = dummy;
p.secondary = question;
}
if (question.questionType.indexOf('Number') > -1)
p.mainStatistics = ["Average"];
else
p.mainStatistics = ["Row %"];
});

var extra = '';
if (!does_support_templates)
extra = ' Please convert these to Line Plots using the Show Data As menu.';
log('Plots with different weights for each period have been added to your report.' + extra);

// 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);
});
else
new_question.dataReduction.hide("NET");

return new_question;
}


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