[Rd] NAs and weighted.mean

From: McGehee, Robert <Robert.McGehee_at_geodecapital.com>
Date: Wed, 23 Jun 2010 19:38:44 -0400

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

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

R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed 23 Jun 2010 - 23:41:02 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 Jun 2010 - 16:01:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive