I have a time series data with 4 variables over about 5 years. I would like to cluster the data using hclust method in R. I would like to cluster the observations. My code works. However, I would like to identify the specific cluster for each observation. That is, I would like to add the number of the cluster beside each observation. My code gave me an error. I understand the error. So is there any way can I achieve my point.
Here is my try:
library(TSclust)
library(cluster) # clustering algorithms
library(tseries)
library(zoo)
library(dtw)
library(dtwclust)
library(dplyr)
##Load the data
data("EuStockMarkets")
##Save the data
dat <- EuStockMarkets
res <- lapply(split(as.zoo(EuStockMarkets), as.integer(time(EuStockMarkets))), as.ts)
## Re-define the data
datNew <- ts(rbind(res$`1995`,res$`1996`,res$`1997`, res$`1998`))
d <- dist(datNew, method = "DTW")
hc1 <- hclust(d, method = "average" )
sub_grp <- cutree(hc1, k = 4)
table(sub_grp)
datNew%>%
mutate(cluster = sub_grp) %>%
head
It returns me an error:
Error in UseMethod("mutate_") :
no applicable method for 'mutate_' applied to an object of class "c('mts', 'ts', 'matrix')"
In addition: Warning message:
`mutate_()` is deprecated as of dplyr 0.7.0.
Please use `mutate()` instead.
See vignette('programming') for more help