# Load data
data(uciCT)
# Reinterpolate to same length
data <- lapply(CharTraj, reinterpolate, newLength = 180)
# 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, 1, which.min)
# Calculate the DTW distances between all elements (about three times 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, 1, which.min)
# Same results?
all(NN1 == NN2)
#### 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]))
Run the code above in your browser using DataLab