On Sun, Nov 14, 2010 at 13:09, Gabor Grothendieck
<ggrothendieck@gmail.com>wrote:

> On Sun, Nov 14, 2010 at 12:52 PM, richard.c.herron@gmail.com
> <richard.c.herron@gmail.com> wrote:
> > OK, I'm learning :). With more tinkering I found that the zoo lag
> operator
> > lags by rows, not by time stamps, so it fails if there are multiple
> entries
> > per time stamp. I may be forced to switch to data frame, perform the
> > following operation, then switch back to zoo.
> > Here is the manual coding of the operation I'd like (with a small data
> > frame) to do all in the zoo class (or some other time series):
> >> # create a zoo multiple symbols per month
> >> temp <- data.frame(date=rep(200808:200811, each=3),
> >> symbol=rep(letters[1:3], times=4), ret=1:12)
> >> # find unique date entries
> >> unique.date <- unique(temp$date)
> >> # shift the date num.lags to match future performance with current
> >> stock/date
> >> num.lags <- 1
> >> temp.lag <- temp[which(temp$date > unique.date[num.lags]), ]
> >> for (i in seq(num.lags+1, length(unique.date), 1)) {
> > +     temp.lag$date[which(temp.lag$date==unique.date[i])] <-
> > unique.date[i-num.lags]
> > + }
> >> # merge
> >> temp.merged <- merge(temp, temp.lag, by=c("date", "symbol"),
> >> suffixes=c(".now", ".next"))
> >> temp.merged
> >     date symbol ret.now ret.next
> > 1 200808      a       1        4
> > 2 200808      b       2        5
> > 3 200808      c       3        6
> > 4 200809      a       4        7
> > 5 200809      b       5        8
> > 6 200809      c       6        9
> > 7 200810      a       7       10
> > 8 200810      b       8       11
> > 9 200810      c       9       12
> >
>
> zoo series are time series and time series are functions of time so
> you can't have multiple times that are the same (or else it would not
> be a function). See zoo FAQ #1
> vignette("zoo-faq")
>
> Normally in zoo you would represent your data in wide form rather than
> long form in which case there is no problem:
>
> > library(zoo)
> > f <- function(x) as.yearmon(as.character(x), "%Y%m")
> > z <- read.zoo(temp, split = "symbol", FUN = f)
> > lag(z, 0:1)
>         a.lag0 b.lag0 c.lag0 a.lag1 b.lag1 c.lag1
> Aug 2008      1      2      3      4      5      6
> Sep 2008      4      5      6      7      8      9
> Oct 2008      7      8      9     10     11     12
> Nov 2008     10     11     12     NA     NA     NA
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
>

Thanks, Gabor!

I knew about zoo's preference for single row per date, but I didn't know
read.zoo() converted to wide or the lag(x, 0:1) trick.

Seeing this has me questioning my ideas on portfolio sorting. Is there a
good reference on forming portfolios along a given parameter (kind of like
Jegadeesh-Titman momentum portfolios, but sorted along a parameter other
than return)? The search terms I'm using in rseek.org aren't yielding much.
Thanks!

	[[alternative HTML version deleted]]

