Ivan Kalafatic
2010-Feb-22 20:52 UTC
[R] Creating regularly spaced time series from irregular one
Hello, I have a series of intraday (high-frequency) price data in the form of POSIX timestamp followed by the value. I sucesfuly loaded that into "its" package object. I would like to create from it a regularly spaced time series of prices (for example 1min, 5min, etc apart) so i could calcualte returns. There is an interpolation function locf() that for timestamp with value NA uses last known observation. I guess the idea would be to start from the begining of my series and, for example, if there is no timestamp for t+5min add that time with value NA. Than I could use locf() function to fill those NAs. Finaly I should extract from that series, series with 5min spaced timestamps with prices. Appart from applying locf() function, I have no idea how to add NAs into original series or extract the regular series after. Can someone help me with this? Thank you. [[alternative HTML version deleted]]
Jeff Ryan
2010-Feb-22 21:09 UTC
[R] [R-SIG-Finance] Creating regularly spaced time series from irregular one
Firstly, don't cross-post. Second, take a look at the archives on both these lists for answers to your questions. 'its' is rather old, and not where you want to be looking. Take a look at xts for fast time-series manipulation like you need, specifically to.period, endpoints, and align.time. There is a wealth of documentation in the package, in the vignette, and even online in more than a few places, but you can start here: http://cran.r-project.org/web/packages/xts/vignettes/xts.pdf http://www.quantmod.com/examples/data/ Also zoo for na.locf etc. Additional options include the fts package and timeSeries. HTH Jeff On Mon, Feb 22, 2010 at 2:52 PM, Ivan Kalafatic <ivan.kalafatic at gmail.com> wrote:> Hello, > I have a series of intraday (high-frequency) price data in the form of POSIX > timestamp followed by the value. > I sucesfuly loaded that into "its" package object. I would like to create > from it a regularly spaced time series of prices (for example 1min, 5min, > etc apart) so i could calcualte returns. > There is an interpolation function locf() that for timestamp with value NA > uses last known observation. > I guess the idea would be to start from the begining of my series and, for > example, if there is no timestamp for t+5min add that time with value NA. > Than I could use locf() function to fill those NAs. Finaly I should extract > from that series, series with 5min spaced timestamps with prices. > Appart from applying locf() function, I have no idea how to add NAs into > original series or extract the regular series after. > Can someone help me with this? > Thank you. > > ? ? ? ?[[alternative HTML version deleted]] > > _______________________________________________ > 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. >-- Jeffrey Ryan jeffrey.ryan at insightalgo.com ia: insight algorithmics www.insightalgo.com
Gabor Grothendieck
2010-Feb-23 00:17 UTC
[R] [R-SIG-Finance] Creating regularly spaced time series from irregular one
You probably want to ask the author of the its package directly about that. Note that as.zoo has an its method and as.its has a zoo method and they will allow you to convert back and forth between its and zoo so you can effectively use the functionality of both packages: # converting back and forth library(zoo) library(its) ii <- its(1:3, seq(Sys.time(), length = 3, by = "day")) z <- as.zoo(ii) ii2 <- as.its(z) In zoo, see the discussion of the intraday.discretize function which is defined in the following vignette: library(zoo) vignette("zoo-quickref") # also see vignette("zoo") vignette("zoo-faq") The discretizing discussion mentioned may apply in concept if not literally to its as well. Also note that zoo has an its method for the lattice xyplot function. The xts package is defined as a layer on top of zoo and shares some functions and methods with zoo and is particularly geared to finance along with the quantmod package by the same author so see them too. Also there is a list of R packages in the zoo-faq vignette mentioned above that may be of interest. On Mon, Feb 22, 2010 at 3:52 PM, Ivan Kalafatic <ivan.kalafatic at gmail.com> wrote:> Hello, > I have a series of intraday (high-frequency) price data in the form of POSIX > timestamp followed by the value. > I sucesfuly loaded that into "its" package object. I would like to create > from it a regularly spaced time series of prices (for example 1min, 5min, > etc apart) so i could calcualte returns. > There is an interpolation function locf() that for timestamp with value NA > uses last known observation. > I guess the idea would be to start from the begining of my series and, for > example, if there is no timestamp for t+5min add that time with value NA. > Than I could use locf() function to fill those NAs. Finaly I should extract > from that series, series with 5min spaced timestamps with prices. > Appart from applying locf() function, I have no idea how to add NAs into > original series or extract the regular series after. > Can someone help me with this? > Thank you.