User Tools

Site Tools


en:hier-agglom_rscript
library (vegan)
data (dune)
 
# single link
single <- hclust (vegdist (dune), method = 'single')
complete <- hclust (vegdist (dune), method = 'complete')
average <- hclust (vegdist (dune), method = 'average')
 
png ('classification-single-complete-average.png', width = 12, height = 4, units = 'in', res = 300)
par (mfrow = c(1,3))
plot (as.dendrogram (single), main = 'Bray-Curtis distance\nSingle linkage', ylim = c(0,1))
plot (as.dendrogram (complete), main = 'Bray-Curtis distance\nComplete linkage', ylim = c(0,1))
plot (as.dendrogram (average), main = 'Bray-Curtis distance\nAverage linkage (UPGMA)', ylim = c(0,1))
dev.off ()
 
png ('classification-single-complete-average_vertical.png', width = 4, height = 12, units = 'in', res = 300, pointsize = 16)
par (mfrow = c(3,1))
plot (as.dendrogram (single), main = 'Bray-Curtis distance\nSingle linkage', axes = F)
plot (as.dendrogram (complete), main = 'Bray-Curtis distance\nComplete linkage', axes = F)
plot (as.dendrogram (average), main = 'Bray-Curtis distance\nAverage linkage (UPGMA)', axes = F)
dev.off ()
 
ward <- hclust (sqrt (vegdist (dune)), method = 'ward')
png ('classification-ward.png', width = 4, height = 4, units = 'in', res = 300)
plot (as.dendrogram (ward), main = "sqrt (Bray-Curtis distance)\nWard's minimum variance method")
dev.off ()
 
# beta flexible - three options
library (cluster)
library (vegan)
data (dune)
dune.dist <- vegdist (dune)
 
png ('classification-flexible-beta.png', width = 12, height = 4, units = 'in', res = 300)
par (mfrow = c(1,3))
beta <- -.95
beta.flex <- agnes (dune.dist, method = 'flexible', par.method = (1-beta)/2)
plot (as.dendrogram (beta.flex), main = 'Flexible beta (beta = -0.95)\nBray-Curtis distance')
 
beta <- -0.25
beta.flex <- agnes (dune.dist, method = 'flexible', par.method = (1-beta)/2)
plot (as.dendrogram (beta.flex), main = 'Flexible beta (beta = -0.25)\nBray-Curtis distance')
 
beta <- +.95
beta.flex <- agnes (dune.dist, method = 'flexible', par.method = (1-beta)/2)
plot (as.dendrogram (beta.flex), main = 'Flexible beta (beta = +0.95)\nBray-Curtis distance')
 
dev.off ()
 
 
 
# effect of transformation
danube.spe <- read.delim ('https://raw.githubusercontent.com/zdealveindy/anadat-r/master/data/danube.spe.txt', row.names = 1)
 
single.notransf <- hclust (vegdist (danube.spe), method = 'single')
single.transf <- hclust (vegdist (log1p (danube.spe)), method = 'single')
png ('classification-effect-of-transformation.png', width = 12, height = 6, units = 'in', res = 300)
par (mfrow = c(1,2))
plot (as.dendrogram (single.notransf), main = 'Species data [%]\n Bray-Curtis distance\nSingle linkage', ylim = c(0,.6))
plot (as.dendrogram (single.transf), main = 'log1p (Species data [%])\nBray-Curtis distance\nSingle linkage', ylim = c(0,.6))
dev.off ()
en/hier-agglom_rscript.txt · Last modified: 2019/04/14 17:47 by David Zelený