I'm having trouble returning a rolling n period highest value for a data set. For each day I want to calculate the highest value over the last 3 days. I am using the following packages: zoo, xts, quantmod and TTR. Thanks, Jason GLD.Close 2010-10-01 128.91 2010-10-04 128.46 2010-10-05 130.99 2010-10-06 131.81 2010-10-07 130.37 2010-10-08 131.66 2010-10-11 132.29 2010-10-12 131.96 2010-10-13 134.07 2010-10-14 134.75 2010-10-15 133.68 2010-10-18 134.28 2010-10-19 130.11 2010-10-20 131.32 2010-10-21 129.47 2010-10-22 129.73 2010-10-25 130.85 2010-10-26 130.88 2010-10-27 129.52>[[alternative HTML version deleted]]
Hi Jason, Please consider the following example:> require(zoo) > z2 <- zoo(rnorm(6)) > z21 2 3 4 5 6 -0.53305704 -1.09374867 1.55171109 -0.05830751 -0.25987647 -0.02009973> rollapply(z2, 3, max, by = 1)2 3 4 5 1.55171109 1.55171109 1.55171109 -0.02009973 See ?rollapply for more information. HTH, Jorge On Thu, Oct 28, 2010 at 12:27 PM, Jason Kwok <> wrote:> I'm having trouble returning a rolling n period highest value for a data > set. For each day I want to calculate the highest value over the last 3 > days. I am using the following packages: zoo, xts, quantmod and TTR. > > Thanks, Jason > > GLD.Close > 2010-10-01 128.91 > 2010-10-04 128.46 > 2010-10-05 130.99 > 2010-10-06 131.81 > 2010-10-07 130.37 > 2010-10-08 131.66 > 2010-10-11 132.29 > 2010-10-12 131.96 > 2010-10-13 134.07 > 2010-10-14 134.75 > 2010-10-15 133.68 > 2010-10-18 134.28 > 2010-10-19 130.11 > 2010-10-20 131.32 > 2010-10-21 129.47 > 2010-10-22 129.73 > 2010-10-25 130.85 > 2010-10-26 130.88 > 2010-10-27 129.52 > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
On 2010-10-28 09:27, Jason Kwok wrote:> I'm having trouble returning a rolling n period highest value for a data > set. For each day I want to calculate the highest value over the last 3 > days. I am using the following packages: zoo, xts, quantmod and TTR.Isn't that exactly what rollmax() does? -Peter Ehlers> > Thanks, Jason > > GLD.Close > 2010-10-01 128.91 > 2010-10-04 128.46 > 2010-10-05 130.99 > 2010-10-06 131.81 > 2010-10-07 130.37 > 2010-10-08 131.66 > 2010-10-11 132.29 > 2010-10-12 131.96 > 2010-10-13 134.07 > 2010-10-14 134.75 > 2010-10-15 133.68 > 2010-10-18 134.28 > 2010-10-19 130.11 > 2010-10-20 131.32 > 2010-10-21 129.47 > 2010-10-22 129.73 > 2010-10-25 130.85 > 2010-10-26 130.88 > 2010-10-27 129.52 >> > > [[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.
Jason: Please read AND FOLLOW the posting guide on how to ask clear questions. Here, you need to more carefully define what you mean by "the last 3 days." Do you mean:(a) the last 3 values in the series (including or excluding the present one?) or the last 3 calendar days -- e.g. for 10-05, only 10-05 and 10-04, since 10-01 is not within the last 3 calendar days.Also, do you have missing values, and, if so, how do you want to handle them. If you mean the former, for small amounts of data without any missings(say 100 million numeric values or less) and small n (like n=3), it's easy and should be pretty fast just to produce lagged columns and use pmax rowwise. If you mean the latter and have missing values, it may be considerably more difficult. However, offering anything more seems pointless until you have adequately specified what you want. Reproducible data and code for a start. Cheers, Bert On Thu, Oct 28, 2010 at 9:27 AM, Jason Kwok <jaykwok at gmail.com> wrote:> I'm having trouble returning a rolling n period highest value for a data > set. ?For each day I want to calculate the highest value over the last 3 > days. ?I am using the following packages: zoo, xts, quantmod and TTR. > > Thanks, Jason > > ? ? ? ? ? GLD.Close > 2010-10-01 ? ?128.91 > 2010-10-04 ? ?128.46 > 2010-10-05 ? ?130.99 > 2010-10-06 ? ?131.81 > 2010-10-07 ? ?130.37 > 2010-10-08 ? ?131.66 > 2010-10-11 ? ?132.29 > 2010-10-12 ? ?131.96 > 2010-10-13 ? ?134.07 > 2010-10-14 ? ?134.75 > 2010-10-15 ? ?133.68 > 2010-10-18 ? ?134.28 > 2010-10-19 ? ?130.11 > 2010-10-20 ? ?131.32 > 2010-10-21 ? ?129.47 > 2010-10-22 ? ?129.73 > 2010-10-25 ? ?130.85 > 2010-10-26 ? ?130.88 > 2010-10-27 ? ?129.52 >> > > ? ? ? ?[[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. >-- Bert Gunter Genentech Nonclinical Biostatistics
On Thu, Oct 28, 2010 at 7:59 PM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> On Thu, Oct 28, 2010 at 2:02 PM, Jason Kwok <jaykwok at gmail.com> wrote: >> I was able to get what I wanted using the lag function to offset an addition >> period. >> >> lag(rollapply(xx,3,max),-2) or lag(rollapply(xx,3,max,align="right"),-1) >> > > Another possibility is to apply the function over the last 4 points > including the current point and then throw away the last one before > taking the max: > >> library(zoo) >> z <- zoo(101:110) >> rollapply(z, 4, function(x) max(x[-4]), align = "right") > ?4 ? 5 ? 6 ? 7 ? 8 ? 9 ?10 > 103 104 105 106 107 108 109Also, in the devel version of zoo available on R-Forge there is a new which= argument which can be used to specify which relative positions to use. Using z from above:> # devel version of zoo from R-Forge > rollapply(z, which = c(-3, -2, -1), FUN = max)4 5 6 7 8 9 10 103 104 105 106 107 108 109 -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com