# NOT RUN {
#--- Example for clustering a set of time series by feeding well known
# clustering methods with DTW-distance objects. First we simulate
# two prototype random walks and some cluster members. The cluster
# members are simulated by adding noise and randomly stretching and
# comressing the time series, to get time warped time series of
# varying lengths. The built clusters are 1:6 and 7:12.
set.seed(123)
N <- 100
rw_a <- cumsum(rnorm(N))
rw_b <- cumsum(rnorm(N))
sth <- sample(seq(0, 0.2, 0.01), size = 10)
cmp <- sample(seq(0, 0.2, 0.01), size = 10)
lot <- c(list(rw_a),
lapply(1:5, function(i){
simulate_timewarp(rw_a + rnorm(N), sth[i], cmp[i])
}),
list(rw_b),
lapply(6:10, function(i){
simulate_timewarp(rw_b + rnorm(N), sth[i], cmp[i])
}))
# Next get the distance matrix, as dist object. Per default all
# minus 1 available cores are used:
result <- dtw_dismat(lot = lot, dist_method = "norm2", ws = 50,
return_matrix = FALSE)
class(result$dismat)
# Finally you can cluster the result with the following
# well known methods:
require(cluster)
myclus <- hclust(result$dismat)
plot(myclus)
summary(myclus)
myclus <- agnes(result$dismat)
plot(myclus)
summary(myclus)
myclus <- pam(result$dismat, k=2)
plot(myclus)
summary(myclus)
myclus$medoids
# }
Run the code above in your browser using DataLab