R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("flexclust-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('flexclust') > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "cclust" > > ### * cclust > > flush(stderr()); flush(stdout()) > > ### Name: cclust > ### Title: Convex Clustering > ### Aliases: cclust > ### Keywords: cluster > > ### ** Examples > > ## a 2-dimensional example > x<-rbind(matrix(rnorm(100,sd=0.3),ncol=2), + matrix(rnorm(100,mean=1,sd=0.3),ncol=2)) > cl<-cclust(x,2) > plot(x, col=predict(cl)) > points(cl@centers, pch="x", cex=2, col=3) > > ## a 3-dimensional example > x<-rbind(matrix(rnorm(150,sd=0.3),ncol=3), + matrix(rnorm(150,mean=2,sd=0.3),ncol=3), + matrix(rnorm(150,mean=4,sd=0.3),ncol=3)) > cl<-cclust(x, 6, method="neuralgas") > pairs(x, col=predict(cl)) > plot(cl, data=x) Loading required package: ellipse > > > > cleanEx(); ..nameEx <- "flexclustControl-class" > > ### * flexclustControl-class > > flush(stderr()); flush(stdout()) > > ### Name: flexclustControl-class > ### Title: Classes "flexclustControl" and "cclustControl" > ### Aliases: flexclustControl-class coerce,list,flexclustControl-method > ### coerce,NULL,flexclustControl-method cclustControl-class > ### coerce,list,cclustControl-method coerce,NULL,cclustControl-method > ### Keywords: classes > > ### ** Examples > > ## have a look at the defaults > new("flexclustControl") An object of class "flexclustControl" Slot "iter.max": [1] 200 Slot "tolerance": [1] 1e-06 Slot "verbose": [1] 0 Slot "classify": [1] "auto" Slot "gamma": [1] 1 Slot "simann": [1] 0.30 0.95 10.00 Slot "ntry": [1] 5 Slot "min.size": [1] 2 > > ## corce a list > mycont = list(iter=500, tol=0.001, class="w") > as(mycont, "flexclustControl") An object of class "flexclustControl" Slot "iter.max": [1] 500 Slot "tolerance": [1] 0.001 Slot "verbose": [1] 0 Slot "classify": [1] "weighted" Slot "gamma": [1] 1 Slot "simann": [1] 0.30 0.95 10.00 Slot "ntry": [1] 5 Slot "min.size": [1] 2 > > ## some additional slots > as(mycont, "cclustControl") An object of class "cclustControl" Slot "pol.rate": [1] 1 0 Slot "exp.rate": [1] 1e-01 1e-04 Slot "ng.rate": [1] 5e-01 5e-03 1e+01 1e-02 Slot "method": [1] "polynomial" Slot "iter.max": [1] 500 Slot "tolerance": [1] 0.001 Slot "verbose": [1] 0 Slot "classify": [1] "w" Slot "gamma": [1] 1 Slot "simann": [1] 0.30 0.95 10.00 Slot "ntry": [1] 5 Slot "min.size": [1] 2 > > > > cleanEx(); ..nameEx <- "kcca" > > ### * kcca > > flush(stderr()); flush(stdout()) > > ### Name: kcca > ### Title: K-Centroids Cluster Analysis > ### Aliases: kcca kccaFamily flexclust-class kcca-class kccaFamily-class > ### show,kcca-method > ### Keywords: cluster > > ### ** Examples > > data("Nclus", package="flexmix") > plot(Nclus) > > ## try kmeans > cl1 = kcca(Nclus, k=4) > cl1 kcca object of family 'kmeans' call: kcca(x = Nclus, k = 4) cluster sizes: 1 2 3 4 99 64 88 299 convergence after 10 iterations sum of within cluster distances : 1117.346 > > image(cl1) > points(Nclus) > > ## A barplot of the centroids > barplot(cl1) > > ## now use k-medians, cluster centroids should be similar ... > cl2 = kcca(Nclus, k=4, family=kccaFamily("kmedians")) > cl2 kcca object of family 'kmedians' call: kcca(x = Nclus, k = 4, family = kccaFamily("kmedians")) cluster sizes: 1 2 3 4 200 105 147 98 convergence after 8 iterations sum of within cluster distances : 963.1808 > > ## ... but the boundaries of the partitions have a different shape > image(cl2) > points(Nclus) > > > > cleanEx(); ..nameEx <- "qtclust" > > ### * qtclust > > flush(stderr()); flush(stdout()) > > ### Name: qtclust > ### Title: QT Clustering > ### Aliases: qtclust > ### Keywords: cluster > > ### ** Examples > > x <- matrix(10*runif(1000), ncol=2) > > ## maximum distrance of point to cluster center is 3 > cl1 <- qtclust(x, radius=3) > > ## maximum distrance of point to cluster center is 1 > ## -> more clusters, longer runtime > cl2 <- qtclust(x, radius=1) > > opar <- par(c("mfrow","mar")) > par(mfrow=c(2,1), mar=c(2.1,2.1,1,1)) > plot(x, col=predict(cl1), xlab="", ylab="") > plot(x, col=predict(cl2), xlab="", ylab="") > par(opar) > > > > graphics::par(get("par.postscript", env = .CheckExEnv)) > cleanEx(); ..nameEx <- "stepFlexclust" > > ### * stepFlexclust > > flush(stderr()); flush(stdout()) > > ### Name: stepFlexclust > ### Title: Run FlexClust Algorithms Repeatedly > ### Aliases: stepFlexclust stepFlexclust-class show,stepFlexclust-method > ### plot,stepFlexclust,missing-method getModel > ### getModel,stepFlexclust-method > ### Keywords: cluster > > ### ** Examples > > data("Nclus", package="flexmix") > plot(Nclus) > > cl1 = stepFlexclust(Nclus, K=2:7, FUN=cclust) 2 : * * * 3 : * * * 4 : * * * 5 : * * * 6 : * * * 7 : * * * > cl1 stepFlexclust object of family 'kmeans' call: stepFlexclust(Nclus, K = 2:7, FUN = cclust) iter converged distsum 2 5 TRUE 1681.2730 3 6 TRUE 1220.3927 4 6 TRUE 742.5601 5 8 TRUE 685.9157 6 11 TRUE 649.6834 7 21 TRUE 564.5816 > > plot(cl1) > > getModel(cl1, 4) kcca object of family 'kmeans' call: stepFlexclust(Nclus, K = 5, FUN = cclust) cluster sizes: 1 2 3 4 5 63 98 86 198 105 convergence after 8 iterations sum of within cluster distances : 685.9157 > > opar=par("mfrow") > par(mfrow=c(2,2)) > for(k in 3:6){ + image(getModel(cl1, as.character(k)), data=Nclus) + title(main=paste(k, "clusters")) + } Loading required package: ellipse > par(opar) NULL > > > > graphics::par(get("par.postscript", env = .CheckExEnv)) > ### *