Skip to content

zero weights in weighted.mean

5 messages · Duncan Murdoch, Göran Broström, Sorkin, John

#
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.

G,
#
On 14/07/2021 6:00 a.m., G?ran Brostr?m wrote:
Inf*0 is not zero.  Setting weights to zero on infinite observations (or 
NA, or NaN) will give different results in your two expressions.

Duncan Murdoch
#
Den 2021-07-14 kl. 13:16, skrev Duncan Murdoch:
Thanks, agreed.

G,
#
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)
#
On 2021-07-14 19:43, Sorkin, John wrote:
Try this:

 > my <- function(x, w) sum(x * w) / sum(w)

 > x <- c(1, 1 / 0)

 > w <- 1:0
 > my(x, w)

[1] NaN

 > weighted.mean(x, w)

[1] 1


See?

Best, G?ran
PLEASE do read the posting guide 
https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&amp;data=04%7C01%7CJSorkin%40som.umaryland.edu%7C3a3546f3bb4541fdc30808d946d6482b%7C717009a620de461a88940312a395cac9%7C0%7C0%7C637618709012695753%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=A0GsBl75Pq3MpWmncmtBz31z%2FJybPNWKWx8sgCbhKJ4%3D&amp;reserved=0