Create New Variables - Square-Root Variable(s)

From Q
Jump to navigation Jump to search

Create new variable(s) by applying the square root transformation to the selected variable(s)

Example

Example in the graphic shown below with the original selected numeric variable called 'Minutes spent on home phone - weekdays' with the results of the square root transformed variable.

Square-root Transform Example in Q Square-root Transform Example in Displayr

If a different power transformation is required, the user can modify the R code used to compute the transformation by selecting the individual variables and selecting Edit R variable and modifying the R code window shown above.and modifying the R code in the R code window.

R code to change power

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 Utility Functions");
includeWeb("QScript Selection Functions");
includeWeb("QScript Functions to Generate Outputs");
includeWeb("QScript R Output Functions");

function getVariableOrQuestionLabel(variable) {
	if(/- Multi/.test(variable.question.variableSetStructure)) {
		return variable.question.name + " " + variable.label;
	} else {
		return variable.label;
	}
}

checkQuestionsNegative = function(questions) {
	return questions.some(function(question){
		var attributes = question.valueAttributes;
		var values = question.uniqueValues;
		var num_vals = values.length;
		for(var j = 0; j < num_vals; j++){
			if(attributes.getValue(values[j]) < 0){
				return true;
			}
		}
		return false;
	});
}

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

function main() {

    var allowed_types = ["Numeric - Grid", "Numeric - Multi", "Numeric"];
    var selected_questions = selectInputQuestions(allowed_types);
    if (!selected_questions)
        return false;
    if (!areQuestionsValidAndNonEmpty(selected_questions))
        return false;

	var is_displayr = (!!Q.isOnTheWeb && Q.isOnTheWeb());
	var structure_name = is_displayr ? "variable sets" : "questions";
    var data_file = getDataFileFromQuestions(selected_questions);
	var variables = getVariablesFromQuestions(selected_questions);
	var variable_labels = variables.map(function(x) {return(getVariableOrQuestionLabel(x))});
	var question_name = variable_labels.filter(onlyUnique).join(" + ");
	var new_question_name = preventDuplicateQuestionName(data_file, question_name);
	var last_variable = getLastVariable(variables);
	var temp_var_name = randomVariableName(16); // temporary name, random to (almost) guarantee uniqueness
	if(variables.length === 1) {
		var var_name = variables[0].name;
		var expr_name = checkDuplicateVariable(var_name) ? generateDisambiguatedVariableName(variables[0]) : stringToRName(var_name);
		var r_name = "variable";
		var expression = r_name + " <- " + expr_name + "\n";
	} else {
		var expr_names = variables.map(function(v) {
		return checkDuplicateVariable(v.name) ? generateDisambiguatedVariableName(v) : stringToRName(v.name);
		});
		var expr_name = [];
		for (i = 0; i < variables.length; i += 1) {
			expr_name[i] = stringToRName(variable_labels[i]) + " = " +  expr_names[i];
		}
		var r_name = structure_name.replace(" ", ".").slice(0, -1);
		var expr_prefix = r_name + ' <- data.frame(';
		var white_space = " ".repeat(expr_prefix.length);
		var expression = expr_prefix + expr_name.join(",\n" + white_space) + ',\n' +
									white_space + 'check.names = FALSE)\n';
	}
	var give_warning = checkQuestionsNegative(selected_questions);
	var warning_message = "The Square-root transformation is only valid for non-negative values. Negative values have been replaced with missing values";
	if(give_warning){
		expression += r_name + "[" + r_name + " < 0] <- NA\n";
		if(is_displayr) {
			expression += 'warning("' + warning_message + '")\n';
		}
	}
	expression += r_name + "^(1/2)\n" +
						"# If you want a different power transformation, change the index in the above code \n" +
						"# E.g. for a square transformation change (1/2) to 2\n" +
						"# E.g. for a cube root transformation change (1/2) to (1/3)";

	try {
		var new_r_question = data_file.newRQuestion(expression, new_question_name, temp_var_name, last_variable);
		question_name = variables.map(function(v) {
			return(/- Multi/.test(v.question.variableSetStructure) ? v.question.name : v.label);
		})
		new_r_question.name = preventDuplicateQuestionName(data_file, "Square-root of " + question_name.filter(onlyUnique).join(" & "));
		if(variables.length === 1 || selected_questions.length == 1)
			new_r_question.questionType = selected_questions[0].questionType;
        insertAtHoverButtonIfShown(new_r_question);

	} catch (e) {
		log("The Square-root transform could not be computed for this " + structure_name + " : " + e);
		return false;
	}

	// Replace temporary variable names
	nameSequentialVariables(new_r_question.variables, "sqrt.vals");

	if (!is_displayr && give_warning)
        log(warning_message);
    reportNewRQuestion(new_r_question, "Square-root transformed question");
	return true;
}

See also