[R-SIG-Finance] Interpolating/comparing two irregular time/price sequences?
Achim Zeileis
Achim.Zeileis at wu-wien.ac.at
Wed Nov 7 19:59:54 CET 2007
On Wed, 7 Nov 2007, Rory Winston wrote:
<snip>
> Has anyone done anything like this before? I'm looking at the zoo
> package to see if it can help me, but I havent quite figured out how
> to do this kind of thing yet.
<snip>
You can merge the two series and generate NAs for the missing time points
and then use a na.*() method to eliminate the NAs, e.g., by linear
interpolation or by carrying observations forward or backward.
A simple example is attached below. See Section 2.8 of
vignette("zoo", package = "zoo")
for more details on NA methods.
## two time series
x <- zoo(c(2.01, 2.54, 2.37),
as.POSIXct("2007-11-07 18:47 GMT") + c(0, 125, 334))
y <- zoo(c(2, 2.5, 2.35),
as.POSIXct("2007-11-07 18:47 GMT") + c(2, 120, 339))
## the merged version
xy <- merge(x, y)
## interpolate by linear approximation
xy <- na.approx(xy, rule = 2)
## extract interpolated version of y at index of x
y2 <- window(xy[,2], index(x))
hth,
Z
More information about the R-SIG-Finance
mailing list