1
votes

I'm a new R user. I got an error with the caret package I can't explain.

My experiment is made of 305 observations of 8 variables, and 3 classes. I want to train a model (here a neural network) and then predict classes for a test set of observations, with the probability for each class. As I want to try several models, I want to use the 'caret' package.

When running the following script, R stops with the following error while trying to predict :

Error in dimnames(out)[[2]] <- rev(modelFit$obsLevels) :
  la longueur de 'dimnames' [2] n'est pas égale à l'étendue du tableau

De plus : Warning message:
In nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo,  :
  There were missing values in resampled performance measures.

My Test object contains 14 observations with the 8 variables required. There are no missing data in any set.

Someone can help ?

Many thanks in advance ! Luc

Script:

library(nnet)
library(caret)

Vars <- read.csv('/home/moumou/ter/Lit.csv',header=FALSE)
Clas <- read.csv('/home/moumou/ter/Lot.csv',header=FALSE)
Test <- read.csv('/home/moumou/ter/Lie.csv',header=FALSE)
colnames(Vars) <- paste('col',1:ncol(Vars),sep='')
colnames(Clas) <- paste('cls',1:ncol(Clas),sep='')
colnames(Test) <- paste('col',1:ncol(Test),sep='')
dt1 <- data.frame(Clas,Vars)
summary(dt1)
dt2 <- as.data.frame(Test)
summary(dt2)
model <- train(cls1 + cls2 + cls3 ~ col1 + col2 + col3 + col4 + col5 + col6 + col7 + col8 , data = dt1, method='nnet')

pred <- predict(model,newdata=dt2,type='prob')
1
can you share these csv files?milos.ai
Sure ! Go to (lucmoulinier.fr) and you'll find the three files at the 'R CSV Files' section. Thanks you very much for having a look !luckyluc67

1 Answers

0
votes

Here's code that is working:

  Vars <- read.csv('/home/moumou/ter/Lit.csv',header=FALSE)
  Clas <- read.csv('/home/moumou/ter/Lot.csv',header=FALSE)
  Test <- read.csv('/home/moumou/ter/Lie.csv',header=FALSE)
  colnames(Vars) <- paste('col',1:ncol(Vars),sep='')
  colnames(Clas) <- paste('cls',1:ncol(Clas),sep='')
  colnames(Test) <- paste('col',1:ncol(Test),sep='')
  dt1 <- data.frame(Clas,Vars)
  summary(dt1)
  dt2 <- as.data.frame(Test)
  summary(dt2)

  dt1$class = as.factor(paste0("v",dt1$cls1 ,dt1$cls2, dt1$cls3))

  model <- train(class ~ col1 + col2 + col3 + col4 + col5 + col6 + col7 + col8 , data = dt1, method='nnet')

  pred <- predict.train(model,newdata=dt2,type='prob')

So there were several issues with your code. It seems class variable when combining (cls1 + cls2 + cls3) was actually numeric so instead of classification you were making regression. And type="prob" is just for classification.

Second, I had to add v in front of class variable values in order to get probabilities. Please check accepted answer on this SO question.