0

Its the follow I have two files one with the training data other with the test data without the class I want to predict and I am trying to execute the follow code:

full  <- bind_rows(train, test)

full$Survived <- factor(full$Survived)



train <- full[1:n,]
test <- full[n+1:total,]


model.svm <- svm(Survived~.,train)
predictions <- predict(model.svm,test)

But when I tried to predict give me the follow error:

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :contrasts can be applied only to factors with 2 or more levels

As I understand is because the class column is all NA's but don't know what to do about this, I already try to fill it with dummy values only to get the predictions and I get this:

Error in newdata[, object$scaled, drop = FALSE] : (subscript) logical subscript too long

Someone can tell me what I am doing wrong and how to correct? Edit: Note I am doing binary classification if it help. Thanks in the advance.

EDIT: The dataset is the titanic survived people, that I am using for learn how to use some models( I am begging to learn this kind of things).

str(full):

'data.frame':   1309 obs. of  12 variables:
 $ PassengerId: int  1 2 3 4 5 6 7 8 9 10 ...
 $ Survived   : Factor w/ 2 levels "0","1": 1 2 2 2 1 1 1 1 2 2 ...
 $ Pclass     : int  3 1 3 1 3 3 1 3 3 2 ...
 $ Name       : chr  "Braund, Mr. Owen Harris" "Cumings, Mrs. John Bradley (Florence Briggs Thayer)" "Heikkinen, Miss. Laina" "Futrelle, Mrs. Jacques Heath (Lily May Peel)" ...
 $ Sex        : chr  "male" "female" "female" "female" ...
 $ Age        : num  22 38 26 35 35 NA 54 2 27 14 ...
 $ SibSp      : int  1 1 0 1 0 0 0 3 0 1 ...
 $ Parch      : int  0 0 0 0 0 0 0 1 2 0 ...
 $ Ticket     : chr  "A/5 21171" "PC 17599" "STON/O2. 3101282" "113803" ...
 $ Fare       : num  7.25 71.28 7.92 53.1 8.05 ...
 $ Cabin      : chr  "" "C85" "" "C123" ...
 $ Embarked   : chr  "S" "C" "S" "S" ...

dput is to long to put it here.

I think I have some NA's in age is it a problem?

warwcat
  • 309
  • 3
  • 5
  • 13
  • you will need to provide the dataset. Please use dput on the `full` object and paste the result in your post. – Taran Dec 08 '17 at 14:49
  • There's an input in your model which is either a factor or a character. Please provide the structure of your data. – Sam Dec 08 '17 at 14:50
  • I dont see the `class` column in your training data, yet thats what you're using to train the model i.e. `model.svm <- svm(class ~.,train)`. Is that a typo? – Taran Dec 08 '17 at 15:17
  • Sorry I will correct, its correct in my code the class is Survived – warwcat Dec 08 '17 at 15:20

0 Answers0