Predict Method for Feature Selection SVM
predict.Rd
This function predicts values based upon a model trained by svm. If class assigment is provided, confusion table, missclassification table, sensitivity and specificity are calculated.
Usage
# S3 method for class 'penSVM'
predict(object, newdata, newdata.labels = NULL, labels.universe=NULL, ...)
Arguments
- object
Object of class "penSVM", created by 'svmfs'
- newdata
A matrix containing the new input data, samples in rows, features in columns
- newdata.labels
optional, new data class labels
- labels.universe
important for models produced by loocv: all possible labels in the particular data set
- ...
additional argument(s)
Value
returns a list of prediction values for classes
- pred.class
predicted class
- tab
confusion table
- error
missclassification error
- sensitivity
sensitivity
- specificity
specificity
Examples
seed<- 123
train<-sim.data(n = 200, ng = 100, nsg = 10, corr=FALSE, seed=seed )
print(str(train))
#> List of 3
#> $ x : num [1:100, 1:200] 0.547 0.635 -0.894 0.786 2.028 ...
#> ..- attr(*, "dimnames")=List of 2
#> .. ..$ : chr [1:100] "pos1" "pos2" "pos3" "pos4" ...
#> .. ..$ : chr [1:200] "1" "2" "3" "4" ...
#> $ y : Named num [1:200] 1 -1 -1 1 -1 -1 1 -1 -1 -1 ...
#> ..- attr(*, "names")= chr [1:200] "1" "2" "3" "4" ...
#> $ seed: num 123
#> NULL
#train standard svm
my.svm<-svm(x=t(train$x), y=train$y, kernel="linear")
# test with other data
test<- sim.data(n = 200, ng = 100, nsg = 10, seed=(seed+1) )
# Check accuracy standard SVM
my.pred <-ifelse( predict(my.svm, t(test$x)) >0,1,-1)
# Check accuracy:
table(my.pred, test$y)
#>
#> my.pred -1 1
#> -1 81 23
#> 1 30 66
if (FALSE) # define set values of tuning parameter lambda1 for SCAD
lambda1.scad <- c (seq(0.01 ,0.05, .01), seq(0.1,0.5, 0.2), 1 )
# for presentation don't check all lambdas : time consuming!
# computation intensive; for demostration reasons only for the first 100 features
# and only for 10 Iterations maxIter=10, default maxIter=700
system.time(fit.scad<- svmfs(x=t(train$x)[,1:100],y=train$y, fs.method="scad", cross.outer= 0,
grid.search = "discrete", lambda1.set=lambda1.scad[1:3], show="none",
parms.coding = "none", maxIter=10,
inner.val.method = "cv", cross.inner= 5, seed=seed, verbose=FALSE))
#> [1] "grid search"
#> [1] "discrete"
#> [1] "show"
#> [1] "none"
#> [1] "feature selection method is scad"
#> Error: object 'lambda1.scad' not found
#> Timing stopped at: 0.001 0.001 0.001
# SCAD
test.error.scad<-predict(fit.scad, newdata=t(test$x)[,1:100],newdata.labels=test$y )
#> Error: object 'fit.scad' not found
# Check accuracy SCAD SVM
print(test.error.scad$tab)
#> Error: object 'test.error.scad' not found
# \dontrun{}
#########################################
## analog for 1-norm SVM
#epsi.set<-vector(); for (num in (1:9)) epsi.set<-sort(c(epsi.set, c(num*10^seq(-5, -1, 1 ))) )
#lambda1.1norm <- epsi.set[c(3,5)] # 2 params
#
## train 1norm SVM
# norm1.fix<- svmfs(t(train$x), y=train$y, fs.method="1norm",
# cross.outer= 0, grid.search = "discrete",
# lambda1.set=lambda1.1norm, show="none",
# parms.coding = "none",
# maxIter = 700, inner.val.method = "cv", cross.inner= 5,
# seed=seed, verbose=FALSE )
#
# print(norm1.fix)
#
## L1-norm SVM
#test.error.1norm<-predict(norm1.fix, newdata=t(test$x),newdata.labels=test$y )
# # Check accuracy L1-norm SVM
#print(test.error.1norm$tab)