Create New Variables - Log Transform Variable(s)

From Q
Jump to navigation Jump to search

This tool applies the logarithmic transformation to the selected questionsvariable sets.

Example

Example in the graphic shown below with the original selected numeric variable called 'Estimated profit to the industry' with the results of the log transformed.

Log Transform Example in Q Log Transform Example in Displayr

The log transform uses the natural logarithm (base e) by default. If a different logarithm base is required, you can modify the R code used to compute the transformation by selecting the individual variables in the Variables and Questions tab, right-clicking, selecting Edit R variable, and then modifying the R code window shown above.and modifying the R code in the R CODE window on the right.

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;
	}
}


checkQuestionsNonPositive = 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;
	});
}

logTransform()

function logTransform() {

	const is_displayr = inDisplayr();
    const structure_name = is_displayr ? "variable sets" : "questions";
    const allowed_types = ["Numeric", "Numeric - Multi", "Numeric - Grid", "Nominal",
			 "Ordinal", "Nominal - Multi", "Ordinal - Multi"];
    let selected_questions = selectInputQuestions(allowed_types);
    if (!selected_questions)
        return false;
    if (!areQuestionsValidAndNonEmpty(selected_questions))
        return false;

    let data_file = getDataFileFromQuestions(selected_questions);
	let variables = getVariablesFromQuestions(selected_questions);
	let variable_labels = variables.map(function(x){ return(getVariableOrQuestionLabel(x)); });
	let question_name = variable_labels.filter(onlyUnique).join(" + ");
	let new_question_name = preventDuplicateQuestionName(data_file, question_name);
	let last_variable = getLastVariable(variables);
	let temp_var_name = randomVariableName(16); // temporary name, random to (almost) guarantee uniqueness
	let expression, r_name, expr_name;
	if (variables.length === 1) {
		let var_name = variables[0].name;
		expr_name = checkDuplicateVariable(var_name) ? generateDisambiguatedVariableName(variables[0]) : stringToRName(var_name);
		r_name = "variable";
		expression = r_name + " <- " + expr_name + "\n";
	} else {
		let expr_names = variables.map(function(v){
			return checkDuplicateVariable(v.name) ? generateDisambiguatedVariableName(v) : stringToRName(v.name);
		});
		expr_name = [];
		for (let i = 0; i < variables.length; i += 1) {
			expr_name[i] = stringToRName(variable_labels[i]) + " = " +  expr_names[i];
		}
		r_name = structure_name.replace(" ", ".").slice(0, -1);
		let expr_prefix = r_name + ' <- data.frame(';
		let white_space = " ".repeat(expr_prefix.length);
		expression = expr_prefix + expr_name.join(",\n" + white_space) + ',\n' +
									white_space + 'check.names = FALSE)\n';
	}
        expression += r_name + " <- flipTransformations::AsNumeric(" + r_name + ", binary = FALSE)\n";
	let give_warning = checkQuestionsNonPositive(selected_questions);
	let warning_message = "Log transformation is only valid for positive values. Non-positive values (zero or smaller) 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 += "log(" + r_name + ", base = exp(1))\n" +
				  "# Modify base argument to change the type of log transformation \n" +
				  "# The code above gives the natural log with base e = exp(1) = 2.718282... \n" +
				  "# E.g. for log base ten, change base = exp(1) to base = 10\n" +
				  "# E.g. for log base two, set base = 2\n";

	let new_r_question;
	try {
		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, "log of " + question_name.filter(onlyUnique).join(" & "));
        insertAtHoverButtonIfShown(new_r_question);

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

	// Replace temporary variable names
	nameSequentialVariables(new_r_question.variables, "log.vals");
	
	if (give_warning && !is_displayr)
		log(warning_message);
    reportNewRQuestion(new_r_question, "Log transformed question");
	return true;
}

See also