Support vector machines, using kernlab

if (rlang::is_installed("kernlab")) {
  library(tidypredict)
  library(kernlab)
  library(dplyr)
  eval_code <- TRUE
} else {
  eval_code <- FALSE
}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = eval_code
)
set.seed(100)

| Function |Works| |---------------------------------------------------------------|-----| |tidypredict_fit(), tidypredict_sql(), parse_model() | ✔ | |tidypredict_to_column() | ✔ | |tidypredict_test() | ✔ | |tidypredict_interval(), tidypredict_sql_interval() | ✗ | |parsnip | ✔ |

How it works

Only linear (vanilladot) kernels are supported. With a linear kernel the decision function collapses to a weighted sum of the predictors, which is exactly the shape tidypredict can write out.

library(kernlab)
library(dplyr)
library(tidypredict)

model <- ksvm(
  mpg ~ wt + hp + disp,
  data = mtcars,
  kernel = "vanilladot",
  type = "eps-svr"
)

Under the hood

The parser multiplies the support vectors by their coefficients to recover one weight per predictor, undoes the scaling ksvm() applied while fitting, and adds the bias term.

pm <- parse_model(model)
str(pm, 2)

The parsed model is transformed into a dplyr, a.k.a. Tidy Eval, formula.

tidypredict_fit(model)

From there, the Tidy Eval formula can be used anywhere it can be evaluated. tidypredict provides three paths:

mtcars %>%
  tidypredict_to_column(model) %>%
  glimpse()
tidypredict_sql(model, dbplyr::simulate_mssql())

How it performs

tidypredict_test(model, mtcars)

Classification

Binary classification models are supported, and return the probability of the second outcome level. ksvm() turns its decision values into probabilities with a fitted sigmoid, so the model has to be fitted with prob.model = TRUE for that sigmoid to exist.

df <- mtcars
df$vs <- factor(df$vs)

model_class <- ksvm(
  vs ~ wt + mpg,
  data = df,
  kernel = "vanilladot",
  type = "C-svc",
  prob.model = TRUE
)

tidypredict_fit(model_class)

parsnip

tidypredict also supports ksvm model objects fitted via the parsnip package, using svm_linear() with the "kernlab" engine.

library(parsnip)

parsnip_model <- svm_linear(mode = "regression") |>
  set_engine("kernlab") |>
  fit(mpg ~ wt + hp, data = mtcars)

tidypredict_fit(parsnip_model)

Limitations



Try the tidypredict package in your browser

Any scripts or data that you put into this service are public.

tidypredict documentation built on Oct. 18, 2026, 5:06 p.m.