The Art of R Programming

(WallPaper) #1

That’s even more compact than our first version. But it’s a lot harder to
read, and for reasons we will explore soon, it may not be so fast. Let’s check.



system.time(predc(y,1000))
user system elapsed
3.872 0.016 3.945



Well, our second version remains the champion so far. This actually
should be expected, as a look at the source code shows. Typing the follow-
ing shows the source for that function:



filter



This reveals (not shown here) thatfilter1()is called. The latter is writ-
ten in C, which should give us some speedup, but it still suffers from the
duplicate computation problem—hence the slowness.
So, let’s write our own C code.


#include <R.h>


void predd(intx, intn, intk, doubleerrrate)
{
int nval =n, kval =k, nk = nval - kval, i;
int sm = 0; // moving sum
int errs = 0; // error count
int pred; // predicted value
double k2 = kval/2.0;
// initialize by computing the initial window
for (i = 0; i < kval; i++) sm += x[i];
if (sm >= k2) pred = 1; else pred = 0;
errs = abs(pred-x[kval]);
for (i = 1; i < nk; i++) {
sm = sm + x[i+kval-1] - x[i-1];
if (sm >= k2) pred = 1; else pred = 0;
errs += abs(pred-x[i+kval]);
}
*errrate = (double) errs / nk;
}


This is basicallypredb()from before, “hand translated” into C. Let’s see if
it will outdopredb().



system.time(.C("predd",as.integer(y),as.integer(length(y)),as.integer(1000),



  • errrate=double(1)))
    user system elapsed
    0.004 0.000 0.003



Interfacing R to Other Languages 329
Free download pdf