1

我在 R 中找到了一个很好的自组织地图聚类教程,其中解释了如何在单位空间中显示输入数据(见下文)。为了设置一些标记规则,我想计算每个神经元中每个类的概率并绘制它。计算概率相当容易:为每个单元取第 i 类的观察次数,然后除以该单元中的观察总数。我最终得到了data.frame pc。现在我很难映射这个结果,关于如何做到这一点的任何线索?

library(kohonen)
data(yeast)
set.seed(7)
yeast.supersom <- supersom(yeast, somgrid(8, 8, "hexagonal"),whatmap = 3:6)

classes <- levels(yeast$class)
colors <- c("yellow", "green", "blue", "red", "orange")
par(mfrow = c(3, 2))
plot(yeast.supersom, type = "mapping",pch = 1, main = "All", keepMargins = TRUE,bgcol = gray(0.85))

library(plyr)
pc <- data.frame(Var1=c(1:64))

for (i in seq(along = classes)) {
  X.class <- lapply(yeast, function(x) subset(x, yeast$class == classes[i]))
  X.map <- map(yeast.supersom, X.class)
  plot(yeast.supersom, type = "mapping", classif = X.map,
  col = colors[i], pch = 1, main = classes[i], add=TRUE)

  # compute percentage per unit
  v1F <- levels(as.factor(X.map$unit.classif))
  v2F <- levels(as.factor(yeast.supersom$unit.classif))
  fList<- base::union(v2F,v1F)
  pc <- join(pc,as.data.frame(table(factor(X.map$unit.classif,levels=fList))/table(factor(yeast.supersom$unit.classif,levels=fList))*100),by = 'Var1')
  colnames(pc)[NCOL(pc)]<-classes[i]
}
4

1 回答 1

1

好的,伙计们,这是一个解决方案:一旦我计算了概率,它就会从定义的渐变 ( rbPal) 中导出颜色代码。渐变由上限和下限定义,颜色的深浅与它们的间隔成正比。这是通过函数完成的findInterval

# compute percentage per unit
  v1F <- levels(as.factor(X.map$unit.classif))
  v2F <- levels(as.factor(yeast.supersom$unit.classif))
  fList<- base::union(v2F,v1F)
  pc <- join(pc,as.data.frame(table(factor(X.map$unit.classif,levels=fList))/table(factor(yeast.supersom$unit.classif,levels=fList))*100),by = 'Var1')
  colnames(pc)[NCOL(pc)]<-classes[i]
  rbPal <- colorRampPalette(c('blue','yellow','red'))
  plot(yeast.supersom, type="mapping", bgcol = rbPal((100))[(findInterval(pc[,which(colnames(pc)==as.character(classes[i]))], seq(0:100))+1)], main = paste("Probabily Clusters:", classes[i]))
于 2014-11-27T14:27:20.073 回答