0

I'm trying to run a PCA on r following some codes I found online, yet unlike who wrote the code I get the error:

object of type 'closure' is not subsettable

I'm using leaf dataset, which you may find here:

http://archive.ics.uci.edu/ml/datasets/Leaf

the features are: Species, Specimen Number, Eccentricity, Aspect Ratio, Elongation, Solidity, Stochastic Convexity, Isoperimetric Factor, MaximalIndentation Depth, Lobedness, Average Intensity, Average Contrast, Smothness, Third Moment, Uniformity, Entropy

each column is numeric, and as I want to predict 'Species' I won't need Specimen Number:

leaf_data <- leaf_data[, c(1,3:ncol(leaf_data))]

Now, to prepeare the training data I'm using the following function:

stratified_labels <- function(df, variable, size){
set.seed(1000)
require(sampling)
temp = df

dfCounts <- table(df[variable])
if (size > min(dfCounts)){
  size <- min(dfCounts)
  }

if (size < 1 ){
  size = ceiling(table(temp[variable])*size)
  } else if (size >=1){
      size = rep(size, times=length(table(temp[variable])))
      }

strat = strata(temp, stratanames=names(temp[variable]),
             size = size, method = 'srswor')
return(strat$ID_unit)
}

which ensures each class has a uniform amount of representatives. Then we can prepare the training and test sets:

training_set <- stratified_labels(leaf_data, 'Species', .8)
leaf_data$Species <- as.factor(leaf_data$Species)
leaf_train <- leaf_data[training_set,]
leaf_test <- leaf_data[-training_set,]

As variables are not standardized, I make them as such:

leaf_train_standard <- leaf_train

standardization <- function(x){
x <- (x-mean(x))/sd(x)
return(x)
}
leaf_train_standard[2:15]<-apply(leaf_train[2:15],2, standardization)

So now I am theorically ready to run prcomp:

set.seed(1000)

pca_train <- leaf_train_standard[2:15]
pca_test <- leaf_test
pca <- prcomp[data = pca_train, center=FALSE, scale=FALSE]

but after this last line of code I get the above said error, and I really don't see why. Any help is appreciated.

Davide Trono
  • 99
  • 1
  • 1
  • 8

1 Answers1

2
pca <- prcomp[data = pca_train, center=FALSE, scale=FALSE]

Just as the error message indicates, you’re using subsetting here ([…] operators). You want to use (…):

pca <- prcomp(data = pca_train, center = FALSE, scale = FALSE)

Two other remarks:

Your standardization function is unnecessary. Just pass center = TRUE and scale = TRUE to prcomp.

And don’t use require — use library instead.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214