Re: [R] moving mean and moving variance functions

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Mon, 04 Apr 2011 09:17:16 -0400

On Mon, Apr 4, 2011 at 8:30 AM, Steve Friedman <skfglades_at_gmail.com> wrote:
> 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?
>

See rollmean and rollapply in the zoo package and runmean and runsd in the caTools package.

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
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 - 13:20:54 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 - 21: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