1
votes

I am using dtwclust package to cluster a data frame contains multivariate time series data. After clustering these data, I would like to produce a plot presenting observation in $k$ groups according tot the cluster, not the time series data!

dtw_cluster2 = tsclust(sample_data, type="partitional",k=6,preproc = zscore,distance="dtw_basic",centroid = "pam",trace=T)

then to plot,

plot(dtw_cluster2)

and I got:enter image description here

I don't want these plots! I want the cluster of observations. I tried to extract the clusters from tsclust() as dtw_cluster2$cluster but I got an error "$ operator not defined for this S4 class".

My data set looks like

 V1        V2         V3         V4          V5         V6         V7
1  0 0.1182197 0.09057301 0.08089888 0.003350084 0.00000000 0.00000000
2  0 0.1276078 0.09242144 0.01348315 0.060301508 0.02245599 0.02298152
3  0 0.1369958 0.12569316 0.03595506 0.159128978 0.04491198 0.04596305
4  0 0.1029207 0.10166359 0.08089888 0.201005025 0.06736798 0.06894457
5  0 0.1585535 0.14510166 0.08089888 0.112227806 0.08982397 0.09192609
6  0 0.1488178 0.00000000 0.07415730 0.212730318 0.11227996 0.11490761

edit

I want to plot the observations according to the clusters in something like this "knowing that I am using dtw distance":

enter image description here

1

1 Answers

2
votes

Looking at the result, you can see this:

str(dtw_cluster2)

Formal class 'PartitionalTSClusters' [package "dtwclust"] with 20 slots
  #.. (NB here there are things I've skipped )
  ..@ distance : chr "dtw_basic"
  ..@ centroid : chr "pam"
  ..@ preproc  : chr "zscore"
  ..@ k        : int 2
  ..@ cluster  : int [1:6] 1 1 1 2 1 2
  #.. (NB here there are things I've skipped )

So you can extract the cluster in this way:

dtw_cluster2@cluster
[1] 1 1 1 2 1 2

Given:

library(dtwclust)
dtw_cluster2 <- tsclust(sample_data, type="partitional",
                                     k=2,
                                     preproc = zscore,
                                     distance="dtw_basic",
                                     centroid = "pam",
                                     trace=T)

with data:

sample_data <-
structure(list(V1 = c(0L, 0L, 0L, 0L, 0L, 0L), V2 = c(0.1182197, 
0.1276078, 0.1369958, 0.1029207, 0.1585535, 0.1488178), V3 = c(0.09057301, 
0.09242144, 0.12569316, 0.10166359, 0.14510166, 0), V4 = c(0.08089888, 
0.01348315, 0.03595506, 0.08089888, 0.08089888, 0.0741573), V5 = c(0.003350084, 
0.060301508, 0.159128978, 0.201005025, 0.112227806, 0.212730318
), V6 = c(0, 0.02245599, 0.04491198, 0.06736798, 0.08982397, 
0.11227996), V7 = c(0, 0.02298152, 0.04596305, 0.06894457, 0.09192609, 
0.11490761)), class = "data.frame", row.names = c(NA, -6L))