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("gclus-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('gclus') > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "ac" > > ### * ac > > flush(stderr()); flush(stdout()) > > ### Name: ac > ### Title: Clustering coefficients from package cluster. > ### Aliases: ac sil > ### Keywords: cluster > > ### ** Examples > > x <- runif(20) > y <- runif(20) > g <- rep(c("a","b"),10) > > ac(x,y) Loading required package: cluster [1] 0.8070754 > sil(x,y,g) [1] 0.07807186 > > > > cleanEx(); ..nameEx <- "colpairs" > > ### * colpairs > > flush(stderr()); flush(stdout()) > > ### Name: colpairs > ### Title: Applies a function to all pairs of columns > ### Aliases: colpairs > ### Keywords: multivariate > > ### ** Examples > > data(state) > state.m <- colpairs(state.x77, + function(x,y) cor.test(x,y,"two.sided","kendall")$estimate, diag=1) > state.col <- dmat.color(state.m) > # This is equivalent to state.m <- cor(state.x77,method="kendall") > > layout(matrix(1:2,nrow=1,ncol=2)) > cparcoord(state.x77, panel.color= state.col) > # Get rid of the panels with lots of line crossings (yellow) by reorderings > cparcoord(state.x77, order.endlink(state.m), state.col) > layout(matrix(1,1)) > > # m is a homogeneity measure of each pairwise variable plot > m <- -colpairs(scale(state.x77), gave) > > o<- order.single(m) > pcols = dmat.color(m) > # Color panels by level of m and reorder variables so that > # pairs with high m are near the diagonal. > cpairs(state.x77,order=o, panel.colors=pcols) > > # In this case panels showing either of Area or Population > # exhibit the most clumpiness because these variables > # are skewed. > > > > > cleanEx(); ..nameEx <- "cpairs" > > ### * cpairs > > flush(stderr()); flush(stdout()) > > ### Name: cpairs > ### Title: Enhanced scatterplot matrix > ### Aliases: cpairs > ### Keywords: multivariate color hplot > > ### ** Examples > > > data(USJudgeRatings) > judge.cor <- cor(USJudgeRatings) > judge.color <- dmat.color(judge.cor) > # Colors variables by their correlation. > cpairs(USJudgeRatings,panel.colors=judge.color,pch=".",gap=.5) > judge.o <- order.single(judge.cor) > # Reorder variables so that those with highest correlation > # are close to the diagonal. > cpairs(USJudgeRatings,judge.o,judge.color,pch=".",gap=.5) > > # Specify your own color scheme > judge.color <- dmat.color(judge.cor, breaks=c(-1,0,.5,.9,1), colors = + cm.colors(4)) > > data(bank) > # m is a homogeneity measure of each pairwise variable plot > m <- -colpairs(scale(bank[,-1]), partition.crit,gfun=gave,groups=bank[,1]) > > # Color panels by level of m and reorder variables so that > # pairs with high m are near the diagonal. Panels shown > # in pink have the highest amount of group homogeneity, as measured by > # gave. > cpairs(bank[,-1],order=order.single(m), panel.colors=dmat.color(m), + gap=.3,col=c("purple","black")[bank[,"Status"]+1], + pch=c(5,3)[bank[,"Status"]+1]) > > > > > cleanEx(); ..nameEx <- "cparcoord" > > ### * cparcoord > > flush(stderr()); flush(stdout()) > > ### Name: cparcoord > ### Title: Enhanced parallel coordinate plot > ### Aliases: cparcoord > ### Keywords: multivariate color hplot > > ### ** Examples > > data(state) > state.m <- colpairs(state.x77, + function(x,y) cor.test(x,y,"two.sided","kendall")$estimate, diag=1) > # OR, Works only in R1.8, state.m <-cor(state.x77,method="kendall") > > state.col <- dmat.color(state.m) > > cparcoord(state.x77, panel.color= state.col) > # Get rid of the panels with lots of line crossings (yellow) by reordering: > cparcoord(state.x77, order.endlink(state.m), state.col) > > # To get rid of the panels with lots of long line segments: > # use a different panel merit measure- pclen: > > mins <- apply(state.x77,2,min) > ranges <- apply(state.x77,2,max) - mins > state.m <- -colpairs(scale(state.x77,mins,ranges), pclen) > cparcoord(state.x77, order.endlink(state.m), dmat.color(state.m)) > > > > > > cleanEx(); ..nameEx <- "diameter" > > ### * diameter > > flush(stderr()); flush(stdout()) > > ### Name: diameter > ### Title: Cluster heterogeneity of 2-d data > ### Aliases: diameter star km2 gtot gave > ### Keywords: cluster > > ### ** Examples > > x <- runif(20) > y <- runif(20) > diameter(x,y) [1] 1.144591 > > > > cleanEx(); ..nameEx <- "dmat.color" > > ### * dmat.color > > flush(stderr()); flush(stdout()) > > ### Name: dmat.color > ### Title: Colors a symmetric matrix > ### Aliases: dmat.color default.dmat.color > ### Keywords: multivariate color > > ### ** Examples > > data(longley) > longley.cor <- cor(longley) > # A matrix with equal (or nearly equal) number of entries of each color. > longley.color <- dmat.color(longley.cor) > > # Plot the colors > plotcolors(longley.color,dlabels=rownames(longley.color)) > > # Try different color schemes > > # A matrix where each color represents an equal-length interval. > longley.color <- dmat.color(longley.cor, byrank=FALSE) > # Specify colors and breaks > > longley.color <- dmat.color(longley.cor, breaks=c(-1,0,.5,.8,1), + cm.colors(4)) > > # Could also reorder variables prior to plotting: > > longley.o <- order.single(longley.cor) > longley.color <- longley.color[longley.o,longley.o] > > # The colors can be used in a scatterplot matrix or parallel > # coordinate display: > > cpairs(longley, panel.color= longley.color) > cparcoord(longley, panel.color= longley.color) > > > > > cleanEx(); ..nameEx <- "hclust" > > ### * hclust > > flush(stderr()); flush(stdout()) > > ### Name: reorder.hclust > ### Title: Reorders object order of hclust, keeping objects within a > ### cluster contiguous to each other. > ### Aliases: reorder.hclust > ### Keywords: multivariate cluster > > ### ** Examples > > > data(eurodist) > dis <- as.dist(eurodist) > hc <- hclust(dis, "ave") > > layout(matrix(1:2,nrow=2,ncol=1)) > op <- par(mar=c(1,1,1,1)) > plot(hc) > hc1 <- reorder.hclust(hc, dis) > plot(hc1) > par(op) > layout(matrix(1,1)) > > # Both dedrograms correspond to the same tree structure, > # but the second one shows that > # Paris is closer to Cherbourg than Munich, and > # Rome is closer to Gibralter than to Barcelona. > > # We can also compare both orderings with an > # image plot of the colors. > # The second ordering seems to place nearby cities > # closer to each other. > > layout(matrix(1:2,nrow=2,ncol=1)) > op <- par(mar=c(1,6,1,1)) > cmat <- dmat.color(eurodist, rev(cm.colors(5))) > plotcolors(cmat[hc$order,hc$order], rlabels=labels(eurodist)[hc$order]) > > plotcolors(cmat[hc1$order,hc1$order], rlabels=labels(eurodist)[hc1$order]) > > layout(matrix(1,1)) > par(op) > > > > > graphics::par(get("par.postscript", env = .CheckExEnv)) > cleanEx(); ..nameEx <- "order" > > ### * order > > flush(stderr()); flush(stdout()) > > ### Name: order.single > ### Title: Orders objects using hierarchical clustering > ### Aliases: order.single order.endlink order.hclust > ### Keywords: multivariate cluster > > ### ** Examples > > data(state) > state.cor <- cor(state.x77) > order.single(state.cor) [1] 7 4 6 2 8 3 5 1 > order.endlink(state.cor) [1] 1 5 3 8 2 6 4 7 > order.hclust(state.cor,method="average") [1] 1 5 3 8 2 6 4 7 > > # Use for plotting... > > cpairs(state.x77, panel.colors=dmat.color(state.cor), order.single(state.cor),pch=".",gap=.4) > cparcoord(state.x77, order.endlink(state.cor),panel.colors=dmat.color(state.cor)) > > # Order the states instead of the variables... > > state.d <- dist(state.x77) > state.o <- order.single(-state.d) > > op <- par(mar=c(1,6,1,1)) > cmat <- dmat.color(as.matrix(state.d), rev(cm.colors(5))) > plotcolors(cmat[state.o,state.o], rlabels=state.name[state.o]) > par(op) > > > > > graphics::par(get("par.postscript", env = .CheckExEnv)) > cleanEx(); ..nameEx <- "order.clusters" > > ### * order.clusters > > flush(stderr()); flush(stdout()) > > ### Name: order.clusters > ### Title: Orders clustered objects using hierarchical clustering > ### Aliases: order.clusters > ### Keywords: multivariate cluster > > ### ** Examples > > data(state) > state.d <- dist(state.x77) > > # Order the states, keeping states in a division together. > state.o <- order.clusters(-state.d, as.numeric(state.division)) > cmat <- dmat.color(as.matrix(state.d), rev(cm.colors(5))) > > op <- par(mar=c(1,6,1,1)) > rlabels <- state.name[state.o] > plotcolors(cmat[state.o,state.o], rlabels=rlabels) > par(op) > > # Alternatively, use kmeans to place the states into 6 clusters > state.km <- kmeans(state.d,6)$cluster > > # An ordering obtained from the kmeans clustering... > state.o <- unlist(memship2clus(state.km)) > > layout(matrix(1:2,nrow=1,ncol=2),widths=c(0.1,1)) > op <- par(mar=c(1,1,1,.2)) > state.colors <- cbind(state.km,state.km) > plotcolors(state.colors[state.o,]) > > par(mar=c(1,6,1,1)) > rlabels <- state.name[state.o] > plotcolors(cmat[state.o,state.o], rlabels=rlabels) > > par(op) > layout(matrix(1,1)) > > > # In the ordering above, the ordering of clusters and the > # ordering of objects within the clusters is arbitrary. > # order.clusters gives an improved order but preserves the kmeans clusters. > > state.o <- order.clusters(-state.d, state.km) > > # and replot > layout(matrix(1:2,nrow=1,ncol=2),widths=c(0.1,1)) > op <- par(mar=c(1,1,1,.2)) > state.colors <- cbind(state.km,state.km) > plotcolors(state.colors[state.o,]) > > par(mar=c(1,6,1,1)) > rlabels <- state.name[state.o] > plotcolors(cmat[state.o,state.o], rlabels=rlabels) > > par(op) > layout(matrix(1,1)) > > > > > graphics::par(get("par.postscript", env = .CheckExEnv)) > cleanEx(); ..nameEx <- "partition.crit" > > ### * partition.crit > > flush(stderr()); flush(stdout()) > > ### Name: partition.crit > ### Title: Combines the results of appplying an index to each group of > ### observations > ### Aliases: partition.crit > ### Keywords: multivariate cluster > > ### ** Examples > > x <- runif(20) > y <- runif(20) > g <- rep(c("a","b"),10) > > partition.crit(x,y,g) [1] 8.785694 > > data(bank) > # m is a homogeneity measure of each pairwise variable plot > m <- -colpairs(scale(bank[,-1]), partition.crit,gfun=gave,groups=bank[,1]) > > # Color panels by level of m and reorder variables so that > # pairs with high m are near the diagonal. Panels shown > # in pink have the highest amount of group homogeneity, as measured by > # gave. > cpairs(bank[,-1],order=order.single(m), panel.colors=dmat.color(m), + gap=.3,col=c("purple","black")[bank[,"Status"]+1], + pch=c(5,3)[bank[,"Status"]+1]) > > # Try a different measure > m <- -colpairs(scale(bank[,-1]), partition.crit,gfun=diameter,groups=bank[,1]) > > cpairs(bank[,-1],order=order.single(m), panel.colors=dmat.color(m), + gap=.3,col=c("purple","black")[bank[,"Status"]+1], + pch=c(5,3)[bank[,"Status"]+1]) > > # Result is the same, in this case. > > > > > cleanEx(); ..nameEx <- "pclen" > > ### * pclen > > flush(stderr()); flush(stdout()) > > ### Name: pclen > ### Title: Profile smoothness measures > ### Aliases: pclen pcglen > ### Keywords: hplot multivariate > > ### ** Examples > > x <- runif(20) > y <- runif(20) > pclen(x,y) [1] 6.986512 > > data(state) > mins <- apply(state.x77,2,min) > ranges <- apply(state.x77,2,max) - mins > state.m <- -colpairs(scale(state.x77,mins,ranges), pclen) > state.col <- dmat.color(state.m) > cparcoord(state.x77, panel.color= state.col) > # Get rid of the panels with long line segments (yellow) by reordering: > cparcoord(state.x77, order.endlink(state.m), state.col) > > > > > cleanEx(); ..nameEx <- "plotcolors" > > ### * plotcolors > > flush(stderr()); flush(stdout()) > > ### Name: plotcolors > ### Title: Plots a matrix of colors > ### Aliases: plotcolors imageinfo > ### Keywords: color hplot > > ### ** Examples > > > plotcolors(matrix(1:20,nrow=4,ncol=5)) > > plotcolors(matrix(1:20,nrow=4,ncol=5),ptype="points",cex=6) > > plotcolors(matrix(1:20,nrow=4,ncol=5),rlabels = c("a","b","c","d")) > > data(longley) > longley.cor <- cor(longley) > # A matrix with equal (or nearly equal) number of entries of each color. > longley.color <- dmat.color(longley.cor) > > plotcolors(longley.color, dlabels=rownames(longley.color)) > > # Could also reorder variables prior to plotting: > longley.o <- order.single(longley.cor) > longley.color <- longley.color[longley.o,longley.o] > > op <- par(mar=c(1,6,6,1)) > plotcolors(longley.color,rlabels=rownames(longley.color),clabels=rownames(longley.color) ) > par(op) > > > > > graphics::par(get("par.postscript", env = .CheckExEnv)) > cleanEx(); ..nameEx <- "utilities" > > ### * utilities > > flush(stderr()); flush(stdout()) > > ### Name: vec2distm > ### Title: Various utility functions > ### Aliases: vec2distm vec2dist lower2upper.tri.inds diag.off clus2memship > ### memship2clus > ### Keywords: cluster > > ### ** Examples > > vec <- 1:15 > vec2distm(vec) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 0 1 2 3 4 5 [2,] 1 0 6 7 8 9 [3,] 2 6 0 10 11 12 [4,] 3 7 10 0 13 14 [5,] 4 8 11 13 0 15 [6,] 5 9 12 14 15 0 > vec2dist(vec) 1 2 3 4 5 2 1 3 2 6 4 3 7 10 5 4 8 11 13 6 5 9 12 14 15 > diag.off(vec2distm(vec)) [1] 1 6 10 13 15 > lower2upper.tri.inds(5) [1] 1 2 5 3 6 8 4 7 9 10 > clus2memship(list(c(1,3,5),c(2,6),4)) [1] 1 2 1 3 1 2 > memship2clus(c(1,3,4,2,1,4,2,3,2,3)) [[1]] [1] 1 5 [[2]] [1] 4 7 9 [[3]] [1] 2 8 10 [[4]] [1] 3 6 > > > > ### *