[R-SIG-Finance] Réf. : Re: Interpolating/comparing two irregular time/price sequences?
guillaume.nicoulaud at halbis.com
guillaume.nicoulaud at halbis.com
Thu Nov 8 10:20:40 CET 2007
Rory,
Here is a generic version using the same idea:
data.frame(tm = Sys.time() - 1:10, dt = runif(10)) -> A
data.frame(tm = Sys.time() - cumsum( sample(1:3, 10, replace = TRUE) ),
dt = runif(10)) -> B
by = "tm"
f = function(A, B, by) {
# merge the 2 data.frames
merge(A, B, by = by, all = TRUE) -> a
# replace NAs with last observation (no linear approx here)
colnames(a)[ colnames(a) != by ] -> ii
lapply(ii, function(i) {
# I found this on the web... and I'm still trying to understand how it works
a[, i][ c(NA, which(!is.na(a[, i])))[cumsum(!is.na(a[, i])) + 1] ]
} ) -> b
# build an output data.frame from this
do.call(data.frame, c(list(a[, by]), b) ) -> res
colnames(res) <- c(by, ii)
return(res)
}
Pour : Rory Winston <rory.winston at gmail.com>
cc : r-sig-finance at stat.math.ethz.ch
Objet : Re: [R-SIG-Finance] Interpolating/comparing two irregular time/price sequences?
Achim Zeileis
<Achim.Zeileis at wu-wien.ac.at>
Envoyé par :
r-sig-finance-bounces at stat.math.ethz.
ch
07/11/2007 19:59
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
_______________________________________________
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.
Les informations contenues dans ce message sont confidentielles et peuvent constituer des informations privilegiees. Si vous n etes pas le destinataire de ce message, il vous est interdit de le copier, de le faire suivre, de le divulguer ou d en utiliser tout ou partie. Si vous avez recu ce message par erreur, merci de le supprimer de votre systeme, ainsi que toutes ses copies, et d en avertir immediatement l expediteur par message de retour.
Il est impossible de garantir que les communications par messagerie electronique arrivent en temps utile, sont securisees ou denuees de toute erreur ou virus. En consequence, l expediteur n accepte aucune responsabilite du fait des erreurs ou omissions qui pourraient en resulter.
--- ----------------------------------------------------- ---
The information contained in this e-mail is confidential...{{dropped:9}}
More information about the R-SIG-Finance
mailing list