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

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri, 25 Jun 2010 12:36:30 -0400

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]))



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 25 Jun 2010 - 16:41:22 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 25 Jun 2010 - 17:20:44 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive