How to use dtw distance measure in pvclust

34 Views Asked by At

I am using the package dtwclust for time series clustering (hierarchical method "h"). I pasted below the relavant parts of the code I am using in R:

clust.hier <- tsclust(transposed_data, type = "h", k = 6L, distance = "dtw")


I now want to use the pvclust function to determine the p-values for the clusters I found from dtw clustering.

set.seed(123)
    res.pv <- pvclust(data, method.dist = ???,
                  method.hclust="average", nboot = 1000)


I have read in the pvclust manual https://cran.r-project.org/web/packages/pvclust/pvclust.pdf that it is possible to use a custom distance measure as method.dist (in my case it would be the the distance function used by dtw). The example present in the manual is provided below:

### Using a custom distance measure
## Define a distance function which returns an object of class "dist".
## The function must have only one argument "x" (data matrix or data.frame).
    
cosine <- function(x) {
    x <- as.matrix(x)
    y <- t(x) %*% x
    res <- 1 - y / (sqrt(diag(y)) %*% t(sqrt(diag(y))))
    res <- as.dist(res)
    attr(res, "method") <- "cosine"
    return(res)
    }......"

Based on the above, I need to define a distance function for dtw to transfer to method.dist in pvclust however I cannot figure out how to do this.

Does anyone have previous experience with this and can give any suggestions?

0

There are 0 best solutions below