[R] Fast and simple tool for re-sampling of asynchronous time series ?

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jun 25 18:36:30 CEST 2010


On Fri, Jun 25, 2010 at 11:34 AM, bruno Piguet <bruno.piguet at gmail.com> wrote:
> Hi all,
>
>   I'm looking for a function which could do some fast and simple
> re-sampling of asynchronous time series.
>
>   Below is a MCE of the kind of algorithm I need. As you can see, it's
> quite crude, but it's enough for my current needs.  The only problem is that
> it is quite slow on real use case.
>   I've got a C version which is much faster, but I'd like to have a pure-R
> program.
>
>   Any pointer to the relevant part of the doc one one of the time-series
> packages ? Any suggestion or advice ?
>
>   Thanks in advance,
>
> B. Piguet.
>
> Here is the exemple :
> Tx <- seq(1, 50, 0.5)
> Tx <- Tx + rnorm(length(Tx), 0, 0.1)
> X <- sin(Tx/10.0) +  sin(Tx/5.0) + rnorm(length(Tx), 0, 0.1)
> Ty <- seq(1, 50, 0.3333)
> Ty <- Ty + rnorm(length(Ty), 0, 0.02)
> Y <- sin(Ty/10.0) + sin(Ty/5.0) + rnorm(length(Ty), 0, 0.1)
>
> w <- 0.25
>
> Y_sync <- rep(NA, length(Tx))
> for (i in 1:length(Tx))
> {
>   T_min <- Tx[i] - w
>   T_max <- Tx[i] + w
>   Y_sync[i] <- mean(Y[Ty >= T_min & Ty <= T_max ])
> }
>
> diff = X - Y_sync
> print(summary(diff))
>
> print(summary(lm(Y_sync~X)))
> plot (diff~Tx, type="l")

This isn't substantially different than what you have but does replace
the explicit loop and associated indexing with an implicit loop:

   sapply(Tx, function(tx) mean(Y[Ty >= tx-w & Ty <= tx+w]))



More information about the R-help mailing list