Create New Variables - Log

From Q
Jump to: navigation, search

This QScript aApplies the log transformation to the selected numeric variable 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 log (base e) by default. If a different log 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 checkDuplicateVariable(variable_name) {
	let all_variables = project.dataFiles.map(d => d.variables).flat();
	let variables = all_variables.filter(v => {
		return v.name === variable_name || v.label === variable_name;
	})
	return variables.length !== 1;
}

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

function onlyUnique(value, index, self) {
	return self.indexOf(value) === index;
}

printTypes = function(x, conjunction) {
	var comma_separated = x.slice(0, x.length - 1);
	if(typeof(conjunction) === "undefined" || !conjunction) {
		conjunction = " or ";
	}
	return comma_separated.join(", ") + conjunction + x[x.length - 1];
}

checkQuestionsNonPositive = function(questions) {
	return questions.some(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() {
	// Check datafile exists
	if (!requireDataFile()) {
		return false;
	}
	var is_displayr = (!!Q.isOnTheWeb && Q.isOnTheWeb());
	// If Q, get the user to select one data file when there is more than one.
	var structure_name = is_displayr ? "variable sets" : "questions";
	if (!is_displayr){
 		if(fileFormatVersion() < 13.05) {
 			log("This QScript is not supported in this version of Q. Please use release version 5.4.1.0 or later to use this QScript.");
 			return false;
 		}
		var data_file = requestOneDataFileFromProject();
		var allowed_types = ["Number", "Number - Multi", "Number - Grid"];
		var candidate_questions = getAllQuestionsByTypes([data_file], allowed_types);

		if (candidate_questions.length === 0) {
			log("No " + allowed_types[user_specified_type] + " questions found in the data file.");
			return false;
		}
		var selected_questions = selectManyQuestions("Select which Number questions to transform:", candidate_questions, true).questions;
	} else {
		var allowed_types = ["Numeric - Grid", "Numeric - Multi", "Numeric"];
		var selected_questions = project.report.selectedQuestions();
		// Check if user hasn't selected anything
		if (selected_questions.length == 0) {
			log("To compute the numeric log transform, you must select at least one variable or variable set with one of the following structures: "
				+ printTypes(allowed_types));
			return false;
		}
		var sorted_selection = splitArrayIntoApplicableAndNotApplicable(selected_questions, function (q) { return allowed_types.indexOf(q.variableSetStructure) != -1 && !q.isBanner; });
		selected_questions = sorted_selection.applicable;
	
		if (sorted_selection.notApplicable.length != 0){
			var question = sorted_selection.notApplicable[0];
			log("The selected variable sets must one of the types : " + printTypes(allowed_types) +
				". Other variable set types cannot be transformed since they are not numeric." + 
				" For example, the selected Variable Set : '" + question.name + "' is a " + question.variableSetStructure +
				" and the log transform is not possible.");
			return false;
		}
		var data_file = selected_questions[0].dataFile;
		// Make sure all questions are from the same data set
		if (!selected_questions.map(function (q) { return q.dataFile.name; }).every(function (type) { return type == data_file.name; })) {
			log("Variable sets are from different datasets and cannot be combined. Please select variable sets from a single dataset.");
			return false;
		}
	}
	var variables = getVariablesFromQuestions(selected_questions);
	var variable_labels = variables.map(x => getVariableOrQuestionLabel(x));
	var question_name = variable_labels.filter(onlyUnique).join(" + ");
	var new_question_name = preventDuplicateQuestionName(data_file, question_name);
	var last_variable = getLastVariable(variables);
	if(variables.length === 1) {
		var new_var_name = preventDuplicateVariableName(data_file, variables[0].name);
		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 new_var_name = new_question_name.replace(/[^a-zA-Z0-9_@\#\$\\]/g, '_').toLowerCase() + "_";
		new_var_name = randomVariableName(16, new_var_name);
		var expr_names = variables.map(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 = checkQuestionsNonPositive(selected_questions);
	var 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";

	try {
		var new_r_question = data_file.newRQuestion(expression, new_question_name, new_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(" & "));
		if(variables.length === 1 || selected_questions.length == 1) {
			new_r_question.questionType = selected_questions[0].questionType;
		}
	} catch (e) {
		log("The log transform could not be computed for this " + structure_name + " : " + e);
		return false;
	}
	
	if(give_warning && !is_displayr) {
		log(warning_message);
	}
	
	if(!is_displayr){
		var new_name = prompt("Enter a name for the new log transformed question:", new_r_question.name);
		if(new_name != new_r_question.name) {
			new_r_question.name = new_name;
		}
		var top_group_name = "Log transformed question";
		var new_group = generateGroupOfSummaryTables(top_group_name, [new_r_question]);
		// More recent Q versions can point the user to the new items.
		if (fileFormatVersion() > 8.65) {
			project.report.setSelectedRaw([new_group.subItems[0]]);
		} else {
			log("log transformed question named " + new_r_question.name + " has been added to the dataset " + data_file.name);
		}
	}
	return true;
}


See also