### Example Usage of Online Kernel Learning Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Demonstrates creating toy data, initializing an 'onlearn' object, and performing online learning. ```R ## create toy data set x <- rbind(matrix(rnorm(100),,2),matrix(rnorm(100)+3,,2)) y <- matrix(c(rep(1,50),rep(-1,50)),,1) ## initialize onlearn object on <- inlearn(2, kernel = "rbfdot", kpar = list(sigma = 0.2), type = "classification") ## learn one data point at the time for(i in sample(1:100,100)) on <- onlearn(on,x[i,],y[i],nu=0.03,lambda=0.1) sign(predict(on,x)) ``` -------------------------------- ### Initialize RBF Kernel and Check Properties Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Demonstrates how to initialize an RBF kernel with a specified sigma and check its properties. This is a common setup for kernel-based methods. ```R rbfkernel <- rbfdot(sigma = 0.1) rbfkernel is(rbfkernel) kpar(rbfkernel) ``` -------------------------------- ### RBF Kernel Function Example Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Demonstrates creating and using the Radial Basis Function (RBF) kernel. Access kernel parameters using kpar. ```R rbfkernel <- rbfdot(sigma = 0.1) rbfkernel kpar(rbfkernel) ## create two vectors x <- rnorm(10) y <- rnorm(10) ## calculate dot product rbfkernel(x,y) ``` -------------------------------- ### Kernel Quantile Regression with Prediction and Plotting Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Demonstrates training a kernel quantile regression (KQR) model for median and 0.9 quantile prediction, followed by plotting the results against the original data. Ensure the 'kernlab' package is installed. ```R # create data x <- sort(runif(300)) y <- sin(pi*x) + rnorm(300,0,sd=exp(sin(2*pi*x))) # first calculate the median qrm <- kqr(x, y, tau = 0.5, C=0.15) # predict and plot plot(x, y) ytest <- predict(qrm, x) lines(x, ytest, col="blue") # calculate 0.9 quantile qrm <- kqr(x, y, tau = 0.9, kernel = "rbfdot", kpar= list(sigma=10), C=0.15) ytest <- predict(qrm, x) lines(x, ytest, col="red") ``` -------------------------------- ### Perform Kernel Maximum Mean Discrepancy Test Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Example demonstrating the creation of sample data and the execution of the kmmd test. ```R # create data x <- matrix(runif(300),100) y <- matrix(runif(300)+1,100) mmdo <- kmmd(x, y) mmdo ``` -------------------------------- ### Kernel PCA Usage Example Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html An example demonstrating Kernel Principal Component Analysis on the iris dataset, including plotting the data projection and embedding remaining points. ```R # another example using the iris data(iris) test <- sample(1:150,20) kpc <- kpca(~.,data=iris[-test,-5],kernel="rbfdot", kpar=list(sigma=0.2),features=2) #print the principal component vectors pc v(kpc) #plot the data projection on the components plot(rotated(kpc),col=as.integer(iris[-test,5]), xlab="1st Principal Component",ylab="2nd Principal Component") #embed remaining points emb <- predict(kpc,iris[test,-5]) points(emb,col=as.integer(iris[test,5])) ``` -------------------------------- ### Example Usage of kha Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Demonstrates how to use the 'kha' function with the iris dataset to perform kernel principal component analysis. Shows how to extract principal component vectors, kernel function, and normalization values. ```R data(iris) test <- sample(1:50,20) kpc <- kha(~.,data=iris[-test,-5], kernel="rbfdot", kpar=list(sigma=0.2),features=2, eta=0.001, maxiter=65) #print the principal component vectors pcvi(kpc) kernelf(kpc) eig(kpc) ``` -------------------------------- ### Train SVM Classifier with promotergene Data Source: https://cran.r-project.org/web/packages/kernlab/vignettes/kernlab.Rnw Demonstrates a basic example of using the ksvm function with the promotergene dataset. It involves creating training and testing sets for classification. ```R ## simple example using the promotergene data set data(promotergene) ## create test and training set tindex <- sample(1:dim(promotergene)[1],5) genetrain <- promotergene[-tindex, ] genetest <- promotergene[tindex,] ``` -------------------------------- ### Train and Predict with SVM using promotergene data Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Demonstrates training a Support Vector Machine (SVM) with a radial basis function kernel on the promotergene dataset and predicting gene type probabilities on a test set. Ensure the 'kernlab' package is installed and loaded. ```R data(promotergene) ## create test and training set ind <- sample(1:dim(promotergene)[1],20) genetrain <- promotergene[-ind, ] genetest <- promotergene[ind, ] ## train a support vector machine gene <- ksvm(Class~.,data=genetrain,kernel="rbfdot", kpar=list(sigma=0.015),C=70,cross=4,prob.model=TRUE) gene ## predict gene type probabilities on the test set genetype <- predict(gene,genetest,type="probabilities") genetype ``` -------------------------------- ### Perform Kernel Feature Analysis Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Example usage of the kfa function with the promotergene dataset. ```R data(promotergene) f <- kfa(~.,data=promotergene) ``` -------------------------------- ### Perform Online Learning with onlearn Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Demonstrates initializing an onlearn object and iteratively updating it with new data points. ```R ## create toy data set x <- rbind(matrix(rnorm(100),,2),matrix(rnorm(100)+3,,2)) y <- matrix(c(rep(1,50),rep(-1,50)),,1) ## initialize onlearn object on <- inlearn(2,kernel="rbfdot",kpar=list(sigma=0.2), type="classification") ## learn one data point at the time for(i in sample(1:100,100)) on <- onlearn(on,x[i,],y[i],nu=0.03,lambda=0.1) sign(predict(on,x)) ``` -------------------------------- ### Initialize Kernel and Data for Kernlab Source: https://cran.r-project.org/web/packages/kernlab/vignettes/kernlab.Rnw Sets up a polynomial kernel and generates artificial data matrices for testing kernel operations. ```R ## create a RBF kernel function with sigma hyper-parameter 0.05 poly <- polydot(degree=2) ## create artificial data set x <- matrix(rnorm(60), 6, 10) y <- matrix(rnorm(40), 4, 10) ``` -------------------------------- ### Onlearn object initialization Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Initializes an Online Kernel Algorithm object 'onlearn' with specified parameters. ```APIDOC ## S4 method for signature 'numeric' inlearn(d, kernel = "rbfdot", kpar = list(sigma = 0.1), type = "novelty", buffersize = 1000) ### Description Online Kernel Algorithm object `onlearn` initialization function. ### Arguments - `d` (numeric) - the dimensionality of the data to be learned - `kernel` (string or function) - the kernel function used in training and predicting. Can be one of: `rbfdot`, `polydot`, `vanilladot`, `tanhdot`, `laplacedot`, `besseldot`, `anovadot`, or a user-defined function. - `kpar` (list) - the list of hyper-parameters for the kernel function. Examples include `sigma` for `rbfdot` and `laplacedot`, `degree`, `scale`, `offset` for `polydot`, etc. - `type` (string) - the type of problem to be learned: `classification`, `regression`, `novelty`. - `buffersize` (numeric) - the size of the buffer to be used. ### Details The `inlearn` function is used to initialize a blank `onlearn` object. ### Value An `S4` object of class `onlearn` that can be used by the `onlearn` function. ### Author(s) Alexandros Karatzoglou ### See Also `onlearn`, `onlearn-class` ### Examples ```R # create toy data set x <- rbind(matrix(rnorm(100),,2),matrix(rnorm(100)+3,,2)) y <- matrix(c(rep(1,50),rep(-1,50)),,1) # initialize onlearn object on <- inlearn(2, kernel = "rbfdot", kpar = list(sigma = 0.2), type = "classification") # learn one data point at the time for(i in sample(1:100,100)) on <- onlearn(on,x[i,],y[i],nu=0.03,lambda=0.1) sign(predict(on,x)) ``` ``` -------------------------------- ### Initialize and Use RBF Kernel with inchol Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Demonstrates initializing a Radial Basis Function (RBF) kernel with a specified sigma and then using it with the inchol function for incomplete Cholesky decomposition. Shows how to check dimensions and retrieve pivots. ```R data(iris) datamatrix <- as.matrix(iris[,-5]) # initialize kernel function rbf <- rbfdot(sigma=0.1) rbf Z <- inchol(datamatrix,kernel=rbf) dim(Z) pivots(Z) ``` -------------------------------- ### Create Classification Model with Support Vector Machines Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Illustrates creating a classification model using Support Vector Machines with the 'ksvm' function. Allows selection of kernel, kernel parameters, cost parameter 'C', and cross-validation. ```R ## Create model using Support Vector Machines promsv <- ksvm(Class~.,data=promotergene,kernel="laplacedot", kpar="automatic",C=60,cross=4) promsv ``` -------------------------------- ### Fit Median, 0.9 Quantile, and 0.1 Quantile using kqr Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Demonstrates fitting kernel quantile regression models for different quantiles (median, 0.9, 0.1) using specified kernels and parameters. Requires prior data creation. ```R # create data x <- sort(runif(300)) y <- sin(pi*x) + rnorm(300,0,sd=exp(sin(2*pi*x))) # first calculate the median qrm <- kqr(x, y, tau = 0.5, C=0.15) # predict and plot plot(x, y) ytest <- predict(qrm, x) lines(x, ytest, col="blue") # calculate 0.9 quantile qrm <- kqr(x, y, tau = 0.9, kernel = "rbfdot", kpar= list(sigma=10), C=0.15) ytest <- predict(qrm, x) lines(x, ytest, col="red") # calculate 0.1 quantile qrm <- kqr(x, y, tau = 0.1,C=0.15) ytest <- predict(qrm, x) lines(x, ytest, col="green") ``` -------------------------------- ### Execute Online Kernel Learning Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Initializes an onlearn object and performs online learning on a toy dataset using stochastic gradient descent. ```R ## create toy data set x <- rbind(matrix(rnorm(100),,2),matrix(rnorm(100)+3,,2)) y <- matrix(c(rep(1,50),rep(-1,50)),,1) ## initialize onlearn object on <- inlearn(2,kernel="rbfdot",kpar=list(sigma=0.2), type="classification") ind <- sample(1:100,100) ## learn one data point at the time for(i in ind) on <- onlearn(on,x[i,],y[i],nu=0.03,lambda=0.1) ## or learn all the data on <- onlearn(on,x[ind,],y[ind],nu=0.03,lambda=0.1) sign(predict(on,x)) ``` -------------------------------- ### Initialize Online Kernel Learning Object Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Initializes an S4 object of class 'onlearn' for online kernel learning. ```R ## S4 method for signature 'numeric' inlearn(d, kernel = "rbfdot", kpar = list(sigma = 0.1), type = "novelty", buffersize = 1000) ``` -------------------------------- ### Kernlab Class 'onlearn' Methods Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Documentation for methods associated with the 'onlearn' class in the kernlab package, used for online learning algorithms. ```APIDOC ## Methods for Class "onlearn" ### alpha `signature(object = "onlearn")`: returns the model parameters ### b `signature(object = "onlearn")`: returns the offset ### buffer `signature(object = "onlearn")`: returns the buffer size ### fit `signature(object = "onlearn")`: returns the last decision function value ### kernelf `signature(object = "onlearn")`: return the kernel function used ### kpar `signature(object = "onlearn")`: returns the hyper-parameters used ### onlearn `signature(obj = "onlearn")`: the learning function ### predict `signature(object = "onlearn")`: the predict function ### rho `signature(object = "onlearn")`: returns model parameter ### show `signature(object = "onlearn")`: show function ### type `signature(object = "onlearn")`: returns the type of problem ### xmatrix `signature(object = "onlearn")`: returns the stored data points ``` -------------------------------- ### Initialize Relevance Vector Machine for regression with matrix input Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Initializes a Relevance Vector Machine model for regression using matrix input, specifying kernel, variance, and iteration parameters. ```R rvm(x, y, type="regression", kernel="rbfdot", kpar="automatic", alpha= ncol(as.matrix(x)), var=0.1, var.fix=FALSE, iterations=100, verbosity = 0, tol = .Machine$double.eps, minmaxdiff = 1e-3, cross = 0, fit = TRUE, ... , subset, na.action = na.omit) ``` -------------------------------- ### Class 'vm' Documentation Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Details about the 'vm' virtual class in the kernlab package, including its purpose, slots, and methods. ```APIDOC ## Class "vm" ### Description An S4 VIRTUAL class used as a base for the various vector machine classes in kernlab ### Objects from the Class Objects from the class cannot be created directly but only contained in other classes. ### Slots `alpha`: Object of class "listI" containing the resulting alpha vector (list in case of multiclass classification) (support vectors) `type`: Object of class "character" containing the vector machine type e.g., ("C-svc", "nu-svc", "C-bsvc", "spoc-svc", "one-svc", "eps-svr", "nu-svr", "eps-bsvr") `kernelf`: Object of class "function" containing the kernel function `kpar`: Object of class "list" containing the kernel function parameters (hyperparameters) `kcall`: Object of class "call" containing the function call `terms`: Object of class "ANY" containing the terms representation of the symbolic model used (when using a formula) `xmatrix`: Object of class "input" the data matrix used during computations (support vectors) (possibly scaled and without NA) `ymatrix`: Object of class "output" the response matrix/vector `fitted`: Object of class "output" with the fitted values, predictions using the training set. `lev`: Object of class "vector" with the levels of the response (in the case of classification) `nclass`: Object of class "numeric" containing the number of classes (in the case of classification) `error`: Object of class "vector" containing the training error `cross`: Object of class "vector" containing the cross-validation error `n.action`: Object of class "ANY" containing the action performed for NA ### Methods alpha `signature(object = "vm")`: returns the complete alpha vector (wit zero values) cross `signature(object = "vm")`: returns the cross-validation error error `signature(object = "vm")`: returns the training error fitted `signature(object = "vm")`: returns the fitted values (predict on training set) kernelf `signature(object = "vm")`: returns the kernel function kpar `signature(object = "vm")`: returns the kernel parameters (hyperparameters) lev `signature(object = "vm")`: returns the levels in case of classification kcall `signature(object="vm")`: returns the function call type `signature(object = "vm")`: returns the problem type xmatrix `signature(object = "vm")`: returns the data matrix used(support vectors) ymatrix `signature(object = "vm")`: returns the response vector ### Author(s) Alexandros Karatzoglou alexandros.karatzolgou@ci.tuwien.ac.at ### See Also `ksvm-class`, `rvm-class`, `gausspr-class` ``` -------------------------------- ### Initialize Online Learning Object in R Source: https://cran.r-project.org/web/packages/kernlab/vignettes/kernlab.Rnw Initializes an 'onlearn' object for online classification using an RBF dot kernel. Requires specifying the input dimension, kernel type, kernel parameters, and the type of learning task. ```R ## create toy data set x <- rbind(matrix(rnorm(90),,2),matrix(rnorm(90)+3,,2)) y <- matrix(c(rep(1,45),rep(-1,45)),,1) ## initialize onlearn object on <- inlearn(2,kernel="rbfdot",kpar=list(sigma=0.2),type="classification") ind <- sample(1:90,90) ``` -------------------------------- ### Train and Predict with Gaussian Process using promotergene data Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Illustrates training a Gaussian Process model with a radial basis function kernel on the promotergene dataset and predicting gene type probabilities. Requires the 'kernlab' package. ```R data(promotergene) ## create test and training set ind <- sample(1:dim(promotergene)[1],20) genetrain <- promotergene[-ind, ] genetest <- promotergene[ind, ] ## train a support vector machine gene <- gausspr(Class~.,data=genetrain,kernel="rbfdot", kpar=list(sigma=0.015)) gene ## predict gene type probabilities on the test set genetype <- predict(gene,genetest,type="probabilities") genetype ``` -------------------------------- ### Initialize Relevance Vector Machine with vector input Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Initializes a Relevance Vector Machine model with a vector input. ```R rvm(x, ...) ``` -------------------------------- ### kqr Function Parameters Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Details the arguments and configuration options for the kqr function. ```APIDOC ## kqr ### Description Performs kernel quantile regression on the provided data. ### Parameters - **x** (matrix/vector/formula) - Required - Training data or symbolic description of the model. - **data** (data frame) - Optional - Variables in the model. - **y** (numeric vector/column matrix) - Required - The response variable. - **scaled** (logical) - Optional - Whether to scale variables to zero mean and unit variance (default: TRUE). - **tau** (numeric) - Optional - The quantile to be estimated (default: 0.5). - **C** (numeric) - Optional - Cost regularization parameter (default: 1). - **kernel** (string/function) - Optional - Kernel function (e.g., 'rbfdot', 'polydot', 'vanilladot'). - **kpar** (list) - Optional - Hyper-parameters for the kernel function (default: 'automatic'). - **reduced** (logical) - Optional - Use incomplete cholesky decomposition for large datasets (default: FALSE). - **rank** (integer) - Optional - Rank m of the decomposed matrix. - **fit** (logical) - Optional - Whether to compute and include fitted values (default: TRUE). - **cross** (integer) - Optional - Number of folds for k-fold cross validation. - **subset** (index vector) - Optional - Cases to be used in the training sample. - **na.action** (function) - Optional - Action to take if NAs are found (default: na.omit). ``` -------------------------------- ### Initialize Relevance Vector Machine for regression Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Initializes a Relevance Vector Machine model for regression using a formula interface. ```R rvm(x, data=NULL, ..., subset, na.action = na.omit) ``` -------------------------------- ### Initialize Relevance Vector Machine with list input Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Initializes a Relevance Vector Machine model using list input, specifying kernel, variance, and iteration parameters. ```R rvm(x, y, type = "regression", kernel = "stringdot", kpar = list(length = 4, lambda = 0.5), alpha = 5, var = 0.1, var.fix = FALSE, iterations = 100, verbosity = 0, tol = .Machine$double.eps, minmaxdiff = 1e-3, cross = 0, fit = TRUE, ..., subset, na.action = na.omit) ``` -------------------------------- ### Solve SVM Optimization with ipop Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Demonstrates solving a Support Vector Machine optimization problem using the ipop interior point solver. ```R ## solve the Support Vector Machine optimization problem data(spam) ## sample a scaled part (500 points) of the spam data set m <- 500 set <- sample(1:dim(spam)[1],m) x <- scale(as.matrix(spam[,-58]))[set,] y <- as.integer(spam[set,58]) y[y==2] <- -1 ##set C parameter and kernel C <- 5 rbf <- rbfdot(sigma = 0.1) ## create H matrix etc. H <- kernelPol(rbf,x,,y) c <- matrix(rep(-1,m)) A <- t(y) b <- 0 l <- matrix(rep(0,m)) u <- matrix(rep(C,m)) r <- 0 sv <- ipop(c,H,A,b,l,u,r) sv dual(sv) ``` ```R ## solve the Support Vector Machine optimization problem data(spam) ## sample a scaled part (300 points) of the spam data set m <- 300 set <- sample(1:dim(spam)[1],m) x <- scale(as.matrix(spam[,-58]))[set,] y <- as.integer(spam[set,58]) y[y==2] <- -1 ##set C parameter and kernel C <- 5 rbf <- rbfdot(sigma = 0.1) ## create H matrix etc. H <- kernelPol(rbf,x,,y) c <- matrix(rep(-1,m)) A <- t(y) b <- 0 l <- matrix(rep(0,m)) u <- matrix(rep(C,m)) r <- 0 sv <- ipop(c,H,A,b,l,u,r) primal(sv) dual(sv) how(sv) ``` -------------------------------- ### kernlab Function Arguments Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Detailed explanation of the arguments for functions within the kernlab package, such as ksvm. ```APIDOC ## kernlab Function Arguments ### Description This section details the common arguments used in functions provided by the kernlab R package, such as `ksvm`, for building and training machine learning models. ### Arguments - **x** (matrix, vector, kernelMatrix, list) - Symbolic description of the model or training data. Can be a matrix, vector, kernel matrix, or a list of character vectors. The intercept is always excluded. - **data** (data.frame) - Optional data frame containing the training data when using a formula. Defaults to the environment. - **y** (vector) - Response vector for training. Can be a factor (classification) or numeric (regression). - **scaled** (logical vector) - Indicates variables to be scaled. If length 1, recycled. Defaults to scaling all non-binary variables to zero mean and unit variance. - **type** (string) - Type of task: classification, regression, or novelty detection. Valid options include `C-svc`, `nu-svc`, `C-bsvc`, `spoc-svc`, `kbb-svc`, `one-svc`, `eps-svr`, `nu-svr`, `eps-bsvr`. Defaults based on the type of `y`. - **kernel** (function or string) - The kernel function to use. Can be a predefined string (e.g., `rbfdot`, `polydot`, `vanilladot`, `tanhdot`, `laplacedot`, `besseldot`, `anovadot`, `splinedot`, `stringdot`) or a user-defined kernel function. Setting to "matrix" uses the `kernelMatrix` interface. - **kpar** (list or string) - List of hyper-parameters for the kernel function. For `rbfdot` and `laplacedot`, `sigma` is used. For `polydot`, `degree`, `scale`, `offset`. For `tanhdot`, `scale`, `offset`. For `besseldot`, `sigma`, `order`, `degree`. For `anovadot`, `sigma`, `degree`. For `stringdot`, `length`, `lambda`, `normalized`. Can also be set to "automatic" for `rbfdot` and `laplacedot` to estimate `sigma`. - **C** (numeric) - Cost of constraints violation. Default is 1. - **nu** (numeric) - Parameter for `nu-svc`, `one-svc`, `nu-svr`. Sets upper bound on training error and lower bound on support vectors. Default is 0.2. - **epsilon** (numeric) - Epsilon in the insensitive-loss function for regression types. Default is 0.1. - **prob.model** (logical) - If TRUE, builds a model for class probability calculation or regression scaling parameter. Default is FALSE. - **class.weights** (named vector) - Weights for different classes, used for asymmetric class sizes. - **cache** (numeric) - Cache memory in MB. Default is 40. - **tol** (numeric) - Tolerance of termination criterion. Default is 0.001. - **shrinking** (logical) - Option to use shrinking-heuristics. Default is TRUE. ``` -------------------------------- ### Train and Inspect a Support Vector Machine Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Demonstrates training a Support Vector Machine using the ksvm function with an RBF kernel and inspecting its components like kernel function, alpha values, coefficients, fitted values, and cross-validation error. ```R data(promotergene) ## train a support vector machine gene <- ksvm(Class~.,data=promotergene,kernel="rbfdot", kpar=list(sigma=0.015),C=50,cross=4) gene # the kernel function kernelf(gene) # the alpha values alpha(gene) # the coefficients coef(gene) # the fitted values fitted(gene) # the cross validation error cross(gene) ``` -------------------------------- ### Create Spectral Clustering Object Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Demonstrates the creation of a spectral clustering object `sc` from the `spirals` dataset with 2 centers, and accessing its properties. ```R data(spirals) sc <- specc(spirals, centers=2) centers(sc) size(sc) ``` -------------------------------- ### Perform Kernel Quantile Regression Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Demonstrates data generation, model fitting with kqr, prediction, and visualization of quantile regression results. ```R # create data x <- sort(runif(300)) y <- sin(pi*x) + rnorm(300,0,sd=exp(sin(2*pi*x))) # first calculate the median qrm <- kqr(x, y, tau = 0.5, C=0.15) # predict and plot plot(x, y) ytest <- predict(qrm, x) lines(x, ytest, col="blue") # calculate 0.9 quantile qrm <- kqr(x, y, tau = 0.9, kernel = "rbfdot", kpar = list(sigma = 10), C = 0.15) ytest <- predict(qrm, x) lines(x, ytest, col="red") # print model coefficients and other information coef(qrm) b(qrm) error(qrm) kernelf(qrm) ``` -------------------------------- ### kernlab Function Arguments Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Detailed explanation of the arguments used in kernlab functions. ```APIDOC ## kernlab Function Arguments ### Description This section describes the common arguments used across various functions within the kernlab R package, such as data input, model configuration, kernel selection, and training parameters. ### Arguments - **x** (matrix or vector or kernelMatrix or list) - Training data, a symbolic model description, or a kernel matrix. - **data** (data.frame, optional) - Data frame containing model variables. - **y** (vector) - Response vector for training data. Can be a factor (classification) or numeric (regression). - **scaled** (logical vector) - Indicates variables to be scaled. Defaults to scaling non-binary variables. - **type** (string) - Type of problem: "classification" or "regression". Defaults based on the type of `y`. - **kernel** (function or string) - The kernel function to use. Can be a predefined string (e.g., "rbfdot", "polydot") or a user-defined kernel function. - Supported kernels: `rbfdot`, `polydot`, `vanilladot`, `tanhdot`, `laplacedot`, `besseldot`, `anovadot`, `splinedot`, `stringdot`. - If set to "matrix", `x` is treated as a kernel matrix. - **kpar** (list or string) - List of hyper-parameters for the kernel function. Can also be set to "automatic" to estimate sigma for RBF or Laplace kernels. - Parameters for `rbfdot`, `laplacedot`: `sigma` - Parameters for `polydot`: `degree`, `scale`, `offset` - Parameters for `tanhdot`: `scale`, `offset` - Parameters for `besseldot`: `sigma`, `order`, `degree` - Parameters for `anovadot`: `sigma`, `degree` - Parameters for `stringdot`: `length`, `lambda`, `normalized` - **tau** (numeric) - Regularization parameter. Default is 0.01. - **reduced** (logical) - If TRUE, uses a reduced method (`csi`); otherwise, solves the full linear problem. - **rank** (integer) - Maximal rank of the decomposed kernel matrix (used with `csi`). - **delta** (integer) - Number of columns for Cholesky decomposition in advance (used with `csi`). Default is 40. - **tol** (numeric) - Tolerance for the `csi` function's termination criterion. Default is 0.0001. - **fit** (logical) - Indicates whether to compute and include fitted values. Default is TRUE. - **cross** (integer) - If k > 0, performs k-fold cross-validation. - **subset** (integer vector) - Index vector for training sample cases. - **na.action** (function) - Function to handle missing values (e.g., `na.omit`, `na.fail`). - **...** - Additional parameters. ``` -------------------------------- ### Create Sample Data for RVM Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Generates sample data for training and testing Relevance Vector Machines. Includes a sine wave with added noise. ```R x <- seq(-20,20,0.1) y <- sin(x)/x + rnorm(401,sd=0.05) ``` -------------------------------- ### kernlab Methods Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Documentation for various methods available in the kernlab package, primarily related to the 'kmmd' object. ```APIDOC ## kernelf ### Description Returns the kernel function used. ### Signature `signature(object = "kmmd")` ## H0 ### Description Returns the value of H0 being rejected. ### Signature `signature(object = "kmmd")` ## AsympH0 ### Description Returns the value of H0 being rejected according to the asymptotic bound. ### Signature `signature(object = "kmmd")` ## mmdstats ### Description Returns the values of the mmd statistics. ### Signature `signature(object = "kmmd")` ## Radbound ### Description Returns the value of the Rademacher bound. ### Signature `signature(object = "kmmd")` ## Asymbound ### Description Returns the value of the asymptotic bound. ### Signature `signature(object = "kmmd")` ``` -------------------------------- ### Fit and predict with LSSVM Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Demonstrates training an LSSVM model using the formula interface and the kernel matrix interface. ```R ## simple example data(iris) lir <- lssvm(Species~.,data=iris) lir lirr <- lssvm(Species~.,data= iris, reduced = FALSE) lirr ## Using the kernelMatrix interface iris <- unique(iris) rbf <- rbfdot(0.5) k <- kernelMatrix(rbf, as.matrix(iris[,-5])) klir <- lssvm(k, iris[, 5]) klir pre <- predict(klir, k) ``` -------------------------------- ### Load kernlab Package and Set Options Source: https://cran.r-project.org/web/packages/kernlab/vignettes/kernlab.Rnw This code snippet loads the kernlab package and sets the output width for R console messages. It's typically used at the beginning of an R session or script that utilizes the kernlab package. ```R library(kernlab) options(width = 70) ``` -------------------------------- ### Print first 10 model coefficients Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Extracts and prints the first 10 model coefficients (alpha) from a fitted kqr model. Assumes 'qrm' is a pre-fitted kqr object. ```R coef(qrm)[1:10] ``` -------------------------------- ### Kernel Quantile Regression (kqr) Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Documentation for the Kernel Quantile Regression function 'kqr' in the kernlab package. ```APIDOC ## Kernel Quantile Regression. ### Description The Kernel Quantile Regression algorithm `kqr` performs non-parametric Quantile Regression. ### Usage ```R ## S4 method for signature 'formula' kqr(x, data=NULL, ..., subset, na.action = na.omit, scaled = TRUE) ## S4 method for signature 'vector' kqr(x,...) ## S4 method for signature 'matrix' kqr(x, y, scaled = TRUE, tau = 0.5, C = 0.1, kernel = "rbfdot", kpar = "automatic", reduced = FALSE, rank = dim(x)[1]/6, fit = TRUE, cross = 0, na.action = na.omit) ## S4 method for signature 'kernelMatrix' kqr(x, y, tau = 0.5, C = 0.1, fit = TRUE, cross = 0) ## S4 method for signature 'list' kqr(x, y, tau = 0.5, C = 0.1, kernel = "strigdot", kpar= list(length=4, C=0.5), fit = TRUE, cross = 0) ``` ``` -------------------------------- ### Estimate and use sigma values with ksvm Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Demonstrates estimating the sigma hyperparameter range for a Support Vector Machine and using it to train and test a model on the promotergene dataset. ```R ## estimate good sigma values for promotergene data(promotergene) srange <- sigest(Class~.,data = promotergene) srange s <- srange[2] s ## create test and training set ind <- sample(1:dim(promotergene)[1],20) genetrain <- promotergene[-ind, ] genetest <- promotergene[ind, ] ## train a support vector machine gene <- ksvm(Class~.,data=genetrain,kernel="rbfdot", kpar=list(sigma = s),C=50,cross=3) gene ## predict gene type on the test set promoter <- predict(gene,genetest[,-1]) ## Check results table(promoter,genetest[,1]) ``` -------------------------------- ### Create Classification Model with Gaussian Processes Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Demonstrates how to create a classification model using Gaussian Processes with the 'gausspr' function. Specify the kernel type and parameters, and optionally set cross-validation folds. ```R data(promotergene) ## Create classification model using Gaussian Processes prom <- gausspr(Class~.,data=promotergene,kernel="rbfdot", kpar=list(sigma=0.02),cross=4) prom ``` -------------------------------- ### Perform Incomplete Cholesky Decomposition with csi Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Demonstrates initializing a kernel, performing the decomposition on the iris dataset, and verifying the approximation against the full kernel matrix. ```R data(iris) ## create multidimensional y matrix yind <- t(matrix(1:3,3,150)) ymat <- matrix(0, 150, 3) ymat[yind==as.integer(iris[,5])] <- 1 datamatrix <- as.matrix(iris[,-5]) # initialize kernel function rbf <- rbfdot(sigma=0.1) rbf Z <- csi(datamatrix,ymat, kernel=rbf, rank = 30) dim(Z) pivots(Z) # calculate kernel matrix K <- crossprod(t(Z)) # difference between approximated and real kernel matrix (K - kernelMatrix(kernel=rbf, datamatrix))[6,] ``` -------------------------------- ### Class "gausspr" Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Documentation for the Gaussian Processes object class in the kernlab package. ```APIDOC ## Class "gausspr" ### Description The Gaussian Processes object class ### Objects from the Class Objects can be created by calls of the form `new("gausspr", ...)`. or by calling the `gausspr` function ### Slots `tol`: Object of class `"numeric"` contains tolerance of termination criteria `kernelf`: Object of class `"kfunction"` contains the kernel function used `kpar`: Object of class `"list"` contains the kernel parameter used `kcall`: Object of class `"list"` contains the used function call `type`: Object of class `"character"` contains type of problem `terms`: Object of class `"ANY"` contains the terms representation of the symbolic model used (when using a formula) `xmatrix`: Object of class `"input"` containing the data matrix used `ymatrix`: Object of class `"output"` containing the response matrix `fitted`: Object of class `"output"` containing the fitted values `lev`: Object of class `"vector"` containing the levels of the response (in case of classification) `nclass`: Object of class `"numeric"` containing the number of classes (in case of classification) `alpha`: Object of class `"listI"` containing the computes alpha values `alphaindex`: Object of class `"list"` containing the indexes for the alphas in various classes (in multi-class problems). `sol`: Object of class `"matrix"` containing the solution to the Gaussian Process formulation, it is used to compute the variance in regression problems. `scaling`: Object of class `"ANY"` containing the scaling coefficients of the data (when case `scaled = TRUE` is used). `nvar`: Object of class `"numeric"` containing the computed variance `error`: Object of class `"numeric"` containing the training error `cross`: Object of class `"numeric"` containing the cross validation error `n.action`: Object of class `"ANY"` containing the action performed in NA ### Methods alpha `signature(object = "gausspr")`: returns the alpha vector cross `signature(object = "gausspr")`: returns the cross validation error error `signature(object = "gausspr")`: returns the training error fitted `signature(object = "vm")`: returns the fitted values kcall `signature(object = "gausspr")`: returns the call performed kernelf `signature(object = "gausspr")`: returns the kernel function used kpar `signature(object = "gausspr")`: returns the kernel parameter used lev `signature(object = "gausspr")`: returns the response levels (in classification) type `signature(object = "gausspr")`: returns the type of problem xmatrix `signature(object = "gausspr")`: returns the data matrix used ymatrix `signature(object = "gausspr")`: returns the response matrix used scaling `signature(object = "gausspr")`: returns the scaling coefficients of the data (when `scaled = TRUE` is used) ### Author(s) Alexandros Karatzoglou alexandros.karatzoglou@ci.tuwien.ac.at ### See Also `gausspr`, `ksvm-class`, `vm-class` ### Examples ```R # train model data(iris) test <- gausspr(Species~.,data=iris,var=2) test alpha(test) error(test) lev(test) ``` ``` -------------------------------- ### String Kernel Functions Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Documentation for string kernel functions in kernlab, including 'stringdot'. Explains parameters like length, lambda, type, and normalized, and details different string kernel types. ```APIDOC ## String Kernel Functions ### Description String kernels. ### Usage ``` stringdot(length = 4, lambda = 1.1, type = "spectrum", normalized = TRUE) ``` ### Arguments `length` | The length of the substrings considered ---|--- `lambda` | The decay factor `type` | Type of string kernel, currently the following kernels are supported : `spectrum` the kernel considers only matching substring of exactly length `nnn` (also know as string kernel). Each such matching substring is given a constant weight. The length parameter in this kernel has to be `length>1length > 1length>1`. `boundrange` this kernel (also known as boundrange) considers only matching substrings of length less than or equal to a given number N. This type of string kernel requires a length parameter `length>1length > 1length>1` `constant` The kernel considers all matching substrings and assigns constant weight (e.g. 1) to each of them. This `constant` kernel does not require any additional parameter. `exponential` Exponential Decay kernel where the substring weight decays as the matching substring gets longer. The kernel requires a decay factor `λ>1 \lambda > 1λ>1` `string` essentially identical to the spectrum kernel, only computed using a more conventional way. `fullstring` essentially identical to the boundrange kernel only computed in a more conventional way. `normalized` | normalize string kernel values, (default: `TRUE`) ### Details The kernel generating functions are used to initialize a kernel function which calculates the dot (inner) product between two feature vectors in a Hilbert Space. These functions or their function generating names can be passed as a `kernel` argument on almost all functions in kernlab(e.g., `ksvm`, `kpca` etc.). The string kernels calculate similarities between two strings (e.g. texts or sequences) by matching the common substring in the strings. Different types of string kernel exists and are mainly distinguished by how the matching is performed i.e. some string kernels count the exact matchings of `nnn` characters (spectrum kernel) between the strings, others allow gaps (mismatch kernel) etc. ### Value Returns an S4 object of class `stringkernel` which extents the `function` class. The resulting function implements the given kernel calculating the inner (dot) product between two character vectors. `kpar` | a list containing the kernel parameters (hyperparameters) used. ---|--- The kernel parameters can be accessed by the `kpar` function. ### Note The `spectrum` and `boundrange` kernel are faster and more efficient implementations of the `string` and `fullstring` kernels which will be still included in `kernlab` for the next two versions. ### Author(s) Alexandros Karatzoglou alexandros.karatzoglou@ci.tuwien.ac.at ### See Also `dots `, `kernelMatrix `, `kernelMult`, `kernelPol` ### Examples ``` sk <- stringdot(type="string", length=5) sk ``` ``` -------------------------------- ### ksvm Function Parameters Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Details on the configuration parameters for the ksvm model fitting function. ```APIDOC ## ksvm Parameters ### Parameters - **cross** (integer) - Optional - If k>0, performs k-fold cross validation to assess model quality (accuracy for classification, MSE for regression). - **fit** (boolean) - Optional - Indicates whether fitted values should be computed and included (default: TRUE). - **subset** (index vector) - Optional - Specifies cases to be used in the training sample. - **na.action** (function) - Optional - Specifies action for missing values (default: na.omit; alternative: na.fail). ``` -------------------------------- ### Quadratic Programming Solver Usage Source: https://cran.r-project.org/web/packages/kernlab/refman/kernlab.html Defines the signature for the ipop quadratic programming solver. ```R ipop(c, H, A, b, l, u, r, sigf = 7, maxiter = 40, margin = 0.05, bound = 10, verb = 0) ```