R - Faster Way to Calculate Rolling Statistics Over a Variable Interval

后端 未结 4 1445
不知归路
不知归路 2020-12-03 02:00

I\'m curious if anyone out there can come up with a (faster) way to calculate rolling statistics (rolling mean, median, percentiles, etc.) over a variable interval of time (

4条回答
  •  温柔的废话
    2020-12-03 02:28

    In reply to my question to "Kevin" above, I think I figured something out below.

    This function takes ticks data (time observations come in at random intervals are and indicated by a time stamp) and calculates the the mean over an interval.

    library(Rcpp)
    
    cppFunction('
      NumericVector rollmean_c2( NumericVector x, NumericVector y, double width,
                                  double Min, double Max) {
    
    double total = 0, redge,center;
    unsigned int n = (Max - Min) + 1,
                      i, j=0, k, ledge=0, redgeIndex;
    NumericVector out(n);
    
    
    for (i = 0; i < n; i++){
      center = Min + i + 0.5;
      redge = center - width / 2;
      redgeIndex = 0;
      total = 0;
    
      while (x[redgeIndex] < redge){
        redgeIndex++;
      }
      j = redgeIndex;
    
      while (x[j] < redge + width){
        total += y[j++];
    
      }
    
      out[i] = total / (j - redgeIndex);
    }
    return out;
    
      }')
    
    # Set up example data
    x = seq(0,4*pi,length.out=2500)
    y = sin(x) + rnorm(length(x),0.5,0.5)
    plot(x,y,pch=20,col="black",
         main="Sliding window mean; width=1",
         sub="rollmean_c in red      rollmean_r overlaid in white.")
    
    
    c.out = rollmean_c2(x,y,width=1,Min = min(x), Max = max(x)) 
    lines(0.5:12.5,c.out,col="red",lwd=3)
    

    enter image description here

提交回复
热议问题