# Comparing the quantiles of two groups

Comparing the quantiles of two groups provides information that is lost by simply looking at means or medians. This post shows how to do that.

Traditionally,  the comparison of two groups focuses on comparing means or medians.  But, as Wilcox (2012) explains, there are many more features of the distributions of two groups that we may compare in order to shed light on how the groups differ. An interesting approach is to estimate the difference between the quantiles of the two groups.  Wilcox (2012, pp. 138-150) shows us an approach that is based on the shift function. The procedure boils down to estimating the quantiles of both groups, and plotting the quantiles of the first group against the difference between the quantiles.

In order to aid in comparing the quantiles of the groups, I’ve created a function for R that can be used for plotting the comparison between the two groups. The functions uses the ggplot2 package and the WRS package (that can be found here: WRS: A package of R.R. Wilcox’ robust statistics functions version 0.24 from R-Forge (rdrr.io)) ; see also: Installation of WRS package (Wilcox’ Robust Statistics) | R-bloggers (r-bloggers.com).).

library(WRS)
library(ggplot2)

plotSband <- function(x, y, x.name = "Control") {
x <- sort(x[!is.na(x)])
y <- sort(y[!is.na(y)])
qhat <- 1:length(x)/length(x)
idx.y <- floor(qhat*length(y) + .5)
idx.y[idx.y <= 0] <- 1
idx.y[idx.y > length(y)] <- length(y)

delta <- y[idx.y] - x

cis <- WRS::sband(x, y, plotit=F)\$m[, c(2, 3)]

check.missing <- apply(cis, 2, function(x) sum(is.na(x)))
if (sum(check.missing == length(x)) > 1) {
stop("All CI limits equal to - or + Infinity")
}
ylims <- c(min(cis[!is.na(cis[,1]), 1]) - .50,
max(cis[!is.na(cis[,2]), 2]) + .50)

cis[is.na(cis[, 1]), 1] <- ylims*5
cis[is.na(cis[, 2]), 2] <- ylims*5

thePlot <- ggplot(mapping = aes(x)) +
xlab(x.name) +
geom_smooth(aes(x = x, y = delta), se = F, col="blue") +
ylab("Delta") +
geom_point(aes(x = quantile(x, c(.25, .50, .75)),
y = rep(ylims, 3)), pch=c(3, 2, 3), size=2) +
geom_ribbon(aes(ymin = cis[,1], ymax = cis[,2]), alpha=.20) +
coord_cartesian(ylim = ylims)
suppressMessages(print(thePlot))
}


Let’s look at an example. Figure 1 presents data from an experiment investigating the persuasive effect of narratives on intentions of adopting a healthy lifestyle (see for details Boeijinga, Hoeken, and Sanders (2017)). The plotted data are the differences in intention between the quantiles of a group of participants who read a narrative focusing on risk-perception (detailing the risks of unhealthy behavior) and a group of participants who read a narrative focusing on action-planning (here called the control group), focusing on how the healthy behavior may actually be implemented by the participant.