[R] Weighted box or violin plots in Lattice?

David Winsemius dwinsemius at comcast.net
Mon May 9 14:12:18 CEST 2011


On May 9, 2011, at 12:35 AM, Deepayan Sarkar wrote:

> On Mon, May 9, 2011 at 2:20 AM, Thomas Lumley <tlumley at uw.edu> wrote:
>> On Mon, May 9, 2011 at 6:35 AM, Deepayan Sarkar
>> <deepayan.sarkar at gmail.com> wrote:
>>> On Sat, May 7, 2011 at 1:55 AM, Raphael Mazor  
>>> <raphaelm at sccwrp.org> wrote:
>>>> Is it possible to create weighted boxplots or violin plots in  
>>>> lattice?
>>>>
>>>> It seems that you can specify weights for panel.histogram() and
>>>> panel.densityplot(), but not for panel.bwplot or panel.violin().
>>>
>>> Not for panel.histogram() either.
>>>
>>> It's not immediately obvious how you would get weighted boxplots.
>>
>> The way the survey world does it is to get weighted quantiles and  
>> work
>> from them, as in survey:::svyboxplot().  The only tricky decision is
>> what to do with outliers, since I haven't been able to think of any
>> good way of indicating weights on them.
>
> So I guess with bwplot() one would have to write a version of
> boxplot.stats() that uses weighted quantiles, and then write a
> modified panel function.
>
> A crude approximation (if the weights don't vary too much) may be to
> scale and round the weights to integers, and then repeat each data
> point; e.g., newx <- rep(x, w), etc.

You could do a bit better if you just substituted wtd.quantile from  
Hmisc with an x and weights argument for the fivenum call in  
boxplot.stats.

require(Hmisc)
 > ?wtd.quantile
 > fivenum(x=1:10)
[1]  1.0  3.0  5.5  8.0 10.0
 > wtd.quantile(x=1:10)
    0%   25%   50%   75%  100%
  1.00  3.25  5.50  7.75 10.00
 > wtd.quantile( x = 1:10, weights = 1:10)
   0%  25%  50%  75% 100%
    1    5    7    9   10

  (Most people think the hinges are at the 25th and 75th percentiles  
anyway.)


>
> -Deepayan
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list