# Load data
data(uciCT)
# Reinterpolate to same length
data <- reinterpolate(CharTraj, new.length = max(lengths(CharTraj)))
# Calculate the DTW distance between a certain subset aided with the lower bound
system.time(d <- dtw_lb(data[1:5], data[6:50], window.size = 20))
# Nearest neighbors
NN1 <- apply(d, 1L, which.min)
# Calculate the DTW distances between all elements (slower)
system.time(d2 <- proxy::dist(data[1:5], data[6:50], method = "DTW",
window.type = "slantedband", window.size = 20))
# Nearest neighbors
NN2 <- apply(d2, 1L, which.min)
# Calculate the DTW distances between all elements using dtw_basic (actually faster, see notes)
system.time(d3 <- proxy::dist(data[1:5], data[6:50], method = "DTW_BASIC",
window.size = 20))
# Nearest neighbors
NN3 <- apply(d3, 1L, which.min)
# Same results?
all(NN1 == NN2)
all(NN1 == NN3)
## Not run:
# #### Running DTW_LB with parallel support
# # For such a small dataset, this is probably slower in parallel
# require(doParallel)
#
# # Create parallel workers
# cl <- makeCluster(detectCores())
# invisible(clusterEvalQ(cl, library(dtwclust)))
# registerDoParallel(cl)
#
# # Distance matrix
# D <- dtw_lb(data[1:50], data[51:100], window.size = 20)
#
# # Stop parallel workers
# stopCluster(cl)
#
# # Return to sequential computations
# registerDoSEQ()
#
# # Nearest neighbors
# NN <- apply(D, 1, which.min)
# cbind(names(data[1:50]), names(data[51:100][NN]))
# ## End(Not run)
Run the code above in your browser using DataLab