Create New Variables - Case-Level Shares

From Q
Jump to: navigation, search

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

SharesNumber.PNG

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");

var web_mode = (!!Q.isOnTheWeb && Q.isOnTheWeb());
if (!web_mode){
    var data_file = requestOneDataFileFromProject();

    var candidate_questions = data_file.questions.filter(function (q) {    return q.questionType == "Number - Multi" });
    if (candidate_questions.length < 1) {
        log("Could not find any Number - Multi questions.");
        var question = null;
    }else{
        var question = selectOneQuestion("Select the question to be used in the computation of shares.", candidate_questions, true);
    }
} else {
    var allowed_types = ["Numeric - Multi"];
    var selected_questions = project.report.selectedQuestions();
    var sorted_selection = splitArrayIntoApplicableAndNotApplicable(selected_questions, function (q) { return allowed_types.indexOf(q.variableSetStructure) != -1 && !q.isBanner; });
    var question = sorted_selection.applicable;

    question = question[0]; // Only use the first selected Number - Multi question if multiple selected
    var not_applicable_questions = sorted_selection.notApplicable;	
    if (question == null){
        log("You must select a Number - Multi question from Data Sets to use this QScript.");
    }else
        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);
    var new_group = project.report.appendGroup();
    new_group.name = "Shares";
    var new_t = new_group.appendTable();
    new_t.primary = new_q;
    // More recent Q versions can point the user to the new items.
    if (fileFormatVersion() > 8.65)
        project.report.setSelectedRaw([new_group.subItems[0]]); 
    
    if (!web_mode)
        conditionallyEmptyLog("A Group has been added to the end of the project containing the new variables.");
    else
        log("A new variable set called '" + new_q_name + "' has been created and a summary table has been added to the bottom of your document.");
}

See also