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,
zero weights in weighted.mean
5 messages · Duncan Murdoch, Göran Broström, Sorkin, John
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. Duncan Murdoch
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,
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 at r-project.org> on behalf of G?ran Brostr?m <goran.brostrom at umu.se>
Sent: Wednesday, July 14, 2021 10:46 AM
To: Duncan Murdoch; r-help at r-project.org
Subject: Re: [R] zero weights in weighted.mean
Sent: Wednesday, July 14, 2021 10:46 AM
To: Duncan Murdoch; r-help at 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 at 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.
On 2021-07-14 19:43, Sorkin, John wrote:
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.
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
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 at r-project.org> on behalf of G?ran Brostr?m <goran.brostrom at umu.se> Sent: Wednesday, July 14, 2021 10:46 AM To: Duncan Murdoch; r-help at 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 at 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.