bruno Piguet
2010-Jun-25 15:34 UTC
[R] Fast and simple tool for re-sampling of asynchronous time series ?
Un texte encapsul? et encod? dans un jeu de caract?res inconnu a ?t? nettoy?... Nom : non disponible URL : <https://stat.ethz.ch/pipermail/r-help/attachments/20100625/44fbea79/attachment.pl>
Charles C. Berry
2010-Jun-25 16:28 UTC
[R] Fast and simple tool for re-sampling of asynchronous time series ?
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.25Personally, 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 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Gabor Grothendieck
2010-Jun-25 16:36 UTC
[R] Fast and simple tool for re-sampling of asynchronous time series ?
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]))
bruno Piguet
2010-Jun-29 13:00 UTC
[R] Fwd: Fast and simple tool for re-sampling of asynchronous time series ?
Un texte encapsul? et encod? dans un jeu de caract?res inconnu a ?t? nettoy?... Nom : non disponible URL : <https://stat.ethz.ch/pipermail/r-help/attachments/20100629/335df200/attachment.pl>