[R] Finding seasonal peaks in a time series....

Dr Carbon drcarbon at gmail.com
Wed Jan 12 23:18:38 CET 2005


 I have a seasonal time series. I want to calculate the annual mean
value of the time series at its peak

 (say the mean of the three values before the peak, the peak, and the
three values after the peak).

 The peak of the time series might change cycle slightly from year to year.

# E.g.,
nPts <- 254
foo <- sin((2 * pi * 1/24) * 1:nPts)
foo <- foo + rnorm(nPts, 0, 0.05)
bar <- ts(foo, start = c(1980,3), frequency = 24)
plot(bar)
start(bar)
end(bar)

# I want to find the peak value from each year, and then get the mean
of the values on either side.
# So, if the peak value in the year 1981 is
max.in.1981 <- max(window(bar, start = c(1981,1), end = c(1981,24)))
# e.g, cycle 7 or 8
window(bar, start = c(1981,1), end = c(1981,24)) == max.in.1981
# E.g. if the highest value in 1981 is in cycle 8 I want
mean.in.1981 <- mean(window(bar, start = c(1981,5), end = c(1981,11)))
plot(bar)
points(ts(mean.in.1981, start = c(1981,8), frequency = 24), col =
"red", pch = "+")


 Is there a way to "automate" this for each year.

 How can I return the cycle of the max value by year?

 Thanks in advance. -DC




More information about the R-help mailing list