RStudio - 找不到对象 - kohonen pack

时间:2018-01-14 15:18:38

标签: r som

我正在尝试为som地图编写脚本。它来自tutorial。我的问题是Rstudio不起作用。我有这段代码:

require(kohonen)

# Create a training data set (rows are samples, columns are variables
# Here I am selecting a subset of my variables available in "data"
data_train <- data[, c(2,4,5,8)]

# Change the data frame with training data to a matrix
# Also center and scale all variables to give them equal importance during
# the SOM training process. 
data_train_matrix <- as.matrix(scale(data_train))

# Create the SOM Grid - you generally have to specify the size of the 
# training grid prior to training the SOM. Hexagonal and Circular 
# topologies are possible
som_grid <- somgrid(xdim = 20, ydim=20, topo="hexagonal")

# Finally, train the SOM, options for the number of iterations,
# the learning rates, and the neighbourhood are available
som_model <- som(data_train_matrix, 
                 grid=som_grid, 
                 rlen=500, 
                 alpha=c(0.05,0.01), 
                 keep.data = TRUE )
plot(som_model, type="changes")

如果我尝试运行此脚本,则会写入此错误:

Error in supersom(list(X), ...) : object 'data_train_matrix' not found
> plot(som_model, type="changes")
Error in plot(som_model, type = "changes") : object 'som_model' not found

我不明白这一点。这意味着什么没有data_train_matrix?我之前有data_train_matrix几行。当我只运行前3行代码(到data_train_matrix <- as.matrix(scale(data_train)))时,它会写出这个错误:

data_train_matrix <- as.matrix(scale(data_train))
Error in scale(data_train) : object 'data_train' not found

当我只运行前两行时:

data_train <- data[, c(2,4,5,8)]
Error in data[, c(2, 4, 5, 8)] : 
  object of type 'closure' is not subsettable  

如果使用相同的代码有很多错误,这段代码怎么可能在教程中工作?

1 个答案:

答案 0 :(得分:0)

看起来错误来自于没有原始数据帧类对象。变量&#34;数据列&#34;,&#34;数据&#34;的子集从未正确分配。 您需要先按照创建训练数据集的注释行。

    # Create a training data set (rows are samples, columns are variables
    # Here I am selecting a subset of my variables available in "data"
    data_train <- data[, c(2,4,5,8)]

R还有一个名为&#34; data&#34;的函数。这就是它解释代码的方式。与R中的大多数函数一样,此函数不是可子集化的。

如果你在最前面创建一些数据,一切都应该有效。

    data = data.frame(matrix(rnorm(20), nrow=2))
    data_train <- data[, c(2,4,5,8)]
    # the rest of the script as written