0

Is it possible to train the data in relation to both the trainData$sp and trainData$sex ?

library(dplyr)
library(caret)
library(e1071)

data(crabs, package = "MASS")
crabs = mutate_if(crabs, is.character, as.factor)
set.seed(1234)
index <- createDataPartition(crabs$sp, p=0.70, list=FALSE)
trainData= crabs[index,]
testData= crabs[-index,]
model_knn1 = knn3(trainData[,4:8], testData$sp, cl=trainData$sp, k=1)

In the code above I'm only training the data in relation to the species (trainData$sp).

Lymmuar
  • 1
  • 1
  • 3
    The code that you provided does not work. I believe that you meant `model_knn1 = knn3Train(train[,4:8], test[,4:8], cl=train$sp, k=1)` – G5W Nov 16 '17 at 02:00
  • It works for me but I had to install the packages dplyr, caret and e1071. – Lymmuar Nov 16 '17 at 02:35
  • Your code defines train and test, but then uses testData and trainData that are not defined. – G5W Nov 16 '17 at 12:47
  • Sorry for being inconsistent I changed the names but forgot to change in the knn function. – Lymmuar Nov 16 '17 at 16:06

1 Answers1

0

The solution was to create a column with both values as one and use that :

crabs$parameter <- paste(crabs$sp , crabs$sex, sep="|")
Lymmuar
  • 1
  • 1