splice: Splice and Smooth a Timeseries

Description Usage Arguments Details Author(s)

View source: R/splice.R


splice will splice a timeseries.

splooth will smooth a spliced timeseries.


splice(x, at)

splooth(x, at, method=c("diff","ratio","wavg"))



numeric matrix. see below.


logical vector indicating splice times.


character. "diff", "ratio", or "wavg"


Time series splicing is required when we want to construct a series out of contiguous time windows across a set of separate series (e.g., constructing the "front" futures contract's time series from the set of time series of expiry month A, Month B, etc.). The matrix of time series from which to take the data is called the splicing series. The time series returned is called the spliced series.

x is the splicing series and must be coercible by as.matrix. Let X[n,c] be the n by c matrix passed to x and w[1], w[2], ..., w[m] be the m members of which(at). splice returns the spliced n by d matrix Y[n,d], where d = max(m - c + 1, 1).

The spliced matrix is obtained by concatenating a vector diagonal of Y[t,c] defined as:

Y[t,j] = X[t,f(t,j)]

where t ranges over all values between w[j] and w[j+c] - 1 (w[j] and n when j = d), and

f(t,j) = a[j] + a[j+1] + ... + a[t]

, which is the column-choosing diagonal function, where a[i] is the i-th value in at. In words: as we move from row w[j] forward we take values from the first column of X until we hit another splice time w[j+1], when we start taking values from the second column, and so on until we finish taking values from the last column of X, the last value being row w[j+c]-1. Now we have the j-th column of Y. Repeat the process, but this time starting with w[j+1]. Keep going until we have all 1, 2, ..., d columns.

If Y[t] is a spliced timeseries and t is a splice time, then the change between observations Y[t] and Y[t+1] contains a jump due solely to the fact that the two observations were originally obtained from different series. These jumps generally distort analysis of the spliced series. Sploothing is the technique of smoothing out these jumps to remove the distortion. splooth is passed a spliced matrix to x and the splice times used in creating it are passed as a logical vector to at. method defines the splicing technique to use.

Three different sploothing methods are supported. Each method adjusts the series x[, i] using data contained in series x[, i + 1]. A matrix of identical dimension but with sploothed values is returned. (The right-most column of x is left unchanged.)

When method="diff" the difference method is used. The sploothed series Z is calculated by adjusting each observation with this equation:

Z[t,j] = Y[t,j] - (Y[n,j] - Y[n,j+1])a[n] + ... + (Y[t,j] - Y[t,j+1])a[t]

where n is nrow(x). The observations after the last splice time are unchanged, but as we move back in time the difference between Y[t,j] and Y[t,j+1] at splice time t is cumulatively subtracted from the series. Note that if the mean jump is meaningfully different from zero, a trend will be infused into the sploothed series. It is also possible for this method to cause the series to change sign.

The ratio method is used when method="ratio". The process is the same as with the difference method except that the ratio of the values is used instead of their difference, and we take a cumulative product instead of a sum. It is calculated as:

Z[t,j] = Y[t,j]((Y[n,j] / Y[n,j+1] - 1)a[n] + 1) ... ((Y[t,j] / Y[t,j+1] - 1)a[t] + 1)

The same proviso above about trend applies to this technique, although the sign will never change. All observations must be greater than zero or an error will be raised.

The weighted average method is used when method="wavg". It is calculated as:

Z[t,j] = Y[t,j](k-t)/(k-1) + Y[t,j+1](t-i)/(k-i)

where k is the next splice time on or after t, and i is the last splice time prior to t. This method takes a weighted average of the two series at each point in time, instead of back-adjusting the jump observed at splice time only. The closer t gets to the splice time, the more weight is given to Y[t,j+1]. The same proviso holds regarding trend.


Robert Sams [email protected]

tradesys documentation built on May 31, 2017, 2:29 a.m.