[R] zero weights in weighted.mean
Sorkin, John
j@ork|n @end|ng |rom @om@um@ry|@nd@edu
Wed Jul 14 19:43:11 CEST 2021
Gentlemen,
At the risk of beating a dead horse, but in he spirit of learning more about R, aren't the two expressions functionally the same? One drops values where weight is zero. The other (in the case where we and infinity * 0, something one would not expect to see in data) also drops data as in R infinity * 0 = Nan. In either case the observation would be dropped. I am certain I am missing something, but I don't know what I am missing.
John
John David Sorkin M.D., Ph.D.
Professor of Medicine
University of Maryland School of Medicine
Associate Director for Biostatistics and Informatics Baltimore VA Medical Center
Geriatrics, Research, Education, and Clinical Center
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology, Geriatrics and Palliative Care
Senior Statistician
University of Maryland Center for Vascular Research
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)
________________________________________
From: R-help <r-help-bounces using r-project.org> on behalf of Göran Broström <goran.brostrom using umu.se>
Sent: Wednesday, July 14, 2021 10:46 AM
To: Duncan Murdoch; r-help using r-project.org
Subject: Re: [R] zero weights in weighted.mean
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,
______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=04%7C01%7CJSorkin%40som.umaryland.edu%7C3a3546f3bb4541fdc30808d946d6482b%7C717009a620de461a88940312a395cac9%7C0%7C0%7C637618709012695753%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=OE%2B97ntdhOx2x19hUx0wUFg9d%2BhMrsN8v5G%2BFHv69tA%3D&reserved=0
PLEASE do read the posting guide https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=04%7C01%7CJSorkin%40som.umaryland.edu%7C3a3546f3bb4541fdc30808d946d6482b%7C717009a620de461a88940312a395cac9%7C0%7C0%7C637618709012695753%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=A0GsBl75Pq3MpWmncmtBz31z%2FJybPNWKWx8sgCbhKJ4%3D&reserved=0
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list