FW: [R] Cent. Mov. Ave

Warnes, Gregory R gregory_r_warnes at groton.pfizer.com
Sat Feb 23 00:00:16 CET 2002


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")








> -----Original Message-----
> From: Erin Hodgess [mailto:hodgess at uhddx01.dt.uh.edu]
> Sent: Friday, February 22, 2002 2:10 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Cent. Mov. Ave
> 
> 
> Dear R People:
> 
> Here is an interesting question(I think)
> Suppose I want to calculate Centered Moving Averages; i.e.
> x[1] <- ( sum(y[1:12]) )/12
> x[2] <- ( sum(y[2:13]) )/12
> 
> and so on.
> 
> Of course, this is easily done through loops.  However, I have
> been trying to do this more elegantly, but have failed.  I have
> tried things like
> j1 <- 1:109
> j2 <- 12:120
> 
> x[1:109] <- ( sum( y[j1:j2]) )/12
> 
> but it doesn't work.  (Surprise!)
> 
> 
> R 1.4.0 on windows.
> 
> 
> Any ideas would be much appreciated!
> 
> Thanks!
> 
> Sincerely,
> Erin Hodgess
> Associate Professor
> Department of Computer and Mathematical Sciences
> University of Houston - Downtown
> 1 Main Street 
> Houston, TX 77002
> mailto: hodgess at uhddx01.dt.uh.edu
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._._._._._._._._
> 


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



More information about the R-help mailing list