Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed Time Shift/Lag Function #242

Open
rossb34 opened this issue May 10, 2018 · 4 comments
Open

Proposed Time Shift/Lag Function #242

rossb34 opened this issue May 10, 2018 · 4 comments
Assignees
Labels
feature request New features

Comments

@rossb34
Copy link
Collaborator

rossb34 commented May 10, 2018

I propose adding functionality to shift or lag the index of an xts object by a specified number of seconds. This is similar to lag.xts, but the key difference is shifting by units of time (e.g. seconds) rather than an index. This is particularly useful for irregular time series such as high frequency market data.

Here is a prototype function that provides the proposed functionality.

timeshift <- function (x, k = 1)
{
    offset.xts <- .xts(, .index(x) + k)
    m <- merge(offset.xts, merge(offset.xts, x, fill = na.locf), join = "left")
    .index(m) <- .index(x)
    if (is.null(dimnames(x)[[2L]]))
        dimnames(m) <- NULL
    m
}
@rossb34 rossb34 added the feature request New features label May 10, 2018
@rossb34 rossb34 self-assigned this May 10, 2018
@joshuaulrich
Copy link
Owner

joshuaulrich commented May 11, 2018

One thing I've considered is allowing the k argument to lag.xts() to be specified like n can be in last.xts(). Here's an example of last.xts():

require(xts)
data(sample_matrix)
x <- as.xts(sample_matrix, dateFormat = "Date")
last(x, "week")  # may also be preceded by a number (e.g. "2 weeks")
#                Open     High      Low    Close
# 2007-06-25 47.20471 47.42772 47.13405 47.42772
# 2007-06-26 47.44300 47.61611 47.44300 47.61611
# 2007-06-27 47.62323 47.71673 47.60015 47.62769
# 2007-06-28 47.67604 47.70460 47.57241 47.60716
# 2007-06-29 47.63629 47.77563 47.61733 47.66471
# 2007-06-30 47.67468 47.94127 47.67468 47.76719

So if lag.xts() is called with that type of k, it would do what I imagine timeshift() does.

Thoughts?

@rossb34
Copy link
Collaborator Author

rossb34 commented May 17, 2018

Here is a quick example that demonstrates the functionality. The function allows one to answer the question, "For each observation, what is the value k seconds in the future?"

x <- .xts(rnorm(10), c(1, 2, 2.4, 2.5, 4, 4.2, 4.5, 4.6, 5.2, 6),
          dimnames = list(NULL, "price"))

# the price 2 seconds in the future
x$price.future <- timeshift(x, 2)

@rossb34
Copy link
Collaborator Author

rossb34 commented May 17, 2018

I don't like the inconsistency with lag.xts my initial prototype introduces. Note the negative k in lag.xts and positive k in timeshift.

# lagging by time and lagging by an index are equivalent on regularly spaced seconds
x <- .xts(1:5, 1:5 + 0.0)
lag(x, k = -1)
timeshift(x, k = 1)

I think it would be better to name the function timelag and treat k the same as in lag.xts. This can be handled simply by subtracting k from the index.

timelag<- function (x, k = 1)
{
    offset.xts <- .xts(, .index(x) - k)
    m <- merge(offset.xts, merge(offset.xts, x, fill = na.locf), join = "left")
    .index(m) <- .index(x)
    if (is.null(dimnames(x)[[2L]]))
        dimnames(m) <- NULL
    m
}

# equivalent
lag(x, k = 1)
timelag(x, k = 1)

@rossb34
Copy link
Collaborator Author

rossb34 commented May 17, 2018

I agree that we should probably roll this into lag.xts with e.g. lag.xts(x, k = "5 seconds"). For now, I'll just implement it as a separate function while I think about that.

rossb34 added a commit that referenced this issue May 17, 2018
- Added timelag function to lag by units of time rather than
  the discrete index values.
- The arguments and functionality are consistent with lag.xts,
  only difference is to lag by units of time. This function should
  probably get integrated into lag.xts per comments from josh in
  issue #242.

see issue #242
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New features
Projects
None yet
Development

No branches or pull requests

2 participants