Business Intelligence Tools / Data Science & Analytics / Developers Corner

Making a QQ Plot in Spotfire with TERR

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!

1 thought on “Making a QQ Plot in Spotfire with TERR

  1. Dane Reply

    Would you mind sharing the custom expression? I can’t seem to find it in the previous posts. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *