3

I have a multiple sequences of different lengths in a list in R. I would like to construct the DTW distance matrix to be used by hierarchical clustering. When I run the following command in R:

a2<-dtw(Data[[1]][1],Data[[1]][2])

Error in dtw(Data[[1]][1], Data[[1]][2]) : 
  No warping path exists that is allowed by costraints

I get above error. I have no NAN or missing value but they are different lengths.I know matlab dtw can support sequence with different lengths .Can anyone suggest me how to solve this problem in R?

Also to calculate the DTW distance between all sets of observation should I write a for loop for each two data series or is there any command in R or Python that can calculate the Distance given the list or dictionary as an input.

Update: Input : 1 1 2 5 6 7 2 4
                 2 1 1 2 4 5 3 3 2 2 1
                 1 2 1 1 2 3 1 2 1 2 6 6 5
                 1 2 3 5 6 2 4 2 2 3 

I want to be have the distance matrix for the above input.

Cœur
  • 37,241
  • 25
  • 195
  • 267
stack
  • 93
  • 8
  • 2
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jul 30 '18 at 18:47
  • If your series have very different lengths, you will get that error, and there's no way around it (considering you're not even using window constraints). Check out [my answer to a similar question](https://stackoverflow.com/a/50776685/5793905), and note that the `dtwclust` implementation returns Infinite instead of throwing an error like `dtw` does. – Alexis Jul 30 '18 at 20:29
  • On second thought, if you do use the `dtw` version, maybe using its `open.begin`/`open.end` parameters could help, assuming that makes sense for your data. – Alexis Jul 30 '18 at 20:46
  • Thanks for your response. If I calculate the distance manually by taking two time series together everytime. Like two for loop nested and calculate the dtw distance. is it going to be like matrix distance between each two time series where 0 is in main diagonal ? To do clustering can I feed this matrix input to hclust in R? – stack Jul 30 '18 at 21:08
  • Yes, but you don't have to do it manually, you can use `proxy::dist`. – Alexis Jul 30 '18 at 21:56
  • But my observations have different length so how can I do it with proxy::dist? – stack Jul 30 '18 at 23:56
  • Can you show me how to do the proxy::dist if the observations have different lengths? since when I construct the matrix manually R gives an error since it expects the input to hclust to be structure not matrix. – stack Jul 31 '18 at 00:46
  • For that you'd need to follow MrFlick's advice and show your data structure, not just the contents. – Alexis Jul 31 '18 at 05:25
  • I made up the above example . I do not understand what you mean by your data structure.it can be list of list in python or R. It also can be a cell array in Matlab. – stack Jul 31 '18 at 07:09

0 Answers0