R-developers,
In version R 2.11.0, weighted.mean was changed such that:
weighted.mean(NA, na.rm=TRUE)
[1] 0
rather than NaN as in previous versions of R. I see a note in the NEWS
file indicates that weighted.mean was changed "so an infinite value with
zero weight does not force an NaN result."
In case the side effect of returning 0 rather than NaN in this case was
unintentional, I'd like to propose that this case be reverted such that
weighted.mean(NA, na.rm=TRUE) returns a NaN to be consistent with the
mean function. Otherwise perhaps a note should be added in the
documentation explaining this inconsistent behavior.
I believe this patch should fix the problem while preserving the
behavior in the NEWS file:
- w <- w/sum(w)
- sum((x*w)[w != 0])
+ sum((x*w)[w != 0])/sum(w)
Here's the full note from the NEWS file.
o The default method of weighted.mean(x, w) coerces 'w' to be
numeric (aka double); previously only integer weights were
coerced. Zero weights are handled specially so an infinite
value with zero weight does not force an NaN result.
Thanks, Robert
Robert McGehee, CFA
Geode Capital Management, LLC
One Post Office Square, 28th Floor | Boston, MA | 02109
Tel: 617/392-8396 Fax:617/476-6389
mailto:robert.mcgehee at geodecapital.com
This e-mail, and any attachments hereto, are intended for use by the
addressee(s) only and may contain information that is (i) confidential
information of Geode Capital Management, LLC and/or its affiliates,
and/or (ii) proprietary information of Geode Capital Management, LLC
and/or its affiliates. If you are not the intended recipient of this
e-mail, or if you have otherwise received this e-mail in error, please
immediately notify me by telephone (you may call collect), or by e-mail,
and please permanently delete the original, any print outs and any
copies of the foregoing. Any dissemination, distribution or copying of
this e-mail is strictly prohibited.
"RobMcG" == McGehee, Robert <Robert.McGehee at geodecapital.com>
on Wed, 23 Jun 2010 19:38:44 -0400 writes:
RobMcG> R-developers,
RobMcG> In version R 2.11.0, weighted.mean was changed such that:
>> weighted.mean(NA, na.rm=TRUE)
RobMcG> [1] 0
RobMcG> rather than NaN as in previous versions of R.
which versions?
Even in 2.10.0 it gave 0.
RobMcG> rather than NaN as in previous versions of R. I see a note in the NEWS
RobMcG> file indicates that weighted.mean was changed "so an infinite value with
RobMcG> zero weight does not force an NaN result."
RobMcG> In case the side effect of returning 0 rather than NaN in this case was
RobMcG> unintentional, I'd like to propose that this case be reverted such that
RobMcG> weighted.mean(NA, na.rm=TRUE) returns a NaN to be consistent with the
RobMcG> mean function.
I tend to agree with you.
Note that also, for such functions FUN
FUN(NA, na.rm=TRUE)
identical to
FUN( numeric(0) )
and for "mean-like" functions I agree it should return NaN (or NA, perhaps).
RobMcG> Otherwise perhaps a note should be added in the
RobMcG> documentation explaining this inconsistent
RobMcG> behavior.
RobMcG> I believe this patch should fix the problem while preserving the
RobMcG> behavior in the NEWS file:
RobMcG> - w <- w/sum(w)
RobMcG> - sum((x*w)[w != 0])
RobMcG> + sum((x*w)[w != 0])/sum(w)d
Yes, it seems so, and I will probably commit it, for R-devel
at first, and R-patched later after a while.
Martin Maechler, ETH Zurich
RobMcG> Here's the full note from the NEWS file.
RobMcG> o The default method of weighted.mean(x, w) coerces 'w' to be
RobMcG> numeric (aka double); previously only integer weights were
RobMcG> coerced. Zero weights are handled specially so an infinite
RobMcG> value with zero weight does not force an NaN result.
RobMcG> Thanks, Robert
............