Skip to content

Cent. Mov. Ave

1 message · Warnes, Gregory R

#
I recently wrote a function to compute functions over moving windows.  (See
below).  It varies from your request by using the last N points rather than
the last N/2 and next N/2 points, so you may have to modify it to get what
you want.

It will be in the next gregmisc package, which will go out in a month or so
when the publication review process here finishes. (I hate red tape!) 

-Greg



########### CODE #############
# $Id: running.R,v 1.2 2001/09/01 00:01:54 warneg Exp $
#
# $Log: running.R,v $
# Revision 1.2  2001/09/01 00:01:54  warneg
# Release 0.3.0
#
# Revision 1.1  2001/08/25 05:53:37  warneg
# Initial CVS checkin.
#
#
"running" _ function( X, fun=mean, width=min(length(X),20),
                     allow.fewer=FALSE,...)
{
  n _ length(X)

  from  <-  sapply( (1:n) - width + 1, function(x) max(x,1) )
  to    <-  1:n

  elements  <- apply(cbind(from,to), 1,function(x) seq(x[1], x[2]) )

  if(is.matrix(elements))
    elements  <- as.data.frame(elements)
  
  funct _ function(which,what,fun,...) fun(what[which],...)
  
  Xvar _ sapply(elements, funct, what=X, fun=fun, ...)
  names(Xvar) <- paste(from,to,sep=":")

  if(!allow.fewer)
    Xvar[1:(width-1)]  <- NA
  
  return(Xvar)
}

########## DOCUMENTATION ###############
running               package:gregmisc               R Documentation

Apply a Function Over Adjacent Subsets of a Vector

Description:

     Applies a function over subsets of the vector formed by taking a
     fixed number of previous points.

Usage:

     running(X, fun=mean, width=min(length(X),20), allow.fewer=FALSE,...)

Arguments:

       X: data vector 

     fun: function to apply. Default is `mean'

   width: integer giving the number of vector elements to include in
          the subsets.  Defaults to the lesser of the length of the
          data and 20 elements.

allow.fewer: Boolean indicating whether the function should be computed
          for initial subsets with fewer than `width' points

     ...: parameters to be passed to `fun' 

Value:

     Vector containg the results of applying the function `fun' to the
     subsets.

Author(s):

     Gregory R. Warnes Gregory_R_Warnes\@groton.pfizer.com

Examples:

     running(1:20,width=5)

     plot(1:20, running(1:20,width=5))
     plot(1:20, running(1:20,width=5, allow.fewer=T))

     # plot running mean and central 2 standard deviation range
     # estimated by last 40 observations
     dat <- rnorm(500, sd=1 + (1:500)/500 )
     plot(dat)
     fun <- function(x,sign=1) mean(x) + sign * sqrt(var(x))
     lines(running(dat,width=50,fun=mean,allow=T),col="blue")
     lines(running(dat,width=50,fun=fun, sign=-1, allow=T),col="red")
     lines(running(dat,width=50,fun=fun, sign=1, allow=T),col="red")
LEGAL NOTICE
Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._