On Fri, 25 Jun 2010, bruno Piguet 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


Personally, I'd incline towards leaving the next lines to C, perhaps using the inline package.

But if you want a purely R solution, the bioConductor IRanges package should help. I think the viewMeans() function will handle this loop.

See

        http://comments.gmane.org/gmane.comp.lang.r.sequencing/1296

for some discussion.

HTH,

Chuck


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

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.


Charles C. Berry                            (858) 534-2098
                                            Dept of Family/Preventive Medicine
E mailto:cbe...@tajo.ucsd.edu               UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

______________________________________________
R-help@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.

Reply via email to