Abdullah Al Imran
Abdullah Al Imran

Reputation: 1176

R (Caret) - Error while training "mlpML" model in loop

I'm using the Caret package from R to train Multilayer Perceptron (mlpML) models.

What I'm trying to do is Forward feature elimination technique and see how mlpML models perform for a different number of features.

That's why I was training mlpML models in a loop wherein every iteration, one new feature was added and fed into the model.

Here is the error I got -

Error: Please use column names for `x`

Here is my code -

ig_features <- c("F1", "F2", "F3", "F4", "F5")

library(caret)

x_features <- c()

for (i in ig_features) {

  x_features <- c(x_features, i)
  y_features <- c("Status")

  #------------------------------------------ Building Model ---------------------------------------------------
  set.seed(1234)

  mlp_grid = expand.grid(layer1 = 10,
                         layer2 = 10,
                         layer3 = 10)

  mlp_fit = caret::train(x = TRAIN[,x_features], 
                         y = TRAIN[,y_features],
                         method = "mlpML",
                         preProc =  c('center', 'scale', 'knnImpute', 'pca'),
                         trControl = trainControl(method = "cv", verboseIter = TRUE, returnData = FALSE),
                         tuneGrid = mlp_grid)

  #------------------------------------------ Prediction & Evaluation -----------------------------------------
  predictions <- predict(mlp_fit, newdata=TEST[,x_features])

  cat("Accuracy:",confusionMatrix(predictions, 
                                  PARKINSON_TRAIN$Status, 
                                  dnn = c("Prediction", "Actual"), 
                                  positive="1")$overall[[1]],"\n")
}

Upvotes: 0

Views: 1258

Answers (1)

nadizan
nadizan

Reputation: 1373

You are missing column names for your x, which in this case is TRAIN[,x_features]. See ?caret::train documentation, which states:

x: For the default method, x is an object where samples are in rows and features are in columns. This could be a simple matrix, data frame or other type (e.g. sparse matrix) but must have column names (see Details below).

Upvotes: 1

Related Questions