Splitting Time Series for dtwclust

Viewed 14

I'm attempting to prepare a dataframe of time series data (panel data) for dtwclust::tsclust. My current code looks like this:

library(dtwclust)
library(parallel)
library(dplyr)
library(zoo)
library(data.table)

Panel_VisitPages = data.table(
RecordID = c(1,1,1,1,2,3,3,5,5,5,5,5,5),
VisitPages = c(5,3,5,6,3,100,5,3,5,22,3,22,11),
DayDif = (0,-1,-2,-15,0,0,-5,0,-3,-5,-15,-20)
)


Panel_VisitPages = #df_view_ts[, TotalVisitPages:=sum(VisitPages),by=list(RecordID, DayDif)]
   df_view_ts %>%group_by(RecordID, DayDif)%>%summarise(TotalVisitPages= sum(VisitPages))

groups = unique(Panel_VisitPages$RecordID)

set.seed(1)

groups = sample(groups, 3)

Panel_VisitPages = Panel_VisitPages[Panel_VisitPages$RecordID %in% groups,]

PageSeries = read.zoo(Panel_VisitPages
        #, format = 
         , split='RecordID'
        , index='DayDif'
        ,FUN = identity
        )

PageSeries = na.fill(PageSeries, fill=0)

# Cluster Test------------------------------------------------------------------

workers <- makeCluster(detectCores()-4)
# load dtwclust in each one, and make them use 1 thread per worker
invisible(clusterEvalQ(workers, {
  library(dtwclust)
  library(bigmemory)
  RcppParallel::setThreadOptions(1L)
}))
# register your workers, e.g. with doParallel
require(doParallel)
registerDoParallel(workers)


clustTest = tsclust(
  series = t(PageSeries)
  , type = 'partitional'
  , k = 2L:10L
  , distance = 'SBD'
  , seed = 1
  , trace = 1
)


The first line is of course much slower than it would be if I were able to use the data.table command I have commented out, but when I reach the tsclust command I get

Error in merge.zoo(--blocked out--,  : 
  series cannot be merged with non-unique index entries in a series

Can anyone tell me how the data.table code at the top of this block differs from the similar dplyr code and what I can do to get it to perform the same as the dplyr line? Thanks!

0 Answers
Related