Hi list, How do I calculate a moving average without using filter(). filter() does not seem to give weighted averages. I am looking into apply(), tapply,... But nothing "moves". For example, dat<-c(1:20) mean(dat[1:3]) mean(dat[4:6]) mean(dat[7:9]) mean(dat[10:12]) etc... I understand the point of apply is to avoid loops, how should I incorporate this idea into using an apply()? Thanks, Mike [[alternative HTML version deleted]]
Bert Gunter
2014-Feb-17 19:05 UTC
[R] How to calculate moving average without using filter()?
There are a zillion answers to this, because your question is really: How do I smooth a time series? So you can search on appropriate keywords. My answer is: don't use moving averages -- that's pathetically ancient. ?loess is one among the zillions of alternatives you might consider. Post on CV (stats.stackexchange.com) for other statistical alternatives for time series smoothing. Also, the "understanding" you expressed above is flawed. apply-type constructs **are** (R-level) loops. So have you done your homework by reading An Intro to R (http://cran.r-project.org/doc/manuals/R-intro.pdf) or other web tutorials? If not, please do so before posting here further. Cheers, Bert -- Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." H. Gilbert Welch On Mon, Feb 17, 2014 at 10:45 AM, C W <tmrsg11 at gmail.com> wrote:> Hi list, > How do I calculate a moving average without using filter(). filter() does > not seem to give weighted averages. > > I am looking into apply(), tapply,... But nothing "moves". > > For example, > > dat<-c(1:20) > mean(dat[1:3]) > mean(dat[4:6]) > mean(dat[7:9]) > mean(dat[10:12]) > > etc... > > I understand the point of apply is to avoid loops, how should I incorporate > this idea into using an apply()? > > Thanks, > Mike > > [[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.
Rui Barradas
2014-Feb-17 19:07 UTC
[R] How to calculate moving average without using filter()?
Hello, Many packages have a movind average function. For instance package forecast. Or library(sos) findFn("moving average") In your example, what you compute is not exactly a moving average, but in can be computed with something like the following. s <- (seq_along(dat) - 1) %/% 3 sapply(split(dat, s), mean) Hope this helps, Rui Barradas Em 17-02-2014 18:45, C W escreveu:> Hi list, > How do I calculate a moving average without using filter(). filter() does > not seem to give weighted averages. > > I am looking into apply(), tapply,... But nothing "moves". > > For example, > > dat<-c(1:20) > mean(dat[1:3]) > mean(dat[4:6]) > mean(dat[7:9]) > mean(dat[10:12]) > > etc... > > I understand the point of apply is to avoid loops, how should I incorporate > this idea into using an apply()? > > Thanks, > Mike > > [[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. >
David Winsemius
2014-Feb-17 19:27 UTC
[R] How to calculate moving average without using filter()?
On Feb 17, 2014, at 10:45 AM, C W wrote:> Hi list, > How do I calculate a moving average without using filter(). filter() does > not seem to give weighted averages. > > I am looking into apply(), tapply,... But nothing "moves". > > For example, > > dat<-c(1:20) > mean(dat[1:3]) > mean(dat[4:6]) > mean(dat[7:9]) > mean(dat[10:12]) > > etc... > > I understand the point of apply is to avoid loops, how should I incorporate > this idea into using an apply()? >Construct a vector for grouping and use tapply. Modulo division is a common method for achieving this. Sometimes the seq-function can be used if you adjust the length properly.> tapply(dat, (0:(length(dat)-1))%/%3, mean)0 1 2 3 4 5 6 2.0 5.0 8.0 11.0 14.0 17.0 19.5 tapply(dat, round(seq(1, (length(dat)/3), len=length(dat))), mean) 1 2 3 4 5 6 7 1.5 4.5 8.0 11.0 14.5 18.0 20.0 The comment about weighting dos not seem to be exemplified in your example.> Thanks, > Mike > > [[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.David Winsemius Alameda, CA, USA