rsom

RStudio - object not found - kohonen pack


I'm trying to write a script for som map. It comes from this tutorial. My problem is that Rstudio doesn't work. I have this code :

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")

If I try to run this script it writes this error :

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

I dont understand this. What does it means there is not data_train_matrix? I have data_train_matrix a few lines before. When I run just first 3 lines of code (to data_train_matrix <- as.matrix(scale(data_train))) it writes this error :

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

and when I run just the first two lines it writes :

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

How is it possible that this code works in tutorial while I have so many errors using the same code ?


Solution

  • It looks like the error comes from having no original dataframe-like object. The variable "data-train", a subset of "data", was never properly assigned. You need to first follow the commented line of creating a training data set.

        # 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 also has a function named "data" and that is how it interprets the code. This function is not subsettable, like most functions in R.

    If you create some data at the very front, everything should work.

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