本文介绍了在 R 中绘制 Kohonen 地图时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读关于 R-bloggers 的这篇博文,但我对代码的最后一部分感到困惑,无法弄清楚.

I was reading through this blog post on R-bloggers and I'm confused by the last section of the code and can't figure it out.

http://www.r-bloggers.com/self-organising-maps-for-customer-segmentation-using-r/

I've attempted to recreate this with my own data. I have 5 variables that follow an exponential distribution with 2755 points.

I am fine with and can plot the map that it generates:

plot(som_model, type="codes")

The section of the code I don't understand is the:

var <- 1
var_unscaled <- aggregate(as.numeric(training[,var]),by=list(som_model$unit.classif),FUN = mean, simplify=TRUE)[,2]
plot(som_model, type = "property", property=var_unscaled, main = names(training)[var], palette.name=coolBlueHotRed)

As I understand it, this section of the code is suppose to be plotting one of the variables over the map to see what it looks like but this is where I run into problems. When I run this section of the code I get the warning:

Warning message:
In bgcolors[!is.na(showcolors)] <- bgcol[showcolors[!is.na(showcolors)]] :
number of items to replace is not a multiple of replacement length

and it produces the plot:

Which just some how doesn't look right...

Now what I think it has come down to is the way the aggregate function has re-ordered the data. The length of var_unscaled is 789 and the length of som_model$data, training[,var] and unit.classif are all of length 2755. I tried plotting the aggregated data, the result was no warning but an unintelligible graph (as expected).

Now I think it has done this because unit.classif has a lot of repeated numbers inside it and that's why it has reduced in size.

The question is, do I worry about the warning? Is it producing an accurate graph? What exactly is the "Property"'s section looking for in the plot command? Is there a different way I could "Aggregate" the data?

解决方案

I think that you have to create the palette color. If you put the argument

coolBlueHotRed <- function(n, alpha = 1) {rainbow(n, end=4/6, alpha=alpha)[n:1]}

and then try to get a plot, for example

plot(som_model, type = "count", palette.name = coolBlueHotRed)

the end is succesful.

This link can help you: http://rgm3.lab.nig.ac.jp/RGM/R_rdfile?f=kohonen/man/plot.kohonen.Rd&d=R_CC

这篇关于在 R 中绘制 Kohonen 地图时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:18