2013-06-20 3 views
1

감독되지 않은 직사각형 SOM 모델의 결과를 플로팅 할 필요가 있습니다. 추가 요구 사항 : 1) 각 노드를 해당 관찰 된 클래스가있는 파이 차트로 그립니다. 차트의 크기는 노드의 샘플 수를 반영해야합니다. 기본값 plot.kohonen은 그런 경우에 적합하지 않습니다.감독되지 않은 SOM 시각화

답변

1

가능한 해결책은 다음과 같습니다. 첫 번째 함수 som.prep.df은 두 번째 'som.draw'에서 호출됩니다. 두 번째 매개 변수는 SOM 모델과 관찰 된 교육 집합 클래스 만 사용합니다.

som.prep.df <- function(som.model, obs.classes, scaled) { 
    require(reshape2) 
    lev <- factor(wine.classes) 
    df <- data.frame(cbind(unit=som.model$unit.classif, class=as.integer(lev))) 
    # create table 
    df2 <- data.frame(table(df)) 
    df2 <- dcast(df2, unit ~ class, value.var="Freq") 
    df2$unit <- as.integer(df2$unit) 
    # calc sum 
    df2$sum <- rowSums(df2[,-1]) 
    # calc fraction borders of classes in each node 
    tmp <- data.frame(cbind(X0=rep(0,nrow(df2)), 
          t(apply(df2[,-1], 1, function(x) { 
          cumsum(x[1:(length(x)-1)])/x[length(x)] 
          })))) 
    df2 <- cbind(df2, tmp) 
    df2 <- melt(df2, id.vars=which(!grepl("^\\d$", colnames(df2)))) 
    df2 <- df2[,-ncol(df2)] 
    # define border for each classs in each node 
    tmp <- t(apply(df2, 1, function(x) { 
    c(x[paste0("X", as.character(as.integer(x["variable"])-1))], 
     x[paste0("X", as.character(x["variable"]))]) 
    })) 
    tmp <- data.frame(tmp, stringsAsFactors=FALSE) 
    tmp <- sapply(tmp, as.numeric) 
    colnames(tmp) <- c("ymin", "ymax") 
    df2 <- cbind(df2, tmp) 
    # scale size of pie charts 
    if (is.logical(scaled)) { 
    if (scaled) { 
     df2$xmax <- log2(df2$sum) 
    } else { 
     df2$xmax <- df2$sum 
    } 
    } 
    df2 <- df2[,c("unit", "variable", "ymin", "ymax", "xmax")] 
    colnames(df2) <- c("unit", "class", "ymin", "ymax", "xmax") 
    # replace classes with original levels names 
    df2$class <- levels(lev)[df2$class] 
    return(df2) 
} 

som.draw <- function(som.model, obs.classes, scaled=FALSE) { 
    # scaled - make or not a logarithmic scaling of the size of each node 
    require(ggplot2) 
    require(grid) 
    g <- som.model$grid 
    df <- som.prep.df(som.model, obs.classes, scaled) 
    df <- cbind(g$pts, df[,-1]) 
    df$class <- factor(df$class) 
    g <- ggplot(df, aes(fill=class, ymax=ymax, ymin=ymin, xmax=xmax, xmin=0)) + 
    geom_rect() + 
    coord_polar(theta="y") + 
    facet_wrap(x~y, ncol=g$xdim, nrow=g$ydim) + 
    theme(axis.ticks = element_blank(), 
      axis.text.y = element_blank(), 
      axis.text.x = element_blank(), 
      panel.margin = unit(0, "cm"), 
      strip.background = element_blank(), 
      strip.text = element_blank(), 
      plot.margin = unit(c(0,0,0,0), "cm"), 
      panel.background = element_blank(), 
      panel.grid = element_blank()) 
    return(g) 
} 

사용 예.

# Scaled map 
som.draw(som.wines, wine.classes, TRUE) 

enter image description here

enter image description here

이 함수
require(kohonen) 
data(wines) 
som.wines <- som(scale(wines), grid = somgrid(5, 5, "rectangular")) 

# Non-scaled map 
som.draw(som.wines, wine.classes) 

은 물론 관리 대상 모델의 시각화를 위해 이용 될 수있다. 그러나 직사각형지도에만 적합합니다. 희망이 사람을 도울 것입니다.

는 몇 가지 개선 사항이 있습니다

  1. 는 대수보다 더 나은 스케일링 기능을 선택합니다. 왜냐하면 이제는 단일 샘플을 가진 노드가 스케일링 후에 보이지 않기 때문입니다.
  2. 노드의 크기를 반영하는 전체 플롯에 범례를 추가하십시오.
  3. 또는 각 차트의 노드 채우기에 대한 정보를 추가하십시오.

추신. 코드는 매우 우아하지 않으므로 모든 제안 및 개선을 환영합니다.

관련 문제