# SVMMAJ - SUPPORT VECTOR MACHINE - AUS CREDIT SCORING 
library("SVMMaj", lib.loc="~/R/win-library/3.1")
## Loading required package: kernlab
attach(AusCredit)
str(AusCredit);View(AusCredit)
## List of 2
##  $ X:'data.frame':   690 obs. of  14 variables:
##   ..$ X1 : logi [1:690] TRUE FALSE FALSE FALSE TRUE FALSE ...
##   ..$ X2 : num [1:690] 22.1 22.7 29.6 21.7 20.2 ...
##   ..$ X3 : num [1:690] 11.46 7 1.75 11.5 8.17 ...
##   ..$ X4 : num [1:690] 2 2 1 1 2 2 2 2 1 2 ...
##   ..$ X5 : num [1:690] 4 8 4 5 6 8 3 11 2 4 ...
##   ..$ X6 : num [1:690] 4 4 4 3 4 8 4 8 8 8 ...
##   ..$ X7 : num [1:690] 1.585 0.165 1.25 0 1.96 ...
##   ..$ X8 : logi [1:690] FALSE FALSE FALSE TRUE TRUE TRUE ...
##   ..$ X9 : logi [1:690] FALSE FALSE FALSE TRUE TRUE TRUE ...
##   ..$ X10: num [1:690] 0 0 0 11 14 2 0 6 0 3 ...
##   ..$ X11: logi [1:690] TRUE FALSE TRUE TRUE FALSE FALSE ...
##   ..$ X12: num [1:690] 2 2 2 2 2 2 2 2 2 2 ...
##   ..$ X13: num [1:690] 100 160 280 0 60 100 60 43 176 100 ...
##   ..$ X14: num [1:690] 1213 1 1 1 159 ...
##  $ y: Factor w/ 2 levels "Accepted","Rejected": 2 2 2 1 1 1 2 1 2 2 ...
# 
# summary(X)
# summary(y)
# detach(AusCredit)
#
?svmmaj
## starting httpd help server ... done
#  in SVMMaj - class of each attribute can be either numeric, logical or factor.
# This is Unlike RWeka - 

# nnX<-X[1:400,]
# str(nnX) # nnX Data Frame - takes in all variables besides Y , thus X1 to X14 All IN ...
# # X1 : logi , then --- X8 and X9: logi ....All IN till X14 
# #
# nny<-y[1:400] # Note - No "," next to 400 as "y" here is a Column Vector ...
# str(nny) # nny Data Frame 
#
#

## model training from Aus Credit data , using 400 Obs for Train Set .. 
model_SVMMaj <- svmmaj(X[1:400,],y[1:400],hinge='quadratic', lambda=1)
# str(model_SVMMaj)
## model prediction - using balance 290 Obs for Prediction or Test set .. 
q4 <- predict(model_SVMMaj,X[-(1:400),],y[-(1:400)],show.plot=TRUE)

q4
## Prediction frequencies:
##           Accepted Rejected
## frequency      144      146
## 
## Confusion matrix:
##             Predicted(yhat)
## Observed (y) Accepted Rejected Total
##     Accepted      114       14   128
##     Rejected       30      132   162
##     Total         144      146   290
## 
## Classification Measures:
##                                            
##    hit rate                           0.848
##    misclassification rate             0.152
## 
##                     TP        FP Precision
##     Accepted     0.891     0.109     0.792
##     Rejected     0.815     0.185     0.904
#
## model training-1  , using 500 Obs for Train Set .. 
model_SVMMaj1 <- svmmaj(X[1:500,],y[1:500],hinge='quadratic', lambda=1)
# str(model_SVMMaj1)
## model prediction-1  - using balance 190 Obs for Prediction or Test set .. 
SVM_1_Plot<- predict(model_SVMMaj1,X[-(1:500),],y[-(1:500)],show.plot=TRUE)

SVM_1_Plot # Here we see correctly Predicted Objects == 88.42% as compared to 84.83% above ...
## Prediction frequencies:
##           Accepted Rejected
## frequency       93       97
## 
## Confusion matrix:
##             Predicted(yhat)
## Observed (y) Accepted Rejected Total
##     Accepted       75        4    79
##     Rejected       18       93   111
##     Total          93       97   190
## 
## Classification Measures:
##                                            
##    hit rate                           0.884
##    misclassification rate             0.116
## 
##                     TP        FP Precision
##     Accepted     0.949    0.0506     0.806
##     Rejected     0.838    0.1622     0.959
#
# #yet to be Done --- 
# I-spline basis
# model3 <- svmmaj(X,y,weights.obs=weights.obs,spline.knots=3,spline.degree=2)
# plotWeights(model3,plotdim=c(2,4))