[R] moving mean and moving variance functions

From: Steve Friedman <skfglades_at_gmail.com>
Date: Mon, 04 Apr 2011 08:30:43 -0400


Hello

Lets say as an example I have a dataframe with the following attributes: rownum(1:405), colnum(1:287), year(2000:2009), daily(rownum x colnum x year) and foragePotential (0:1, by 0.01). The data is actually stored in a netcdf file and I'm trying to provide a conceptual version of the data.

Ok. I need to calculate a moving mean and a moving variance for each cell on the following temporal
windows - 7 day, 14 day, and 28 day. So far I have code for the moving average.

ma <- function(x , n) {

          filter(x, rep(1/n, n), sides = 1)
      }   # note that when the function is used, n is defined for the
temporal period (7, 14, and 28), and x is the input variable.

ma7 <- ma(dat, 7) # where dat is accessing the foraging potential of the birds.
ma14 <- ma(dat, 14)
ma28 <- ma(dat, 28)

This works fine. What I don't have is the code for a moving variance.

filter in the function above is included in the stats package and conducts a linear filtering on a Time Series.

Is there comparable code some place in R for a moving variance?

Thanks in advance.

Thanks,
Steve

        [[alternative HTML version deleted]]



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 04 Apr 2011 - 12:34:29 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 04 Apr 2011 - 13:40:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive