cv.tropsvm: Cross-Validation for Tropical Support Vector Machines

Description Usage Arguments Value See Also Examples

View source: R/cv.tropsvm.R

Description

Conduct k-fold cross validation for tropsvm and return an object "cv.tropsvm".

Usage

1
cv.tropsvm(x, y, parallel = FALSE, nfold = 10, nassignment = 10, ncores = 2)

Arguments

x

a data matrix, of dimension nobs x nvars; each row is an observation vector.

y

a response vector with one label for each row/component of x.

parallel

a logical value indicating if parallel computing should be used. (default: FALSE)

nfold

a numeric value of the number of data folds for cross-validation. (default: 10)

nassignment

a numeric value indicating the size of the parameter grid of assignments. (default: 10)

ncores

a numeric value indicating the number of threads utilized for multi-cored CPUs. (default: 2)

Value

object with S3 class cv.tropsvm containing the fitted model, including:

apex

The negative apex of the fitted optimal tropical hyperplane.

assignment

The best assignment tuned by cross-validation.

index

The best classification method tuned by cross-validation.

levels

The name of each category, consistent with categories in y.

accuracy

The validation accuracy for each fold.

nfold

The number of folds used in cross-validation.

See Also

summary, predict, coef and the tropsvm function.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# data generation
library(Rfast)
set.seed(101)
e <- 20
n <- 10
N <- 10
s <- 5
x <- rbind(
  rmvnorm(n, mu = c(5, -5, rep(0, e - 2)), sigma = diag(s, e)),
  rmvnorm(n, mu = c(-5, 5, rep(0, e - 2)), sigma = diag(s, e))
)
y <- as.factor(c(rep(1, n), rep(2, n)))
newx <- rbind(
  rmvnorm(N, mu = c(5, -5, rep(0, e - 2)), sigma = diag(s, e)),
  rmvnorm(N, mu = c(-5, 5, rep(0, e - 2)), sigma = diag(s, e))
)
newy <- as.factor(rep(c(1, 2), each = N))

# train the tropical svm
cv_tropsvm_fit <- cv.tropsvm(x, y, parallel = FALSE)

summary(cv_tropsvm_fit)
coef(cv_tropsvm_fit)

# test with new data
pred <- predict(cv_tropsvm_fit, newx)

# check with accuracy
table(pred, newy)

# compute testing accuracy
sum(pred == newy) / length(newy)

Rtropical documentation built on Nov. 10, 2021, 1:07 a.m.