Problem to converting a negative days in data frame into a list of time series

I'd like to use time series clustering using the dtwclust package. The problem is the conversion of my data.frame to list of time series. All my blocks ID (named STAND) has 180 days in negative values (DATE_TIME) The B2_MAX is my variable response. In my example:

    library(dplyr)
    library(ggplot2)
    library(dtwclust)
    
    all.B2_MAX.stands <- read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/my_ts_data.csv")
    
    all.B2_MAX.tsc <-  all.B2_MAX %>%
      group_by(STAND) %>%
      summarise(var = list(B2_MAX[order(DATE_TIME)]), 
                var_ts = purrr::map(var, ts))
    
    clusters <- tsclust(all.B2_MAX.tsc[-1], 
                       type="partitional", 
                       k=2L, 
                       distance="dtw",
                       centroid = "pam")
    
    #plot
    plot(cluster, type = "sc")
    
    #Error in lapply(series, base::as.numeric) : 
    #  'list' object cannot be coerced to type 'double'

Please, any help with it?

If the problem is the negative values and the actual values aren't important, but the order is, you could add 180 to the DATE_TIME column. So -180 would be 0 and so on.

But I assume there are other issues.

library(dtwclust)
#> Loading required package: proxy
#> 
#> Attaching package: 'proxy'
#> The following objects are masked from 'package:stats':
#> 
#>     as.dist, dist
#> The following object is masked from 'package:base':
#> 
#>     as.matrix
#> Loading required package: dtw
#> Loaded dtw v1.23-1. See ?dtw for help, citation("dtw") for use in publication.
#> dtwclust:
#> Setting random number generator to L'Ecuyer-CMRG (see RNGkind()).
#> To read the included vignettes type: browseVignettes("dtwclust").
#> See news(package = "dtwclust") after package updates.

d <- read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/my_ts_data.csv")
l <- split(d$B2_MAX,d$STAND)
o <- tsclust(l, 
        type="partitional", 
        k=2L, 
        distance="dtw_basic",
        centroid = "pam")
plot(o)


# suspect
d[which.max(d$B2_MAX),]
#>                      STAND DATE_TIME        B2_MAX
#> 12740 RINCAODOSSOARES_015E         1 1.641827e+199

Created on 2023-10-24 with reprex v2.0.2

1 Like

Thank you so much @technocrat, problem solved!!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.