0
$\begingroup$

I'm new to Caret and I've been trying a couple things to get the hang of things. But this error happened to me and I'm not sure why.

I've been trying to train a model with some data I got from "PimaIndiansDiabetes". These are the "x" and "y" I'm using:

>str(pima.Datos.Train[pima.Vars.Entrada.Usadas])
'data.frame': 615 obs. of 8 variables:
\$ pregnant: num 6 1 8 1 3 10 2 8 4 10 ...
\$ glucose : num 148 85 183 89 78 115 197 125 110 139 ...
\$ pressure: num 72 66 64 66 50 0 70 96 92 80 ...
\$ triceps : num 35 29 0 23 32 0 45 0 0 0 ...
\$ insulin : num 0 0 0 94 88 0 543 0 0 0 ...
\$ mass : num 33.6 26.6 23.3 28.1 31 35.3 30.5 0 37.6 27.1 ...
\$ pedigree: num 0.627 0.351 0.672 0.167 0.248 ...
\$ age : num 50 31 32 21 26 29 53 54 30 57 ...

>str(pima.Datos.Train[pima.Var.Salida.Usada])
'data.frame': 615 obs. of 1 variable:
\$ diabetes: Factor w/ 2 levels "neg","pos": 2 1 2 1 2 1 2 2 1 1 ...

>pima.modelo <- train(pima.Datos.Train[pima.Vars.Entrada.Usadas],
pima.Datos.Train[pima.Var.Salida.Usada],
method='mlp')

train returns an Error: Please make sure y is a factor or numeric value. But as far as I know, "y" is a factor, so I'm not really sure where the Error comes from. ¿Any help with this?
Thanks in advance

$\endgroup$

1 Answer 1

1
$\begingroup$

Have you done any pre-processing or data manipulation before training the model?

Its hard for me to say what is the reason the error, but I have tried to do the same task and it worked whithout errors, here is my code:

library(mlbench)
library(caret)
df <- PimaIndiansDiabetes

#Using x, y arguments

modelo <- train(x = df[, 1:8] , y = df$diabetes, method='mlp') 

#Using formula
modelo <- train(diabetes ~.,  data = df, method='mlp') 

Both approaches valid, and produce result like this:

> modelo <- train(x = df[, 1:8] , y = df$diabetes, method='mlp')
> modelo
Multi-Layer Perceptron 

768 samples
  8 predictor
  2 classes: 'neg', 'pos' 

No pre-processing
Resampling: Bootstrapped (25 reps) 
Summary of sample sizes: 768, 768, 768, 768, 768, 768, ... 
Resampling results across tuning parameters:

  size  Accuracy   Kappa        
  1     0.6545936   0.0000000000
  3     0.6416382  -0.0008031577
  5     0.6397277   0.0002662915

Accuracy was used to select the optimal model using the largest value.
The final value used for the model was size = 1.

Please note that your results might differ a little.

$\endgroup$
5
  • $\begingroup$ Yes, but I only created a Data Partition (to have my Train and Test partitions). pima.Var.Salida.Usada is just "diabetes", and "pima.Vars.Entrada.Usadas" is everything minus "diabetes". I'll try your code and then modify it so it looks like mine, and I'll try to see where the mistake was. Thank you for your time! $\endgroup$ Commented Nov 30, 2018 at 13:15
  • $\begingroup$ @JaimeMartínez hope the code helps. I have tried the same with train/test partitions, the code works. I have done it like this, maybe it will be usefull for you. df <- PimaIndiansDiabetes trainRowNumbers <- createDataPartition(df$diabetes, p = 0.8, list = F) trainData <- df[trainRowNumbers, ] testData <- df[-trainRowNumbers, ] modelo <- train(diabetes ~., data = trainData, method='mlp') $\endgroup$ Commented Nov 30, 2018 at 13:26
  • $\begingroup$ Analyzing your code and mine, I have just seen that the problem is this: pima.out<-c("diabetes") pima.ins<-setdiff(names(df),pima.out) #Everything but diabetes model<-train(df[pima.in], y = df[pima.out], method='mlp'). When y = df$diabetes it does not return the error, but when I try to use y = df[pima.out] it does. $\endgroup$ Commented Nov 30, 2018 at 13:42
  • $\begingroup$ @JaimeMartínez if you try to check types for typeof(df[pima.out]) you get "list" and for typeof(df$diabetes) you get integer - I guess this is the source of the problem. $\endgroup$ Commented Nov 30, 2018 at 14:50
  • $\begingroup$ Yes that seems to be it. Thank you for your help, I will try not to repeat this mistake in the future! $\endgroup$ Commented Nov 30, 2018 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.