# 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 = 20L))
# 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 = "sakoechiba", window.size = 20L))
# Nearest neighbors
NN2 <- apply(d2, 1L, which.min)
# Calculate the DTW distances between all elements using dtw_basic
# (might be faster, see notes)
system.time(d3 <- proxy::dist(data[1:5], data[6:50], method = "DTW_BASIC",
window.size = 20L))
# Nearest neighbors
NN3 <- apply(d3, 1L, which.min)
# Change order and margin for nearest neighbor search
# (usually fastest, see notes)
system.time(d4 <- dtw_lb(data[6:50], data[1:5],
window.size = 20L, nn.margin = 2L))
# Nearest neighbors *column-wise*
NN4 <- apply(d4, 2L, which.min)
# Same results?
identical(NN1, NN2)
identical(NN1, NN3)
identical(NN1, NN4)
Run the code above in your browser using DataLab