The jitter {base} R function is useful for smoothed bootstrapping, but manual tuning of the jitter amount is often necessary. Should we change the jitter factor, select the alternative jitter amount estimator or directly specify the jitter amount? I thought of using order statistics for robust estimation of the jitter amount using the rangebased estimator. This robust automatic jitter is demonstrated for two smoothed bootstrap examples.
Robust automatic jitter
From the R documentation, if jitter amount = NULL (which is the default) then amount = factor * d/5 where d is the smallest difference between adjacent unique x values. This jitter amount is typically smaller than the alternative estimator examined in this post.
If jitter amount = 0 then amount = factor * (max(x) – min(x)) / 50. When the range (max(x) – min(x)) is large, the jitter amount is large. Knowing that the sample range is strongly affected by sample size, outliers and not a robust estimator of scale, I thought to use the interquartile range IQR(x) instead.
To investigate the relationship between the range and IQR(x), I assumed a normal distribution for x and simulated some data below. The sample range increases with the sample size and for smaller sample sizes the range is about three times IQR(x). Accordingly, the proposed robust jitter amount = 1*3*IQR(x)/50 ≈ IQR(x)/17 (with factor = 1).


Interquartile range (IQR) and range for the normal distribution. For smaller sample sizes, the range is about three times the IQR. 
Here’s the code for the above table:
res=matrix(nrow=99, ncol=4) # results matrix
print(paste(“n”,”IQR”, “Range”, “Range/IQR”)) # header
for (i in c(10,20,30,40,50,100)){ # sample sizes
for (j in 1:99){ # simulated data sample sizes
x=rnorm(i)
res[j,1]=I # sample size
res[j,2]=IQR(x) # interquartile range
res[j,3]=max(x)min(x) # range
res[j,4]=(max(x)min(x))/IQR(x) # ratio of above
}
# print results for sample size i
print(paste(min(res[,1]), round(mean(res[,2]),1), round(mean(res[,3]),1), round(mean(res[,4]),1)))
}
Two examples
In the two examples below, the objective was to estimate the shift in location for some paired data that were strongly skewed and included outliers. I used the median paired difference as a robust estimator for the shift in location and bootstrapping to estimate the confidence interval for the median paired difference.
For small samples, the bootstrap distribution for the median often appears with “holes” in it because the median of a bootstrap resample is usually one of the few observations in the middle of the original sample. Adding a small amount of jitter to each resample helps to fill the “holes”. Adding too much jitter results in wide bootstrap distributions. The examples below show that the proposed automatic jitter is robust against outliers and offers a reasonable compromise between smoothing and detail.
Here’s some of the R code for the above plots:
## packages
library(MASS) # for truehist
library(boot) # for bootstrapping
## median functions for boot
# median difference function
dif.median1=function(data, i) {median(data$x1[i]data$x2[i])}
# median amount=NULL jittered difference function for boot
# this is the R default
dif.median2=function(data,i) {median(jitter(data$x1[i]data$x2[i]))}
# median amount=0 jittered difference function for boot
# this is the S default
dif.median3=function(data,i) {median(jitter(data$x1[i]data$x2[i], amount=0))}
# median robust jittered difference function for boot
dif.median4=function(data,i) {median(jitter(data$x1[i]data$x2[i], amount=myamount))}
## bootstrap median difference
# replace dif.median1 with any of the above median functions
dif.boot=boot(mydata,dif.median1,R=999)
mean(dif.boot$t) # bootstrap estimate, should be close to the median difference
boot.ci(dif.boot) # bootstrap confidence intervals
truehist(dif.boot$t, nbins=”FD”)
abline(v=median(dif.boot$t), lty=”dashed”)