# Confidence interval for median

I have to find a 95% c.i. on the median and other percentiles. I don't know how to approach this. Any help appreciated. I mainly use R as a programming tool.

-

> x=faithful$waiting > bootmed=apply(matrix(sample(x,rep=TRUE,10^4*length(x)),nrow=10^4),1,median) > quantile(bootmed,c(.025,0.975)) 2.5% 97.5% 73.5 77  which gives a (73.5,77) confidence interval on the median. (Note: Corrected version, thanks to John. I used$10^3$in the nrow earlier, which led to the confusion!) - Seems suspiciously narrow to me. Using functions from library(boot) appears to confirm this: > boot.ci(boot(x,function(x,i) median(x[i]), R=1000)) Intervals : Level Normal Basic 95% (74.42, 78.22 ) (75.00, 78.49 ) Level Percentile BCa 95% (73.51, 77.00 ) (73.00, 77.00 ) – onestop Jan 15 '12 at 15:13 You are right, sorry about the mistake! – Xi'an Jan 15 '12 at 17:10 you're welcome Xi'an... As an aside, I always prefer to set the original N value in the matrix because that's a constant across various bootstrap sizes I might make. So, I would typically have said ncol = length(x). I find there's less chance for error that way. – John Jan 15 '12 at 23:44 Thank you for giving an example, it was very helpful. – dominic999 Jan 16 '12 at 22:41 Check out bootstrap resampling. Search R help for the boot function. Depending on your data with resampling you can estimate confidence intervals for just about anything. - Yes, bootstrap would be my first suggestion as well. – Xi'an Jan 15 '12 at 7:47 Agree. This is the best approach. Underused in the biomedical sciences, in my opinion. – pmgjones Jan 15 '12 at 13:41 Consider looking into the smoothed bootstrap for estimating population quantiles as the conventional boostrap seems to have problems in that case - references can be found in this pdf. If you were just interested in the theoretical Median, the Hodges-Lehman estimator can be used - as provided by, e.g., R's wilcox.test(..., conf.int=TRUE) function. – caracal Jan 15 '12 at 19:50 Another approach is based on quantiles of the binomial distribution. e.g.: > x=faithful$waiting