Error: nrow (x) == n is not TRUE when using Train in Caret

I have a training kit that looks like

Name Day Area XY Month Night ATTACK Monday LA -122.41 37.78 8 0 VEHICLE Saturday CHICAGO -1.67 3.15 2 0 MOUSE Monday TAIPEI -12.5 3.1 9 1 

Name is the variable result / depend. I converted Name , Area and Day to factors, but I was not sure if I should be for Month and Night , which only accept integer values ​​1-12 and 0-1, respectively.

I then convert the data to a matrix

 ynn <- model.matrix(~Name , data = trainDF) mnn <- model.matrix(~ Day+Area +X + Y + Month + Night, data = trainDF) 

Then I adjust the settings

 nnTrControl=trainControl(method = "repeatedcv",number = 3,repeats=5,verboseIter = TRUE, returnData = FALSE, returnResamp = "all", classProbs = TRUE, summaryFunction = multiClassSummary,allowParallel = TRUE) nnGrid = expand.grid(.size=c(1,4,7),.decay=c(0,0.001,0.1)) model <- train(y=ynn, x=mnn, method='nnet',linout=TRUE, trace = FALSE, trControl = nnTrControl,metric="logLoss", tuneGrid=nnGrid) 

However, I get the error Error: nrow(x) == n is not TRUE for model<-train

I also get a similar error if I use xgboost instead of nnet

Does anyone know what causes this?

+7
r dataframe neural-network r-caret
source share
2 answers

y must be a numerical or factorial vector containing the result for each sample, not a matrix. Using

 train(y = make.names(trainDF$Name), ...) 

helps where make.names modifies the values ​​so that they can be valid variable names.

+10
source share

Although the train help file assumes that you expect either a maxtrix or a data frame, you can try to convert the matrix to a data frame:

 model <- train(y=ynn, x=as.data.frame(mnn), method='nnet',linout=TRUE, trace = FALSE, trControl = nnTrControl,metric="logLoss", tuneGrid=nnGrid) 
0
source share

All Articles