# NOT RUN {
#--- Do the vector-based incremental DTW
# calculation and compare it with the basic
Q <- cumsum(rnorm(100))
C <- Q[11:100] + rnorm(90, 0, 0.5)
# initial calculation
res0 <- idtw2vec(Q = Q, newObs = C, gcm_lc = NULL)
# incremental calculation for new observations
nobs <- rnorm(10)
res1 <- idtw2vec(Q, newObs = nobs, gcm_lc = res0$gcm_lc_new)
# compare with result from scratch
res2 <- dtw2vec(Q, c(C, nobs))
res1$distance - res2$distance
#--- Perform an incremental DTW calculation with a
# customized distance function.
d_cos <- function(x, y){
1 - sum(x * y)/(sqrt(sum(x^2)) * sqrt(sum(y^2)))
}
x <- matrix(rnorm(100), ncol = 5, nrow = 20)
y <- matrix(rnorm(150), ncol = 5, nrow = 30)
cm1 <- cm(x, y, dist_method = d_cos)
# initial calculation
res0 <- idtw2vec(Q = cm(x, y[1:20,], dist_method = d_cos),
newObs = "cm")
# incremental calculation for new observations
res1 <- idtw2vec(Q = cm(x, y[21:30,], d_cos), newObs = "cm",
gcm_lc = res0$gcm_lc_new)$distance
# compare with result from scratch
res2 <- dtw2vec(Q = cm1, C = "cm")$distance
res1 - res2
# }
Run the code above in your browser using DataLab