QQ Plots are a standard visualization that compares the distribution of your data under study to the normal distribution. Since most statistical tests assume normality, the QQ Plot is an important diagnostic visualization during any analysis of uni-variate or multi-variate studies. We had a previous post that made a QQ Plot using custom expressions, and in this post we will show how to do it in TERR.

grouped-qq-plot

Calculating the data for the QQ Plot in TERR is advantageous because then it’s easier to factor your data – for instance by groups or levels.

For the TERR code, we are going to assume that your inputs to the data function are a “Column” which we call “data”. Your only output is a data table called “Result”. The magic R function is `qqnorm`.

# Get rid of na's
data = na.omit(Data);

qq <- qqnorm(data)
qqPlot <- as.data.frame(cbind(qq$x, qq$y)) 
colnames(qqPlot) <- c('Normal Quantiles','Sample Quantiles')

Result <- qqPlot;

This will create an output data table with two columns – ‘Normal Quantiles’ and ‘Sample Quantiles’.

Now, let’s make this a little bit more sophisticated. Let’s calculate the normal quantiles for groups. So, we add a new input called “Group”, which is a string column. We will make a dataframe called samples that we use to loop over the groups and calculate the qqnorm for each subgroup sample. The result gets appended to the Result table.

samples = na.omit(data.frame(group=Groups, x=Data))

Result <- NULL

for(group in unique(samples$group)) {
	data <- samples$x[ samples$group == group ]
	qq <- qqnorm(data)
	qqPlot <- as.data.frame(cbind(qq$x, qq$y)) 
	qqPlot$group = group
	colnames(qqPlot) <- c('Normal Quantiles','Sample Quantiles')
	
	if(is.null(Result)) { Result <- qqPlot }
	else { Result = rbind(Result, qqPlot) }
}

Good luck out there and REMEMBER TO CHECK YOUR DISTRIBUTIONS!

Written by Troy Ruths
You'll conquer the present suspiciously fast if you smell of the future....and stink of the past.