RStudio - 找不到对象 - kohonen 包

RStudio - object not found - kohonen pack

我正在尝试为 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  

当我使用相同的代码时出现这么多错误时,这段代码怎么可能在教程中起作用?

看起来错误是由于没有原始的类似数据框的对象。变量 "data-train","data" 的子集,从未被正确分配。 您需要首先按照创建训练数据集的注释行进行操作。

    # 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 还有一个名为 "data" 的函数,这就是它解释代码的方式。与 R 中的大多数函数一样,此函数不可子集化。

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

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