Create New Variables - Case-Level Shares

From Q
Jump to navigation Jump to search

Create a new variable which computes shares from numeric data at the case (e.g., respondent) level This QScript computes shares from numeric data at the case (e.g., respondent) level. A simpler and often better method for computing shares is to use the share statistics available from Statistics - Cells.

Example

Technical details

  • Data with missing values will have shares of NaN for all variables.
  • Where data contains negative values, negative shares will be computed.
  • Generally the shares computed when showing the Average of the variables created using this QScript will differ to those obtained if selecting one of the share Statistics from Statistics - Cells, as the Average will show the average of the shares computed at the respondent (case level), whereas the shares computed using Statistics - Cells are created by summing up the data for each case and thus give greater weight to cases with larger values.

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.

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.

JavaScript

includeWeb("QScript Selection Functions");
includeWeb("QScript Functions to Generate Outputs");
includeWeb("QScript Utility Functions");

if (!main())
    log("QScript cancelled.");
else 
    conditionallyEmptyLog("QScript finished.");

function main() {
    var question = selectInputQuestions(["Numeric - Multi"], single = true);
    if (!question)
        return false;
    if (!areQuestionsValidAndNonEmpty(question))
        return false;
    
    var data_file = question.dataFile;
    if (question != null){    
        var variables = question.variables;

        // Computing the total
        var total_var = preventDuplicateVariableName(data_file, "total");

        var total = "var " + total_var + " = " + variables[0].name;
        for (var j = 1; j < variables.length; j++)
            total += " + " + variables[j].name;
        total += ";\r\n" + total_var + " /= 100;\r\n";

        var new_variables = [];
        var last_var = null;
        var prefix = makeid();
        for (var j = 0; j < variables.length; j++) {
            var variable = variables[j];
            var new_name = preventDuplicateVariableName(data_file, "shares" + prefix + "_" + (j + 1));
            var expression = total + variable.name + " / " + total_var;
            var new_var = data_file.newJavaScriptVariable(expression, false, new_name, variable.label, last_var, false);
            new_variables.push(new_var);
            last_var = new_var;
        }

        // Set question and create table
        var new_q_name = preventDuplicateQuestionName(data_file, question.name + " -  Shares (%)");
        var new_q = data_file.setQuestion(new_q_name, "Number - Multi", new_variables);
        insertAtHoverButtonIfShown(new_q);
        reportNewRQuestion(new_q, "Shares");
        return true;
    }
}

See also