[Rd] NAs and weighted.mean

McGehee, Robert Robert.McGehee at geodecapital.com
Thu Jun 24 01:38:44 CEST 2010

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. 

More information about the R-devel mailing list