[R] zero weights in weighted.mean
gor@n@bro@trom @end|ng |rom umu@@e
Wed Jul 14 20:26:08 CEST 2021
On 2021-07-14 19:43, Sorkin, John wrote:
> 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.
> my <- function(x, w) sum(x * w) / sum(w)
> x <- c(1, 1 / 0)
> w <- 1:0
> my(x, w)
> weighted.mean(x, w)
> 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
> 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?)
>>> 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.
> ______________________________________________ R-help using r-project.org
> mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help