Skip to content

Optional return value to work with cluster::clusGap #3

@MahShaaban

Description

@MahShaaban

I tried to use the timeclust function to calculate the appropriate number of clusters using cluster::clusGap but got an error because cluster::clusGap expects a return value as list with an item cluster. This is in fact the case for popular clustering functions in R such as stats::kmeans and e1071::cmeans.

I understand that TCseq uses S4 classes, so I wonder whether an optional argumnet, say cluster_only = TRUE can be added to give the option of returning a list with an item cluster.

I made a simple example to show the error and a work around.

# loading required libraries
library(TCseq)
library(cluster)

# run example of timeclust
x <- matrix(rnorm(1600,sd=0.3),
            nrow = 200,
            dimnames = list(paste0('peak', 1:200), 1:8))
clust_res <- timeclust(x,
                       algo = 'km',
                       k = 4)

# using timeclust with clusGap (error)
clusGap(x,
        FUNcluster = timeclust,
        algo = 'km',
        K.max = 10)
#> Error: $ operator not defined for this S4 class

# work around
timeclust2 <- function(...) {
  # run timeclust normally
  res <- timeclust(...)
  
  # return a list of one item (cluster slot)
  return(list(cluster = res@cluster))
}

# using timeclust2 with clusGap
clusGap(x,
        FUNcluster = timeclust2,
        algo = 'km',
        K.max = 10,
        B = 10)
#> Clustering Gap statistic ["clusGap"] from call:
#> clusGap(x = x, FUNcluster = timeclust2, K.max = 10, B = 10, algo = "km")
#> B=10 simulated reference sets, k = 1..10; spaceH0="scaledPCA"
#>  --> Number of clusters (method 'firstSEmax', SE.factor=1): 1
#>           logW   E.logW       gap      SE.sim
#>  [1,] 4.065481 4.517970 0.4524893 0.011765972
#>  [2,] 4.004416 4.438673 0.4342573 0.012349933
#>  [3,] 3.961315 4.391034 0.4297197 0.009777780
#>  [4,] 3.921194 4.347815 0.4266205 0.008408944
#>  [5,] 3.894419 4.312041 0.4176216 0.009219877
#>  [6,] 3.869805 4.283160 0.4133550 0.013342736
#>  [7,] 3.843492 4.257513 0.4140212 0.007701472
#>  [8,] 3.814787 4.231085 0.4162979 0.008534093
#>  [9,] 3.794906 4.209470 0.4145634 0.010870945
#> [10,] 3.767030 4.186994 0.4199637 0.010462993

Created on 2019-04-12 by the reprex package (v0.2.1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions