# NOT RUN {
# }
# NOT RUN {
#--- Simulate a query pattern Q and a longer time series C with
# distorted instances of Q within C. Apply rundtw() do detect
# these instances of C.
rw <- function(nr) cumsum(rnorm(nr))
noise <- function(nr) rnorm(nr)
set.seed(1234)
nC <- 500
nQ <- 40
nfits <- 5
nn <- nC - nfits * nQ # length of noise
nn <- nn/nfits + 1
Q <- sin(seq(from = 1, to = 4 * pi, length.out = nQ))
Qscale <- IncDTW::scale(Q, type = "01")
C <- rw(0)
for(i in 1:nfits){
C <- c(C, rw(nn) ,
Q * abs(rnorm(1, 10, 10)) +
rnorm(1, 0, 10) + noise(nQ))
}
# Apply running min-max scaling and allow lower
# bounding to find the 3 NN
x <- rundtw(Q, C, scale = '01', ws = 10, k = 3,
lower_bound = TRUE, return_QC = TRUE)
# Have a look at the result and get the best indices
# with lowest distance.
x
summary(x)
find_peaks(x$dist, nQ)
plot(x, scale = "01")
# The fourth and fifth simuated fits are not returned,
# since the DTW distances are higher than the other found 3 NN.
# The algorithm early abandons and returns NA for these
# indices. Get all distances by the following command:
x_all <- rundtw(Q, C, scale = '01', ws = 10,
k = 0, lower_bound = FALSE)
plot(x_all)
# Do min-max-scaling and lower bound
rundtw(Q, C, scale = '01', ws = 10, lower_bound = TRUE)
# Do z-scaling and lower bound
rundtw(Q, C, scale = 'z', ws = 10, lower_bound = TRUE)
# Don't scaling and don't lower bound
rundtw(Q, C, scale = 'none', ws = 10, lower_bound = FALSE)
# kNN: Do z-scaling and lower bound
rundtw(Q, C, scale = 'z', ws = 10, k = 3)
#--- For multivariate time series
rw <- function(nr, nco) {
matrix(cumsum(rnorm(nr * nco)), nrow = nr, ncol = nco)
}
nC <- 500
nQ <- 50
nco <- 2
nfits <- 5
nn <- nC - nfits * nQ# length of noise
nn <- nn/nfits
Q <- rw(nQ, nco)
Qscale <- IncDTW::scale(Q, type="01")
C <- matrix(numeric(), ncol=nco)
for(i in 1:nfits){
C <- rbind(C, rw(nn, nco), Q)
}
# Do min-max-scaling and lower bound
rundtw(Q, C, scale = '01', ws = 10, threshold = Inf,
lower_bound = TRUE)
# Do z-scaling and lower bound
rundtw(Q, C, scale = 'z', ws = 10, threshold = NULL,
lower_bound = TRUE)
# Don't scale and don't lower bound
rundtw(Q, C, scale = 'none', ws = 10, threshold = NULL,
lower_bound = FALSE)
#--- C can also be a list of (multivariate) time series.
# So rundtw() detects the closest fits of a query pattern
# across all time series in C.
nC <- 500
nQ <- 50
nco <- 2
rw <- function(nr, nco){
matrix(cumsum(rnorm(nr * nco)), nrow = nr, ncol = nco)
}
Q <- rw(nQ, nco)
C1 <- rbind(rw(100, nco), Q, rw(20, nco))
C2 <- rbind(rw(10, nco), Q, rw(50, nco))
C3 <- rbind(rw(200, nco), Q, rw(30, nco))
C_list <- list(C1, C2, C3)
# Do min-max-scaling and lower bound
x <- rundtw(Q, C_list, scale = '01', ws = 10, threshold = Inf,
lower_bound = TRUE, k = 3, return_QC = TRUE)
x
# Plot the kNN fit of the 2nd or 3rd list entry of C
plot(x, lix = 2)
plot(x, lix = 3)
# Do z-scaling and lower bound
rundtw(Q, C_list, scale = 'z', ws = 10, threshold = Inf,
lower_bound = TRUE, k = 3)
# Don't scale and don't lower bound
x <- rundtw(Q, C_list, scale = 'none', ws = 10,
lower_bound = FALSE, k = 0, return_QC = TRUE)
x
plot(x)
# }
Run the code above in your browser using DataLab