Skip to content
Back to formatted view

Raw Message

Message-ID: <7f931c9e-8cd9-076a-1a92-29d95c216cf9@umu.se>
Date: 2021-07-14T14:46:53Z
From: Göran Broström
Subject: zero weights in weighted.mean
In-Reply-To: <2512e737-a04f-ec3e-ad63-b3a86553e116@gmail.com>

Den 2021-07-14 kl. 13:16, skrev Duncan Murdoch:
> On 14/07/2021 6:00 a.m., G?ran Brostr?m wrote:
>> I wonder about the last sentence in the Details section of the
>> documentation of 'weighted.mean':
>>
>> "However, zero weights _are_ handled specially and the corresponding ?x?
>> values are omitted from the sum."
>>
>> The return value of weighted.mean.default is
>>
>> sum((x * w)[w != 0])/sum(w)
>>
>> and indeed, it looks as if zero weights are getting special treatment,
>> but what is wrong with the alternative (equivalent?) expression
>>
>> sum(x * w) / sum(w)?
>>
>> Is it a good idea to remove zeros from a vector before applying 'sum' to
>> it? I don't think so. Anyway, the sentence in the documentation seems to
>> be uncalled for.
> 
> Inf*0 is not zero.? Setting weights to zero on infinite observations (or 
> NA, or NaN) will give different results in your two expressions.

Thanks, agreed.

G,