### Prepare QP Matrices for SVM Optimization Source: https://context7.com/cran/kernlab/llms.txt Prepares the necessary matrices (H and objective function components) for solving the quadratic programming problem associated with SVM optimization. This involves scaling data and defining SVM parameters. ```R library(kernlab) data(spam) # Sample and scale data 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 # SVM parameters C <- 5 rbf <- rbfdot(sigma = 0.1) # Create QP matrices for SVM optimization # min 1/2 * alpha' * H * alpha - sum(alpha) ``` -------------------------------- ### ipop - Quadratic Programming Solver API Source: https://context7.com/cran/kernlab/llms.txt The `ipop` function solves quadratic programming problems using an interior point method. It is used internally by kernlab's SVM and quantile regression implementations but can also be used directly for custom optimization. ```APIDOC ## ipop - Quadratic Programming Solver ### Description The `ipop` function solves quadratic programming problems using an interior point method. It is used internally by kernlab's SVM and quantile regression implementations but can also be used directly for custom optimization. ### Method Not specified, likely a function call in R. ### Endpoint Not applicable (R function). ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```r library(kernlab) data(spam) # Sample and scale data 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 # SVM parameters C <- 5 rbf <- rbfdot(sigma = 0.1) # Create QP matrices for SVM optimization # min 1/2 * alpha' * H * alpha - sum(alpha) ``` ### Response #### Success Response (200) Returns the solution to the quadratic programming problem. #### Response Example Not provided in the source text. ``` -------------------------------- ### Ranking with Convergence Tracking Source: https://context7.com/cran/kernlab/llms.txt Performs data ranking with convergence tracking enabled, allowing monitoring of the optimization process over a specified number of iterations. Requires the 'ranking' function to be called with 'convergence = TRUE'. ```R ranked_conv <- ranking(ran, 54, kernel = "rbfdot", kpar = list(sigma = 100), convergence = TRUE, iterations = 1000) convergence(ranked_conv) ``` -------------------------------- ### RVM Model Operations Source: https://context7.com/cran/kernlab/llms.txt Demonstrates basic RVM usage including prediction, plotting, formula interface, and cross-validation. ```r # Number of relevance vectors (typically much smaller than training set) nvar(rvmmodel) # [1] 12 # Predict and plot ytest <- predict(rvmmodel, x) plot(x, y, type = "l") lines(x, ytest, col = "red") # RVM with formula interface data(iris) # Note: RVM currently supports regression only iris_num <- iris[iris$Species != "virginica", ] iris_num$y <- as.numeric(iris_num$Species) - 1 rvm_formula <- rvm(y ~ Sepal.Length + Sepal.Width, data = iris_num) # Cross-validation rvm_cv <- rvm(x, y, cross = 5) rvm_cv # Cross validation error: 0.00234 ``` -------------------------------- ### Train and Predict with Support Vector Machines Source: https://context7.com/cran/kernlab/llms.txt Implement SVMs for classification, regression, and novelty detection using the ksvm function. ```r library(kernlab) data(spam) # Split data into training and test sets index <- sample(1:dim(spam)[1]) spamtrain <- spam[index[1:floor(dim(spam)[1]/2)], ] spamtest <- spam[index[((ceiling(dim(spam)[1]/2)) + 1):dim(spam)[1]], ] # Train SVM classifier with RBF kernel filter <- ksvm(type ~ ., data = spamtrain, kernel = "rbfdot", kpar = list(sigma = 0.05), C = 5, cross = 3) filter # Support Vector Machine object of class "ksvm" # Type: C-svc (classification) # Parameter: cost C = 5 # Gaussian Radial Basis kernel function. # Hyperparameter: sigma = 0.05 # Number of Support Vectors: 524 # Training error: 0.012 # Cross validation error: 0.076 # Predict on test set mailtype <- predict(filter, spamtest[, -58]) table(mailtype, spamtest[, 58]) # nonspam spam # nonspam 1234 45 # spam 32 689 # Classification with probability output data(iris) rbf <- rbfdot(sigma = 0.1) irismodel <- ksvm(Species ~ ., data = iris, type = "C-bsvc", kernel = rbf, C = 10, prob.model = TRUE) predict(irismodel, iris[1:5, -5], type = "probabilities") # setosa versicolor virginica # [1,] 0.9823456 0.012345678 0.005308643 # [2,] 0.9756789 0.018765432 0.005555556 # SVM regression x <- seq(-20, 20, 0.1) y <- sin(x)/x + rnorm(401, sd = 0.03) regm <- ksvm(x, y, epsilon = 0.01, kpar = list(sigma = 16), cross = 3) predictions <- predict(regm, x) # Using kernel matrix interface K <- as.kernelMatrix(crossprod(t(as.matrix(iris[, -5])))) svp2 <- ksvm(K, iris[, 5], type = "C-svc") ``` -------------------------------- ### Implement String Kernels for Text Data Source: https://context7.com/cran/kernlab/llms.txt String kernels allow kernel methods to operate on text data. Use stringdot to define kernel types and ksvm for training models. ```r library(kernlab) data(reuters) # Create spectrum string kernel (matches substrings of exact length) sk_spectrum <- stringdot(type = "spectrum", length = 5) sk_spectrum # String kernel object of class "stringkernel" # Type: spectrum # Length: 5 # Create boundrange kernel (matches substrings up to length n) sk_boundrange <- stringdot(type = "boundrange", length = 4) # Create exponential decay kernel sk_exp <- stringdot(type = "exponential", lambda = 1.2) # Evaluate kernel on two strings text1 <- "machine learning algorithms" text2 <- "machine learning methods" sk_spectrum(text1, text2) # [1] 0.7654321 # Train SVM on text data (reuters dataset) # reuters is a list of character vectors, rlabels are class labels tsv <- ksvm(reuters, rlabels, kernel = "stringdot", kpar = list(length = 5), cross = 3, C = 10) tsv # Support Vector Machine object of class "ksvm" # Cross validation error: 0.123 # Kernel matrix for string data K_text <- kernelMatrix(sk_spectrum, reuters[1:20]) # Custom string kernel SVM text_svm <- ksvm(reuters, rlabels, kernel = "stringdot", kpar = list(type = "spectrum", length = 4, lambda = 0.5, normalized = TRUE), C = 5) ``` -------------------------------- ### Solve Quadratic Programming Problems with ipop Source: https://context7.com/cran/kernlab/llms.txt Use ipop to solve constrained quadratic programming problems. The solver requires defining the kernel matrix, linear terms, and constraints. ```r H <- kernelPol(rbf, x, , y) # Kernel matrix weighted by labels c <- matrix(rep(-1, m)) # Linear term A <- t(y) # Equality constraint matrix b <- 0 # Equality constraint bound l <- matrix(rep(0, m)) # Lower bound u <- matrix(rep(C, m)) # Upper bound r <- 0 # Constraint range # Solve QP problem sv <- ipop(c, H, A, b, l, u, r) sv # Primal solution found # Get primal solution (alpha values) primal(sv)[1:10] # [1] 0.0000000 5.0000000 0.0000000 1.2345678 ... # Get dual solution dual(sv) # Check convergence how(sv) # [1] "converged" ``` ```r # Custom QP problem example # Minimize: x^2 + y^2 subject to x + y >= 1 H_simple <- matrix(c(2, 0, 0, 2), 2, 2) c_simple <- matrix(c(0, 0)) A_simple <- matrix(c(-1, -1), 1, 2) b_simple <- -1 l_simple <- matrix(c(-Inf, -Inf)) u_simple <- matrix(c(Inf, Inf)) r_simple <- 0 qp_result <- ipop(c_simple, H_simple, A_simple, b_simple, l_simple, u_simple, r_simple) primal(qp_result) # [1] 0.5 0.5 ``` -------------------------------- ### Generate and Use Kernel Functions Source: https://context7.com/cran/kernlab/llms.txt Create kernel objects for inner product calculations and access their parameters. ```r library(kernlab) # Create a Gaussian RBF kernel with sigma = 0.1 rbfkernel <- rbfdot(sigma = 0.1) rbfkernel # Gaussian Radial Basis kernel function. # Hyperparameter : sigma = 0.1 # Access kernel parameters kpar(rbfkernel) # $sigma # [1] 0.1 # Create two vectors and calculate the kernel dot product x <- rnorm(10) y <- rnorm(10) rbfkernel(x, y) # [1] 0.8234567 # example output # Create other kernel types polykernel <- polydot(degree = 3, scale = 1, offset = 1) linearkernel <- vanilladot() laplacekernel <- laplacedot(sigma = 0.5) anovakernel <- anovadot(sigma = 1, degree = 2) splinekernel <- splinedot() # String kernel for text data stringkernel <- stringdot(type = "spectrum", length = 5) ``` -------------------------------- ### Apply Kernel K-means Clustering Source: https://context7.com/cran/kernlab/llms.txt Uses kkmeans to perform weighted kernel k-means clustering, allowing for non-linearly separable clusters. ```r library(kernlab) data(iris) # Kernel k-means clustering kk <- kkmeans(as.matrix(iris[, -5]), centers = 3) kk # Kernel K-means object of class "specc" # Get cluster assignments (integer vector) as.integer(kk) # [1] 1 1 1 1 1 ... 2 2 2 ... 3 3 3 # Get cluster centers centers(kk) # Sepal.Length Sepal.Width Petal.Length Petal.Width # [1,] 5.006 3.428 1.462 0.246 # [2,] 5.936 2.770 4.260 1.326 # [3,] 6.588 2.974 5.552 2.026 # Get cluster sizes size(kk) # [1] 50 50 50 # Get within-cluster sum of squares withinss(kk) # [1] 15.15100 39.82097 23.87947 # Compare with true labels table(kk, iris[, 5]) # setosa versicolor virginica # 1 50 0 0 # 2 0 48 2 # 3 0 2 48 # Kernel k-means with automatic sigma estimation kk_auto <- kkmeans(as.matrix(iris[, -5]), centers = 3, kernel = "rbfdot", kpar = "automatic") ``` -------------------------------- ### Kernel Quantile Regression (KQR) Source: https://context7.com/cran/kernlab/llms.txt Estimates conditional quantiles using kernel methods for nonparametric regression. ```r library(kernlab) # Create heteroscedastic data x <- sort(runif(300)) y <- sin(pi*x) + rnorm(300, 0, sd = exp(sin(2*pi*x))) # Estimate the median (tau = 0.5) qrm_median <- kqr(x, y, tau = 0.5, C = 0.15) qrm_median # Kernel Quantile Regression object of class "kqr" # Plot data and median estimate plot(x, y) ytest_median <- predict(qrm_median, x) lines(x, ytest_median, col = "blue", lwd = 2) # Estimate 0.9 quantile (upper bound) qrm_upper <- kqr(x, y, tau = 0.9, kernel = "rbfdot", kpar = list(sigma = 10), C = 0.15) ytest_upper <- predict(qrm_upper, x) lines(x, ytest_upper, col = "red", lwd = 2) # Estimate 0.1 quantile (lower bound) qrm_lower <- kqr(x, y, tau = 0.1, C = 0.15) ytest_lower <- predict(qrm_lower, x) lines(x, ytest_lower, col = "green", lwd = 2) # Access model coefficients coef(qrm_median)[1:10] # [1] 0.01234567 -0.02345678 0.03456789 ... # With cross-validation qrm_cv <- kqr(x, y, tau = 0.5, C = 0.15, cross = 5) ``` -------------------------------- ### Visualize Ranking Results Source: https://context7.com/cran/kernlab/llms.txt Visualizes data points where point size is proportional to their rank. Assumes 'ran' and 'ranked' objects are already computed from the 'ranking' function. ```R plot(ran) # Original data plot(ran, cex = (1:nrow(ranked))[ranked[, 3]]/40) # Sized by rank ``` -------------------------------- ### Perform Gaussian Process Modeling Source: https://context7.com/cran/kernlab/llms.txt Use Gaussian processes for classification and regression with uncertainty estimates. ```r library(kernlab) data(iris) # Gaussian Process classification gpmodel <- gausspr(Species ~ ., data = iris, var = 2) gpmodel # Gaussian Processes object of class "gausspr" # Problem type: classification # Gaussian Radial Basis kernel function. # Get model parameters alpha(gpmodel) # Predict classes predict(gpmodel, iris[, -5]) # Class probabilities predict(gpmodel, iris[, -5], type = "probabilities") # setosa versicolor virginica # [1,] 0.9987654 0.00123456 0.00000000 # [2,] 0.9976543 0.00234567 0.00000000 # Gaussian Process regression with variance estimation x <- seq(-20, 20, 0.1) y <- sin(x)/x + rnorm(401, sd = 0.03) gpr <- gausspr(x, y) ytest <- predict(gpr, x) ``` -------------------------------- ### Train SVM with Precomputed Kernel Matrix Source: https://context7.com/cran/kernlab/llms.txt Trains a Support Vector Machine (SVM) model using a precomputed kernel matrix. This is useful when the kernel matrix is already available or needs to be computed separately. ```R K_train <- as.kernelMatrix(crossprod(t(dt))) y_train <- as.factor(spam[c(10:20, 3000:3010), 58]) svm_km <- ksvm(K_train, y_train, type = "C-svc") ``` -------------------------------- ### Rank Data Points by Similarity Source: https://context7.com/cran/kernlab/llms.txt Ranks data points based on their similarity to a query point using a specified kernel. Requires the 'kernlab' library and data loaded with 'data(spirals)'. The 'edgegraph = TRUE' argument enables edge graph computation. ```R library(kernlab) data(spirals) # Select a subset of points ran <- spirals[rowSums(abs(spirals) < 0.55) == 2, ] # Rank points by similarity to query point (index 54) ranked <- ranking(ran, 54, kernel = "rbfdot", kpar = list(sigma = 100), edgegraph = TRUE) ranked ``` -------------------------------- ### Compute Full Kernel Matrix Source: https://context7.com/cran/kernlab/llms.txt Computes the full kernel matrix K[i,j] = k(x_i, x_j) for a given kernel function and dataset. Requires the 'kernlab' library and data loaded with 'data(spam)'. ```R library(kernlab) data(spam) # Sample data dt <- as.matrix(spam[c(10:20, 3000:3010), -58]) # Initialize kernel function rbf <- rbfdot(sigma = 0.05) # Compute full kernel matrix K[i,j] = k(x_i, x_j) K <- kernelMatrix(rbf, dt) dim(K) ``` -------------------------------- ### Ranking with Multiple Query Points Source: https://context7.com/cran/kernlab/llms.txt Ranks data points based on similarity to multiple query points specified in a vector. The query vector should have the same length as the number of data points, with non-zero entries indicating query points. ```R query_vec <- rep(0, nrow(ran)) query_vec[c(10, 54)] <- 1 # Two query points ranked_multi <- ranking(ran, query_vec, kernel = "rbfdot", kpar = list(sigma = 100)) ``` -------------------------------- ### kernelMatrix - Kernel Matrix Computation API Source: https://context7.com/cran/kernlab/llms.txt The `kernelMatrix`, `kernelPol`, and `kernelMult` functions provide efficient computation of kernel matrices and kernel expressions, useful for custom implementations and working with precomputed kernel matrices. ```APIDOC ## kernelMatrix - Kernel Matrix Computation ### Description The `kernelMatrix`, `kernelPol`, and `kernelMult` functions provide efficient computation of kernel matrices and kernel expressions, useful for custom implementations and working with precomputed kernel matrices. ### Method Not specified, likely function calls in R. ### Endpoint Not applicable (R functions). ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```r library(kernlab) data(spam) # Sample data dt <- as.matrix(spam[c(10:20, 3000:3010), -58]) # Initialize kernel function rbf <- rbfdot(sigma = 0.05) # Compute full kernel matrix K[i,j] = k(x_i, x_j) K <- kernelMatrix(rbf, dt) dim(K) # Compute kernel matrix between two different datasets dt2 <- as.matrix(spam[1:10, -58]) K_cross <- kernelMatrix(rbf, dt, dt2) dim(K_cross) # Create labels yt <- as.matrix(as.integer(spam[c(10:20, 3000:3010), 58])) yt[yt == 2] <- -1 # Compute quadratic kernel expression H[i,j] = z_i * z_j * k(x_i, x_j) H <- kernelPol(rbf, dt, , yt) dim(H) # Compute kernel expansion f(x) = sum_i z_i * k(x_i, x) expansion <- kernelMult(rbf, dt, , yt) dim(expansion) # Fast kernel matrix computation (for iterative algorithms) a <- rowSums(dt^2) # Pre-compute squared norms K_fast <- kernelFast(rbf, dt, dt, a) # Using kernel matrix with ksvm K_train <- as.kernelMatrix(crossprod(t(dt))) y_train <- as.factor(spam[c(10:20, 3000:3010), 58]) svm_km <- ksvm(K_train, y_train, type = "C-svc") ``` ### Response #### Success Response (200) Returns kernel matrices or kernel expansions as matrices. #### Response Example ``` dim(K) # [1] 22 22 dim(K_cross) # [1] 22 10 dim(H) # [1] 22 22 dim(expansion) # [1] 22 1 ``` ``` -------------------------------- ### ranking - Data Ranking API Source: https://context7.com/cran/kernlab/llms.txt The ranking function implements a universal ranking algorithm that assigns importance scores to data points based on their similarity to query points, exploiting the intrinsic geometric structure of the data. ```APIDOC ## ranking - Data Ranking ### Description The `ranking` function implements a universal ranking algorithm that assigns importance scores to data points based on their similarity to query points, exploiting the intrinsic geometric structure of the data. ### Method Not specified, likely a function call in R. ### Endpoint Not applicable (R function). ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```r library(kernlab) data(spirals) # Select a subset of points ran <- spirals[rowSums(abs(spirals) < 0.55) == 2, ] # Rank points by similarity to query point (index 54) ranked <- ranking(ran, 54, kernel = "rbfdot", kpar = list(sigma = 100), edgegraph = TRUE) ranked ``` ### Response #### Success Response (200) Returns a matrix with columns: index, score, rank. #### Response Example ``` # index score rank # [1,] 54 1.0000000 1 # [2,] 53 0.8765432 2 # [3,] 55 0.8654321 3 # ... ``` ``` -------------------------------- ### LS-SVM Classification and Regression Source: https://context7.com/cran/kernlab/llms.txt Implements Least Squares SVM for classification and regression, including reduced versions and kernel matrix interfaces. ```r library(kernlab) data(iris) # Basic LS-SVM classification lir <- lssvm(Species ~ ., data = iris) lir # Least Squares Support Vector Machine object of class "lssvm" # Problem type: classification # Gaussian Radial Basis kernel function. # Full (non-reduced) LS-SVM lirr <- lssvm(Species ~ ., data = iris, reduced = FALSE) lirr # Get model coefficients coef(lir) # Get model offset b(lir) # Predict pred <- predict(lir, iris[, -5]) table(pred, iris$Species) # Using kernel matrix interface iris_unique <- unique(iris) rbf <- rbfdot(0.5) K <- kernelMatrix(rbf, as.matrix(iris_unique[, -5])) klir <- lssvm(K, iris_unique[, 5]) predict(klir, K) # LS-SVM with custom parameters lir_custom <- lssvm(Species ~ ., data = iris, kernel = "rbfdot", kpar = list(sigma = 0.1), tau = 0.01, reduced = TRUE, rank = 50) ``` -------------------------------- ### Train SVM with Estimated Optimal Sigma Source: https://context7.com/cran/kernlab/llms.txt Trains an SVM model using an RBF kernel with an automatically estimated optimal sigma value obtained from 'sigest'. Includes data splitting, training with cross-validation, and prediction. ```R # Use median sigma value sigma_optimal <- srange[2] # Create training and test sets ind <- sample(1:dim(promotergene)[1], 20) genetrain <- promotergene[-ind, ] genetest <- promotergene[ind, ] # Train SVM with estimated sigma gene <- ksvm(Class ~ ., data = genetrain, kernel = "rbfdot", kpar = list(sigma = sigma_optimal), C = 50, cross = 3) gene # Predict on test set promoter <- predict(gene, genetest[, -1]) table(promoter, genetest[, 1]) ``` -------------------------------- ### Train SVM with Automatic Sigma Estimation Source: https://context7.com/cran/kernlab/llms.txt Trains an SVM model using the RBF kernel with automatic sigma estimation, where 'ksvm' internally calls 'sigest'. This simplifies hyperparameter tuning by letting the function determine a suitable sigma value. ```R # Using automatic sigma estimation in ksvm gene_auto <- ksvm(Class ~ ., data = genetrain, kernel = "rbfdot", kpar = "automatic", C = 50) # Uses sigest internally ``` -------------------------------- ### Compute Kernel Expansion Source: https://context7.com/cran/kernlab/llms.txt Computes the kernel expansion f(x) = sum_i z_i * k(x_i, x) for a given kernel, dataset, and labels. This is often used in kernel methods for prediction. ```R # Compute kernel expansion f(x) = sum_i z_i * k(x_i, x) expansion <- kernelMult(rbf, dt, , yt) dim(expansion) ``` -------------------------------- ### Train Relevance Vector Machine Source: https://context7.com/cran/kernlab/llms.txt Implements RVM for sparse regression modeling using Bayesian inference. ```r library(kernlab) # Create regression data x <- seq(-20, 20, 0.1) y <- sin(x)/x + rnorm(401, sd = 0.05) # Train Relevance Vector Machine rvmmodel <- rvm(x, y) rvmmodel # Relevance Vector Machine object of class "rvm" # Problem type: regression # Gaussian Radial Basis kernel function. # Number of Relevance Vectors: 12 # Get relevance vectors (model parameters) alpha(rvmmodel) # [1] 0.12345678 0.23456789 0.34567890 ... # Get indices of relevance vectors RVindex(rvmmodel) # [1] 15 45 82 123 167 ... ``` -------------------------------- ### Estimate Sigma from Matrix Input Source: https://context7.com/cran/kernlab/llms.txt Estimates the sigma range for an RBF kernel directly from a matrix input using the 'sigest' function. The 'frac' and 'scaled' arguments control the estimation process. ```R # Estimate sigma from matrix input X <- as.matrix(promotergene[, -1]) srange_mat <- sigest(X, frac = 0.5, scaled = TRUE) ``` -------------------------------- ### Execute Kernel Principal Components Analysis Source: https://context7.com/cran/kernlab/llms.txt Performs nonlinear dimensionality reduction using kpca. Supports both formula interfaces and direct kernel matrix inputs. ```r library(kernlab) data(iris) # Split data test <- sample(1:150, 20) # Perform Kernel PCA with RBF kernel kpc <- kpca(~ ., data = iris[-test, -5], kernel = "rbfdot", kpar = list(sigma = 0.2), features = 2) # Get principal component vectors pcv(kpc) # [,1] [,2] # [1,] -0.01234567 0.02345678 # [2,] 0.01567890 -0.01890123 # ... # Get eigenvalues eig(kpc) # [1] 2.345678 1.234567 # Get rotated (projected) data rotated_data <- rotated(kpc) head(rotated_data) # Plot projected data colored by species plot(rotated(kpc), col = as.integer(iris[-test, 5]), xlab = "1st Principal Component", ylab = "2nd Principal Component") # Project new data (test points) onto learned components embedded_test <- predict(kpc, iris[test, -5]) points(embedded_test, col = as.integer(iris[test, 5]), pch = 19) # Using kernel matrix interface rbf <- rbfdot(sigma = 0.2) K <- kernelMatrix(rbf, as.matrix(iris[, -5])) kpc_km <- kpca(K, features = 3) ``` -------------------------------- ### Estimate Sigma Range for RBF Kernel Source: https://context7.com/cran/kernlab/llms.txt Estimates a range of suitable sigma values for the Gaussian RBF kernel using the 'sigest' function. This is typically used to find optimal hyperparameters for SVMs. Requires 'kernlab' and data loaded with 'data(promotergene)'. ```R library(kernlab) data(promotergene) # Estimate sigma range for RBF kernel srange <- sigest(Class ~ ., data = promotergene) srange ``` -------------------------------- ### Compute Quadratic Kernel Expression Source: https://context7.com/cran/kernlab/llms.txt Computes the quadratic kernel expression H[i,j] = z_i * z_j * k(x_i, x_j) for a given kernel and dataset, along with labels. This is useful for certain kernel-based algorithms. ```R # Create labels yt <- as.matrix(as.integer(spam[c(10:20, 3000:3010), 58])) yt[yt == 2] <- -1 # Compute quadratic kernel expression H[i,j] = z_i * z_j * k(x_i, x_j) H <- kernelPol(rbf, dt, , yt) dim(H) ``` -------------------------------- ### Perform Gaussian Process Regression with Uncertainty Source: https://context7.com/cran/kernlab/llms.txt Uses the gausspr function to model data and predict values with standard deviation for confidence intervals. ```r x <- c(-4, -3, -2, -1, 0, 0.5, 1, 2) y <- c(-2, 0, -0.5, 1, 2, 1, 0, -1) gpr_var <- gausspr(x, y, variance.model = TRUE) xtest <- seq(-4, 2, 0.2) pred <- predict(gpr_var, xtest) sdev <- predict(gpr_var, xtest, type = "sdeviation") # Plot with confidence bands plot(x, y) lines(xtest, pred) lines(xtest, pred + 2*sdev, col = "red") # Upper 95% confidence lines(xtest, pred - 2*sdev, col = "red") # Lower 95% confidence ``` -------------------------------- ### MMD Test with Custom Kernel Source: https://context7.com/cran/kernlab/llms.txt Performs a Maximum Mean Discrepancy (MMD) test using a custom kernel function (laplacedot) with specified parameters. Requires prior data preparation and MMD object creation. ```R mmdo_custom <- kmmd(x, y, kernel = "laplacedot", kpar = list(sigma = 1), alpha = 0.01) ``` -------------------------------- ### Fast Kernel Matrix Computation Source: https://context7.com/cran/kernlab/llms.txt Performs fast kernel matrix computation, optimized for iterative algorithms by pre-computing squared norms of data points. Requires the kernel function, datasets, and pre-computed squared norms. ```R # Fast kernel matrix computation (for iterative algorithms) a <- rowSums(dt^2) # Pre-compute squared norms K_fast <- kernelFast(rbf, dt, dt, a) ``` -------------------------------- ### Perform Spectral Clustering Source: https://context7.com/cran/kernlab/llms.txt Clusters data using the specc function, which is effective for non-convex shapes. Includes options for custom kernels, local scaling, and Nystrom approximation. ```r library(kernlab) data(spirals) # Spectral clustering on interleaved spirals sc <- specc(spirals, centers = 2) sc # Spectral Clustering object of class "specc" # Get cluster centers centers(sc) # [,1] [,2] # [1,] 0.1234567 -0.2345678 # [2,] -0.3456789 0.4567890 # Get cluster sizes size(sc) # [1] 150 150 # Get within-cluster sum of squares withinss(sc) # [1] 12.34567 13.45678 # Visualize clustered spirals plot(spirals, col = sc) # Spectral clustering with custom kernel parameters sc_custom <- specc(spirals, centers = 2, kernel = "rbfdot", kpar = list(sigma = 0.5)) # Local scaling (adaptive kernel width per point) data(iris) sc_iris <- specc(as.matrix(iris[, -5]), centers = 3, kernel = "rbfdot", kpar = "local") # Using Nystrom approximation for large datasets sc_nystrom <- specc(spirals, centers = 2, nystrom.red = TRUE, nystrom.sample = 50, iterations = 200) ``` -------------------------------- ### Kernel Maximum Mean Discrepancy (KMMD) Source: https://context7.com/cran/kernlab/llms.txt Performs a two-sample test to determine if two datasets originate from the same distribution. ```r library(kernlab) # Create samples from different distributions x <- matrix(runif(300), 100) y <- matrix(runif(300) + 1, 100) # Shifted distribution # Perform MMD test mmdo <- kmmd(x, y) mmdo # Kernel Maximum Mean Discrepancy object of class "kmmd" # The hypothesis that the samples are from the same distribution # is rejected at the 0.05 level # Check if null hypothesis is rejected H0(mmdo) # [1] TRUE (samples are from different distributions) # Get MMD test statistics mmdstats(mmdo) # [1] 0.1234567 0.0987654 # Get Rademacher bound Radbound(mmdo) # [1] 0.0456789 # Test with asymptotic bound mmdo_asymp <- kmmd(x, y, asymptotic = TRUE) AsympH0(mmdo_asymp) Asymbound(mmdo_asymp) ``` -------------------------------- ### sigest - Hyperparameter Estimation API Source: https://context7.com/cran/kernlab/llms.txt The `sigest` function estimates suitable values for the sigma parameter of the Gaussian RBF kernel based on the data, providing a range of values likely to produce good results with SVMs. ```APIDOC ## sigest - Hyperparameter Estimation ### Description The `sigest` function estimates suitable values for the sigma parameter of the Gaussian RBF kernel based on the data, providing a range of values likely to produce good results with SVMs. ### Method Not specified, likely a function call in R. ### Endpoint Not applicable (R function). ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```r library(kernlab) data(promotergene) # Estimate sigma range for RBF kernel srange <- sigest(Class ~ ., data = promotergene) srange # Use median sigma value sigma_optimal <- srange[2] # Create training and test sets ind <- sample(1:dim(promotergene)[1], 20) genetrain <- promotergene[-ind, ] genetest <- promotergene[ind, ] # Train SVM with estimated sigma gene <- ksvm(Class ~ ., data = genetrain, kernel = "rbfdot", kpar = list(sigma = sigma_optimal), C = 50, cross = 3) gene # Predict on test set promoter <- predict(gene, genetest[, -1]) table(promoter, genetest[, 1]) # Estimate sigma from matrix input X <- as.matrix(promotergene[, -1]) srange_mat <- sigest(X, frac = 0.5, scaled = TRUE) # Using automatic sigma estimation in ksvm gene_auto <- ksvm(Class ~ ., data = genetrain, kernel = "rbfdot", kpar = "automatic", C = 50) # Uses sigest internally ``` ### Response #### Success Response (200) Returns a named vector with estimated sigma values (e.g., 10%, 50%, 90% quantiles). #### Response Example ``` # 10% 50% 90% # 0.01234567 0.05678901 0.12345678 # Cross validation error: 0.089 ``` ``` -------------------------------- ### Kernel Canonical Correlation Analysis (KCCA) Source: https://context7.com/cran/kernlab/llms.txt Performs nonlinear CCA using kernel methods to find correlated projections between two datasets. ```r library(kernlab) # Create two related datasets x <- matrix(rnorm(30), 15) y <- matrix(rnorm(30), 15) # Perform Kernel CCA kcca_result <- kcca(x, y, ncomps = 2) kcca_result # Kernel Canonical Correlation Analysis # Get correlation coefficients in feature space kcor(kcca_result) # [1] 0.8765432 0.5432109 # Get coefficients for x variables xcoef(kcca_result) # Get coefficients for y variables ycoef(kcca_result) # Kernel CCA with different kernel kcca_poly <- kcca(x, y, kernel = "polydot", kpar = list(degree = 2, scale = 1, offset = 1), gamma = 0.1, ncomps = 3) # Use with larger datasets data(iris) x_iris <- as.matrix(iris[, 1:2]) y_iris <- as.matrix(iris[, 3:4]) kcca_iris <- kcca(x_iris, y_iris, kernel = "rbfdot", kpar = list(sigma = 0.5), ncomps = 2) ``` -------------------------------- ### Compute Cross Kernel Matrix Source: https://context7.com/cran/kernlab/llms.txt Computes the kernel matrix between two different datasets using a specified kernel function. Useful for tasks involving training and testing sets with different dimensions. ```R dt2 <- as.matrix(spam[1:10, -58]) K_cross <- kernelMatrix(rbf, dt, dt2) dim(K_cross) ``` -------------------------------- ### MMD Test for Same Distribution Source: https://context7.com/cran/kernlab/llms.txt Performs an MMD test to check if two samples are from the same distribution. The result indicates whether the null hypothesis (samples are from the same distribution) can be rejected. ```R x_same <- matrix(rnorm(300), 100) y_same <- matrix(rnorm(300), 100) mmdo_same <- kmmd(x_same, y_same) H0(mmdo_same) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.