Dear all, I found a strange result using R's weightedMedian function. Consider the following:> x <- c (0.2, 0.3, 0.5) > w <- c (1,1,2) > weightedMedian(x,w) > 0.3666In cases like above, when the weights are integers, one could argue that the weighted median should be the same as the standard median with the elements repeated according to their weights. This is trivially true for the mean. In the example above, we simply double the occurrence of the 0.5 entry> x1 <- c(0.2, 0.3, 0.5, 0.5) > median(x1) 0.4Does anyone know the answer to that inconsistency? It must have to do with the interpolated version. If you switch of the interpolation you get:> weightedMedian(x,w,interpolate=FALSE) > 0.4However, I prefer the interpolated version since it is continuous with respect to the weights. Is there a interpolated version of the weightedMedian which does not show this inconsistency? All the best, Oliver
Oliver Duerr <Oliver.Duerr <at> genedata.com> writes:> > Dear all, > I found a strange result using R's weightedMedian function.... I could not find weightedMedian on CRAN; I know there is such a function in some Java lib, googling gave a reference to limma (Linear Models for Microarray Data by Gordon Smyth and others). Best contact the authors about the subject. Dieter
weightedMedian is in the R.basic packag. To download see: See http://www.maths.lth.se/help/R/R.classes/#1.%20Introduction Best, Oliver
Oliver Duerr wrote:> Dear all, > I found a strange result using R's weightedMedian function. > Consider the following: > > >>x <- c (0.2, 0.3, 0.5) >>w <- c (1,1,2) >>weightedMedian(x,w) >>0.3666 > > > In cases like above, when the weights are integers, one could argue that > the weighted > median should be the same as the standard median with the elements > repeated according to their weights. This is trivially true for the mean. > In the example above, we simply double the occurrence of the 0.5 entry > > >>x1 <- c(0.2, 0.3, 0.5, 0.5) >>median(x1) 0.4 > > > Does anyone know the answer to that inconsistency? > It must have to do with the interpolated version. > If you switch of the interpolation you get: > >>weightedMedian(x,w,interpolate=FALSE) >>0.4 > > > However, I prefer the interpolated version since it is continuous with > respect to the weights. Is there a interpolated version of the > weightedMedian which does not show this inconsistency? > > > All the best, > OliverBy the way: > library(Hmisc) > wtd.quantile(x,w) 0% 25% 50% 75% 100% 0.200 0.275 0.400 0.500 0.500 Also see the type argument to wtd.quantile -- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University