Sujith Shivaprakash
Sujith Shivaprakash

Reputation: 161

Error in predict.svm in R

Click here to access the train and test data I used. I m new to SVM. I was trying the svm package in R to train my data which consists of 40 attributes and 39 labels. All attributes are of double type(most of them are 0's or 1's becuase I performed dummy encoding on the categorical attriubutes ) , the class label was of different strings which i later converted to a factor and its now of Integer type.

model=svm(Category~.,data=train1,scale=FALSE)
p1=predict(model,test1,"prob")

This was the result i got once i trained the model using SVM.

Call:
svm(formula = Category ~ ., data = train1, scale = FALSE)
Parameters:
   SVM-Type:  C-classification 
   SVM-Kernel:  radial 
   cost:  1 
  gamma:  0.02564103 

   Number of Support Vectors:  2230

I used the predict function

Error in predict.svm(model, test1, "prob") : 
NAs in foreign function call (arg 1)
In addition: Warning message:
In predict.svm(model, test1, "prob") : NAs introduced by coercion

I'm not understanding why this error is appearing, I checked all attributes of my training data none of them have NA's in them. Please help me with this. Thanks

Upvotes: 1

Views: 1701

Answers (1)

lrnzcig
lrnzcig

Reputation: 3947

I'm assuming you are using the package e1071 (you don't specify which package are you using, and as far as I know there is no package called svm).

The error message is confusing, but the problem is that you are passing "prob" as the 3rd argument, while the function expects a boolean. Try it like this:

require(e1071)
model=svm(Category~.,data=train1, scale=FALSE, probability=TRUE)
p1=predict(model,test1, probability = TRUE)
head(attr(p1, "probabilities"))

This is a sample of the output I get.

  WARRANTS OTHER OFFENSES LARCENY/THEFT VEHICLE THEFT  VANDALISM NON-CRIMINAL    ROBBERY    ASSAULT WEAPON LAWS   BURGLARY
1 0.04809877      0.1749634     0.2649921    0.02899535 0.03548131    0.1276913 0.02498949 0.08322866  0.01097913 0.03800846
  SUSPICIOUS OCC DRUNKENNESS FORGERY/COUNTERFEITING DRUG/NARCOTIC STOLEN PROPERTY SECONDARY CODES    TRESPASS MISSING PERSON
1 0.03255891     0.003790755   0.006249521    0.01944938     0.004843043      0.01305858 0.009727582     0.01840337
     FRAUD  KIDNAPPING     RUNAWAY DRIVING UNDER THE INFLUENCE SEX OFFENSES FORCIBLE PROSTITUTION DISORDERLY CONDUCT       ARSON
1 0.01884472 0.006089563 0.001378799                 0.003289503            0.01071418  0.004562048        0.003107619 0.002124643
  FAMILY OFFENSES LIQUOR LAWS      BRIBERY EMBEZZLEMENT      SUICIDE
1    0.0004787845 0.001669914 0.0007471968 0.0007465053 0.0007374036

Hope it helps.

Upvotes: 3

Related Questions