[R] moving mean and moving variance functions
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Apr 4 15:17:16 CEST 2011
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
More information about the R-help
mailing list