From zach.harrington at gmail.com  Thu Jul 25 21:44:04 2013
From: zach.harrington at gmail.com (Zach Harrington)
Date: Thu, 25 Jul 2013 15:44:04 -0400
Subject: [Rd] What algorithm is R using to calculate mean?
Message-ID: <51F18004.3060509@gmail.com>

I am curious to know what algorithm R's mean function uses. Is there 
some reference to the numerical properties of this algorithm?

I found the following C code in summary.c:do_summary():
case REALSXP:
     PROTECT(ans = allocVector(REALSXP, 1));
     for (i = 0; i < n; i++) s += REAL(x)[i];
     s /= n;
     if(R_FINITE((double)s)) {
         for (i = 0; i < n; i++) t += (REAL(x)[i] - s);
         s += t/n;
     }
     REAL(ans)[0] = s;
     break;

It seems to do a straight up mean:
for (i = 0; i < n; i++) s += REAL(x)[i];
s /= n;

Then it adds what i assume is a numerical correction which seems to be 
the mean difference from the mean of the data:
for (i = 0; i < n; i++) t += (REAL(x)[i] - s);
s += t/n;

I haven't been able to track this algorithm down anywhere (mean is not a 
great search term).

Any help would be much appreciated,

Zach Harrington



[Rd] What algorithm is R using to calculate mean?
Zach Harrington