Fits weighted kernel SVM. To be used for OWL with hinge loss (but can be used more generally)

weighted.ksvm(
  y,
  x,
  weights,
  C = c(0.1, 0.5, 1, 2, 10),
  kernel = "rbfdot",
  kpar = "automatic",
  nfolds = 10,
  foldid = NULL,
  eps = 1e-08,
  ...
)

Arguments

y

The response vector (either a character vector, factor vector, or numeric vector with values in -1, 1)

x

The design matrix (not including intercept term)

weights

vector of sample weights for weighted SVM

C

cost of constraints violation, see ksvm

kernel

kernel function used for training and prediction. See ksvm and kernels

kpar

list of hyperparameters for the kernel function. See ksvm

nfolds

number of cross validation folds for selecting value of C

foldid

optional vector of values between 1 and nfolds specifying which fold each observation is in. If specified, it will override the nfolds argument.

eps

penalty nugget parameter. Defaults to 1e-8

...

extra arguments to be passed to ipop from the kernlab package

See also

predict.wksvm for predicting from fitted weighted.ksvm objects

Examples


library(kernlab)
#> 
#> Attaching package: ‘kernlab’
#> The following object is masked from ‘package:ggplot2’:
#> 
#>     alpha

x <- matrix(rnorm(200 * 2), ncol = 2)

y <- 2 * (sin(x[,2]) ^ 2 * exp(-x[,2]) - 0.2 > rnorm(200, sd = 0.1)) - 1

weights <- runif(100, max = 1.5, min = 0.5)

wk <- weighted.ksvm(x = x[1:100,], y = y[1:100],
                    C = c(0.1, 0.5, 1, 2),
                    nfolds = 5,
                    weights = weights[1:100])

pr <- predict(wk, newx = x[101:200,])

mean(pr == y[101:200])
#> [1] 0.78