[R] How to write an interface function for bootstrapping with clusterboot() in R?

Ja Müller j@merk|e|n @end|ng |rom y@hoo@de
Mon Aug 24 15:06:57 CEST 2020


Dear all,
I am trying to write an interface function (CBI function) in order to use the bootstrapping function clusterboot() together with the clustering algorithm kmodes(). See here:

https://www.rdocumentation.org/packages/fpc/versions/2.2-7/topics/clusterboot
kmodes function | R Documentation
Has anyone already written interface functions for clusterboot() and can assist me or send me some example code for other interface functions for clusterboot()?

Please find my code below:   
   - I use the built-in dataset USArrests for the purpose of this minimal reproducible code only, which yields the same error message as when I use my actual data set.
   - mykmodesCBI = The name of the interface function I wish to create.
   - kmodes_boot10 = The result of the clusterboot() function using the interface function mykmodesCBI
   - I am getting the error message 'Error in matrix(0, nrow = c1$nc, ncol = B) :non-numeric matrix extent'
I have not found any assistance online so far, this is why I now turn to the R-help mailing list. Thank you in advance, any help is much appreciated!Julia

#--------------------------------------------------------------beginning of code snippet------------------------------------------------------------------------
library("fpc")
library("cluster")

data(USArrests)
mydata_example <- USArrests %>%
  na.omit() %>%          
  scale()

mykmodesCBI <- function(data, k) {
  result = kmodes(data, modes=k, iter.max = 50, weighted = FALSE ); 
  nc = n_distinct( kmodes(data, modes=k, iter.max = 50, weighted = FALSE )$cluster ) ; 
  clusterlist=  list( as.vector(lapply( kmodes(data, modes=1, iter.max = 50, weighted = FALSE )$cluster,function(x) if(x==1) {TRUE} else {FALSE} )),
                      as.vector(lapply( kmodes(data, modes=2, iter.max = 50, weighted = FALSE )$cluster,function(x) if(x==2) {TRUE} else {FALSE} )),
                      as.vector(lapply( kmodes(data, modes=3, iter.max = 50, weighted = FALSE )$cluster,function(x) if(x==3) {TRUE} else {FALSE} )),
                      as.vector(lapply( kmodes(data, modes=4, iter.max = 50, weighted = FALSE )$cluster,function(x) if(x==5) {TRUE} else {FALSE} )),
                      as.vector(lapply( kmodes(data, modes=5, iter.max = 50, weighted = FALSE )$cluster,function(x) if(x==5) {TRUE} else {FALSE} )),
                      as.vector(lapply( kmodes(data, modes=6, iter.max = 50, weighted = FALSE )$cluster,function(x) if(x==6) {TRUE} else {FALSE} ))
                    )
  partition = as.vector(list( kmodes(data, modes=k, iter.max = 50, weighted = FALSE )$cluster )); 
  clustermethod = "kmodes"; 
  return( list( result, nc, clusterlist, partition, clustermethod ) )
}

kmodes_boot10 <- clusterboot(
    data=mydata_example,          
    B = 10,                    
    distances=FALSE,            
    bootmethod = "boot",       
    multipleboot = FALSE, 
    clustermethod = mykmodesCBI, 
    k = 6,   
    seed = 123,           
    datatomatrix=TRUE)               
kmodes_boot10#--------------------------------------------------------------end of code snippet------------------------------------------------------------------------



	[[alternative HTML version deleted]]



More information about the R-help mailing list