## ----global_options, include=FALSE--------------------------------------- knitr::opts_chunk$set(fig.width=6, fig.height=4, fig.path='Figs/', echo=T, warning=T,message=FALSE,cache=F,comment=NULL,fig_retina=NULL,purl=T) ## ---- echo=FALSE, fig.height=6------------------------------------------- hc <- hclust(dist(USArrests[15:25,]), "ave") plot(hc, xlab=" ", sub='') ## ------------------------------------------------------------------------ library(ape) hc <- hclust(dist(USArrests[15:25,]), "ward.D") grupos<-cutree(hc, k=3) hc.phylo<-as.phylo(hc) ## ------------------------------------------------------------------------ plot(hc.phylo, type='phylogram', tip.color=grupos) ## ------------------------------------------------------------------------ plot(hc.phylo, type='cladogram', tip.color=grupos) ## ------------------------------------------------------------------------ plot(hc.phylo, type='fan', tip.color=grupos) ## ------------------------------------------------------------------------ plot(hc.phylo, type='unrooted', tip.color=grupos) ## ------------------------------------------------------------------------ plot(hc.phylo, type='radial', tip.color=grupos) ## ------------------------------------------------------------------------ library(FactoMineR) library(readxl) library(bpca) library(graphics) library(cluster) library(fpc) ## ---- results = "hide", message = FALSE---------------------------------- cafe<-read_excel('cafe.xls') ## ------------------------------------------------------------------------ head(cafe) ## ------------------------------------------------------------------------ par(mfrow=c(1,2)) cafe.pca<-PCA(cafe[,2:4]) # 2:4: colunas ## ------------------------------------------------------------------------ par(mfrow=c(1,2)) plot(cafe.pca,choix=c('ind')) # linhas: consumidores plot(cafe.pca,choix=c('var')) # colunas: produtos ## ------------------------------------------------------------------------ head(round(cafe.pca$ind$contrib[,1:2],2), n=10) # consumidores ## ------------------------------------------------------------------------ round(cafe.pca$var$contrib[,1:2],2) # produtos ## ------------------------------------------------------------------------ round(cafe.pca$var$coord[,1:2],2) round(cafe.pca$eig,2) ## ------------------------------------------------------------------------ bp2 <- bpca(cafe[,2:4],d=1:2,scale=F) plot.bpca.2d(bp2,var.cex=1.2,obj.cex=1.2,var.offset=.2, var.factor=.45) ## ------------------------------------------------------------------------ bp3 <- bpca(cafe[,2:4],d=1:3) plot.bpca.3d(bp3) ## ----eval=F-------------------------------------------------------------- ## plot.bpca.3d(bp3,rgl=T) ## ------------------------------------------------------------------------ cafe.hpc<-HCPC(PCA(cafe[,2:4]),graph=FALSE) ## ------------------------------------------------------------------------ par(mfrow=c(1,2)) plot(cafe.hpc) plot(cafe.hpc,choice = 'map') par(mfrow=c(1,1)) ## ------------------------------------------------------------------------ cafe.hpc$data.clust ## ------------------------------------------------------------------------ cafe.d<-dist(cafe[,2:4]) # matriz de distâncias hc1<-hclust(cafe.d) # ligação completa ## ------------------------------------------------------------------------ plot(hc1,main= "Dendrograma - Café", xlab="Café", sub="Ligação Completa - Dist. Euclidiana") ## ------------------------------------------------------------------------ hc2 <- hclust(cafe.d,method="ward.D") # Ward ## ------------------------------------------------------------------------ plot(hc2, main= "Dendrograma - Café", xlab="Café", sub="Ward - Dist. Euclidiana") rect.hclust(hc2, k = 3, border = 'red') # k=3 grupos ## ------------------------------------------------------------------------ cafe.g <- cutree(hc2, k=3) # identificando consumidores cafe.g ## ------------------------------------------------------------------------ names(cafe) table(cafe.g, cafe$Consumidor) ## ------------------------------------------------------------------------ table(cafe.hpc$data.clust$clus) ## ------------------------------------------------------------------------ cafe.hpc$desc.ind$para ## ------------------------------------------------------------------------ cafe.hpc$desc.ind$dist ## ------------------------------------------------------------------------ head(cafe) ## ------------------------------------------------------------------------ cafe.k<-kmeans(cafe[,2:4],3) table(cafe.k$cluster) ## ------------------------------------------------------------------------ cafe.k$cluster