[R] density with weights missing values
m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Tue Jul 13 09:09:56 CEST 2021
>>>>> Matthias Gondan
>>>>> on Mon, 12 Jul 2021 15:09:38 +0200 writes:
> Weighted mean behaves differently:
> • weight is excluded for missing x
> • no warning for sum(weights) != 1
>> weighted.mean(c(1, 2, 3, 4), weights=c(1, 1, 1, 1))
>  2.5
>> weighted.mean(c(1, 2, 3, NA), weights=c(1, 1, 1, 1))
>  NA
>> weighted.mean(c(1, 2, 3, NA), weights=c(1, 1, 1, 1), na.rm=TRUE)
>  2
I'm sure the 'weights' argument in weighted.mean() has been used
much more often than the one in density().
Hence, it's quite "probable statistically" :-) that the
weighted.mean() behavior in the NA case has been more rational
and thought through
So I agree with you, Matthias, that ideally density() should
behave differently here, probably entirely analogously to weighted.mean().
Still, Bert and others are right that there is no bug formally,
but something that possibly should be changed; even though it
breaks back compatibility for those cases, such case may be
very rare (I'm not sure I've ever used weights in density() but
I know I've used it very much all those 25 years ..).
contains good information about determining if something may be
a bug in R *and* tell you how to apply for an account on R's
bugzilla for reporting it formally.
I'm hereby encouraging you, Matthias, to do that and then in
your report mention both density() and weighted.mean(), i.e., a
cleaned up version of the union of your first 2 e-mails..
Thank you for thinking about this and concisely reporting it.
> Von: Richard O'Keefe
> Gesendet: Montag, 12. Juli 2021 13:18
> An: Matthias Gondan
> Betreff: Re: [R] density with weights missing values
> Does your copy of R say that the weights must add up to 1?
> ?density doesn't say that in mine. But it does check.
another small part to could be improved, indeed,
thank you, Richard.
ETH Zurich and R Core team
> On Mon, 12 Jul 2021 at 22:42, Matthias Gondan <matthias-gondan using gmx.de> wrote:
>> Dear R users,
>> This works as expected:
>> • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE))
>> This raises an error
>> • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1, 1)))
>> • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1, NA)))
More information about the R-help