RE: [R] Finding seasonal peaks in a time series....
This is inelegant, but works: # (following the example) 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) mean.in.i - numeric(length(start(bar)[1]:end(bar)[1])) peak.ts - ts(rep(NA, length(foo)), start = c(1980,3), frequency = 24) count - 1 for(i in start(bar)[1]:end(bar)[1]){ bar.win - window(bar, start = c(i,1), end = c(i,24)) max.in.i - max(bar.win) max.cycle.in.i - cycle(bar.win)[bar.win == max.in.i] mean.in.i[count] - mean(window(bar, start = c(i,max.cycle.in.i - 3), end = c(i,max.cycle.in.i + 3))) window(peak.ts, start = c(i, max.cycle.in.i), end = c(i, max.cycle.in.i)) - mean.in.i[count] count - count+1 } plot(bar) points(peak.ts, col = red, pch = +) mean.in.i -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Dr Carbon Sent: Wednesday, January 12, 2005 5:19 PM To: r-help@stat.math.ethz.ch Subject: [R] Finding seasonal peaks in a time series 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 __ R-help@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 __ R-help@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
Re: [R] Finding seasonal peaks in a time series....
Dr Carbon drcarbon at gmail.com writes: : : 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. Calculate the moving average of bar, which we call barma, and define a function f which takes a two column structure, locates the largest entry in column 1 and returns the corresponding entry in column 2. Use 'by' to apply f to the two columns, bar and barma for each year. Finally convert result back to a ts object. barma - filter(bar, rep(1,7)/7) f - function(bar) bar[which.max(bar[,1]),2] barpeakavg - by(cbind(bar, barma), floor(time(bar)+.0001), f) barpeakavg.ts - ts(barpeakavg, start = start(time(aggregate(bar : : How can I return the cycle of the max value by year? : aggregate(bar, FUN = which.max) __ R-help@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
Re: [R] Finding seasonal peaks in a time series....
Gabor Grothendieck ggrothendieck at myway.com writes: : : Dr Carbon drcarbon at gmail.com writes: : : : : : 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. : : Calculate the moving average of bar, which we call barma, and define a : function f which takes a two column structure, locates : the largest entry in column 1 and returns the corresponding : entry in column 2. Use 'by' to apply f to the two columns, bar and barma : for each year. Finally convert result back to a ts object. : : barma - filter(bar, rep(1,7)/7) : f - function(bar) bar[which.max(bar[,1]),2] : barpeakavg - by(cbind(bar, barma), floor(time(bar)+.0001), f) : barpeakavg.ts - ts(barpeakavg, start = start(time(aggregate(bar : : : : : How can I return the cycle of the max value by year? : : : : aggregate(bar, FUN = which.max) Note that this will be off for the first year if that year does not begin at the beginning of the cycle. If thats a problem then use the previous solution but replace barma with barcycle where barcycle - cycle(bar) __ R-help@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
[R] Finding seasonal peaks in a time series....
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 __ R-help@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
RE: [R] Finding seasonal peaks in a time series....
You might find breakpoints in strucchange helpful Tom -Original Message- From: Dr Carbon [mailto:[EMAIL PROTECTED] Sent: Thursday, 13 January 2005 6:19 AM To: r-help@stat.math.ethz.ch Subject: [R] Finding seasonal peaks in a time series 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 __ R-help@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 __ R-help@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
RE: [R] Finding seasonal peaks in a time series....
Sorry I didn't read the question properly. Please disregard, my mind was elsewhere. Tom -Original Message- From: Mulholland, Tom Sent: Thursday, 13 January 2005 10:52 AM To: Dr Carbon; r-help@stat.math.ethz.ch Subject: RE: [R] Finding seasonal peaks in a time series You might find breakpoints in strucchange helpful Tom -Original Message- From: Dr Carbon [mailto:[EMAIL PROTECTED] Sent: Thursday, 13 January 2005 6:19 AM To: r-help@stat.math.ethz.ch Subject: [R] Finding seasonal peaks in a time series 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 __ R-help@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 __ R-help@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 __ R-help@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