Good morning all! I am attempting to superimpose a moving-average smoother onto a graph of daily plots. These plots (in table[,2] below) span about 350 days and looks very noisy. I'd like for this smoother to plot the average of each group of 7 consecutive days (weekly) and show a line which joins these series of averages. Given the definition of MA, the first and last points will generally overlap in the average calculation. It's probably a one-liner, but I still am having some problems with the syntax. The only part I have correct is the "lines" statement to ensure it overlays my original graph. Here's the code I have thus far: y <- table[,2] plot(y,type="l",lty=3) lines( ......) {moving average code here to be placed here} Any time series gurus out there? Be gentle. I'm an R beginner. Thanx! Bernard
On Jun 16, 2005, at 8:04 AM, Bernard L. Dillard wrote:> Good morning all! > > I am attempting to superimpose a moving-average smoother onto a graph > of > daily plots. These plots (in table[,2] below) span about 350 days and > looks very noisy. I'd like for this smoother to plot the average of > each > group of 7 consecutive days (weekly) and show a line which joins these > series of averages. Given the definition of MA, the first and last > points > will generally overlap in the average calculation. > > It's probably a one-liner, but I still am having some problems with the > syntax. The only part I have correct is the "lines" statement to > ensure > it overlays my original graph. > > Here's the code I have thus far: > > y <- table[,2] > plot(y,type="l",lty=3) > lines( ......) {moving average code here to be placed here} > > Any time series gurus out there? Be gentle. I'm an R beginner.R site search is your friend: http://finzi.psych.upenn.edu/cgi-bin/namazu.cgi? query=moving+window&max=20&result=normal&sort=score&idxname=functions&id xname=docs&idxname=Rhelp02a Sean
are you looking the function decompose? see ?decompose On Thu, 16 Jun 2005 08:04:18 -0400 (EDT) "Bernard L. Dillard" <bld at math.umd.edu> wrote:> Good morning all! > > I am attempting to superimpose a moving-average smoother onto a graph of > daily plots. These plots (in table[,2] below) span about 350 days and > looks very noisy. I'd like for this smoother to plot the average of each > group of 7 consecutive days (weekly) and show a line which joins these > series of averages. Given the definition of MA, the first and last points > will generally overlap in the average calculation. > > It's probably a one-liner, but I still am having some problems with the > syntax. The only part I have correct is the "lines" statement to ensure > it overlays my original graph. > > Here's the code I have thus far: > > y <- table[,2] > plot(y,type="l",lty=3) > lines( ......) {moving average code here to be placed here} > > Any time series gurus out there? Be gentle. I'm an R beginner. > > Thanx! > > Bernard > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On Thu, 16 Jun 2005 08:04:18 -0400 (EDT) Bernard L. Dillard wrote:> Good morning all! > > I am attempting to superimpose a moving-average smoother onto a graph > of daily plots. These plots (in table[,2] below) span about 350 days > and looks very noisy. I'd like for this smoother to plot the average > of each group of 7 consecutive days (weekly) and show a line which > joins these series of averages. Given the definition of MA, the first > and last points will generally overlap in the average calculation. > > It's probably a one-liner, but I still am having some problems with > the syntax. The only part I have correct is the "lines" statement to > ensure it overlays my original graph. > > Here's the code I have thus far: > > y <- table[,2] > plot(y,type="l",lty=3) > lines( ......) {moving average code here to be placed here}With the zoo package you can do the following: library(zoo) ## create data x <- rnorm(365) ## transform to regular zoo series with "Date" index x <- zooreg(x, start = as.Date("2004-01-01")) plot(x) ## add rolling/running/moving average with window size 7 lines(rollmean(x, 7), col = 2, lwd = 2) ## if you don't want the rolling mean but rather a weekly ## time series of means you can do nextfri <- function(x) 7 * ceiling(as.numeric(x - 1)/7) + as.Date(1) xw <- aggregate(x, nextfri, mean) ## nextfri is a function which computes for a certain "Date" ## the next friday. xw is then the weekly series. lines(xw, col = 4) Note, that the differnce between is rolling mean and the aggregated series is due to different alignments. This can be changed by changing the `align' argument in rollmean() or the nextfri() function in the aggregate call. hth, Z> Any time series gurus out there? Be gentle. I'm an R beginner. > > Thanx! > > Bernard > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >