# NOT RUN {
#--- univariate
Q <- cumsum(rnorm(100))
C <- Q[11:100] + rnorm(90, 0, 0.5)
tmp <- dtw(Q = Q, C = C, ws = 15, return_diffM = FALSE,
return_QC = TRUE, return_wp = TRUE)
names(tmp)
print(tmp, digits = 3)
plot(tmp)
plot(tmp, type = "warp")
#--- compare different input variations
dtw_base <- dtw(Q = Q, C = C, ws = 15, return_diffM = TRUE)
dtw_diffM <- dtw(Q = dtw_base$diffM, C = "diffM", ws = 15,
return_diffM = TRUE)
dtw_cm <- dtw(Q = abs(dtw_base$diffM), C = "cm", ws = 15,
return_diffM = TRUE)
identical(dtw_base$gcm, dtw_cm$gcm)
identical(dtw_base$gcm, dtw_diffM$gcm)
# of course no diffM is returned in the 'cm'-case
dtw_cm$diffM
#--- multivariate case
Q <- matrix(rnorm(100), ncol=2)
C <- matrix(rnorm(80), ncol=2)
dtw(Q = Q, C = C, ws = 30, dist_method = "norm2")
#--- user defined distance function
# We define the distance function d_cos and use it as input for the cost matrix function cm.
# We can pass the output from cm() to dtw2vec(), and also to idtw2vec() for the
# incrermental calculation:
d_cos <- function(x, y){
1 - sum(x * y)/(sqrt(sum(x^2)) * sqrt(sum(y^2)))
}
Q <- matrix(rnorm(100), ncol=5, nrow=20)
C <- matrix(rnorm(150), ncol=5, nrow=30)
cm1 <- cm(Q, C, dist_method = d_cos)
dtw2vec(Q = cm1, C = "cm")$distance
res0 <- idtw2vec(Q = cm1[ ,1:20], newObs = "cm")
idtw2vec(Q = cm1[ ,21:30], newObs = "cm", gcm_lc = res0$gcm_lc_new)$distance
# The DTW distances -- based on the customized distance function -- of the
# incremental calculation and the one from scratch are identical.
# }
Run the code above in your browser using DataLab