[R-SIG-Finance] zoo: how to find for series x closest day in series y?
mat
matthieu.stigler at gmail.com
Sun Apr 25 14:33:51 CEST 2010
Great, smart!! Thanks a lot Gabor!!!
Matthieu
Gabor Grothendieck a écrit :
> Just apply the previous formula to rollmean(y, 3, align = "left") in place of y.
>
> On Sun, Apr 25, 2010 at 5:29 AM, mat <matthieu.stigler at gmail.com> wrote:
>
>> Thanks a lot Gabor, this is the "clean" code I was looking for :-)
>>
>> Actually, I now need for a further application to take not only the one
>> value a time t or greater than t, but take the mean of the say next 3 values
>> at time t or greater than t...
>>
>> from:
>> x <- zoo(1:3,as.Date(c("1992-12-13", "1997-05-12", "1997-07-13")))
>> y <- zoo(1:14,as.Date(c("1992-12-15", "1992-12-16","1992-12-18",
>> "1992-12-19",
>> "1997-05-10","1997-05-19","1997-05-23","1997-05-24","1997-05-25",
>> "1997-07-13","1997-07-14","1997-07-15","1997-07-16","1997-07-19")))
>>
>>
>> I would need for "1992-12-13" to have the mean of "1992-12-15",
>> "1992-12-16","1992-12-18", and so... Will need probably:
>> aggregate(x2, rep(index(x), each=3), mean)
>> but about how to construct the x2 with the 3 closest values ... I'm stuck
>> again...
>>
>> Thanks a lot for any hint!!
>>
>> Matthieu
>>
>>
>>
>> Gabor Grothendieck a écrit :
>>
>>> If the problem is to find y's at or greater in time than x's then we
>>> can use this method:
>>>
>>> window(na.locf(merge(x, y), fromLast = TRUE), index(x))
>>>
>>>
>>> On Fri, Apr 23, 2010 at 9:04 AM, Matthieu Stigler
>>> <matthieu.stigler at gmail.com> wrote:
>>>
>>>
>>>> Dear gabor
>>>>
>>>> Thanks a lot for your valuable help!!! I finally used your second
>>>> solution,
>>>> which is more say in "zoo spirit". As I actually needed not the closest
>>>> day, but the closest day AFTER, I changed it slightly to:
>>>>
>>>> pos<-function(x) ifelse(x>=0, x, NA)
>>>> f2 <- function(u) which.min(pos(as.numeric(index(y)) - as.numeric(u)))
>>>> ix <- sapply(index(x), f2)
>>>>
>>>> Thanks a lot again for your help!!
>>>>
>>>> Mat
>>>>
>>>> Gabor Grothendieck a écrit :
>>>>
>>>>
>>>>> Here is another solution where x and y are as in your post.
>>>>>
>>>>> f <- function(u) which.min(abs(as.numeric(index(y)) - as.numeric(u)))
>>>>> ix <- sapply(index(x), f)
>>>>> cbind(x, y = coredata(y)[ix])
>>>>>
>>>>> On Fri, Apr 23, 2010 at 5:50 AM, Gabor Grothendieck
>>>>> <ggrothendieck at gmail.com> wrote:
>>>>>
>>>>>
>>>>>
>>>>>> Example 4d on the sqldf home page shows how:
>>>>>> http://sqldf.googlecode.com
>>>>>>
>>>>>> Here it is modified to work with your example data.
>>>>>>
>>>>>> (Note that its important to use different column names in DFx and DFy
>>>>>> since it can otherwise foul up sqldf's class assignment heuristic in
>>>>>> this case. If for some reason you must use names that are the same in
>>>>>> both DFx and DFy then use the method = "raw" argument to sqldf and
>>>>>> convert the date columns to Date yourself. See sqldf home page for
>>>>>> more info.)
>>>>>>
>>>>>> library(zoo)
>>>>>> library(sqldf)
>>>>>>
>>>>>> x <- zoo(1:3,as.Date(c("1992-12-13", "1997-05-12", "1997-07-13")))
>>>>>> y <- zoo(1:5,as.Date(c("1992-12-15", "1992-12-16",
>>>>>> "1997-05-10","1997-05-19",
>>>>>> "1997-07-13")))
>>>>>>
>>>>>> DFx <- data.frame(x = coredata(x), xt = index(x))
>>>>>> DFy <- data.frame(y = coredata(y), yt = index(y))
>>>>>>
>>>>>> out <- sqldf("select x, y, xt, yt from DFx x, DFy y
>>>>>> where abs(xt - yt) =
>>>>>> (select min(abs(xt - y2.yt)) from DFy y2)")
>>>>>>
>>>>>> ix <- !duplicated(out[,3])
>>>>>> zoo(out[ix, 1:2], out[ix, 3])
>>>>>>
>>>>>>
>>>>>> On Fri, Apr 23, 2010 at 5:10 AM, Matthieu Stigler
>>>>>> <matthieu.stigler at gmail.com> wrote:
>>>>>>
>>>>>>
>>>>>>
>>>>>>> Hi
>>>>>>>
>>>>>>> I have two series, x and y, which don't have the same index. I then
>>>>>>> need to
>>>>>>> find which in y is the closest day in x. How can do I do this in a
>>>>>>> clean
>>>>>>> way? I have here a dirty for loop solution...
>>>>>>> library(zoo)
>>>>>>> #my series
>>>>>>> x<-zoo(1:3,as.Date(c("1992-12-13", "1997-05-12", "1997-07-13")))
>>>>>>> y<-zoo(1:5,as.Date(c("1992-12-15", "1992-12-16",
>>>>>>> "1997-05-10","1997-05-19",
>>>>>>> "1997-07-13")))
>>>>>>>
>>>>>>> #but index is not always the same:
>>>>>>> index(x)%in%index(y)
>>>>>>>
>>>>>>> #my (dirty?) solution
>>>>>>> xnew<-x
>>>>>>> for(i in which(!index(x)%in%index(y)))
>>>>>>> xnew[i]<-which.min(abs(as.numeric(index(x)[i]-index(y))))
>>>>>>> xnew
>>>>>>>
>>>>>>> Is there something cleaner one could use to find the closest available
>>>>>>> day?
>>>>>>>
>>>>>>> Thanks a lot!
>>>>>>>
>>>>>>> Matthieu
>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> R-SIG-Finance at stat.math.ethz.ch mailing list
>>>>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>>>>>> -- Subscriber-posting only. If you want to post, subscribe first.
>>>>>>> -- Also note that this is not the r-help list where general R
>>>>>>> questions
>>>>>>> should go.
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>
>>
More information about the R-SIG-Finance
mailing list