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 | ✔ |
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" )
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:
dplyr, mutate(mtcars, !! tidypredict_fit(model))tidypredict_to_column(model) to add it to a piped command settidypredict_sql(model, con) to retrieve the SQL statementmtcars %>% tidypredict_to_column(model) %>% glimpse()
tidypredict_sql(model, dbplyr::simulate_mssql())
tidypredict_test(model, mtcars)
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)
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)
vanilladot kernel is supported. Non-linear kernels cannot be
written as a single formula over the columns.prob.model = TRUE.ksvm(x, y),
because ksvm() mangles them with make.names() and keeps no record of the
originals. See the supported models article for details.Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.