Mayank Raj
Mayank Raj

Reputation: 121

SVM plot not showing

Why is the plot not appearing? There is also no error coming.

usd28 = read.csv("~/ICICI_nse_train_head")
usd30= usd28[1:2000,]

index<-1:nrow(usd30)
testindex<-sample(index,trunc(length(index)/3))
testset<-usd30[testindex,]
trainset<-usd30[-testindex,]
svm.model<-svm(sprd_cross_dir~bid_sprd0+ask_sprd0,data=trainset,cost=5,gamma=1)
svm.pred<-predict(svm.model,testset)
summary(svm.model)
x<-table(pred=svm.pred,true=testset$sprd_cross_dir)
plot(x=svm.model,data=trainset,formula =sprd_cross_dir~bid_sprd0+ask_sprd0 ,fill=TRUE)

Data is

   ask_sprd0 bid_sprd0 sprd_cross_dir
           5         5              0
          65         5              0
          65         5              0
          10        15              0
          20         5              0
          20         5              1
          20        10              1
          20        10              1
          20        10              0
          20        10              0
          20         5              0
          10        15              0
          20         5              0
          20         5              1
          20        10              1
          20        10              1
          20        10              0
          20        10              0
          20         5              1
          20        10              1
          20        10              1
          25        10              0
          10        15             -1
          25        15              0
          25         5              0
          10        15             -1
          10        15             -1
          25        15              1

dput() version:

structure(list(ask_sprd0 = c(5L, 65L, 65L, 10L, 20L, 20L, 20L, 
20L, 20L, 20L, 20L, 10L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
20L, 25L, 10L, 25L, 25L, 10L, 10L, 25L), bid_sprd0 = c(5L, 5L, 
5L, 15L, 5L, 5L, 10L, 10L, 10L, 10L, 5L, 15L, 5L, 5L, 10L, 10L, 
10L, 10L, 5L, 10L, 10L, 10L, 15L, 15L, 5L, 15L, 15L, 15L), sprd_cross_dir = c(0, 
0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 
-1, 0, 0, -1, -1, 1)), .Names = c("ask_sprd0", "bid_sprd0", "sprd_cross_dir"
), class = "data.frame", row.names = c(NA, -28L))

Upvotes: 1

Views: 1790

Answers (1)

zx8754
zx8754

Reputation: 56199

I think response variable sprd_cross_dir should be factor:

require(e1071)

#dummy data
usd28  <- structure(list(ask_sprd0 = c(5L, 65L, 65L, 10L, 20L, 20L, 20L, 
                             20L, 20L, 20L, 20L, 10L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
                             20L, 25L, 10L, 25L, 25L, 10L, 10L, 25L), bid_sprd0 = c(5L, 5L, 
                                                                                    5L, 15L, 5L, 5L, 10L, 10L, 10L, 10L, 5L, 15L, 5L, 5L, 10L, 10L, 
                                                                                    10L, 10L, 5L, 10L, 10L, 10L, 15L, 15L, 5L, 15L, 15L, 15L), sprd_cross_dir = c(0, 
                                                                                                                                                                  0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 
                                                                                                                                                                  -1, 0, 0, -1, -1, 1)), .Names = c("ask_sprd0", "bid_sprd0", "sprd_cross_dir"
usd30 = usd28
#change response variable to factor
usd30$sprd_cross_dir <- as.factor(usd30$sprd_cross_dir)

index<-1:nrow(usd30)
testindex<-sample(index,trunc(length(index)/3))
testset<-usd30[testindex,]
trainset<-usd30[-testindex,]

svm.model<-svm(sprd_cross_dir~bid_sprd0+ask_sprd0,data=trainset,cost=5,gamma=1)
plot(svm.model,data=trainset,fill=TRUE)

Upvotes: 2

Related Questions