├── .github ├── .gitignore └── workflows │ └── pkgdown.yaml ├── LICENSE ├── vignettes ├── .gitignore ├── pre-processors-in-maize.qmd ├── glass-gem.qmd └── ensembling_weak_learners.Rmd ├── data └── corn_data.rda ├── man ├── figures │ └── logo.png ├── autoplot.kcor_df.Rd ├── lssvm_laplace.Rd ├── corn_data.Rd ├── bag_svm_rbf.Rd ├── rus_boost_svm_rbf.Rd ├── svm_cossim.Rd ├── ada_boost_svm_rbf.Rd ├── svm_spline.Rd ├── kqr_laplace.Rd ├── svm_sorensen.Rd ├── svm_tanimoto.Rd ├── rvm_laplace.Rd ├── svm_cauchy.Rd ├── svm_fourier.Rd ├── svm_tstudent.Rd ├── svm_tanh.Rd ├── misvm_orova_rbf.Rd ├── svm_laplace.Rd ├── svm_anova_rbf.Rd ├── maize-package.Rd ├── svm_bessel.Rd ├── svm_string.Rd ├── kcca_correlate.Rd ├── svm_wavelet.Rd ├── score.apd_svm_novel_detection.Rd ├── int_conformal_quantile_svm.Rd ├── apd_svm_novel_detection.Rd ├── arima_svm_laplace.Rd ├── cal_estimate_svm.Rd ├── step_kfm_nystrom.Rd ├── step_kpca_laplace.Rd ├── step_kpca_tanh.Rd ├── step_kfa_laplace.Rd ├── step_kha_tanh.Rd └── step_kha_laplace.Rd ├── pkgdown └── favicon │ ├── favicon.ico │ ├── favicon-96x96.png │ ├── apple-touch-icon.png │ ├── web-app-manifest-192x192.png │ ├── web-app-manifest-512x512.png │ └── site.webmanifest ├── .gitignore ├── _pkgdown.yml ├── .Rbuildignore ├── R ├── maize_imports.R ├── maize-package.R ├── corn_data.R ├── misc.R ├── zzz.R ├── bag_svm_rbf.R ├── lssvm_laplace.R ├── svm_cossim.R ├── ada_boost_svm_rbf.R ├── rus_boost_svm_rbf.R ├── svm_sorensen.R ├── svm_tanimoto.R ├── svm_spline.R ├── lssvm_laplace_data.R ├── rvm_laplace.R ├── kqr_laplace_data.R ├── svm_cauchy.R ├── svm_fourier.R ├── svm_tstudent.R ├── rvm_laplace_data.R ├── bag_svm_rbf_data.R ├── ada_boost_svm_rbf_data.R ├── rus_boost_svm_rbf_data.R ├── kqr_laplace.R ├── misvm_orova_rbf_data.R ├── svm_laplace.R ├── svm_wavelet.R ├── svm_tanh.R ├── apd_svm_novel_detection-score.R ├── svm_anova_rbf.R ├── kcca_correlate.R ├── svm_string_data.R ├── svm_bessel.R ├── svm_cossim_data.R ├── svm_sorensen_data.R ├── svm_tanimoto_data.R ├── svm_spline_data.R ├── svm_cauchy_data.R ├── svm_fourier_data.R ├── svm_tstudent_data.R ├── svm_tanh_data.R ├── svm_laplace_data.R ├── svm_anova_rbf_data.R └── step_kpca_tanh.R ├── maize.Rproj ├── LICENSE.md ├── DESCRIPTION └── _brand.yml /.github/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2024 2 | COPYRIGHT HOLDER: maize authors 3 | -------------------------------------------------------------------------------- /vignettes/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | *.R 3 | *_files 4 | 5 | /.quarto/ 6 | -------------------------------------------------------------------------------- /data/corn_data.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankiethull/maize/HEAD/data/corn_data.rda -------------------------------------------------------------------------------- /man/figures/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankiethull/maize/HEAD/man/figures/logo.png -------------------------------------------------------------------------------- /pkgdown/favicon/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankiethull/maize/HEAD/pkgdown/favicon/favicon.ico -------------------------------------------------------------------------------- /pkgdown/favicon/favicon-96x96.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankiethull/maize/HEAD/pkgdown/favicon/favicon-96x96.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankiethull/maize/HEAD/pkgdown/favicon/apple-touch-icon.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | inst/doc 6 | **/.quarto/ 7 | /doc/ 8 | /Meta/ 9 | docs 10 | -------------------------------------------------------------------------------- /_pkgdown.yml: -------------------------------------------------------------------------------- 1 | url: https://frankiethull.github.io/maize/ 2 | template: 3 | bslib: 4 | version: 5 5 | brand: _brand.yml 6 | 7 | -------------------------------------------------------------------------------- /pkgdown/favicon/web-app-manifest-192x192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankiethull/maize/HEAD/pkgdown/favicon/web-app-manifest-192x192.png -------------------------------------------------------------------------------- /pkgdown/favicon/web-app-manifest-512x512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankiethull/maize/HEAD/pkgdown/favicon/web-app-manifest-512x512.png -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^vignettes/\.quarto$ 4 | ^vignettes/*_files$ 5 | ^doc$ 6 | ^Meta$ 7 | ^_pkgdown\.yml$ 8 | ^docs$ 9 | ^pkgdown$ 10 | ^\.github$ 11 | -------------------------------------------------------------------------------- /R/maize_imports.R: -------------------------------------------------------------------------------- 1 | ## usethis namespace: start 2 | ##' @import parsnip 3 | ##' @import recipes 4 | #' @importFrom rlang enquo expr enquos 5 | #' @importFrom tibble is_tibble as_tibble tibble 6 | ## usethis namespace: end 7 | NULL 8 | -------------------------------------------------------------------------------- /R/maize-package.R: -------------------------------------------------------------------------------- 1 | #' @keywords internal 2 | "_PACKAGE" 3 | 4 | ## usethis namespace: start 5 | #' @importFrom applicable score 6 | #' @importFrom probably cal_apply 7 | #' @importFrom probably cal_apply_regression 8 | #' @importFrom vctrs vec_cbind 9 | ## usethis namespace: end 10 | NULL 11 | -------------------------------------------------------------------------------- /man/autoplot.kcor_df.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/kcca_correlate.R 3 | \name{autoplot.kcor_df} 4 | \alias{autoplot.kcor_df} 5 | \title{visualize component x-y feature space} 6 | \usage{ 7 | \method{autoplot}{kcor_df}(x) 8 | } 9 | \arguments{ 10 | \item{x}{a kcor_df object} 11 | } 12 | \description{ 13 | visualize component x-y feature space 14 | } 15 | -------------------------------------------------------------------------------- /R/corn_data.R: -------------------------------------------------------------------------------- 1 | #' Synthetic Corn Dataset for Corny Example 2 | #' 3 | #' @details Asked Claude Sonnet for a corn data given the README story problem 4 | #' 5 | #' @name corn_data 6 | #' @aliases corn_data 7 | #' @docType data 8 | #' @return \item{corn_data}{a tibble} 9 | #' 10 | #' @source claude-3-5-sonnet-20240620 11 | #' 12 | #' @keywords datasets 13 | #' @examples 14 | #' data(corn_data) 15 | #' str(corn_data) 16 | NULL -------------------------------------------------------------------------------- /man/lssvm_laplace.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lssvm_laplace.R 3 | \name{lssvm_laplace} 4 | \alias{lssvm_laplace} 5 | \title{Laplacian Least Squares Support Vector Machine} 6 | \usage{ 7 | lssvm_laplace(mode = "unknown", engine = "kernlab", laplace_sigma = NULL) 8 | } 9 | \arguments{ 10 | \item{mode}{classification} 11 | 12 | \item{engine}{kernlab lssvm} 13 | 14 | \item{laplace_sigma}{sigma parameter for laplacian} 15 | } 16 | \description{ 17 | laplacian kernel 18 | } 19 | -------------------------------------------------------------------------------- /pkgdown/favicon/site.webmanifest: -------------------------------------------------------------------------------- 1 | { 2 | "name": "", 3 | "short_name": "", 4 | "icons": [ 5 | { 6 | "src": "/web-app-manifest-192x192.png", 7 | "sizes": "192x192", 8 | "type": "image/png", 9 | "purpose": "maskable" 10 | }, 11 | { 12 | "src": "/web-app-manifest-512x512.png", 13 | "sizes": "512x512", 14 | "type": "image/png", 15 | "purpose": "maskable" 16 | } 17 | ], 18 | "theme_color": "#ffffff", 19 | "background_color": "#ffffff", 20 | "display": "standalone" 21 | } -------------------------------------------------------------------------------- /maize.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | ProjectId: 051c2c14-9a37-40bf-b536-a52c4d9b65d0 3 | 4 | RestoreWorkspace: No 5 | SaveWorkspace: No 6 | AlwaysSaveHistory: Default 7 | 8 | EnableCodeIndexing: Yes 9 | UseSpacesForTab: Yes 10 | NumSpacesForTab: 2 11 | Encoding: UTF-8 12 | 13 | RnwWeave: Sweave 14 | LaTeX: pdfLaTeX 15 | 16 | AutoAppendNewline: Yes 17 | StripTrailingWhitespace: Yes 18 | LineEndingConversion: Posix 19 | 20 | BuildType: Package 21 | PackageUseDevtools: Yes 22 | PackageInstallArgs: --no-multiarch --with-keep.source 23 | PackageRoxygenize: rd,collate,namespace 24 | -------------------------------------------------------------------------------- /man/corn_data.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/corn_data.R 3 | \docType{data} 4 | \name{corn_data} 5 | \alias{corn_data} 6 | \title{Synthetic Corn Dataset for Corny Example} 7 | \source{ 8 | claude-3-5-sonnet-20240620 9 | } 10 | \value{ 11 | \item{corn_data}{a tibble} 12 | } 13 | \description{ 14 | Synthetic Corn Dataset for Corny Example 15 | } 16 | \details{ 17 | Asked Claude Sonnet for a corn data given the README story problem 18 | } 19 | \examples{ 20 | data(corn_data) 21 | str(corn_data) 22 | } 23 | \keyword{datasets} 24 | -------------------------------------------------------------------------------- /man/bag_svm_rbf.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bag_svm_rbf.R 3 | \name{bag_svm_rbf} 4 | \alias{bag_svm_rbf} 5 | \title{RUS Bagged SVM with Radial Basis Function Kernel} 6 | \usage{ 7 | bag_svm_rbf( 8 | mode = "unknown", 9 | engine = "ebmc", 10 | num_learners = NULL, 11 | imb_ratio = NULL 12 | ) 13 | } 14 | \arguments{ 15 | \item{mode}{classification} 16 | 17 | \item{engine}{ebmc's ub which uses e1701's svm} 18 | 19 | \item{num_learners}{how many weak learners should be ensembled via bagging} 20 | 21 | \item{imb_ratio}{major-minor class imbalance ratio} 22 | } 23 | \description{ 24 | RUS Bagged SVM with Radial Basis Function Kernel 25 | } 26 | -------------------------------------------------------------------------------- /man/rus_boost_svm_rbf.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/rus_boost_svm_rbf.R 3 | \name{rus_boost_svm_rbf} 4 | \alias{rus_boost_svm_rbf} 5 | \title{RUSBoost SVM with Radial Basis Function Kernel} 6 | \usage{ 7 | rus_boost_svm_rbf( 8 | mode = "unknown", 9 | engine = "ebmc", 10 | num_learners = NULL, 11 | imb_ratio = NULL 12 | ) 13 | } 14 | \arguments{ 15 | \item{mode}{classification} 16 | 17 | \item{engine}{ebmc's rus which uses e1701's svm} 18 | 19 | \item{num_learners}{how many weak learners should be ensembled via boosting} 20 | 21 | \item{imb_ratio}{major-minor class imbalance ratio} 22 | } 23 | \description{ 24 | RUSBoost SVM with Radial Basis Function Kernel 25 | } 26 | -------------------------------------------------------------------------------- /man/svm_cossim.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/svm_cossim.R 3 | \name{svm_cossim} 4 | \alias{svm_cossim} 5 | \title{Cosine Similarity Support Vector Machine} 6 | \usage{ 7 | svm_cossim(mode = "unknown", engine = "kernlab", cost = NULL, margin = NULL) 8 | } 9 | \arguments{ 10 | \item{mode}{regression or classification} 11 | 12 | \item{engine}{kernlab ksvm} 13 | 14 | \item{cost}{A positive number for the cost of predicting a sample within 15 | or on the wrong side of the margin} 16 | 17 | \item{margin}{A positive number for the epsilon in the SVM insensitive 18 | loss function (regression only)} 19 | } 20 | \description{ 21 | cossim kernel for support vector machines 22 | } 23 | -------------------------------------------------------------------------------- /man/ada_boost_svm_rbf.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ada_boost_svm_rbf.R 3 | \name{ada_boost_svm_rbf} 4 | \alias{ada_boost_svm_rbf} 5 | \title{AdaBoost SVM with Radial Basis Function Kernel} 6 | \usage{ 7 | ada_boost_svm_rbf( 8 | mode = "unknown", 9 | engine = "ebmc", 10 | num_learners = NULL, 11 | imb_ratio = NULL 12 | ) 13 | } 14 | \arguments{ 15 | \item{mode}{classification} 16 | 17 | \item{engine}{ebmc's adam2 which uses e1701's svm} 18 | 19 | \item{num_learners}{how many weak learners should be ensembled via boosting} 20 | 21 | \item{imb_ratio}{major-minor class imbalance ratio} 22 | } 23 | \description{ 24 | AdaBoost SVM with Radial Basis Function Kernel 25 | } 26 | -------------------------------------------------------------------------------- /R/misc.R: -------------------------------------------------------------------------------- 1 | #' @export 2 | #' @keywords internal 3 | #' @rdname add_on_exports 4 | check_args <- function(object, call = rlang::caller_env()) { 5 | UseMethod("check_args") 6 | } 7 | 8 | #' @export 9 | check_args.default <- function(object, call = rlang::caller_env()) { 10 | invisible(object) 11 | } 12 | 13 | # for recipes 14 | uses_dim_red <- function(x) { 15 | dr <- inherits(x, "dimRedResult") 16 | if (dr) { 17 | cli::cli_abort( 18 | "Recipes version >= 0.1.17 represents the estimates using a different \\ 19 | format. Please recreate this recipe or use version 0.1.16 or less. See \\ 20 | issue {.href [#823](https://github.com/tidymodels/recipes/issues/823)}." 21 | ) 22 | } 23 | invisible(NULL) 24 | } 25 | -------------------------------------------------------------------------------- /man/svm_spline.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/svm_spline.R 3 | \name{svm_spline} 4 | \alias{svm_spline} 5 | \title{Spline Support Vector Machine} 6 | \usage{ 7 | svm_spline(mode = "unknown", engine = "kernlab", cost = NULL, margin = NULL) 8 | } 9 | \arguments{ 10 | \item{mode}{regression or classification} 11 | 12 | \item{engine}{kernlab ksvm} 13 | 14 | \item{cost}{A positive number for the cost of predicting a sample within 15 | or on the wrong side of the margin} 16 | 17 | \item{margin}{A positive number for the epsilon in the SVM insensitive 18 | loss function (regression only)} 19 | } 20 | \description{ 21 | spline kernel for support vector machines 22 | } 23 | -------------------------------------------------------------------------------- /man/kqr_laplace.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/kqr_laplace.R 3 | \name{kqr_laplace} 4 | \alias{kqr_laplace} 5 | \title{Laplacian Kernel Quantile Regression} 6 | \usage{ 7 | kqr_laplace( 8 | mode = "unknown", 9 | engine = "kernlab", 10 | cost = NULL, 11 | tau = NULL, 12 | laplace_sigma = NULL 13 | ) 14 | } 15 | \arguments{ 16 | \item{mode}{regression} 17 | 18 | \item{engine}{kernlab kqr} 19 | 20 | \item{cost}{A positive number for the cost of predicting a sample within 21 | or on the wrong side of the margin} 22 | 23 | \item{tau}{a quantile for the loss function} 24 | 25 | \item{laplace_sigma}{sigma parameter for laplacian} 26 | } 27 | \description{ 28 | laplacian kernel 29 | } 30 | -------------------------------------------------------------------------------- /man/svm_sorensen.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/svm_sorensen.R 3 | \name{svm_sorensen} 4 | \alias{svm_sorensen} 5 | \title{Sorensen Support Vector Machine} 6 | \usage{ 7 | svm_sorensen(mode = "unknown", engine = "kernlab", cost = NULL, margin = NULL) 8 | } 9 | \arguments{ 10 | \item{mode}{regression or classification} 11 | 12 | \item{engine}{kernlab ksvm} 13 | 14 | \item{cost}{A positive number for the cost of predicting a sample within 15 | or on the wrong side of the margin} 16 | 17 | \item{margin}{A positive number for the epsilon in the SVM insensitive 18 | loss function (regression only)} 19 | } 20 | \description{ 21 | sorensen kernel for support vector machines which is used as a graph kernel for chemical informatics 22 | } 23 | -------------------------------------------------------------------------------- /man/svm_tanimoto.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/svm_tanimoto.R 3 | \name{svm_tanimoto} 4 | \alias{svm_tanimoto} 5 | \title{Tanimoto Support Vector Machine} 6 | \usage{ 7 | svm_tanimoto(mode = "unknown", engine = "kernlab", cost = NULL, margin = NULL) 8 | } 9 | \arguments{ 10 | \item{mode}{regression or classification} 11 | 12 | \item{engine}{kernlab ksvm} 13 | 14 | \item{cost}{A positive number for the cost of predicting a sample within 15 | or on the wrong side of the margin} 16 | 17 | \item{margin}{A positive number for the epsilon in the SVM insensitive 18 | loss function (regression only)} 19 | } 20 | \description{ 21 | tanimoto kernel for support vector machines which is used as a graph kernel for chemical informatics 22 | } 23 | -------------------------------------------------------------------------------- /man/rvm_laplace.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/rvm_laplace.R 3 | \name{rvm_laplace} 4 | \alias{rvm_laplace} 5 | \title{Laplacian Relevance Vector Machine (Experimental RVM)} 6 | \usage{ 7 | rvm_laplace( 8 | mode = "unknown", 9 | engine = "kernlab", 10 | alpha = NULL, 11 | var = NULL, 12 | laplace_sigma = NULL 13 | ) 14 | } 15 | \arguments{ 16 | \item{mode}{regression only for RVM} 17 | 18 | \item{engine}{kernlab rvm} 19 | 20 | \item{alpha}{(alpha) The initial alpha value or vector. 21 | Can be either a vector of length equal to the number of data points or a single number.} 22 | 23 | \item{var}{(var) the initial noise variance} 24 | 25 | \item{laplace_sigma}{sigma parameter for laplacian} 26 | } 27 | \description{ 28 | laplacian kernel 29 | } 30 | -------------------------------------------------------------------------------- /man/svm_cauchy.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/svm_cauchy.R 3 | \name{svm_cauchy} 4 | \alias{svm_cauchy} 5 | \title{Cauchy Support Vector Machine} 6 | \usage{ 7 | svm_cauchy( 8 | mode = "unknown", 9 | engine = "kernlab", 10 | cost = NULL, 11 | margin = NULL, 12 | sigma = NULL 13 | ) 14 | } 15 | \arguments{ 16 | \item{mode}{regression or classification} 17 | 18 | \item{engine}{kernlab ksvm} 19 | 20 | \item{cost}{A positive number for the cost of predicting a sample within 21 | or on the wrong side of the margin} 22 | 23 | \item{margin}{A positive number for the epsilon in the SVM insensitive 24 | loss function (regression only)} 25 | 26 | \item{sigma}{a sigma parameter for cauchy kernels} 27 | } 28 | \description{ 29 | cauchy kernel for support vector machines 30 | } 31 | -------------------------------------------------------------------------------- /man/svm_fourier.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/svm_fourier.R 3 | \name{svm_fourier} 4 | \alias{svm_fourier} 5 | \title{Fourier Support Vector Machine} 6 | \usage{ 7 | svm_fourier( 8 | mode = "unknown", 9 | engine = "kernlab", 10 | cost = NULL, 11 | margin = NULL, 12 | sigma = NULL 13 | ) 14 | } 15 | \arguments{ 16 | \item{mode}{regression or classification} 17 | 18 | \item{engine}{kernlab ksvm} 19 | 20 | \item{cost}{A positive number for the cost of predicting a sample within 21 | or on the wrong side of the margin} 22 | 23 | \item{margin}{A positive number for the epsilon in the SVM insensitive 24 | loss function (regression only)} 25 | 26 | \item{sigma}{a sigma parameter for fourier kernels} 27 | } 28 | \description{ 29 | fourier kernel for support vector machines 30 | } 31 | -------------------------------------------------------------------------------- /man/svm_tstudent.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/svm_tstudent.R 3 | \name{svm_tstudent} 4 | \alias{svm_tstudent} 5 | \title{T-Student Support Vector Machine} 6 | \usage{ 7 | svm_tstudent( 8 | mode = "unknown", 9 | engine = "kernlab", 10 | cost = NULL, 11 | margin = NULL, 12 | degree = NULL 13 | ) 14 | } 15 | \arguments{ 16 | \item{mode}{regression or classification} 17 | 18 | \item{engine}{kernlab ksvm} 19 | 20 | \item{cost}{A positive number for the cost of predicting a sample within 21 | or on the wrong side of the margin} 22 | 23 | \item{margin}{A positive number for the epsilon in the SVM insensitive 24 | loss function (regression only)} 25 | 26 | \item{degree}{a degree parameter for tstudent kernels} 27 | } 28 | \description{ 29 | t-student kernel for support vector machines 30 | } 31 | -------------------------------------------------------------------------------- /man/svm_tanh.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/svm_tanh.R 3 | \name{svm_tanh} 4 | \alias{svm_tanh} 5 | \title{Hyperbolic Tangent Support Vector Machine} 6 | \usage{ 7 | svm_tanh( 8 | mode = "unknown", 9 | engine = "kernlab", 10 | cost = NULL, 11 | scale_factor = NULL, 12 | margin = NULL 13 | ) 14 | } 15 | \arguments{ 16 | \item{mode}{regression or classification} 17 | 18 | \item{engine}{kernlab ksvm} 19 | 20 | \item{cost}{A positive number for the cost of predicting a sample within 21 | or on the wrong side of the margin} 22 | 23 | \item{scale_factor}{scale parameter for tanh} 24 | 25 | \item{margin}{A positive number for the epsilon in the SVM insensitive 26 | loss function (regression only)} 27 | } 28 | \description{ 29 | tanh kernel for support vector machines 30 | } 31 | -------------------------------------------------------------------------------- /man/misvm_orova_rbf.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/misvm_orova_rbf.R 3 | \name{misvm_orova_rbf} 4 | \alias{misvm_orova_rbf} 5 | \title{Multiple Instance SVMs for Ordinal Outcome Data, One-Vs-All, with Radial Basis Function Kernel} 6 | \usage{ 7 | misvm_orova_rbf( 8 | mode = "unknown", 9 | engine = "ebmc", 10 | cost = NULL, 11 | rbf_sigma = NULL 12 | ) 13 | } 14 | \arguments{ 15 | \item{mode}{classification} 16 | 17 | \item{engine}{\code{mildsvm::misvm_orova()} which uses e1701's svm} 18 | 19 | \item{cost}{A positive number for the cost of predicting a sample within 20 | or on the wrong side of the margin} 21 | 22 | \item{rbf_sigma}{A positive number for radial basis function.} 23 | } 24 | \description{ 25 | Multiple Instance SVMs for Ordinal Outcome Data, One-Vs-All, with Radial Basis Function Kernel 26 | } 27 | -------------------------------------------------------------------------------- /man/svm_laplace.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/svm_laplace.R 3 | \name{svm_laplace} 4 | \alias{svm_laplace} 5 | \title{Laplacian Support Vector Machine} 6 | \usage{ 7 | svm_laplace( 8 | mode = "unknown", 9 | engine = "kernlab", 10 | cost = NULL, 11 | margin = NULL, 12 | laplace_sigma = NULL 13 | ) 14 | } 15 | \arguments{ 16 | \item{mode}{regression or classification} 17 | 18 | \item{engine}{kernlab ksvm} 19 | 20 | \item{cost}{A positive number for the cost of predicting a sample within 21 | or on the wrong side of the margin} 22 | 23 | \item{margin}{A positive number for the epsilon in the SVM insensitive 24 | loss function (regression only)} 25 | 26 | \item{laplace_sigma}{sigma parameter for laplacian} 27 | } 28 | \description{ 29 | laplacian kernel for support vector machines 30 | } 31 | -------------------------------------------------------------------------------- /man/svm_anova_rbf.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/svm_anova_rbf.R 3 | \name{svm_anova_rbf} 4 | \alias{svm_anova_rbf} 5 | \title{ANOVA RBF Support Vector Machine} 6 | \usage{ 7 | svm_anova_rbf( 8 | mode = "unknown", 9 | engine = "kernlab", 10 | cost = NULL, 11 | anova_rbf_sigma = NULL, 12 | degree = NULL, 13 | margin = NULL 14 | ) 15 | } 16 | \arguments{ 17 | \item{mode}{regression or classification} 18 | 19 | \item{engine}{kernlab ksvm} 20 | 21 | \item{cost}{A positive number for the cost of predicting a sample within 22 | or on the wrong side of the margin} 23 | 24 | \item{anova_rbf_sigma}{sigma parameter for anova rbf} 25 | 26 | \item{degree}{degree parameter for anova rbf} 27 | 28 | \item{margin}{A positive number for the epsilon in the SVM insensitive 29 | loss function (regression only)} 30 | } 31 | \description{ 32 | anova rbf for support vector machines 33 | } 34 | -------------------------------------------------------------------------------- /man/maize-package.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/maize-package.R 3 | \docType{package} 4 | \name{maize-package} 5 | \alias{maize} 6 | \alias{maize-package} 7 | \title{maize: Specialty Kernels for SVMs} 8 | \description{ 9 | \if{html}{\figure{logo.png}{options: style='float: right' alt='logo' width='120'}} 10 | 11 | Bindings for SVMs and special kernels via kernlab, e1701, ebmc, and mildsvm extending the 'parsnip' package. Specifically related to kernels for support vector machines not available in parsnip. Package also includes interfaces for novel SVM preprocessors (recipes) and postprocessors (probably). 12 | } 13 | \seealso{ 14 | Useful links: 15 | \itemize{ 16 | \item \url{https://frankiethull.github.io/maize/} 17 | } 18 | 19 | } 20 | \author{ 21 | \strong{Maintainer}: Frank Hull \email{frankiethull@gmail.com} 22 | 23 | Other contributors: 24 | \itemize{ 25 | \item Max Kuhn \email{max@posit.co} [contributor] 26 | } 27 | 28 | } 29 | \keyword{internal} 30 | -------------------------------------------------------------------------------- /man/svm_bessel.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/svm_bessel.R 3 | \name{svm_bessel} 4 | \alias{svm_bessel} 5 | \title{Bessel Support Vector Machine} 6 | \usage{ 7 | svm_bessel( 8 | mode = "unknown", 9 | engine = "kernlab", 10 | cost = NULL, 11 | bessel_sigma = NULL, 12 | degree = NULL, 13 | order = NULL, 14 | margin = NULL 15 | ) 16 | } 17 | \arguments{ 18 | \item{mode}{regression or classification} 19 | 20 | \item{engine}{kernlab ksvm} 21 | 22 | \item{cost}{A positive number for the cost of predicting a sample within 23 | or on the wrong side of the margin} 24 | 25 | \item{bessel_sigma}{sigma parameter for bessel} 26 | 27 | \item{degree}{degree parameter for bessel} 28 | 29 | \item{order}{order parameter for bessel} 30 | 31 | \item{margin}{A positive number for the epsilon in the SVM insensitive 32 | loss function (regression only)} 33 | } 34 | \description{ 35 | bessel kernel for support vector machines 36 | } 37 | -------------------------------------------------------------------------------- /man/svm_string.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/svm_string.R 3 | \name{svm_string} 4 | \alias{svm_string} 5 | \title{String Support Vector Machine} 6 | \usage{ 7 | svm_string( 8 | x, 9 | y, 10 | mode = "unknown", 11 | engine = "kernlab", 12 | cost = NULL, 13 | length = NULL, 14 | lambda = NULL, 15 | normalized = TRUE, 16 | margin = NULL 17 | ) 18 | } 19 | \arguments{ 20 | \item{mode}{regression or classification} 21 | 22 | \item{engine}{kernlab ksvm} 23 | 24 | \item{cost}{A positive number for the cost of predicting a sample within 25 | or on the wrong side of the margin} 26 | 27 | \item{length}{The length of the substrings considered} 28 | 29 | \item{lambda}{The decay factor} 30 | 31 | \item{normalized}{normalize string kernel values, (default = TRUE)} 32 | 33 | \item{margin}{A positive number for the epsilon in the SVM insensitive 34 | loss function (regression only)} 35 | } 36 | \description{ 37 | stringdot for support vector machines 38 | } 39 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | 2 | .onLoad <- function(libname, pkgname) { 3 | # kernlab engines + maize kernels ---- 4 | 5 | # SVMs 6 | make_svm_laplace() 7 | make_svm_tanh() 8 | make_svm_bessel() 9 | make_svm_anova_rbf() 10 | make_svm_spline() 11 | make_svm_cossim() 12 | make_svm_cauchy() 13 | make_svm_tanimoto() 14 | make_svm_sorensen() 15 | make_svm_tstudent() 16 | make_svm_fourier() 17 | make_svm_wavelet() 18 | make_svm_string() 19 | 20 | # RVMs 21 | make_rvm_laplace() 22 | 23 | # KQRs 24 | make_kqr_laplace() 25 | 26 | # LSSVMs 27 | make_lssvm_laplace() 28 | 29 | # ebmc random-under-sampling (rus) engines ---- 30 | 31 | # bagged SVMs 32 | make_bag_svm_rbf() 33 | 34 | # boosted SVMs 35 | make_rus_boost_svm_rbf() 36 | make_ada_boost_svm_rbf() 37 | 38 | # mildsvm engines ---- 39 | 40 | # multi-instance SVMs 41 | make_misvm_orova_rbf() 42 | 43 | # modeltime engines ---- 44 | 45 | # arima-svms 46 | make_arima_svm_laplace() 47 | #make_recursive_svm_laplace() # issue with xregs 48 | 49 | } 50 | -------------------------------------------------------------------------------- /man/kcca_correlate.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/kcca_correlate.R 3 | \name{kcca_correlate} 4 | \alias{kcca_correlate} 5 | \title{Kernel Canonical Correlation Analysis} 6 | \usage{ 7 | kcca_correlate(x, y = NULL, kernel = "rbfdot", gamma = 0.1, num_comp = 10, ...) 8 | } 9 | \arguments{ 10 | \item{x}{variable or dataframe} 11 | 12 | \item{y}{variable or dataframe} 13 | 14 | \item{kernel}{a kernel to use} 15 | 16 | \item{gamma}{regularization parameter} 17 | 18 | \item{num_comp}{number of components} 19 | 20 | \item{...}{pass through args for kcca function} 21 | } 22 | \value{ 23 | A kernel canonical correlation analysis data frame \code{kcor_df} 24 | } 25 | \description{ 26 | Computes the canonical correlation analysis in feature space. 27 | Kernel Canonical Correlation Analysis (KCCA) is a non-linear extension of CCA. 28 | Given two random variables (or datasets), KCCA aims at extracting the information which is shared by the two random variables (or datasets). 29 | more information found at \code{\link[kernlab:kcca]{kernlab::kcca()}} 30 | } 31 | -------------------------------------------------------------------------------- /man/svm_wavelet.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/svm_wavelet.R 3 | \name{svm_wavelet} 4 | \alias{svm_wavelet} 5 | \title{Wavelet Support Vector Machine} 6 | \usage{ 7 | svm_wavelet( 8 | mode = "unknown", 9 | engine = "kernlab", 10 | cost = NULL, 11 | margin = NULL, 12 | sigma = NULL, 13 | a = 1, 14 | c = NULL, 15 | h = NULL 16 | ) 17 | } 18 | \arguments{ 19 | \item{mode}{regression or classification} 20 | 21 | \item{engine}{kernlab ksvm} 22 | 23 | \item{cost}{A positive number for the cost of predicting a sample within 24 | or on the wrong side of the margin} 25 | 26 | \item{margin}{A positive number for the epsilon in the SVM insensitive 27 | loss function (regression only)} 28 | 29 | \item{sigma}{sigma parameter for svm wavelet kernel} 30 | 31 | \item{a}{scale adjustment parameter for wavelet kernels (temp name)} 32 | 33 | \item{c}{dist adjustment parameter for wavelet kernels can be NULL (temp name)} 34 | 35 | \item{h}{wavelet function for wavelet kernel, default wavelet if NULL (temp name)} 36 | } 37 | \description{ 38 | wavelet kernel for support vector machines 39 | } 40 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2024 maize authors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: maize 2 | Title: Specialty Kernels for SVMs 3 | Version: 0.0.2.95 4 | Authors@R: c( 5 | person("Frank", "Hull", , "frankiethull@gmail.com", role = c("aut", "cre")), 6 | person("Max", "Kuhn", , "max@posit.co", role = "ctb") 7 | ) 8 | Description: Bindings for SVMs and special kernels via kernlab, e1701, ebmc, and mildsvm extending the 9 | 'parsnip' package. Specifically related to kernels for support vector machines 10 | not available in parsnip. 11 | Package also includes interfaces for novel SVM preprocessors (recipes) and postprocessors (probably). 12 | License: MIT + file LICENSE 13 | Encoding: UTF-8 14 | Roxygen: list(markdown = TRUE) 15 | RoxygenNote: 7.3.2 16 | Imports: 17 | applicable, 18 | parsnip, 19 | probably, 20 | recipes, 21 | rlang, 22 | tibble, 23 | vctrs 24 | Remotes: 25 | frankiethull/qrsvm 26 | Depends: 27 | R (>= 4.4.1.0) 28 | LazyData: true 29 | Suggests: 30 | knitr, 31 | quarto, 32 | rmarkdown, 33 | qrsvm, 34 | quantregForest, 35 | ebmc, 36 | mildsvm, 37 | modeltime, 38 | timetk, 39 | workflows, 40 | kernlab, 41 | ggplot2, 42 | tidyr 43 | VignetteBuilder: quarto, knitr 44 | URL: https://frankiethull.github.io/maize/ 45 | -------------------------------------------------------------------------------- /_brand.yml: -------------------------------------------------------------------------------- 1 | # ask claude-3-5-sonnet-20241022 for desert sunset palette against purple mtn sky 2 | color: 3 | palette: 4 | sunset-orange: "#FF7E3D" # Vibrant sunset orange 5 | sand-orange: "#E8A87C" # Softer, sandy orange 6 | terra-cotta: "#C75D4B" # Earthy, desert rock orange 7 | dusk-coral: "#DE6B48" # Transitional orange-pink 8 | desert-rose: "#B76E79" # Muted pink-purple transition 9 | mountain-purple: "#6B4B7C" # Deep mountain purple 10 | night-purple: "#453759" # Dark purple for depth 11 | sand-white: "#FDF6EC" # Warm white 12 | desert-black: "#1A1614" # Warm-toned black 13 | foreground: desert-black 14 | background: sand-white 15 | primary: sunset-orange 16 | secondary: mountain-purple 17 | info: sand-orange 18 | success: terra-cotta 19 | warning: dusk-coral 20 | danger: night-purple 21 | 22 | logo: 23 | medium: profile.png 24 | 25 | # manually iterating 26 | typography: 27 | fonts: 28 | - family: Space Mono 29 | source: google 30 | - family: JetBrains Mono 31 | source: google 32 | - family: Kode Mono 33 | source: google 34 | base: Space Mono 35 | headings: 36 | family: Kode Mono 37 | color: terra-cotta 38 | monospace: JetBrains Mono 39 | -------------------------------------------------------------------------------- /man/score.apd_svm_novel_detection.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/apd_svm_novel_detection-score.R 3 | \name{score.apd_svm_novel_detection} 4 | \alias{score.apd_svm_novel_detection} 5 | \title{Predict from a \code{apd_svm_novel_detection}} 6 | \usage{ 7 | \method{score}{apd_svm_novel_detection}(object, new_data, type = "numeric", ...) 8 | } 9 | \arguments{ 10 | \item{object}{A \code{apd_svm_novel_detection} object.} 11 | 12 | \item{new_data}{A data frame or matrix of new samples.} 13 | 14 | \item{type}{A single character. The type of predictions to generate. 15 | Valid options are: 16 | \itemize{ 17 | \item \code{"numeric"} for numeric predictions. 18 | }} 19 | 20 | \item{...}{Not used, but required for extensibility.} 21 | } 22 | \value{ 23 | A tibble of predictions. The number of rows in the tibble is guaranteed 24 | to be the same as the number of rows in \code{new_data}. The \code{score} column is the 25 | raw prediction from \code{\link[kernlab:predict]{kernlab::predict()}} while \code{score_pctl} 26 | compares this value to the reference distribution of the score created by 27 | predicting the training set. A value of \emph{X} means that \emph{X} percent of the 28 | training data have scores less than the predicted value. 29 | } 30 | \description{ 31 | Predict from a \code{apd_svm_novel_detection} 32 | } 33 | \details{ 34 | About the score 35 | } 36 | \seealso{ 37 | \code{\link[=apd_svm_novel_detection]{apd_svm_novel_detection()}} 38 | } 39 | -------------------------------------------------------------------------------- /.github/workflows/pkgdown.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | release: 8 | types: [published] 9 | workflow_dispatch: 10 | 11 | name: pkgdown.yaml 12 | 13 | permissions: read-all 14 | 15 | jobs: 16 | pkgdown: 17 | runs-on: ubuntu-latest 18 | # Only restrict concurrency for non-PR jobs 19 | concurrency: 20 | group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }} 21 | env: 22 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 23 | permissions: 24 | contents: write 25 | steps: 26 | - uses: actions/checkout@v4 27 | 28 | - uses: r-lib/actions/setup-pandoc@v2 29 | 30 | - uses: r-lib/actions/setup-r@v2 31 | with: 32 | use-public-rspm: true 33 | 34 | - uses: r-lib/actions/setup-r-dependencies@v2 35 | with: 36 | extra-packages: any::pkgdown, local::. 37 | needs: website 38 | 39 | - name: Build site 40 | run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE) 41 | shell: Rscript {0} 42 | 43 | - name: Deploy to GitHub pages 🚀 44 | if: github.event_name != 'pull_request' 45 | uses: JamesIves/github-pages-deploy-action@v4.5.0 46 | with: 47 | clean: false 48 | branch: gh-pages 49 | folder: docs 50 | -------------------------------------------------------------------------------- /man/int_conformal_quantile_svm.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/int_conformal_quantile_svm.R 3 | \name{int_conformal_quantile_svm} 4 | \alias{int_conformal_quantile_svm} 5 | \alias{int_conformal_quantile_svm.workflow} 6 | \title{Prediction intervals via conformal inference and QRSVM} 7 | \usage{ 8 | int_conformal_quantile_svm(object, ...) 9 | 10 | \method{int_conformal_quantile_svm}{workflow}(object, train_data, cal_data, level = 0.95, ...) 11 | } 12 | \arguments{ 13 | \item{object}{A fitted \code{\link[workflows:workflow]{workflows::workflow()}} object.} 14 | 15 | \item{...}{Options to pass to \code{\link[qrsvm:qrsvm]{qrsvm::qrsvm()}}} 16 | 17 | \item{train_data, cal_data}{Data frames with the \emph{predictor and outcome data}. 18 | \code{train_data} should be the same data used to produce \code{object} and \code{cal_data} is 19 | used to produce predictions (and residuals). If the workflow used a recipe, 20 | these should be the data that were inputs to the recipe (and not the product 21 | of a recipe).} 22 | 23 | \item{level}{The confidence level for the intervals.} 24 | } 25 | \value{ 26 | An object of class \code{"int_conformal_quantile"} containing the 27 | information to create intervals (which includes \code{object}). 28 | The \code{predict()} method is used to produce the intervals. 29 | } 30 | \description{ 31 | To compute quantiles, this function uses Quantile 32 | SVM instead of probably's \code{"int_conformal_quantile"} QRF or classic quantile regression. 33 | } 34 | \details{ 35 | based on the initial probably implementation with slight modification. 36 | 37 | origin: https://github.com/tidymodels/probably/blob/HEAD/R/conformal_infer_quantile.R 38 | 39 | for more information, visit: 40 | https://probably.tidymodels.org 41 | } 42 | -------------------------------------------------------------------------------- /man/apd_svm_novel_detection.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/apd_svm_novel_detection-fit.R 3 | \name{apd_svm_novel_detection} 4 | \alias{apd_svm_novel_detection} 5 | \alias{apd_svm_novel_detection.default} 6 | \alias{apd_svm_novel_detection.data.frame} 7 | \alias{apd_svm_novel_detection.matrix} 8 | \alias{apd_svm_novel_detection.formula} 9 | \alias{apd_svm_novel_detection.recipe} 10 | \title{Fit an SVM novelty detection model to estimate an applicability domain.} 11 | \usage{ 12 | apd_svm_novel_detection(x, ...) 13 | 14 | \method{apd_svm_novel_detection}{default}(x, ...) 15 | 16 | \method{apd_svm_novel_detection}{data.frame}(x, ...) 17 | 18 | \method{apd_svm_novel_detection}{matrix}(x, ...) 19 | 20 | \method{apd_svm_novel_detection}{formula}(formula, data, ...) 21 | 22 | \method{apd_svm_novel_detection}{recipe}(x, data, ...) 23 | } 24 | \arguments{ 25 | \item{x}{Depending on the context: 26 | \itemize{ 27 | \item A \strong{data frame} of predictors. 28 | \item A \strong{matrix} of predictors. 29 | \item A \strong{recipe} specifying a set of preprocessing steps 30 | created from \code{\link[recipes:recipe]{recipes::recipe()}}. 31 | }} 32 | 33 | \item{...}{Options to pass to \code{\link[kernlab:ksvm]{kernlab::ksvm()}}. Options should 34 | not include \code{data}.} 35 | 36 | \item{formula}{A formula specifying the predictor terms on the right-hand 37 | side. No outcome should be specified.} 38 | 39 | \item{data}{When a \strong{recipe} or \strong{formula} is used, \code{data} is specified as: 40 | \itemize{ 41 | \item A \strong{data frame} containing the predictors. 42 | }} 43 | } 44 | \value{ 45 | A \code{apd_svm_novel_detection} object. 46 | } 47 | \description{ 48 | \code{apd_svm_novel_detection()} fits an 'one-svc' novelty detection model. 49 | } 50 | -------------------------------------------------------------------------------- /man/arima_svm_laplace.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/arima_svm_laplace.R 3 | \name{arima_svm_laplace} 4 | \alias{arima_svm_laplace} 5 | \title{General Interface for "Support Vector Machine" ARIMA Regression Models} 6 | \usage{ 7 | arima_svm_laplace( 8 | mode = "regression", 9 | seasonal_period = NULL, 10 | non_seasonal_ar = NULL, 11 | non_seasonal_differences = NULL, 12 | non_seasonal_ma = NULL, 13 | seasonal_ar = NULL, 14 | seasonal_differences = NULL, 15 | seasonal_ma = NULL, 16 | cost = NULL, 17 | margin = NULL, 18 | laplace_sigma = NULL 19 | ) 20 | } 21 | \arguments{ 22 | \item{mode}{A single character string for the type of model. 23 | The only possible value for this model is "regression".} 24 | 25 | \item{seasonal_period}{A seasonal frequency. Uses "auto" by default. 26 | A character phrase of "auto" or time-based phrase of "2 weeks" 27 | can be used if a date or date-time variable is provided. 28 | See Fit Details below.} 29 | 30 | \item{non_seasonal_ar}{The order of the non-seasonal auto-regressive (AR) terms. Often denoted "p" in pdq-notation.} 31 | 32 | \item{non_seasonal_differences}{The order of integration for non-seasonal differencing. Often denoted "d" in pdq-notation.} 33 | 34 | \item{non_seasonal_ma}{The order of the non-seasonal moving average (MA) terms. Often denoted "q" in pdq-notation.} 35 | 36 | \item{seasonal_ar}{The order of the seasonal auto-regressive (SAR) terms. Often denoted "P" in PDQ-notation.} 37 | 38 | \item{seasonal_differences}{The order of integration for seasonal differencing. Often denoted "D" in PDQ-notation.} 39 | 40 | \item{seasonal_ma}{The order of the seasonal moving average (SMA) terms. Often denoted "Q" in PDQ-notation.} 41 | 42 | \item{cost}{A positive number for the cost of predicting a sample within 43 | or on the wrong side of the margin} 44 | 45 | \item{margin}{A positive number for the epsilon in the SVM insensitive 46 | loss function (regression only)} 47 | 48 | \item{laplace_sigma}{sigma parameter for laplacian} 49 | 50 | \item{sample_size}{number for the number (or proportion) of data that is exposed to the fitting routine.} 51 | } 52 | \description{ 53 | \code{arima_svm_laplace()} is a way to generate a \emph{specification} of a time series model 54 | that uses SVMs to improve modeling errors (residuals) on Exogenous Regressors. 55 | It works with both "automated" ARIMA (\code{auto.arima}) and standard ARIMA (\code{arima}). 56 | The main algorithms are: 57 | \itemize{ 58 | \item Auto ARIMA + SVM Errors (engine = \code{auto_arima_svm_laplace}, default) 59 | \item ARIMA + SVM Errors (engine = \code{arima_svm_laplace}) 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /man/cal_estimate_svm.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cal_estimate_svm.R 3 | \name{cal_estimate_svm} 4 | \alias{cal_estimate_svm} 5 | \alias{cal_estimate_svm.data.frame} 6 | \alias{cal_estimate_svm.tune_results} 7 | \alias{cal_estimate_svm.grouped_df} 8 | \title{Uses a support vector regression model to calibrate numeric predictions} 9 | \usage{ 10 | cal_estimate_svm( 11 | .data, 12 | truth = NULL, 13 | estimate = dplyr::matches("^.pred$"), 14 | smooth = TRUE, 15 | parameters = NULL, 16 | ..., 17 | .by = NULL 18 | ) 19 | 20 | \method{cal_estimate_svm}{data.frame}( 21 | .data, 22 | truth = NULL, 23 | estimate = dplyr::matches("^.pred$"), 24 | smooth = TRUE, 25 | parameters = NULL, 26 | ..., 27 | .by = NULL 28 | ) 29 | 30 | \method{cal_estimate_svm}{tune_results}( 31 | .data, 32 | truth = NULL, 33 | estimate = dplyr::matches("^.pred$"), 34 | smooth = TRUE, 35 | parameters = NULL, 36 | ... 37 | ) 38 | 39 | \method{cal_estimate_svm}{grouped_df}( 40 | .data, 41 | truth = NULL, 42 | estimate = NULL, 43 | smooth = TRUE, 44 | parameters = NULL, 45 | ... 46 | ) 47 | } 48 | \arguments{ 49 | \item{.data}{An ungrouped \code{data.frame} object, or \code{tune_results} object, 50 | that contains a prediction column.} 51 | 52 | \item{truth}{The column identifier for the observed outcome data (that is 53 | numeric). This should be an unquoted column name.} 54 | 55 | \item{estimate}{Column identifier for the predicted values} 56 | 57 | \item{smooth}{Applies to the svm models. It switches between a polydot\code{TRUE}, 58 | and vanilladot \code{FALSE}.} 59 | 60 | \item{parameters}{(Optional) An optional tibble of tuning parameter values 61 | that can be used to filter the predicted values before processing. Applies 62 | only to \code{tune_results} objects.} 63 | 64 | \item{...}{Additional arguments passed to the models or routines used to 65 | calculate the new predictions.} 66 | } 67 | \description{ 68 | Uses a support vector regression model to calibrate numeric predictions 69 | } 70 | \details{ 71 | This function uses existing modeling functions from other packages to create 72 | the calibration: 73 | \itemize{ 74 | \item \code{\link[kernlab:ksvm]{kernlab::ksvm()}} with a "vanilladot" is used when \code{smooth} is set to \code{FALSE} 75 | \item \code{\link[kernlab:ksvm]{kernlab::ksvm()}} with a "polydot" is used when \code{smooth} is set to \code{TRUE} 76 | } 77 | 78 | These methods estimate the relationship in the unmodified predicted values 79 | and then remove that trend when \code{\link[=cal_apply]{cal_apply()}} is invoked. 80 | } 81 | \seealso{ 82 | \url{https://www.tidymodels.org/learn/models/calibration/}, 83 | } 84 | -------------------------------------------------------------------------------- /R/bag_svm_rbf.R: -------------------------------------------------------------------------------- 1 | #' RUS Bagged SVM with Radial Basis Function Kernel 2 | #' @param mode classification 3 | #' @param engine ebmc's ub which uses e1701's svm 4 | #' @param num_learners how many weak learners should be ensembled via bagging 5 | #' @param imb_ratio major-minor class imbalance ratio 6 | #' @export 7 | 8 | bag_svm_rbf <- 9 | function(mode = "unknown", engine = "ebmc", 10 | num_learners = NULL, imb_ratio = NULL) { 11 | 12 | args <- list( 13 | num_learners = enquo(num_learners), 14 | imb_ratio = enquo(imb_ratio) 15 | ) 16 | 17 | parsnip::new_model_spec( 18 | "bag_svm_rbf", 19 | args = args, 20 | eng_args = NULL, 21 | mode = mode, 22 | user_specified_mode = !missing(mode), 23 | method = NULL, 24 | engine = engine, 25 | user_specified_engine = !missing(engine) 26 | ) 27 | } 28 | 29 | # ------------------------------------------------------------------------------ 30 | 31 | #' @method update bag_svm_rbf 32 | #' @rdname parsnip_update 33 | #' @export 34 | update.bag_svm_rbf <- 35 | function(object, 36 | parameters = NULL, 37 | num_learners = NULL, imb_ratio = NULL, 38 | fresh = FALSE, 39 | ...) { 40 | 41 | args <- list( 42 | num_learners = enquo(num_learners), 43 | imb_ratio = enquo(imb_ratio) 44 | ) 45 | 46 | parsnip::update_spec( 47 | object = object, 48 | parameters = parameters, 49 | args_enquo_list = args, 50 | fresh = fresh, 51 | cls = "bag_svm_rbf", 52 | ... 53 | ) 54 | } 55 | 56 | # ------------------------------------------------------------------------------ 57 | 58 | #' @export 59 | translate.bag_svm_rbf <- function(x, engine = x$engine, ...) { 60 | x <- parsnip::translate.default(x, engine = engine, ...) 61 | 62 | # slightly cleaner code using 63 | arg_vals <- x$method$fit$args 64 | arg_names <- names(arg_vals) 65 | 66 | # # add checks to error trap or change things for this method 67 | # if (x$engine == "kernlab") { 68 | # 69 | # # unless otherwise specified, classification models predict probabilities 70 | # if (x$mode == "classification" && !any(arg_names == "prob.model")) 71 | # arg_vals$prob.model <- TRUE 72 | # if (x$mode == "classification" && any(arg_names == "epsilon")) 73 | # arg_vals$epsilon <- NULL 74 | # 75 | # } 76 | 77 | x$method$fit$args <- arg_vals 78 | 79 | # worried about people using this to modify the specification 80 | x 81 | } 82 | 83 | # ------------------------------------------------------------------------------ 84 | 85 | #' @export 86 | check_args.bag_svm_rbf <- function(object, call = rlang::caller_env()) { 87 | invisible(object) 88 | } 89 | 90 | -------------------------------------------------------------------------------- /R/lssvm_laplace.R: -------------------------------------------------------------------------------- 1 | #' Laplacian Least Squares Support Vector Machine 2 | #' @description laplacian kernel 3 | #' @param mode classification 4 | #' @param engine kernlab lssvm 5 | #' @param laplace_sigma sigma parameter for laplacian 6 | #' @export 7 | 8 | lssvm_laplace <- 9 | function(mode = "unknown", engine = "kernlab",laplace_sigma = NULL) { 10 | 11 | args <- list( 12 | laplace_sigma = enquo(laplace_sigma) 13 | ) 14 | 15 | parsnip::new_model_spec( 16 | "lssvm_laplace", 17 | args = args, 18 | eng_args = NULL, 19 | mode = mode, 20 | user_specified_mode = !missing(mode), 21 | method = NULL, 22 | engine = engine, 23 | user_specified_engine = !missing(engine) 24 | ) 25 | } 26 | 27 | # ------------------------------------------------------------------------------ 28 | 29 | #' @method update lssvm_laplace 30 | #' @rdname parsnip_update 31 | #' @export 32 | update.lssvm_laplace <- 33 | function(object, 34 | parameters = NULL, laplace_sigma = NULL, 35 | fresh = FALSE, 36 | ...) { 37 | 38 | args <- list( 39 | laplace_sigma = enquo(laplace_sigma) 40 | ) 41 | 42 | parsnip::update_spec( 43 | object = object, 44 | parameters = parameters, 45 | args_enquo_list = args, 46 | fresh = fresh, 47 | cls = "lssvm_laplace", 48 | ... 49 | ) 50 | } 51 | 52 | # ------------------------------------------------------------------------------ 53 | 54 | #' @export 55 | translate.lssvm_laplace <- function(x, engine = x$engine, ...) { 56 | x <- parsnip::translate.default(x, engine = engine, ...) 57 | 58 | # slightly cleaner code using 59 | arg_vals <- x$method$fit$args 60 | arg_names <- names(arg_vals) 61 | 62 | # add checks to error trap or change things for this method 63 | if (x$engine == "kernlab") { 64 | 65 | # unless otherwise specified, classification models predict probabilities 66 | if (x$mode == "classification" && !any(arg_names == "prob.model")) 67 | arg_vals$prob.model <- TRUE 68 | if (x$mode == "classification" && any(arg_names == "epsilon")) 69 | arg_vals$epsilon <- NULL 70 | 71 | # convert sigma and scale to a `kpar` argument. 72 | if (any(arg_names == "sigma")) { 73 | kpar <- rlang::expr(list()) 74 | kpar$sigma <- arg_vals$sigma 75 | arg_vals$sigma <- NULL 76 | arg_vals$kpar <- kpar 77 | } 78 | 79 | } 80 | 81 | x$method$fit$args <- arg_vals 82 | 83 | # worried about people using this to modify the specification 84 | x 85 | } 86 | 87 | # ------------------------------------------------------------------------------ 88 | 89 | #' @export 90 | check_args.lssvm_laplace <- function(object, call = rlang::caller_env()) { 91 | invisible(object) 92 | } 93 | 94 | -------------------------------------------------------------------------------- /R/svm_cossim.R: -------------------------------------------------------------------------------- 1 | #' Cosine Similarity Support Vector Machine 2 | #' @description cossim kernel for support vector machines 3 | #' @param mode regression or classification 4 | #' @param engine kernlab ksvm 5 | #' @param cost A positive number for the cost of predicting a sample within 6 | #' or on the wrong side of the margin 7 | #' @param margin A positive number for the epsilon in the SVM insensitive 8 | #' loss function (regression only) 9 | #' @export 10 | 11 | svm_cossim <- 12 | function(mode = "unknown", engine = "kernlab", 13 | cost = NULL, margin = NULL) { 14 | 15 | args <- list( 16 | cost = enquo(cost), 17 | margin = enquo(margin) 18 | ) 19 | 20 | parsnip::new_model_spec( 21 | "svm_cossim", 22 | args = args, 23 | eng_args = NULL, 24 | mode = mode, 25 | user_specified_mode = !missing(mode), 26 | method = NULL, 27 | engine = engine, 28 | user_specified_engine = !missing(engine) 29 | ) 30 | } 31 | 32 | # ------------------------------------------------------------------------------ 33 | 34 | #' @method update svm_cossim 35 | #' @rdname parsnip_update 36 | #' @export 37 | update.svm_cossim <- 38 | function(object, 39 | parameters = NULL, 40 | cost = NULL, margin = NULL, 41 | fresh = FALSE, 42 | ...) { 43 | 44 | args <- list( 45 | cost = enquo(cost), 46 | margin = enquo(margin) 47 | ) 48 | 49 | parsnip::update_spec( 50 | object = object, 51 | parameters = parameters, 52 | args_enquo_list = args, 53 | fresh = fresh, 54 | cls = "svm_cossim", 55 | ... 56 | ) 57 | } 58 | 59 | # ------------------------------------------------------------------------------ 60 | 61 | #' @export 62 | translate.svm_cossim <- function(x, engine = x$engine, ...) { 63 | x <- parsnip::translate.default(x, engine = engine, ...) 64 | 65 | # slightly cleaner code using 66 | arg_vals <- x$method$fit$args 67 | arg_names <- names(arg_vals) 68 | 69 | # add checks to error trap or change things for this method 70 | if (x$engine == "kernlab") { 71 | 72 | # unless otherwise specified, classification models predict probabilities 73 | if (x$mode == "classification" && !any(arg_names == "prob.model")) 74 | arg_vals$prob.model <- TRUE 75 | if (x$mode == "classification" && any(arg_names == "epsilon")) 76 | arg_vals$epsilon <- NULL 77 | 78 | } 79 | 80 | x$method$fit$args <- arg_vals 81 | 82 | # worried about people using this to modify the specification 83 | x 84 | } 85 | 86 | # ------------------------------------------------------------------------------ 87 | 88 | #' @export 89 | check_args.svm_cossim <- function(object, call = rlang::caller_env()) { 90 | invisible(object) 91 | } 92 | -------------------------------------------------------------------------------- /R/ada_boost_svm_rbf.R: -------------------------------------------------------------------------------- 1 | #' AdaBoost SVM with Radial Basis Function Kernel 2 | #' @param mode classification 3 | #' @param engine ebmc's adam2 which uses e1701's svm 4 | #' @param num_learners how many weak learners should be ensembled via boosting 5 | #' @param imb_ratio major-minor class imbalance ratio 6 | #' @export 7 | 8 | ada_boost_svm_rbf <- 9 | function(mode = "unknown", engine = "ebmc", 10 | num_learners = NULL, imb_ratio = NULL) { 11 | 12 | args <- list( 13 | num_learners = enquo(num_learners), 14 | imb_ratio = enquo(imb_ratio) 15 | ) 16 | 17 | parsnip::new_model_spec( 18 | "ada_boost_svm_rbf", 19 | args = args, 20 | eng_args = NULL, 21 | mode = mode, 22 | user_specified_mode = !missing(mode), 23 | method = NULL, 24 | engine = engine, 25 | user_specified_engine = !missing(engine) 26 | ) 27 | } 28 | 29 | # ------------------------------------------------------------------------------ 30 | 31 | #' @method update ada_boost_svm_rbf 32 | #' @rdname parsnip_update 33 | #' @export 34 | update.ada_boost_svm_rbf <- 35 | function(object, 36 | parameters = NULL, 37 | num_learners = NULL, imb_ratio = NULL, 38 | fresh = FALSE, 39 | ...) { 40 | 41 | args <- list( 42 | num_learners = enquo(num_learners), 43 | imb_ratio = enquo(imb_ratio) 44 | ) 45 | 46 | parsnip::update_spec( 47 | object = object, 48 | parameters = parameters, 49 | args_enquo_list = args, 50 | fresh = fresh, 51 | cls = "ada_boost_svm_rbf", 52 | ... 53 | ) 54 | } 55 | 56 | # ------------------------------------------------------------------------------ 57 | 58 | #' @export 59 | translate.ada_boost_svm_rbf <- function(x, engine = x$engine, ...) { 60 | x <- parsnip::translate.default(x, engine = engine, ...) 61 | 62 | # slightly cleaner code using 63 | arg_vals <- x$method$fit$args 64 | arg_names <- names(arg_vals) 65 | 66 | # # add checks to error trap or change things for this method 67 | # if (x$engine == "kernlab") { 68 | # 69 | # # unless otherwise specified, classification models predict probabilities 70 | # if (x$mode == "classification" && !any(arg_names == "prob.model")) 71 | # arg_vals$prob.model <- TRUE 72 | # if (x$mode == "classification" && any(arg_names == "epsilon")) 73 | # arg_vals$epsilon <- NULL 74 | # 75 | # } 76 | 77 | x$method$fit$args <- arg_vals 78 | 79 | # worried about people using this to modify the specification 80 | x 81 | } 82 | 83 | # ------------------------------------------------------------------------------ 84 | 85 | #' @export 86 | check_args.ada_boost_svm_rbf <- function(object, call = rlang::caller_env()) { 87 | invisible(object) 88 | } 89 | 90 | -------------------------------------------------------------------------------- /R/rus_boost_svm_rbf.R: -------------------------------------------------------------------------------- 1 | #' RUSBoost SVM with Radial Basis Function Kernel 2 | #' @param mode classification 3 | #' @param engine ebmc's rus which uses e1701's svm 4 | #' @param num_learners how many weak learners should be ensembled via boosting 5 | #' @param imb_ratio major-minor class imbalance ratio 6 | #' @export 7 | 8 | rus_boost_svm_rbf <- 9 | function(mode = "unknown", engine = "ebmc", 10 | num_learners = NULL, imb_ratio = NULL) { 11 | 12 | args <- list( 13 | num_learners = enquo(num_learners), 14 | imb_ratio = enquo(imb_ratio) 15 | ) 16 | 17 | parsnip::new_model_spec( 18 | "rus_boost_svm_rbf", 19 | args = args, 20 | eng_args = NULL, 21 | mode = mode, 22 | user_specified_mode = !missing(mode), 23 | method = NULL, 24 | engine = engine, 25 | user_specified_engine = !missing(engine) 26 | ) 27 | } 28 | 29 | # ------------------------------------------------------------------------------ 30 | 31 | #' @method update rus_boost_svm_rbf 32 | #' @rdname parsnip_update 33 | #' @export 34 | update.rus_boost_svm_rbf <- 35 | function(object, 36 | parameters = NULL, 37 | num_learners = NULL, imb_ratio = NULL, 38 | fresh = FALSE, 39 | ...) { 40 | 41 | args <- list( 42 | num_learners = enquo(num_learners), 43 | imb_ratio = enquo(imb_ratio) 44 | ) 45 | 46 | parsnip::update_spec( 47 | object = object, 48 | parameters = parameters, 49 | args_enquo_list = args, 50 | fresh = fresh, 51 | cls = "rus_boost_svm_rbf", 52 | ... 53 | ) 54 | } 55 | 56 | # ------------------------------------------------------------------------------ 57 | 58 | #' @export 59 | translate.rus_boost_svm_rbf <- function(x, engine = x$engine, ...) { 60 | x <- parsnip::translate.default(x, engine = engine, ...) 61 | 62 | # slightly cleaner code using 63 | arg_vals <- x$method$fit$args 64 | arg_names <- names(arg_vals) 65 | 66 | # # add checks to error trap or change things for this method 67 | # if (x$engine == "kernlab") { 68 | # 69 | # # unless otherwise specified, classification models predict probabilities 70 | # if (x$mode == "classification" && !any(arg_names == "prob.model")) 71 | # arg_vals$prob.model <- TRUE 72 | # if (x$mode == "classification" && any(arg_names == "epsilon")) 73 | # arg_vals$epsilon <- NULL 74 | # 75 | # } 76 | 77 | x$method$fit$args <- arg_vals 78 | 79 | # worried about people using this to modify the specification 80 | x 81 | } 82 | 83 | # ------------------------------------------------------------------------------ 84 | 85 | #' @export 86 | check_args.rus_boost_svm_rbf <- function(object, call = rlang::caller_env()) { 87 | invisible(object) 88 | } 89 | 90 | -------------------------------------------------------------------------------- /R/svm_sorensen.R: -------------------------------------------------------------------------------- 1 | #' Sorensen Support Vector Machine 2 | #' @description sorensen kernel for support vector machines which is used as a graph kernel for chemical informatics 3 | #' @param mode regression or classification 4 | #' @param engine kernlab ksvm 5 | #' @param cost A positive number for the cost of predicting a sample within 6 | #' or on the wrong side of the margin 7 | #' @param margin A positive number for the epsilon in the SVM insensitive 8 | #' loss function (regression only) 9 | #' @export 10 | 11 | svm_sorensen <- 12 | function(mode = "unknown", engine = "kernlab", 13 | cost = NULL, margin = NULL) { 14 | 15 | args <- list( 16 | cost = enquo(cost), 17 | margin = enquo(margin) 18 | ) 19 | 20 | parsnip::new_model_spec( 21 | "svm_sorensen", 22 | args = args, 23 | eng_args = NULL, 24 | mode = mode, 25 | user_specified_mode = !missing(mode), 26 | method = NULL, 27 | engine = engine, 28 | user_specified_engine = !missing(engine) 29 | ) 30 | } 31 | 32 | # ------------------------------------------------------------------------------ 33 | 34 | #' @method update svm_sorensen 35 | #' @rdname parsnip_update 36 | #' @export 37 | update.svm_sorensen <- 38 | function(object, 39 | parameters = NULL, 40 | cost = NULL, margin = NULL, 41 | fresh = FALSE, 42 | ...) { 43 | 44 | args <- list( 45 | cost = enquo(cost), 46 | margin = enquo(margin) 47 | ) 48 | 49 | parsnip::update_spec( 50 | object = object, 51 | parameters = parameters, 52 | args_enquo_list = args, 53 | fresh = fresh, 54 | cls = "svm_sorensen", 55 | ... 56 | ) 57 | } 58 | 59 | # ------------------------------------------------------------------------------ 60 | 61 | #' @export 62 | translate.svm_sorensen <- function(x, engine = x$engine, ...) { 63 | x <- parsnip::translate.default(x, engine = engine, ...) 64 | 65 | # slightly cleaner code using 66 | arg_vals <- x$method$fit$args 67 | arg_names <- names(arg_vals) 68 | 69 | # add checks to error trap or change things for this method 70 | if (x$engine == "kernlab") { 71 | 72 | # unless otherwise specified, classification models predict probabilities 73 | if (x$mode == "classification" && !any(arg_names == "prob.model")) 74 | arg_vals$prob.model <- TRUE 75 | if (x$mode == "classification" && any(arg_names == "epsilon")) 76 | arg_vals$epsilon <- NULL 77 | 78 | } 79 | 80 | x$method$fit$args <- arg_vals 81 | 82 | # worried about people using this to modify the specification 83 | x 84 | } 85 | 86 | # ------------------------------------------------------------------------------ 87 | 88 | #' @export 89 | check_args.svm_sorensen <- function(object, call = rlang::caller_env()) { 90 | invisible(object) 91 | } 92 | -------------------------------------------------------------------------------- /R/svm_tanimoto.R: -------------------------------------------------------------------------------- 1 | #' Tanimoto Support Vector Machine 2 | #' @description tanimoto kernel for support vector machines which is used as a graph kernel for chemical informatics 3 | #' @param mode regression or classification 4 | #' @param engine kernlab ksvm 5 | #' @param cost A positive number for the cost of predicting a sample within 6 | #' or on the wrong side of the margin 7 | #' @param margin A positive number for the epsilon in the SVM insensitive 8 | #' loss function (regression only) 9 | #' @export 10 | 11 | svm_tanimoto <- 12 | function(mode = "unknown", engine = "kernlab", 13 | cost = NULL, margin = NULL) { 14 | 15 | args <- list( 16 | cost = enquo(cost), 17 | margin = enquo(margin) 18 | ) 19 | 20 | parsnip::new_model_spec( 21 | "svm_tanimoto", 22 | args = args, 23 | eng_args = NULL, 24 | mode = mode, 25 | user_specified_mode = !missing(mode), 26 | method = NULL, 27 | engine = engine, 28 | user_specified_engine = !missing(engine) 29 | ) 30 | } 31 | 32 | # ------------------------------------------------------------------------------ 33 | 34 | #' @method update svm_tanimoto 35 | #' @rdname parsnip_update 36 | #' @export 37 | update.svm_tanimoto <- 38 | function(object, 39 | parameters = NULL, 40 | cost = NULL, margin = NULL, 41 | fresh = FALSE, 42 | ...) { 43 | 44 | args <- list( 45 | cost = enquo(cost), 46 | margin = enquo(margin) 47 | ) 48 | 49 | parsnip::update_spec( 50 | object = object, 51 | parameters = parameters, 52 | args_enquo_list = args, 53 | fresh = fresh, 54 | cls = "svm_tanimoto", 55 | ... 56 | ) 57 | } 58 | 59 | # ------------------------------------------------------------------------------ 60 | 61 | #' @export 62 | translate.svm_tanimoto <- function(x, engine = x$engine, ...) { 63 | x <- parsnip::translate.default(x, engine = engine, ...) 64 | 65 | # slightly cleaner code using 66 | arg_vals <- x$method$fit$args 67 | arg_names <- names(arg_vals) 68 | 69 | # add checks to error trap or change things for this method 70 | if (x$engine == "kernlab") { 71 | 72 | # unless otherwise specified, classification models predict probabilities 73 | if (x$mode == "classification" && !any(arg_names == "prob.model")) 74 | arg_vals$prob.model <- TRUE 75 | if (x$mode == "classification" && any(arg_names == "epsilon")) 76 | arg_vals$epsilon <- NULL 77 | 78 | } 79 | 80 | x$method$fit$args <- arg_vals 81 | 82 | # worried about people using this to modify the specification 83 | x 84 | } 85 | 86 | # ------------------------------------------------------------------------------ 87 | 88 | #' @export 89 | check_args.svm_tanimoto <- function(object, call = rlang::caller_env()) { 90 | invisible(object) 91 | } 92 | 93 | -------------------------------------------------------------------------------- /R/svm_spline.R: -------------------------------------------------------------------------------- 1 | #' Spline Support Vector Machine 2 | #' @description spline kernel for support vector machines 3 | #' @param mode regression or classification 4 | #' @param engine kernlab ksvm 5 | #' @param cost A positive number for the cost of predicting a sample within 6 | #' or on the wrong side of the margin 7 | #' @param margin A positive number for the epsilon in the SVM insensitive 8 | #' loss function (regression only) 9 | #' @export 10 | 11 | svm_spline <- 12 | function(mode = "unknown", engine = "kernlab", 13 | cost = NULL, margin = NULL) { 14 | 15 | args <- list( 16 | cost = enquo(cost), 17 | margin = enquo(margin) 18 | ) 19 | 20 | parsnip::new_model_spec( 21 | "svm_spline", 22 | args = args, 23 | eng_args = NULL, 24 | mode = mode, 25 | user_specified_mode = !missing(mode), 26 | method = NULL, 27 | engine = engine, 28 | user_specified_engine = !missing(engine) 29 | ) 30 | } 31 | 32 | # ------------------------------------------------------------------------------ 33 | 34 | #' @method update svm_spline 35 | #' @rdname parsnip_update 36 | #' @export 37 | update.svm_spline <- 38 | function(object, 39 | parameters = NULL, 40 | cost = NULL, margin = NULL, 41 | fresh = FALSE, 42 | ...) { 43 | 44 | args <- list( 45 | cost = enquo(cost), 46 | margin = enquo(margin) 47 | ) 48 | 49 | parsnip::update_spec( 50 | object = object, 51 | parameters = parameters, 52 | args_enquo_list = args, 53 | fresh = fresh, 54 | cls = "svm_spline", 55 | ... 56 | ) 57 | } 58 | 59 | # ------------------------------------------------------------------------------ 60 | 61 | #' @export 62 | translate.svm_spline <- function(x, engine = x$engine, ...) { 63 | x <- parsnip::translate.default(x, engine = engine, ...) 64 | 65 | # slightly cleaner code using 66 | arg_vals <- x$method$fit$args 67 | arg_names <- names(arg_vals) 68 | 69 | # add checks to error trap or change things for this method 70 | if (x$engine == "kernlab") { 71 | 72 | # unless otherwise specified, classification models predict probabilities 73 | if (x$mode == "classification" && !any(arg_names == "prob.model")) 74 | arg_vals$prob.model <- TRUE 75 | if (x$mode == "classification" && any(arg_names == "epsilon")) 76 | arg_vals$epsilon <- NULL 77 | 78 | } 79 | 80 | x$method$fit$args <- arg_vals 81 | 82 | # worried about people using this to modify the specification 83 | x 84 | } 85 | 86 | # ------------------------------------------------------------------------------ 87 | 88 | #' @export 89 | check_args.svm_spline <- function(object, call = rlang::caller_env()) { 90 | invisible(object) 91 | } 92 | -------------------------------------------------------------------------------- /R/lssvm_laplace_data.R: -------------------------------------------------------------------------------- 1 | make_lssvm_laplace <- function() { 2 | 3 | parsnip::set_new_model("lssvm_laplace") 4 | 5 | parsnip::set_model_mode("lssvm_laplace", "classification") 6 | 7 | # ------------------------------------------------------------------------------ 8 | 9 | parsnip::set_model_engine("lssvm_laplace", "classification", "kernlab") 10 | parsnip::set_dependency("lssvm_laplace", "kernlab", "kernlab") 11 | parsnip::set_dependency("lssvm_laplace", "kernlab", "maize") 12 | 13 | 14 | 15 | parsnip::set_model_arg( 16 | model = "lssvm_laplace", 17 | eng = "kernlab", 18 | parsnip = "laplace_sigma", 19 | original = "sigma", 20 | func = list(pkg = "dials", fun = "svm_margin"), 21 | has_submodel = FALSE 22 | ) 23 | 24 | 25 | parsnip::set_fit( 26 | model = "lssvm_laplace", 27 | eng = "kernlab", 28 | mode = "classification", 29 | value = list( 30 | interface = "formula", 31 | data = c(formula = "x", data = "data"), 32 | protect = c("x", "data"), 33 | func = c(pkg = "kernlab", fun = "lssvm"), 34 | defaults = list(kernel = "laplacedot") 35 | ) 36 | ) 37 | 38 | parsnip::set_encoding( 39 | model = "lssvm_laplace", 40 | eng = "kernlab", 41 | mode = "classification", 42 | options = list( 43 | predictor_indicators = "none", 44 | compute_intercept = FALSE, 45 | remove_intercept = FALSE, 46 | allow_sparse_x = FALSE 47 | ) 48 | ) 49 | 50 | 51 | 52 | parsnip::set_pred( 53 | model = "lssvm_laplace", 54 | eng = "kernlab", 55 | mode = "classification", 56 | type = "class", 57 | value = list( 58 | pre = NULL, 59 | post = NULL, 60 | func = c(pkg = "kernlab", fun = "predict"), 61 | args = 62 | list( 63 | object = quote(object$fit), 64 | newdata = quote(new_data), 65 | type = "response" 66 | ) 67 | ) 68 | ) 69 | 70 | parsnip::set_pred( 71 | model = "lssvm_laplace", 72 | eng = "kernlab", 73 | mode = "classification", 74 | type = "prob", 75 | value = list( 76 | pre = NULL, 77 | post = function(result, object) as_tibble(result), 78 | func = c(pkg = "kernlab", fun = "predict"), 79 | args = 80 | list( 81 | object = quote(object$fit), 82 | newdata = quote(new_data), 83 | type = "probabilities" 84 | ) 85 | ) 86 | ) 87 | 88 | parsnip::set_pred( 89 | model = "lssvm_laplace", 90 | eng = "kernlab", 91 | mode = "classification", 92 | type = "raw", 93 | value = list( 94 | pre = NULL, 95 | post = NULL, 96 | func = c(pkg = "kernlab", fun = "predict"), 97 | args = list(object = quote(object$fit), newdata = quote(new_data)) 98 | ) 99 | ) 100 | } 101 | -------------------------------------------------------------------------------- /R/rvm_laplace.R: -------------------------------------------------------------------------------- 1 | #' Laplacian Relevance Vector Machine (Experimental RVM) 2 | #' @description laplacian kernel 3 | #' @param mode regression only for RVM 4 | #' @param engine kernlab rvm 5 | #' @param alpha (alpha) The initial alpha value or vector. 6 | #' Can be either a vector of length equal to the number of data points or a single number. 7 | #' @param var (var) the initial noise variance 8 | #' @param laplace_sigma sigma parameter for laplacian 9 | #' @export 10 | 11 | rvm_laplace <- 12 | function(mode = "unknown", engine = "kernlab", 13 | alpha = NULL, var = NULL, laplace_sigma = NULL) { 14 | 15 | args <- list( 16 | alpha = enquo(alpha), 17 | var = enquo(var), 18 | laplace_sigma = rlang::enquo(laplace_sigma) 19 | ) 20 | 21 | parsnip::new_model_spec( 22 | "rvm_laplace", 23 | args = args, 24 | eng_args = NULL, 25 | mode = mode, 26 | user_specified_mode = !missing(mode), 27 | method = NULL, 28 | engine = engine, 29 | user_specified_engine = !missing(engine) 30 | ) 31 | } 32 | 33 | # ------------------------------------------------------------------------------ 34 | 35 | #' @method update rvm_laplace 36 | #' @rdname parsnip_update 37 | #' @export 38 | update.rvm_laplace <- 39 | function(object, 40 | parameters = NULL, 41 | alpha = NULL, var = NULL, laplace_sigma = NULL, 42 | fresh = FALSE, 43 | ...) { 44 | 45 | args <- list( 46 | alpha = enquo(alpha), 47 | var = enquo(var), 48 | laplace_sigma = enquo(laplace_sigma) 49 | ) 50 | 51 | parsnip::update_spec( 52 | object = object, 53 | parameters = parameters, 54 | args_enquo_list = args, 55 | fresh = fresh, 56 | cls = "rvm_laplace", 57 | ... 58 | ) 59 | } 60 | 61 | # ------------------------------------------------------------------------------ 62 | 63 | #' @export 64 | translate.rvm_laplace <- function(x, engine = x$engine, ...) { 65 | x <- parsnip::translate.default(x, engine = engine, ...) 66 | 67 | # slightly cleaner code using 68 | arg_vals <- x$method$fit$args 69 | arg_names <- names(arg_vals) 70 | 71 | # add checks to error trap or change things for this method 72 | if (x$engine == "kernlab") { 73 | 74 | # convert sigma and scale to a `kpar` argument. 75 | if (any(arg_names == "sigma")) { 76 | kpar <- rlang::expr(list()) 77 | kpar$sigma <- arg_vals$sigma 78 | arg_vals$sigma <- NULL 79 | arg_vals$kpar <- kpar 80 | } 81 | 82 | } 83 | 84 | x$method$fit$args <- arg_vals 85 | 86 | # worried about people using this to modify the specification 87 | x 88 | } 89 | 90 | # ------------------------------------------------------------------------------ 91 | 92 | #' @export 93 | check_args.rvm_laplace <- function(object, call = rlang::caller_env()) { 94 | invisible(object) 95 | } 96 | 97 | -------------------------------------------------------------------------------- /R/kqr_laplace_data.R: -------------------------------------------------------------------------------- 1 | make_kqr_laplace <- function() { 2 | 3 | parsnip::set_new_model("kqr_laplace") 4 | 5 | parsnip::set_model_mode("kqr_laplace", "regression") 6 | 7 | # ------------------------------------------------------------------------------ 8 | 9 | parsnip::set_model_engine("kqr_laplace", "regression", "kernlab") 10 | parsnip::set_dependency("kqr_laplace", "kernlab", "kernlab") 11 | parsnip::set_dependency("kqr_laplace", "kernlab", "maize") 12 | 13 | parsnip::set_model_arg( 14 | model = "kqr_laplace", 15 | eng = "kernlab", 16 | parsnip = "cost", 17 | original = "C", 18 | func = list(pkg = "dials", fun = "cost", range = c(-10, 5)), 19 | has_submodel = FALSE 20 | ) 21 | 22 | parsnip::set_model_arg( 23 | model = "kqr_laplace", 24 | eng = "kernlab", 25 | parsnip = "tau", 26 | original = "tau", 27 | func = list(pkg = "maize", fun = "tau", range = c(.01, .99)), 28 | has_submodel = FALSE 29 | ) 30 | 31 | parsnip::set_model_arg( 32 | model = "kqr_laplace", 33 | eng = "kernlab", 34 | parsnip = "laplace_sigma", 35 | original = "sigma", 36 | func = list(pkg = "dials", fun = "svm_margin"), 37 | has_submodel = FALSE 38 | ) 39 | 40 | 41 | parsnip::set_fit( 42 | model = "kqr_laplace", 43 | eng = "kernlab", 44 | mode = "regression", 45 | value = list( 46 | interface = "formula", 47 | data = c(formula = "x", data = "data"), 48 | protect = c("x", "data"), 49 | func = c(pkg = "kernlab", fun = "kqr"), 50 | defaults = list(kernel = "laplacedot") 51 | ) 52 | ) 53 | 54 | parsnip::set_encoding( 55 | model = "kqr_laplace", 56 | eng = "kernlab", 57 | mode = "regression", 58 | options = list( 59 | predictor_indicators = "none", 60 | compute_intercept = FALSE, 61 | remove_intercept = FALSE, 62 | allow_sparse_x = FALSE 63 | ) 64 | ) 65 | 66 | kqr_reg_post <- function(results, object) { 67 | results[,1] 68 | } 69 | parsnip::set_pred( 70 | model = "kqr_laplace", 71 | eng = "kernlab", 72 | mode = "regression", 73 | type = "numeric", 74 | value = list( 75 | pre = NULL, 76 | post = kqr_reg_post, 77 | func = c(pkg = "kernlab", fun = "predict"), 78 | args = 79 | list( 80 | object = quote(object$fit), 81 | newdata = quote(new_data)#, 82 | #type = "response" 83 | ) 84 | ) 85 | ) 86 | 87 | parsnip::set_pred( 88 | model = "kqr_laplace", 89 | eng = "kernlab", 90 | mode = "regression", 91 | type = "raw", 92 | value = list( 93 | pre = NULL, 94 | post = NULL, 95 | func = c(pkg = "kernlab", fun = "predict"), 96 | args = list(object = quote(object$fit), newdata = quote(new_data)) 97 | ) 98 | ) 99 | 100 | } 101 | -------------------------------------------------------------------------------- /R/svm_cauchy.R: -------------------------------------------------------------------------------- 1 | #' Cauchy Support Vector Machine 2 | #' @description cauchy kernel for support vector machines 3 | #' @param mode regression or classification 4 | #' @param engine kernlab ksvm 5 | #' @param cost A positive number for the cost of predicting a sample within 6 | #' or on the wrong side of the margin 7 | #' @param margin A positive number for the epsilon in the SVM insensitive 8 | #' loss function (regression only) 9 | #' @param sigma a sigma parameter for cauchy kernels 10 | #' @export 11 | 12 | svm_cauchy <- 13 | function(mode = "unknown", engine = "kernlab", 14 | cost = NULL, margin = NULL, sigma = NULL) { 15 | 16 | args <- list( 17 | cost = enquo(cost), 18 | margin = enquo(margin), 19 | sigma = enquo(sigma) 20 | ) 21 | 22 | parsnip::new_model_spec( 23 | "svm_cauchy", 24 | args = args, 25 | eng_args = NULL, 26 | mode = mode, 27 | user_specified_mode = !missing(mode), 28 | method = NULL, 29 | engine = engine, 30 | user_specified_engine = !missing(engine) 31 | ) 32 | } 33 | 34 | # ------------------------------------------------------------------------------ 35 | 36 | #' @method update svm_cauchy 37 | #' @rdname parsnip_update 38 | #' @export 39 | update.svm_cauchy <- 40 | function(object, 41 | parameters = NULL, 42 | cost = NULL, margin = NULL, sigma = NULL, 43 | fresh = FALSE, 44 | ...) { 45 | 46 | args <- list( 47 | cost = enquo(cost), 48 | margin = enquo(margin), 49 | sigma = enquo(sigma) 50 | ) 51 | 52 | parsnip::update_spec( 53 | object = object, 54 | parameters = parameters, 55 | args_enquo_list = args, 56 | fresh = fresh, 57 | cls = "svm_cauchy", 58 | ... 59 | ) 60 | } 61 | 62 | # ------------------------------------------------------------------------------ 63 | 64 | #' @export 65 | translate.svm_cauchy <- function(x, engine = x$engine, ...) { 66 | x <- parsnip::translate.default(x, engine = engine, ...) 67 | 68 | # slightly cleaner code using 69 | arg_vals <- x$method$fit$args 70 | arg_names <- names(arg_vals) 71 | 72 | # add checks to error trap or change things for this method 73 | if (x$engine == "kernlab") { 74 | 75 | # unless otherwise specified, classification models predict probabilities 76 | if (x$mode == "classification" && !any(arg_names == "prob.model")) 77 | arg_vals$prob.model <- TRUE 78 | if (x$mode == "classification" && any(arg_names == "epsilon")) 79 | arg_vals$epsilon <- NULL 80 | 81 | } 82 | 83 | x$method$fit$args <- arg_vals 84 | 85 | # worried about people using this to modify the specification 86 | x 87 | } 88 | 89 | # ------------------------------------------------------------------------------ 90 | 91 | #' @export 92 | check_args.svm_cauchy <- function(object, call = rlang::caller_env()) { 93 | invisible(object) 94 | } 95 | -------------------------------------------------------------------------------- /R/svm_fourier.R: -------------------------------------------------------------------------------- 1 | #' Fourier Support Vector Machine 2 | #' @description fourier kernel for support vector machines 3 | #' @param mode regression or classification 4 | #' @param engine kernlab ksvm 5 | #' @param cost A positive number for the cost of predicting a sample within 6 | #' or on the wrong side of the margin 7 | #' @param margin A positive number for the epsilon in the SVM insensitive 8 | #' loss function (regression only) 9 | #' @param sigma a sigma parameter for fourier kernels 10 | #' @export 11 | 12 | svm_fourier <- 13 | function(mode = "unknown", engine = "kernlab", 14 | cost = NULL, margin = NULL, sigma = NULL) { 15 | 16 | args <- list( 17 | cost = enquo(cost), 18 | margin = enquo(margin), 19 | sigma = enquo(sigma) 20 | ) 21 | 22 | parsnip::new_model_spec( 23 | "svm_fourier", 24 | args = args, 25 | eng_args = NULL, 26 | mode = mode, 27 | user_specified_mode = !missing(mode), 28 | method = NULL, 29 | engine = engine, 30 | user_specified_engine = !missing(engine) 31 | ) 32 | } 33 | 34 | # ------------------------------------------------------------------------------ 35 | 36 | #' @method update svm_fourier 37 | #' @rdname parsnip_update 38 | #' @export 39 | update.svm_fourier <- 40 | function(object, 41 | parameters = NULL, 42 | cost = NULL, margin = NULL, sigma = NULL, 43 | fresh = FALSE, 44 | ...) { 45 | 46 | args <- list( 47 | cost = enquo(cost), 48 | margin = enquo(margin), 49 | sigma = enquo(sigma) 50 | ) 51 | 52 | parsnip::update_spec( 53 | object = object, 54 | parameters = parameters, 55 | args_enquo_list = args, 56 | fresh = fresh, 57 | cls = "svm_fourier", 58 | ... 59 | ) 60 | } 61 | 62 | # ------------------------------------------------------------------------------ 63 | 64 | #' @export 65 | translate.svm_fourier <- function(x, engine = x$engine, ...) { 66 | x <- parsnip::translate.default(x, engine = engine, ...) 67 | 68 | # slightly cleaner code using 69 | arg_vals <- x$method$fit$args 70 | arg_names <- names(arg_vals) 71 | 72 | # add checks to error trap or change things for this method 73 | if (x$engine == "kernlab") { 74 | 75 | # unless otherwise specified, classification models predict probabilities 76 | if (x$mode == "classification" && !any(arg_names == "prob.model")) 77 | arg_vals$prob.model <- TRUE 78 | if (x$mode == "classification" && any(arg_names == "epsilon")) 79 | arg_vals$epsilon <- NULL 80 | 81 | } 82 | 83 | x$method$fit$args <- arg_vals 84 | 85 | # worried about people using this to modify the specification 86 | x 87 | } 88 | 89 | # ------------------------------------------------------------------------------ 90 | 91 | #' @export 92 | check_args.svm_fourier <- function(object, call = rlang::caller_env()) { 93 | invisible(object) 94 | } 95 | -------------------------------------------------------------------------------- /R/svm_tstudent.R: -------------------------------------------------------------------------------- 1 | #' T-Student Support Vector Machine 2 | #' @description t-student kernel for support vector machines 3 | #' @param mode regression or classification 4 | #' @param engine kernlab ksvm 5 | #' @param cost A positive number for the cost of predicting a sample within 6 | #' or on the wrong side of the margin 7 | #' @param margin A positive number for the epsilon in the SVM insensitive 8 | #' loss function (regression only) 9 | #' @param degree a degree parameter for tstudent kernels 10 | #' @export 11 | 12 | svm_tstudent <- 13 | function(mode = "unknown", engine = "kernlab", 14 | cost = NULL, margin = NULL, degree = NULL) { 15 | 16 | args <- list( 17 | cost = enquo(cost), 18 | margin = enquo(margin), 19 | degree = enquo(degree) 20 | ) 21 | 22 | parsnip::new_model_spec( 23 | "svm_tstudent", 24 | args = args, 25 | eng_args = NULL, 26 | mode = mode, 27 | user_specified_mode = !missing(mode), 28 | method = NULL, 29 | engine = engine, 30 | user_specified_engine = !missing(engine) 31 | ) 32 | } 33 | 34 | # ------------------------------------------------------------------------------ 35 | 36 | #' @method update svm_tstudent 37 | #' @rdname parsnip_update 38 | #' @export 39 | update.svm_tstudent <- 40 | function(object, 41 | parameters = NULL, 42 | cost = NULL, margin = NULL, degree = NULL, 43 | fresh = FALSE, 44 | ...) { 45 | 46 | args <- list( 47 | cost = enquo(cost), 48 | margin = enquo(margin), 49 | degree = enquo(degree) 50 | ) 51 | 52 | parsnip::update_spec( 53 | object = object, 54 | parameters = parameters, 55 | args_enquo_list = args, 56 | fresh = fresh, 57 | cls = "svm_tstudent", 58 | ... 59 | ) 60 | } 61 | 62 | # ------------------------------------------------------------------------------ 63 | 64 | #' @export 65 | translate.svm_tstudent <- function(x, engine = x$engine, ...) { 66 | x <- parsnip::translate.default(x, engine = engine, ...) 67 | 68 | # slightly cleaner code using 69 | arg_vals <- x$method$fit$args 70 | arg_names <- names(arg_vals) 71 | 72 | # add checks to error trap or change things for this method 73 | if (x$engine == "kernlab") { 74 | 75 | # unless otherwise specified, classification models predict probabilities 76 | if (x$mode == "classification" && !any(arg_names == "prob.model")) 77 | arg_vals$prob.model <- TRUE 78 | if (x$mode == "classification" && any(arg_names == "epsilon")) 79 | arg_vals$epsilon <- NULL 80 | 81 | } 82 | 83 | x$method$fit$args <- arg_vals 84 | 85 | # worried about people using this to modify the specification 86 | x 87 | } 88 | 89 | # ------------------------------------------------------------------------------ 90 | 91 | #' @export 92 | check_args.svm_tstudent <- function(object, call = rlang::caller_env()) { 93 | invisible(object) 94 | } 95 | -------------------------------------------------------------------------------- /R/rvm_laplace_data.R: -------------------------------------------------------------------------------- 1 | make_rvm_laplace <- function() { 2 | 3 | parsnip::set_new_model("rvm_laplace") 4 | 5 | parsnip::set_model_mode("rvm_laplace", "regression") 6 | 7 | # ------------------------------------------------------------------------------ 8 | 9 | parsnip::set_model_engine("rvm_laplace", "regression", "kernlab") 10 | parsnip::set_dependency("rvm_laplace", "kernlab", "kernlab") 11 | parsnip::set_dependency("rvm_laplace", "kernlab", "maize") 12 | 13 | parsnip::set_model_arg( 14 | model = "rvm_laplace", 15 | eng = "kernlab", 16 | parsnip = "alpha", 17 | original = "alpha", 18 | func = list(pkg = "maize", fun = "alpha", range = c(1, 5)), 19 | has_submodel = FALSE 20 | ) 21 | 22 | parsnip::set_model_arg( 23 | model = "rvm_laplace", 24 | eng = "kernlab", 25 | parsnip = "var", 26 | original = "var", 27 | func = list(pkg = "maize", fun = "var", range = c(.01, 100)), 28 | has_submodel = FALSE 29 | ) 30 | 31 | parsnip::set_model_arg( 32 | model = "rvm_laplace", 33 | eng = "kernlab", 34 | parsnip = "laplace_sigma", 35 | original = "sigma", 36 | func = list(pkg = "dials", fun = "svm_margin"), 37 | has_submodel = FALSE 38 | ) 39 | 40 | parsnip::set_fit( 41 | model = "rvm_laplace", 42 | eng = "kernlab", 43 | mode = "regression", 44 | value = list( 45 | interface = "formula", 46 | data = c(formula = "x", data = "data"), 47 | protect = c("x", "data"), 48 | func = c(pkg = "kernlab", fun = "rvm"), 49 | defaults = list(kernel = "laplacedot", 50 | var.fix = FALSE, 51 | iterations = 100) 52 | ) 53 | ) 54 | 55 | parsnip::set_encoding( 56 | model = "rvm_laplace", 57 | eng = "kernlab", 58 | mode = "regression", 59 | options = list( 60 | predictor_indicators = "none", 61 | compute_intercept = FALSE, 62 | remove_intercept = FALSE, 63 | allow_sparse_x = FALSE 64 | ) 65 | ) 66 | 67 | 68 | 69 | parsnip::set_pred( 70 | model = "rvm_laplace", 71 | eng = "kernlab", 72 | mode = "regression", 73 | type = "numeric", 74 | value = list( 75 | pre = NULL, 76 | post = \(results, object) { 77 | results[,1] 78 | }, 79 | func = c(pkg = "kernlab", fun = "predict"), 80 | args = 81 | list( 82 | object = quote(object$fit), 83 | newdata = quote(new_data), 84 | type = "response" 85 | ) 86 | ) 87 | ) 88 | 89 | parsnip::set_pred( 90 | model = "rvm_laplace", 91 | eng = "kernlab", 92 | mode = "regression", 93 | type = "raw", 94 | value = list( 95 | pre = NULL, 96 | post = NULL, 97 | func = c(pkg = "kernlab", fun = "predict"), 98 | args = list(object = quote(object$fit), newdata = quote(new_data)) 99 | ) 100 | ) 101 | 102 | } 103 | -------------------------------------------------------------------------------- /R/bag_svm_rbf_data.R: -------------------------------------------------------------------------------- 1 | make_bag_svm_rbf <- function() { 2 | 3 | parsnip::set_new_model("bag_svm_rbf") 4 | 5 | parsnip::set_model_mode("bag_svm_rbf", "classification") 6 | 7 | # ------------------------------------------------------------------------------ 8 | 9 | parsnip::set_model_engine("bag_svm_rbf", "classification", "ebmc") 10 | parsnip::set_dependency("bag_svm_rbf", "ebmc", "ebmc") 11 | parsnip::set_dependency("bag_svm_rbf", "ebmc", "maize") 12 | 13 | parsnip::set_model_arg( 14 | model = "bag_svm_rbf", 15 | eng = "ebmc", 16 | parsnip = "num_learners", 17 | original = "size", 18 | func = list(pkg = "maize", fun = "num_learners", range = c(1, 20)), 19 | has_submodel = FALSE 20 | ) 21 | 22 | parsnip::set_model_arg( 23 | model = "bag_svm_rbf", 24 | eng = "ebmc", 25 | parsnip = "imb_ratio", 26 | original = "ir", 27 | func = list(pkg = "maize", fun = "imb_ratio", range = c(1, 2)), 28 | has_submodel = FALSE 29 | ) 30 | 31 | parsnip::set_fit( 32 | model = "bag_svm_rbf", 33 | eng = "ebmc", 34 | mode = "classification", 35 | value = list( 36 | interface = "formula", 37 | data = c(formula = "formula", data = "data"), 38 | protect = c("formula", "data"), 39 | func = c(pkg = "ebmc", fun = "ub"), 40 | defaults = list(alg = "svm", svm.ker = "radial") 41 | ) 42 | ) 43 | 44 | parsnip::set_encoding( 45 | model = "bag_svm_rbf", 46 | eng = "ebmc", 47 | mode = "classification", 48 | options = list( 49 | predictor_indicators = "none", 50 | compute_intercept = FALSE, 51 | remove_intercept = FALSE, 52 | allow_sparse_x = FALSE 53 | ) 54 | ) 55 | 56 | 57 | parsnip::set_pred( 58 | model = "bag_svm_rbf", 59 | eng = "ebmc", 60 | mode = "classification", 61 | type = "class", 62 | value = list( 63 | pre = NULL, 64 | post = NULL, 65 | func = c(pkg = "ebmc", fun = "predict.modelBag"), 66 | args = 67 | list( 68 | object = quote(object$fit), 69 | newdata = quote(new_data), 70 | type = "class" 71 | ) 72 | ) 73 | ) 74 | 75 | parsnip::set_pred( 76 | model = "bag_svm_rbf", 77 | eng = "ebmc", 78 | mode = "classification", 79 | type = "prob", 80 | value = list( 81 | pre = NULL, 82 | post = function(result, object) as_tibble(result), 83 | func = c(pkg = "ebmc", fun = "predict.modelBag"), 84 | args = 85 | list( 86 | object = quote(object$fit), 87 | newdata = quote(new_data), 88 | type = "prob" 89 | ) 90 | ) 91 | ) 92 | 93 | parsnip::set_pred( 94 | model = "bag_svm_rbf", 95 | eng = "ebmc", 96 | mode = "classification", 97 | type = "raw", 98 | value = list( 99 | pre = NULL, 100 | post = NULL, 101 | func = c(pkg = "ebmc", fun = "predict.modelBag"), 102 | args = list(object = quote(object$fit), newdata = quote(new_data)) 103 | ) 104 | ) 105 | } 106 | -------------------------------------------------------------------------------- /man/step_kfm_nystrom.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/step_kfm_nystrom.R 3 | \name{step_kfm_nystrom} 4 | \alias{step_kfm_nystrom} 5 | \title{Nystrom kernel feature map approximation (RBF)} 6 | \usage{ 7 | step_kfm_nystrom( 8 | recipe, 9 | ..., 10 | role = "predictor", 11 | trained = FALSE, 12 | res = NULL, 13 | columns = NULL, 14 | sigma = 0.2, 15 | m = NULL, 16 | r = NULL, 17 | sampling = "random", 18 | prefix = "kFM", 19 | keep_original_cols = FALSE, 20 | skip = FALSE, 21 | id = rand_id("kfm_nystrom") 22 | ) 23 | } 24 | \arguments{ 25 | \item{recipe}{A recipe object. The step will be added to the 26 | sequence of operations for this recipe.} 27 | 28 | \item{...}{One or more selector functions to choose variables 29 | for this step. See \code{\link[=selections]{selections()}} for more details.} 30 | 31 | \item{role}{For model terms created by this step, what analysis role should 32 | they be assigned? By default, the new columns created by this step from 33 | the original variables will be used as \emph{predictors} in a model.} 34 | 35 | \item{trained}{A logical to indicate if the quantities for 36 | preprocessing have been estimated.} 37 | 38 | \item{res}{An \code{\link[mildsvm:kfm_nystrom]{mildsvm::kfm_nystrom()}} object is stored 39 | here once this preprocessing step has be trained by 40 | \code{\link[=prep]{prep()}}.} 41 | 42 | \item{columns}{A character string of the selected variable names. This field 43 | is a placeholder and will be populated once \code{\link[=prep]{prep()}} is used.} 44 | 45 | \item{sigma}{A numeric value for the nystrom function parameter.} 46 | 47 | \item{m}{The number rows from df to sample in fitting. defaults to nrow of data} 48 | 49 | \item{r}{The rank of matrix approximation to use. Must be less than or equal to m, the default.} 50 | 51 | \item{prefix}{A character string for the prefix of the resulting new 52 | variables. See notes below.} 53 | 54 | \item{keep_original_cols}{A logical to keep the original variables in the 55 | output. Defaults to \code{FALSE}.} 56 | 57 | \item{skip}{A logical. Should the step be skipped when the 58 | recipe is baked by \code{\link[=bake]{bake()}}? While all operations are baked 59 | when \code{\link[=prep]{prep()}} is run, some operations may not be able to be 60 | conducted on new data (e.g. processing the outcome variable(s)). 61 | Care should be taken when using \code{skip = TRUE} as it may affect 62 | the computations for subsequent operations.} 63 | 64 | \item{id}{A character string that is unique to this step to identify it.} 65 | } 66 | \description{ 67 | \code{step_kfm_nystrom()} creates a \emph{specification} of a recipe step that will 68 | convert numeric data into a feature appoximation. nystrom approximates the 'radial' 69 | kernel approximation. 70 | } 71 | \seealso{ 72 | Other multivariate transformation steps: 73 | \code{\link{step_kfa_laplace}()}, 74 | \code{\link{step_kha_laplace}()}, 75 | \code{\link{step_kha_tanh}()}, 76 | \code{\link{step_kpca_laplace}()}, 77 | \code{\link{step_kpca_tanh}()} 78 | } 79 | \concept{multivariate transformation steps} 80 | -------------------------------------------------------------------------------- /R/ada_boost_svm_rbf_data.R: -------------------------------------------------------------------------------- 1 | make_ada_boost_svm_rbf <- function() { 2 | 3 | parsnip::set_new_model("ada_boost_svm_rbf") 4 | 5 | parsnip::set_model_mode("ada_boost_svm_rbf", "classification") 6 | 7 | # ------------------------------------------------------------------------------ 8 | 9 | parsnip::set_model_engine("ada_boost_svm_rbf", "classification", "ebmc") 10 | parsnip::set_dependency("ada_boost_svm_rbf", "ebmc", "ebmc") 11 | parsnip::set_dependency("ada_boost_svm_rbf", "ebmc", "maize") 12 | 13 | parsnip::set_model_arg( 14 | model = "ada_boost_svm_rbf", 15 | eng = "ebmc", 16 | parsnip = "num_learners", 17 | original = "size", 18 | func = list(pkg = "maize", fun = "num_learners", range = c(1, 20)), 19 | has_submodel = FALSE 20 | ) 21 | 22 | parsnip::set_model_arg( 23 | model = "ada_boost_svm_rbf", 24 | eng = "ebmc", 25 | parsnip = "imb_ratio", 26 | original = "ir", 27 | func = list(pkg = "maize", fun = "imb_ratio", range = c(1, 2)), 28 | has_submodel = FALSE 29 | ) 30 | 31 | parsnip::set_fit( 32 | model = "ada_boost_svm_rbf", 33 | eng = "ebmc", 34 | mode = "classification", 35 | value = list( 36 | interface = "formula", 37 | data = c(formula = "formula", data = "data"), 38 | protect = c("formula", "data"), 39 | func = c(pkg = "ebmc", fun = "rus"), 40 | defaults = list(alg = "svm", svm.ker = "radial") 41 | ) 42 | ) 43 | 44 | parsnip::set_encoding( 45 | model = "ada_boost_svm_rbf", 46 | eng = "ebmc", 47 | mode = "classification", 48 | options = list( 49 | predictor_indicators = "none", 50 | compute_intercept = FALSE, 51 | remove_intercept = FALSE, 52 | allow_sparse_x = FALSE 53 | ) 54 | ) 55 | 56 | 57 | parsnip::set_pred( 58 | model = "ada_boost_svm_rbf", 59 | eng = "ebmc", 60 | mode = "classification", 61 | type = "class", 62 | value = list( 63 | pre = NULL, 64 | post = NULL, 65 | func = c(pkg = "ebmc", fun = "predict.modelBst"), 66 | args = 67 | list( 68 | object = quote(object$fit), 69 | newdata = quote(new_data), 70 | type = "class" 71 | ) 72 | ) 73 | ) 74 | 75 | parsnip::set_pred( 76 | model = "ada_boost_svm_rbf", 77 | eng = "ebmc", 78 | mode = "classification", 79 | type = "prob", 80 | value = list( 81 | pre = NULL, 82 | post = function(result, object) as_tibble(result), 83 | func = c(pkg = "ebmc", fun = "predict.modelBst"), 84 | args = 85 | list( 86 | object = quote(object$fit), 87 | newdata = quote(new_data), 88 | type = "prob" 89 | ) 90 | ) 91 | ) 92 | 93 | parsnip::set_pred( 94 | model = "ada_boost_svm_rbf", 95 | eng = "ebmc", 96 | mode = "classification", 97 | type = "raw", 98 | value = list( 99 | pre = NULL, 100 | post = NULL, 101 | func = c(pkg = "ebmc", fun = "predict.modelBst"), 102 | args = list(object = quote(object$fit), newdata = quote(new_data)) 103 | ) 104 | ) 105 | } 106 | -------------------------------------------------------------------------------- /R/rus_boost_svm_rbf_data.R: -------------------------------------------------------------------------------- 1 | make_rus_boost_svm_rbf <- function() { 2 | 3 | parsnip::set_new_model("rus_boost_svm_rbf") 4 | 5 | parsnip::set_model_mode("rus_boost_svm_rbf", "classification") 6 | 7 | # ------------------------------------------------------------------------------ 8 | 9 | parsnip::set_model_engine("rus_boost_svm_rbf", "classification", "ebmc") 10 | parsnip::set_dependency("rus_boost_svm_rbf", "ebmc", "ebmc") 11 | parsnip::set_dependency("rus_boost_svm_rbf", "ebmc", "maize") 12 | 13 | parsnip::set_model_arg( 14 | model = "rus_boost_svm_rbf", 15 | eng = "ebmc", 16 | parsnip = "num_learners", 17 | original = "size", 18 | func = list(pkg = "maize", fun = "num_learners", range = c(1, 20)), 19 | has_submodel = FALSE 20 | ) 21 | 22 | parsnip::set_model_arg( 23 | model = "rus_boost_svm_rbf", 24 | eng = "ebmc", 25 | parsnip = "imb_ratio", 26 | original = "ir", 27 | func = list(pkg = "maize", fun = "imb_ratio", range = c(1, 2)), 28 | has_submodel = FALSE 29 | ) 30 | 31 | parsnip::set_fit( 32 | model = "rus_boost_svm_rbf", 33 | eng = "ebmc", 34 | mode = "classification", 35 | value = list( 36 | interface = "formula", 37 | data = c(formula = "formula", data = "data"), 38 | protect = c("formula", "data"), 39 | func = c(pkg = "ebmc", fun = "rus"), 40 | defaults = list(alg = "svm", svm.ker = "radial") 41 | ) 42 | ) 43 | 44 | parsnip::set_encoding( 45 | model = "rus_boost_svm_rbf", 46 | eng = "ebmc", 47 | mode = "classification", 48 | options = list( 49 | predictor_indicators = "none", 50 | compute_intercept = FALSE, 51 | remove_intercept = FALSE, 52 | allow_sparse_x = FALSE 53 | ) 54 | ) 55 | 56 | 57 | parsnip::set_pred( 58 | model = "rus_boost_svm_rbf", 59 | eng = "ebmc", 60 | mode = "classification", 61 | type = "class", 62 | value = list( 63 | pre = NULL, 64 | post = NULL, 65 | func = c(pkg = "ebmc", fun = "predict.modelBst"), 66 | args = 67 | list( 68 | object = quote(object$fit), 69 | newdata = quote(new_data), 70 | type = "class" 71 | ) 72 | ) 73 | ) 74 | 75 | parsnip::set_pred( 76 | model = "rus_boost_svm_rbf", 77 | eng = "ebmc", 78 | mode = "classification", 79 | type = "prob", 80 | value = list( 81 | pre = NULL, 82 | post = function(result, object) as_tibble(result), 83 | func = c(pkg = "ebmc", fun = "predict.modelBst"), 84 | args = 85 | list( 86 | object = quote(object$fit), 87 | newdata = quote(new_data), 88 | type = "prob" 89 | ) 90 | ) 91 | ) 92 | 93 | parsnip::set_pred( 94 | model = "rus_boost_svm_rbf", 95 | eng = "ebmc", 96 | mode = "classification", 97 | type = "raw", 98 | value = list( 99 | pre = NULL, 100 | post = NULL, 101 | func = c(pkg = "ebmc", fun = "predict.modelBst"), 102 | args = list(object = quote(object$fit), newdata = quote(new_data)) 103 | ) 104 | ) 105 | } 106 | -------------------------------------------------------------------------------- /man/step_kpca_laplace.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/step_kpca_laplace.R 3 | \name{step_kpca_laplace} 4 | \alias{step_kpca_laplace} 5 | \title{Laplacian function kernel PCA signal extraction} 6 | \usage{ 7 | step_kpca_laplace( 8 | recipe, 9 | ..., 10 | role = "predictor", 11 | trained = FALSE, 12 | num_comp = 5, 13 | res = NULL, 14 | columns = NULL, 15 | sigma = 0.2, 16 | prefix = "kPC", 17 | keep_original_cols = FALSE, 18 | skip = FALSE, 19 | id = rand_id("kpca_laplace") 20 | ) 21 | } 22 | \arguments{ 23 | \item{recipe}{A recipe object. The step will be added to the 24 | sequence of operations for this recipe.} 25 | 26 | \item{...}{One or more selector functions to choose variables 27 | for this step. See \code{\link[=selections]{selections()}} for more details.} 28 | 29 | \item{role}{For model terms created by this step, what analysis role should 30 | they be assigned? By default, the new columns created by this step from 31 | the original variables will be used as \emph{predictors} in a model.} 32 | 33 | \item{trained}{A logical to indicate if the quantities for 34 | preprocessing have been estimated.} 35 | 36 | \item{num_comp}{The number of components to retain as new predictors. 37 | If \code{num_comp} is greater than the number of columns or the number of 38 | possible components, a smaller value will be used. If \code{num_comp = 0} 39 | is set then no transformation is done and selected variables will 40 | stay unchanged, regardless of the value of \code{keep_original_cols}.} 41 | 42 | \item{res}{An S4 \code{\link[kernlab:kpca]{kernlab::kpca()}} object is stored 43 | here once this preprocessing step has be trained by 44 | \code{\link[=prep]{prep()}}.} 45 | 46 | \item{columns}{A character string of the selected variable names. This field 47 | is a placeholder and will be populated once \code{\link[=prep]{prep()}} is used.} 48 | 49 | \item{sigma}{A numeric value for the laplace function parameter.} 50 | 51 | \item{prefix}{A character string for the prefix of the resulting new 52 | variables. See notes below.} 53 | 54 | \item{keep_original_cols}{A logical to keep the original variables in the 55 | output. Defaults to \code{FALSE}.} 56 | 57 | \item{skip}{A logical. Should the step be skipped when the 58 | recipe is baked by \code{\link[=bake]{bake()}}? While all operations are baked 59 | when \code{\link[=prep]{prep()}} is run, some operations may not be able to be 60 | conducted on new data (e.g. processing the outcome variable(s)). 61 | Care should be taken when using \code{skip = TRUE} as it may affect 62 | the computations for subsequent operations.} 63 | 64 | \item{id}{A character string that is unique to this step to identify it.} 65 | } 66 | \description{ 67 | \code{step_kpca_laplace()} creates a \emph{specification} of a recipe step that will 68 | convert numeric data into one or more principal components using a laplace 69 | kernel 70 | } 71 | \seealso{ 72 | Other multivariate transformation steps: 73 | \code{\link{step_kfa_laplace}()}, 74 | \code{\link{step_kfm_nystrom}()}, 75 | \code{\link{step_kha_laplace}()}, 76 | \code{\link{step_kha_tanh}()}, 77 | \code{\link{step_kpca_tanh}()} 78 | } 79 | \concept{multivariate transformation steps} 80 | -------------------------------------------------------------------------------- /R/kqr_laplace.R: -------------------------------------------------------------------------------- 1 | #' Laplacian Kernel Quantile Regression 2 | #' @description laplacian kernel 3 | #' @param mode regression 4 | #' @param engine kernlab kqr 5 | #' @param cost A positive number for the cost of predicting a sample within 6 | #' or on the wrong side of the margin 7 | #' @param tau a quantile for the loss function 8 | #' @param laplace_sigma sigma parameter for laplacian 9 | #' @export 10 | 11 | kqr_laplace <- 12 | function(mode = "unknown", engine = "kernlab", 13 | cost = NULL, tau = NULL, laplace_sigma = NULL) { 14 | 15 | args <- list( 16 | cost = enquo(cost), 17 | tau = enquo(tau), 18 | laplace_sigma = enquo(laplace_sigma) 19 | ) 20 | 21 | parsnip::new_model_spec( 22 | "kqr_laplace", 23 | args = args, 24 | eng_args = NULL, 25 | mode = mode, 26 | user_specified_mode = !missing(mode), 27 | method = NULL, 28 | engine = engine, 29 | user_specified_engine = !missing(engine) 30 | ) 31 | } 32 | 33 | # ------------------------------------------------------------------------------ 34 | 35 | #' @method update kqr_laplace 36 | #' @rdname parsnip_update 37 | #' @export 38 | update.kqr_laplace <- 39 | function(object, 40 | parameters = NULL, 41 | cost = NULL, tau = NULL, laplace_sigma = NULL, 42 | fresh = FALSE, 43 | ...) { 44 | 45 | args <- list( 46 | cost = enquo(cost), 47 | tau = enquo(tau), 48 | laplace_sigma = enquo(laplace_sigma) 49 | ) 50 | 51 | parsnip::update_spec( 52 | object = object, 53 | parameters = parameters, 54 | args_enquo_list = args, 55 | fresh = fresh, 56 | cls = "kqr_laplace", 57 | ... 58 | ) 59 | } 60 | 61 | # ------------------------------------------------------------------------------ 62 | 63 | #' @export 64 | translate.kqr_laplace <- function(x, engine = x$engine, ...) { 65 | x <- parsnip::translate.default(x, engine = engine, ...) 66 | 67 | # slightly cleaner code using 68 | arg_vals <- x$method$fit$args 69 | arg_names <- names(arg_vals) 70 | 71 | # add checks to error trap or change things for this method 72 | if (x$engine == "kernlab") { 73 | 74 | # unless otherwise specified, classification models predict probabilities 75 | if (x$mode == "classification" && !any(arg_names == "prob.model")) 76 | arg_vals$prob.model <- TRUE 77 | if (x$mode == "classification" && any(arg_names == "epsilon")) 78 | arg_vals$epsilon <- NULL 79 | 80 | # convert sigma and scale to a `kpar` argument. 81 | if (any(arg_names == "sigma")) { 82 | kpar <- rlang::expr(list()) 83 | kpar$sigma <- arg_vals$sigma 84 | arg_vals$sigma <- NULL 85 | arg_vals$kpar <- kpar 86 | } 87 | 88 | } 89 | 90 | x$method$fit$args <- arg_vals 91 | 92 | # worried about people using this to modify the specification 93 | x 94 | } 95 | 96 | # ------------------------------------------------------------------------------ 97 | 98 | #' @export 99 | check_args.kqr_laplace <- function(object, call = rlang::caller_env()) { 100 | invisible(object) 101 | } 102 | 103 | -------------------------------------------------------------------------------- /man/step_kpca_tanh.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/step_kpca_tanh.R 3 | \name{step_kpca_tanh} 4 | \alias{step_kpca_tanh} 5 | \title{Laplacian function kernel PCA signal extraction} 6 | \usage{ 7 | step_kpca_tanh( 8 | recipe, 9 | ..., 10 | role = "predictor", 11 | trained = FALSE, 12 | num_comp = 5, 13 | res = NULL, 14 | columns = NULL, 15 | scale_factor = 0.2, 16 | offset = 0, 17 | prefix = "kPC", 18 | keep_original_cols = FALSE, 19 | skip = FALSE, 20 | id = rand_id("kpca_tanh") 21 | ) 22 | } 23 | \arguments{ 24 | \item{recipe}{A recipe object. The step will be added to the 25 | sequence of operations for this recipe.} 26 | 27 | \item{...}{One or more selector functions to choose variables 28 | for this step. See \code{\link[=selections]{selections()}} for more details.} 29 | 30 | \item{role}{For model terms created by this step, what analysis role should 31 | they be assigned? By default, the new columns created by this step from 32 | the original variables will be used as \emph{predictors} in a model.} 33 | 34 | \item{trained}{A logical to indicate if the quantities for 35 | preprocessing have been estimated.} 36 | 37 | \item{num_comp}{The number of components to retain as new predictors. 38 | If \code{num_comp} is greater than the number of columns or the number of 39 | possible components, a smaller value will be used. If \code{num_comp = 0} 40 | is set then no transformation is done and selected variables will 41 | stay unchanged, regardless of the value of \code{keep_original_cols}.} 42 | 43 | \item{res}{An S4 \code{\link[kernlab:kpca]{kernlab::kpca()}} object is stored 44 | here once this preprocessing step has be trained by 45 | \code{\link[=prep]{prep()}}.} 46 | 47 | \item{columns}{A character string of the selected variable names. This field 48 | is a placeholder and will be populated once \code{\link[=prep]{prep()}} is used.} 49 | 50 | \item{scale_factor}{A numeric value for the tanh function parameter.} 51 | 52 | \item{prefix}{A character string for the prefix of the resulting new 53 | variables. See notes below.} 54 | 55 | \item{keep_original_cols}{A logical to keep the original variables in the 56 | output. Defaults to \code{FALSE}.} 57 | 58 | \item{skip}{A logical. Should the step be skipped when the 59 | recipe is baked by \code{\link[=bake]{bake()}}? While all operations are baked 60 | when \code{\link[=prep]{prep()}} is run, some operations may not be able to be 61 | conducted on new data (e.g. processing the outcome variable(s)). 62 | Care should be taken when using \code{skip = TRUE} as it may affect 63 | the computations for subsequent operations.} 64 | 65 | \item{id}{A character string that is unique to this step to identify it.} 66 | } 67 | \description{ 68 | \code{step_kpca_tanh()} creates a \emph{specification} of a recipe step that will 69 | convert numeric data into one or more principal components using a tanh 70 | kernel basis expansion. 71 | } 72 | \seealso{ 73 | Other multivariate transformation steps: 74 | \code{\link{step_kfa_laplace}()}, 75 | \code{\link{step_kfm_nystrom}()}, 76 | \code{\link{step_kha_laplace}()}, 77 | \code{\link{step_kha_tanh}()}, 78 | \code{\link{step_kpca_laplace}()} 79 | } 80 | \concept{multivariate transformation steps} 81 | -------------------------------------------------------------------------------- /R/misvm_orova_rbf_data.R: -------------------------------------------------------------------------------- 1 | make_misvm_orova_rbf <- function() { 2 | 3 | parsnip::set_new_model("misvm_orova_rbf") 4 | 5 | parsnip::set_model_mode("misvm_orova_rbf", "classification") 6 | 7 | # ------------------------------------------------------------------------------ 8 | 9 | parsnip::set_model_engine("misvm_orova_rbf", "classification", "mildsvm") 10 | parsnip::set_dependency("misvm_orova_rbf", "mildsvm", "mildsvm") 11 | parsnip::set_dependency("misvm_orova_rbf", "mildsvm", "maize") 12 | 13 | parsnip::set_model_arg( 14 | model = "misvm_orova_rbf", 15 | eng = "mildsvm", 16 | parsnip = "cost", 17 | original = "cost", 18 | func = list(pkg = "dial", fun = "cost", range = c(1, 20)), 19 | has_submodel = FALSE 20 | ) 21 | 22 | parsnip::set_model_arg( 23 | model = "misvm_orova_rbf", 24 | eng = "mildsvm", 25 | parsnip = "rbf_sigma", 26 | original = "sigma", 27 | func = list(pkg = "dials", fun = "rbf_sigma"), 28 | has_submodel = FALSE 29 | ) 30 | 31 | parsnip::set_fit( 32 | model = "misvm_orova_rbf", 33 | eng = "mildsvm", 34 | mode = "classification", 35 | value = list( 36 | interface = "formula", 37 | data = c(formula = "formula", data = "data"), 38 | protect = c("formula", "data"), 39 | func = c(pkg = "maize", fun = "misvm_orova_fit_spec"), 40 | defaults = list(NULL) 41 | ) 42 | ) 43 | 44 | parsnip::set_encoding( 45 | model = "misvm_orova_rbf", 46 | eng = "mildsvm", 47 | mode = "classification", 48 | options = list( 49 | predictor_indicators = "none", 50 | compute_intercept = FALSE, 51 | remove_intercept = FALSE, 52 | allow_sparse_x = FALSE 53 | ) 54 | ) 55 | 56 | 57 | parsnip::set_pred( 58 | model = "misvm_orova_rbf", 59 | eng = "mildsvm", 60 | mode = "classification", 61 | type = "class", 62 | value = list( 63 | pre = NULL, 64 | post = NULL, 65 | func = c(pkg = "maize", fun = "misvm_predict_spec"), 66 | args = 67 | list( 68 | object = quote(object$fit), 69 | new_data = quote(new_data), 70 | type = "class" 71 | ) 72 | ) 73 | ) 74 | 75 | # only "class" and "raw" in mildsvm 76 | # parsnip::set_pred( 77 | # model = "misvm_orova_rbf", 78 | # eng = "mildsvm", 79 | # mode = "classification", 80 | # type = "prob", 81 | # value = list( 82 | # pre = NULL, 83 | # post = function(result, object) as_tibble(result), 84 | # func = c(pkg = "maize", fun = "misvm_predict_spec"), 85 | # args = 86 | # list( 87 | # object = quote(object$fit), 88 | # new_data = quote(new_data), 89 | # type = "prob" 90 | # ) 91 | # ) 92 | # ) 93 | 94 | parsnip::set_pred( 95 | model = "misvm_orova_rbf", 96 | eng = "mildsvm", 97 | mode = "classification", 98 | type = "raw", 99 | value = list( 100 | pre = NULL, 101 | post = NULL, 102 | func = c(pkg = "maize", fun = "misvm_predict_spec"), 103 | args = list(object = quote(object$fit), new_data = quote(new_data), type = "raw") 104 | ) 105 | ) 106 | } 107 | -------------------------------------------------------------------------------- /man/step_kfa_laplace.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/step_kfa_laplace.R 3 | \name{step_kfa_laplace} 4 | \alias{step_kfa_laplace} 5 | \title{Laplacian function kernel KFA signal extraction} 6 | \usage{ 7 | step_kfa_laplace( 8 | recipe, 9 | ..., 10 | role = "predictor", 11 | trained = FALSE, 12 | num_comp = 5, 13 | res = NULL, 14 | columns = NULL, 15 | sigma = 0.2, 16 | prefix = "kFA", 17 | keep_original_cols = FALSE, 18 | skip = FALSE, 19 | id = rand_id("kfa_laplace") 20 | ) 21 | } 22 | \arguments{ 23 | \item{recipe}{A recipe object. The step will be added to the 24 | sequence of operations for this recipe.} 25 | 26 | \item{...}{One or more selector functions to choose variables 27 | for this step. See \code{\link[=selections]{selections()}} for more details.} 28 | 29 | \item{role}{For model terms created by this step, what analysis role should 30 | they be assigned? By default, the new columns created by this step from 31 | the original variables will be used as \emph{predictors} in a model.} 32 | 33 | \item{trained}{A logical to indicate if the quantities for 34 | preprocessing have been estimated.} 35 | 36 | \item{num_comp}{The number of components to retain as new predictors. 37 | If \code{num_comp} is greater than the number of columns or the number of 38 | possible components, a smaller value will be used. If \code{num_comp = 0} 39 | is set then no transformation is done and selected variables will 40 | stay unchanged, regardless of the value of \code{keep_original_cols}.} 41 | 42 | \item{res}{An S4 \code{\link[kernlab:kfa]{kernlab::kfa()}} object is stored 43 | here once this preprocessing step has be trained by 44 | \code{\link[=prep]{prep()}}.} 45 | 46 | \item{columns}{A character string of the selected variable names. This field 47 | is a placeholder and will be populated once \code{\link[=prep]{prep()}} is used.} 48 | 49 | \item{sigma}{A numeric value for the laplace function parameter.} 50 | 51 | \item{prefix}{A character string for the prefix of the resulting new 52 | variables. See notes below.} 53 | 54 | \item{keep_original_cols}{A logical to keep the original variables in the 55 | output. Defaults to \code{FALSE}.} 56 | 57 | \item{skip}{A logical. Should the step be skipped when the 58 | recipe is baked by \code{\link[=bake]{bake()}}? While all operations are baked 59 | when \code{\link[=prep]{prep()}} is run, some operations may not be able to be 60 | conducted on new data (e.g. processing the outcome variable(s)). 61 | Care should be taken when using \code{skip = TRUE} as it may affect 62 | the computations for subsequent operations.} 63 | 64 | \item{id}{A character string that is unique to this step to identify it.} 65 | } 66 | \description{ 67 | \code{step_kfa_laplace()} creates a \emph{specification} of a recipe step that will 68 | convert numeric data into one or more kernel components using a laplace kernel. 69 | similar to KPCA, but instead of extracting eigenvectors of the dataset in feature space, 70 | it approximates the eigenvectors by selecting patterns which are good basis vectors for the dataset. 71 | } 72 | \seealso{ 73 | Other multivariate transformation steps: 74 | \code{\link{step_kfm_nystrom}()}, 75 | \code{\link{step_kha_laplace}()}, 76 | \code{\link{step_kha_tanh}()}, 77 | \code{\link{step_kpca_laplace}()}, 78 | \code{\link{step_kpca_tanh}()} 79 | } 80 | \concept{multivariate transformation steps} 81 | -------------------------------------------------------------------------------- /R/svm_laplace.R: -------------------------------------------------------------------------------- 1 | #' Laplacian Support Vector Machine 2 | #' @description laplacian kernel for support vector machines 3 | #' @param mode regression or classification 4 | #' @param engine kernlab ksvm 5 | #' @param cost A positive number for the cost of predicting a sample within 6 | #' or on the wrong side of the margin 7 | #' @param margin A positive number for the epsilon in the SVM insensitive 8 | #' loss function (regression only) 9 | #' @param laplace_sigma sigma parameter for laplacian 10 | #' @export 11 | 12 | svm_laplace <- 13 | function(mode = "unknown", engine = "kernlab", 14 | cost = NULL, margin = NULL, laplace_sigma = NULL) { 15 | 16 | args <- list( 17 | cost = rlang::enquo(cost), 18 | margin = rlang::enquo(margin), 19 | laplace_sigma = rlang::enquo(laplace_sigma) 20 | ) 21 | 22 | parsnip::new_model_spec( 23 | "svm_laplace", 24 | args = args, 25 | eng_args = NULL, 26 | mode = mode, 27 | user_specified_mode = !missing(mode), 28 | method = NULL, 29 | engine = engine, 30 | user_specified_engine = !missing(engine) 31 | ) 32 | } 33 | 34 | # additional functions so classification probs work, etc. 35 | # ------------------------------------------------------------------------------ 36 | 37 | #' @method update svm_laplace 38 | #' @rdname parsnip_update 39 | #' @export 40 | update.svm_laplace <- 41 | function(object, 42 | parameters = NULL, 43 | cost = NULL, margin = NULL, laplace_sigma = NULL, 44 | fresh = FALSE, 45 | ...) { 46 | 47 | args <- list( 48 | cost = enquo(cost), 49 | margin = enquo(margin), 50 | laplace_sigma = enquo(laplace_sigma) 51 | ) 52 | 53 | parsnip::update_spec( 54 | object = object, 55 | parameters = parameters, 56 | args_enquo_list = args, 57 | fresh = fresh, 58 | cls = "svm_laplace", 59 | ... 60 | ) 61 | } 62 | 63 | # ------------------------------------------------------------------------------ 64 | 65 | #' @export 66 | translate.svm_laplace <- function(x, engine = x$engine, ...) { 67 | x <- parsnip::translate.default(x, engine = engine, ...) 68 | 69 | # slightly cleaner code using 70 | arg_vals <- x$method$fit$args 71 | arg_names <- names(arg_vals) 72 | 73 | # add checks to error trap or change things for this method 74 | if (x$engine == "kernlab") { 75 | 76 | # unless otherwise specified, classification models predict probabilities 77 | if (x$mode == "classification" && !any(arg_names == "prob.model")) 78 | arg_vals$prob.model <- TRUE 79 | if (x$mode == "classification" && any(arg_names == "epsilon")) 80 | arg_vals$epsilon <- NULL 81 | 82 | # convert sigma and scale to a `kpar` argument. 83 | if (any(arg_names == "sigma")) { 84 | kpar <- rlang::expr(list()) 85 | kpar$sigma <- arg_vals$sigma 86 | arg_vals$sigma <- NULL 87 | arg_vals$kpar <- kpar 88 | } 89 | 90 | } 91 | 92 | x$method$fit$args <- arg_vals 93 | 94 | # worried about people using this to modify the specification 95 | x 96 | } 97 | 98 | # ------------------------------------------------------------------------------ 99 | 100 | #' @export 101 | check_args.svm_laplace <- function(object, call = rlang::caller_env()) { 102 | invisible(object) 103 | } 104 | -------------------------------------------------------------------------------- /man/step_kha_tanh.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/step_kha_tanh.R 3 | \name{step_kha_tanh} 4 | \alias{step_kha_tanh} 5 | \title{tanh function kernel PCA signal extraction via Hebbian Algorithm} 6 | \usage{ 7 | step_kha_tanh( 8 | recipe, 9 | ..., 10 | role = "predictor", 11 | trained = FALSE, 12 | num_comp = 5, 13 | res = NULL, 14 | columns = NULL, 15 | scale_factor = 0.2, 16 | learn_rate = 0.005, 17 | threshold = 1e-06, 18 | stop_iter = 100, 19 | prefix = "kha", 20 | keep_original_cols = FALSE, 21 | skip = FALSE, 22 | id = rand_id("kha_tanh") 23 | ) 24 | } 25 | \arguments{ 26 | \item{recipe}{A recipe object. The step will be added to the 27 | sequence of operations for this recipe.} 28 | 29 | \item{...}{One or more selector functions to choose variables 30 | for this step. See \code{\link[=selections]{selections()}} for more details.} 31 | 32 | \item{role}{For model terms created by this step, what analysis role should 33 | they be assigned? By default, the new columns created by this step from 34 | the original variables will be used as \emph{predictors} in a model.} 35 | 36 | \item{trained}{A logical to indicate if the quantities for 37 | preprocessing have been estimated.} 38 | 39 | \item{num_comp}{The number of components to retain as new predictors. 40 | If \code{num_comp} is greater than the number of columns or the number of 41 | possible components, a smaller value will be used. If \code{num_comp = 0} 42 | is set then no transformation is done and selected variables will 43 | stay unchanged, regardless of the value of \code{keep_original_cols}.} 44 | 45 | \item{res}{An S4 \code{\link[kernlab:kha]{kernlab::kha()}} object is stored 46 | here once this preprocessing step has be trained by 47 | \code{\link[=prep]{prep()}}.} 48 | 49 | \item{columns}{A character string of the selected variable names. This field 50 | is a placeholder and will be populated once \code{\link[=prep]{prep()}} is used.} 51 | 52 | \item{scale_factor}{A numeric value for the tanh function parameter.} 53 | 54 | \item{learn_rate}{hebbian learning rate} 55 | 56 | \item{threshold}{the smallest value of the convergence step} 57 | 58 | \item{stop_iter}{maximum number of iterations} 59 | 60 | \item{prefix}{A character string for the prefix of the resulting new 61 | variables. See notes below.} 62 | 63 | \item{keep_original_cols}{A logical to keep the original variables in the 64 | output. Defaults to \code{FALSE}.} 65 | 66 | \item{skip}{A logical. Should the step be skipped when the 67 | recipe is baked by \code{\link[=bake]{bake()}}? While all operations are baked 68 | when \code{\link[=prep]{prep()}} is run, some operations may not be able to be 69 | conducted on new data (e.g. processing the outcome variable(s)). 70 | Care should be taken when using \code{skip = TRUE} as it may affect 71 | the computations for subsequent operations.} 72 | 73 | \item{id}{A character string that is unique to this step to identify it.} 74 | } 75 | \description{ 76 | \code{step_kha_tanh()} creates a \emph{specification} of a recipe step that will 77 | convert numeric data into one or more principal components using a tanh 78 | kernel basis expansion. 79 | } 80 | \seealso{ 81 | Other multivariate transformation steps: 82 | \code{\link{step_kfa_laplace}()}, 83 | \code{\link{step_kfm_nystrom}()}, 84 | \code{\link{step_kha_laplace}()}, 85 | \code{\link{step_kpca_laplace}()}, 86 | \code{\link{step_kpca_tanh}()} 87 | } 88 | \concept{multivariate transformation steps} 89 | -------------------------------------------------------------------------------- /man/step_kha_laplace.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/step_kha_laplace.R 3 | \name{step_kha_laplace} 4 | \alias{step_kha_laplace} 5 | \title{Laplacian function kernel PCA signal extraction via Hebbian Algorithm} 6 | \usage{ 7 | step_kha_laplace( 8 | recipe, 9 | ..., 10 | role = "predictor", 11 | trained = FALSE, 12 | num_comp = 5, 13 | res = NULL, 14 | columns = NULL, 15 | sigma = 0.2, 16 | learn_rate = 0.005, 17 | threshold = 1e-04, 18 | stop_iter = 100, 19 | prefix = "kha", 20 | keep_original_cols = FALSE, 21 | skip = FALSE, 22 | id = rand_id("kha_laplace") 23 | ) 24 | } 25 | \arguments{ 26 | \item{recipe}{A recipe object. The step will be added to the 27 | sequence of operations for this recipe.} 28 | 29 | \item{...}{One or more selector functions to choose variables 30 | for this step. See \code{\link[=selections]{selections()}} for more details.} 31 | 32 | \item{role}{For model terms created by this step, what analysis role should 33 | they be assigned? By default, the new columns created by this step from 34 | the original variables will be used as \emph{predictors} in a model.} 35 | 36 | \item{trained}{A logical to indicate if the quantities for 37 | preprocessing have been estimated.} 38 | 39 | \item{num_comp}{The number of components to retain as new predictors. 40 | If \code{num_comp} is greater than the number of columns or the number of 41 | possible components, a smaller value will be used. If \code{num_comp = 0} 42 | is set then no transformation is done and selected variables will 43 | stay unchanged, regardless of the value of \code{keep_original_cols}.} 44 | 45 | \item{res}{An S4 \code{\link[kernlab:kha]{kernlab::kha()}} object is stored 46 | here once this preprocessing step has be trained by 47 | \code{\link[=prep]{prep()}}.} 48 | 49 | \item{columns}{A character string of the selected variable names. This field 50 | is a placeholder and will be populated once \code{\link[=prep]{prep()}} is used.} 51 | 52 | \item{sigma}{A numeric value for the laplace function parameter.} 53 | 54 | \item{learn_rate}{hebbian learning rate} 55 | 56 | \item{threshold}{the smallest value of the convergence step} 57 | 58 | \item{stop_iter}{maximum number of iterations} 59 | 60 | \item{prefix}{A character string for the prefix of the resulting new 61 | variables. See notes below.} 62 | 63 | \item{keep_original_cols}{A logical to keep the original variables in the 64 | output. Defaults to \code{FALSE}.} 65 | 66 | \item{skip}{A logical. Should the step be skipped when the 67 | recipe is baked by \code{\link[=bake]{bake()}}? While all operations are baked 68 | when \code{\link[=prep]{prep()}} is run, some operations may not be able to be 69 | conducted on new data (e.g. processing the outcome variable(s)). 70 | Care should be taken when using \code{skip = TRUE} as it may affect 71 | the computations for subsequent operations.} 72 | 73 | \item{id}{A character string that is unique to this step to identify it.} 74 | } 75 | \description{ 76 | \code{step_kha_laplace()} creates a \emph{specification} of a recipe step that will 77 | convert numeric data into one or more principal components using a laplace 78 | kernel basis expansion. 79 | } 80 | \seealso{ 81 | Other multivariate transformation steps: 82 | \code{\link{step_kfa_laplace}()}, 83 | \code{\link{step_kfm_nystrom}()}, 84 | \code{\link{step_kha_tanh}()}, 85 | \code{\link{step_kpca_laplace}()}, 86 | \code{\link{step_kpca_tanh}()} 87 | } 88 | \concept{multivariate transformation steps} 89 | -------------------------------------------------------------------------------- /R/svm_wavelet.R: -------------------------------------------------------------------------------- 1 | #' Wavelet Support Vector Machine 2 | #' @description wavelet kernel for support vector machines 3 | #' @param mode regression or classification 4 | #' @param engine kernlab ksvm 5 | #' @param cost A positive number for the cost of predicting a sample within 6 | #' or on the wrong side of the margin 7 | #' @param margin A positive number for the epsilon in the SVM insensitive 8 | #' loss function (regression only) 9 | #' @param sigma sigma parameter for svm wavelet kernel 10 | #' @param a scale adjustment parameter for wavelet kernels (temp name) 11 | #' @param c dist adjustment parameter for wavelet kernels can be NULL (temp name) 12 | #' @param h wavelet function for wavelet kernel, default wavelet if NULL (temp name) 13 | #' @export 14 | 15 | svm_wavelet <- 16 | function(mode = "unknown", engine = "kernlab", 17 | cost = NULL, margin = NULL, sigma = NULL, a = 1, c = NULL, h = NULL) { 18 | 19 | args <- list( 20 | cost = enquo(cost), 21 | margin = enquo(margin), 22 | sigma = enquo(sigma), 23 | a = enquo(a), 24 | c = enquo(c), 25 | h = enquo(h) 26 | ) 27 | 28 | parsnip::new_model_spec( 29 | "svm_wavelet", 30 | args = args, 31 | eng_args = NULL, 32 | mode = mode, 33 | user_specified_mode = !missing(mode), 34 | method = NULL, 35 | engine = engine, 36 | user_specified_engine = !missing(engine) 37 | ) 38 | } 39 | 40 | # ------------------------------------------------------------------------------ 41 | 42 | #' @method update svm_wavelet 43 | #' @rdname parsnip_update 44 | #' @export 45 | update.svm_wavelet <- 46 | function(object, 47 | parameters = NULL, 48 | cost = NULL, margin = NULL, sigma = NULL, 49 | a = NULL, c = NULL, h = NULL, 50 | fresh = FALSE, 51 | ...) { 52 | 53 | args <- list( 54 | cost = enquo(cost), 55 | margin = enquo(margin), 56 | sigma = enquo(sigma), 57 | a = enquo(a), 58 | c = enquo(c), 59 | h = enquo(h) 60 | ) 61 | 62 | parsnip::update_spec( 63 | object = object, 64 | parameters = parameters, 65 | args_enquo_list = args, 66 | fresh = fresh, 67 | cls = "svm_wavelet", 68 | ... 69 | ) 70 | } 71 | 72 | # ------------------------------------------------------------------------------ 73 | 74 | #' @export 75 | translate.svm_wavelet <- function(x, engine = x$engine, ...) { 76 | x <- parsnip::translate.default(x, engine = engine, ...) 77 | 78 | # slightly cleaner code using 79 | arg_vals <- x$method$fit$args 80 | arg_names <- names(arg_vals) 81 | 82 | # add checks to error trap or change things for this method 83 | if (x$engine == "kernlab") { 84 | 85 | # unless otherwise specified, classification models predict probabilities 86 | if (x$mode == "classification" && !any(arg_names == "prob.model")) 87 | arg_vals$prob.model <- TRUE 88 | if (x$mode == "classification" && any(arg_names == "epsilon")) 89 | arg_vals$epsilon <- NULL 90 | 91 | } 92 | 93 | x$method$fit$args <- arg_vals 94 | 95 | # worried about people using this to modify the specification 96 | x 97 | } 98 | 99 | # ------------------------------------------------------------------------------ 100 | 101 | #' @export 102 | check_args.svm_wavelet <- function(object, call = rlang::caller_env()) { 103 | invisible(object) 104 | } 105 | -------------------------------------------------------------------------------- /R/svm_tanh.R: -------------------------------------------------------------------------------- 1 | #' Hyperbolic Tangent Support Vector Machine 2 | #' @description tanh kernel for support vector machines 3 | #' @param mode regression or classification 4 | #' @param engine kernlab ksvm 5 | #' @param cost A positive number for the cost of predicting a sample within 6 | #' or on the wrong side of the margin 7 | #' @param margin A positive number for the epsilon in the SVM insensitive 8 | #' loss function (regression only) 9 | #' @param scale_factor scale parameter for tanh 10 | #' @export 11 | 12 | svm_tanh <- 13 | function(mode = "unknown", engine = "kernlab", 14 | cost = NULL, scale_factor = NULL, margin = NULL) { 15 | 16 | args <- list( 17 | cost = enquo(cost), 18 | scale_factor = enquo(scale_factor), 19 | margin = enquo(margin) 20 | ) 21 | 22 | parsnip::new_model_spec( 23 | "svm_tanh", 24 | args = args, 25 | eng_args = NULL, 26 | mode = mode, 27 | user_specified_mode = !missing(mode), 28 | method = NULL, 29 | engine = engine, 30 | user_specified_engine = !missing(engine) 31 | ) 32 | } 33 | 34 | # ------------------------------------------------------------------------------ 35 | 36 | #' @method update svm_tanh 37 | #' @rdname parsnip_update 38 | #' @export 39 | update.svm_tanh <- 40 | function(object, 41 | parameters = NULL, 42 | cost = NULL, scale_factor = NULL, margin = NULL, 43 | fresh = FALSE, 44 | ...) { 45 | 46 | args <- list( 47 | cost = enquo(cost), 48 | scale_factor = enquo(scale_factor), 49 | margin = enquo(margin) 50 | ) 51 | 52 | parsnip::update_spec( 53 | object = object, 54 | parameters = parameters, 55 | args_enquo_list = args, 56 | fresh = fresh, 57 | cls = "svm_tanh", 58 | ... 59 | ) 60 | } 61 | 62 | # ------------------------------------------------------------------------------ 63 | 64 | #' @export 65 | translate.svm_tanh <- function(x, engine = x$engine, ...) { 66 | x <- parsnip::translate.default(x, engine = engine, ...) 67 | 68 | # slightly cleaner code using 69 | arg_vals <- x$method$fit$args 70 | arg_names <- names(arg_vals) 71 | 72 | # add checks to error trap or change things for this method 73 | if (x$engine == "kernlab") { 74 | 75 | # unless otherwise specified, classification models predict probabilities 76 | if (x$mode == "classification" && !any(arg_names == "prob.model")) 77 | arg_vals$prob.model <- TRUE 78 | if (x$mode == "classification" && any(arg_names == "epsilon")) 79 | arg_vals$epsilon <- NULL 80 | 81 | # convert scale to a `kpar` argument. 82 | if (any(arg_names %in% c("scale", "offset"))) { 83 | kpar <- rlang::expr(list()) 84 | if (any(arg_names == "scale")) { 85 | kpar$scale <- arg_vals$scale 86 | arg_vals$scale <- NULL 87 | } 88 | if (any(arg_names == "offset")) { 89 | kpar$offset <- arg_vals$offset 90 | arg_vals$offset <- NULL 91 | } 92 | arg_vals$kpar <- kpar 93 | } 94 | 95 | } 96 | x$method$fit$args <- arg_vals 97 | 98 | # worried about people using this to modify the specification 99 | x 100 | } 101 | 102 | # ------------------------------------------------------------------------------ 103 | 104 | #' @export 105 | check_args.svm_tanh <- function(object, call = rlang::caller_env()) { 106 | invisible(object) 107 | } 108 | -------------------------------------------------------------------------------- /R/apd_svm_novel_detection-score.R: -------------------------------------------------------------------------------- 1 | # ---------------------- Notes ------------------------------------------ 2 | # @@@@@@@@@@@@@ one-svc novelty detection from kernlab @@@@@@@@@@@@@@@@@@@ 3 | # based entirely from tidymodels/applicable for isolation anomaly detection 4 | # repurposed for svm novelty detection 5 | # https://github.com/tidymodels/applicable/blob/develop/R/isolation-score.R 6 | # ----------------------------------------------------------------------- 7 | 8 | # ----------------------------------------------------------------------------- 9 | # ---------------------- Model function implementation ------------------------ 10 | # ----------------------------------------------------------------------------- 11 | 12 | score_apd_svm_novel_detection <- function(model, predictors) { 13 | check_kernlab() 14 | predicted_output <- kernlab::predict(model$model, data.matrix(predictors), type = "decision") 15 | predicted_output <- tibble::tibble(score = predicted_output[, 1]) 16 | 17 | # Compute percentile of new SVM scores 18 | new_pctls <- applicable:::get_new_percentile( 19 | model$pctls$score, 20 | predicted_output$score, 21 | grid = model$pctls$percentile 22 | ) 23 | predicted_output$score_pctl <- new_pctls 24 | predicted_output 25 | } 26 | 27 | # ----------------------------------------------------------------------------- 28 | # ------------------------ Model function bridge ------------------------------ 29 | # ----------------------------------------------------------------------------- 30 | 31 | score_apd_svm_novel_detection_bridge <- function(type, model, predictors) { 32 | 33 | predictions <- score_apd_svm_novel_detection(model, predictors) 34 | 35 | hardhat::validate_prediction_size(predictions, predictors) 36 | 37 | predictions 38 | } 39 | 40 | # ----------------------------------------------------------------------------- 41 | # ----------------------- Model function interface ---------------------------- 42 | # ----------------------------------------------------------------------------- 43 | 44 | #' Predict from a `apd_svm_novel_detection` 45 | #' 46 | #' @param object A `apd_svm_novel_detection` object. 47 | #' 48 | #' @param new_data A data frame or matrix of new samples. 49 | #' 50 | #' @param type A single character. The type of predictions to generate. 51 | #' Valid options are: 52 | #' 53 | #' - `"numeric"` for numeric predictions. 54 | #' 55 | #' @param ... Not used, but required for extensibility. 56 | #' 57 | #' @details About the score 58 | #' 59 | #' @return 60 | #' 61 | #' A tibble of predictions. The number of rows in the tibble is guaranteed 62 | #' to be the same as the number of rows in `new_data`. The `score` column is the 63 | #' raw prediction from [kernlab::predict()] while `score_pctl` 64 | #' compares this value to the reference distribution of the score created by 65 | #' predicting the training set. A value of _X_ means that _X_ percent of the 66 | #' training data have scores less than the predicted value. 67 | #' 68 | #' @seealso [apd_svm_novel_detection()] 69 | #' @export 70 | score.apd_svm_novel_detection <- function(object, new_data, type = "numeric", ...) { 71 | forged <- hardhat::forge(new_data, object$blueprint) 72 | rlang::arg_match(type, valid_novel_predict_types()) 73 | score_apd_svm_novel_detection_bridge(type, object, forged$predictors) 74 | } 75 | # ----------------------------------------------------------------------------- 76 | # ----------------------- Helper functions ------------------------------------ 77 | # ----------------------------------------------------------------------------- 78 | 79 | valid_novel_predict_types <- function() { 80 | c("numeric") 81 | } 82 | -------------------------------------------------------------------------------- /R/svm_anova_rbf.R: -------------------------------------------------------------------------------- 1 | #' ANOVA RBF Support Vector Machine 2 | #' @description anova rbf for support vector machines 3 | #' @param mode regression or classification 4 | #' @param engine kernlab ksvm 5 | #' @param cost A positive number for the cost of predicting a sample within 6 | #' or on the wrong side of the margin 7 | #' @param margin A positive number for the epsilon in the SVM insensitive 8 | #' loss function (regression only) 9 | #' @param degree degree parameter for anova rbf 10 | #' @param anova_rbf_sigma sigma parameter for anova rbf 11 | #' @export 12 | 13 | svm_anova_rbf <- 14 | function(mode = "unknown", engine = "kernlab", 15 | cost = NULL, anova_rbf_sigma = NULL, degree = NULL, margin = NULL) { 16 | 17 | args <- list( 18 | cost = enquo(cost), 19 | anova_rbf_sigma = enquo(anova_rbf_sigma), 20 | degree = enquo(degree), 21 | margin = enquo(margin) 22 | ) 23 | 24 | parsnip::new_model_spec( 25 | "svm_anova_rbf", 26 | args = args, 27 | eng_args = NULL, 28 | mode = mode, 29 | user_specified_mode = !missing(mode), 30 | method = NULL, 31 | engine = engine, 32 | user_specified_engine = !missing(engine) 33 | ) 34 | } 35 | 36 | # ------------------------------------------------------------------------------ 37 | 38 | #' @method update svm_anova_rbf 39 | #' @rdname parsnip_update 40 | #' @export 41 | update.svm_anova_rbf <- 42 | function(object, 43 | parameters = NULL, 44 | cost = NULL, anova_rbf_sigma = NULL, degree = NULL, margin = NULL, 45 | fresh = FALSE, 46 | ...) { 47 | 48 | args <- list( 49 | cost = enquo(cost), 50 | anova_rbf_sigma = enquo(anova_rbf_sigma), 51 | degree = enquo(degree), 52 | margin = enquo(margin) 53 | ) 54 | 55 | parsnip::update_spec( 56 | object = object, 57 | parameters = parameters, 58 | args_enquo_list = args, 59 | fresh = fresh, 60 | cls = "svm_anova_rbf", 61 | ... 62 | ) 63 | } 64 | 65 | # ------------------------------------------------------------------------------ 66 | 67 | #' @export 68 | translate.svm_anova_rbf <- function(x, engine = x$engine, ...) { 69 | x <- parsnip::translate.default(x, engine = engine, ...) 70 | 71 | # slightly cleaner code using 72 | arg_vals <- x$method$fit$args 73 | arg_names <- names(arg_vals) 74 | 75 | # add checks to error trap or change things for this method 76 | if (x$engine == "kernlab") { 77 | 78 | # unless otherwise specified, classification models predict probabilities 79 | if (x$mode == "classification" && !any(arg_names == "prob.model")) 80 | arg_vals$prob.model <- TRUE 81 | if (x$mode == "classification" && any(arg_names == "epsilon")) 82 | arg_vals$epsilon <- NULL 83 | 84 | # convert sigma and degree to a `kpar` argument. 85 | if (any(arg_names %in% c("sigma", "degree", "offset"))) { 86 | kpar <- rlang::expr(list()) 87 | if (any(arg_names == "sigma")) { 88 | kpar$sigma <- arg_vals$sigma 89 | arg_vals$sigma <- NULL 90 | } 91 | if (any(arg_names == "degree")) { 92 | kpar$degree <- arg_vals$degree 93 | arg_vals$degree <- NULL 94 | } 95 | if (any(arg_names == "offset")) { 96 | kpar$offset <- arg_vals$offset 97 | arg_vals$offset <- NULL 98 | } 99 | arg_vals$kpar <- kpar 100 | } 101 | 102 | } 103 | x$method$fit$args <- arg_vals 104 | 105 | # worried about people using this to modify the specification 106 | x 107 | } 108 | 109 | # ------------------------------------------------------------------------------ 110 | 111 | #' @export 112 | check_args.svm_anova_rbf <- function(object, call = rlang::caller_env()) { 113 | invisible(object) 114 | } 115 | -------------------------------------------------------------------------------- /R/kcca_correlate.R: -------------------------------------------------------------------------------- 1 | #' Kernel Canonical Correlation Analysis 2 | #' @description 3 | #' Computes the canonical correlation analysis in feature space. 4 | #' Kernel Canonical Correlation Analysis (KCCA) is a non-linear extension of CCA. 5 | #' Given two random variables (or datasets), KCCA aims at extracting the information which is shared by the two random variables (or datasets). 6 | #' more information found at [kernlab::kcca()] 7 | #' @param x variable or dataframe 8 | #' @param y variable or dataframe 9 | #' @param kernel a kernel to use 10 | #' @param gamma regularization parameter 11 | #' @param num_comp number of components 12 | #' @param ... pass through args for kcca function 13 | #' 14 | #' @return A kernel canonical correlation analysis data frame `kcor_df` 15 | #' @export 16 | kcca_correlate <- function(x, y = NULL, 17 | kernel = "rbfdot", 18 | gamma = 0.1, 19 | num_comp = 10, 20 | ...) { 21 | UseMethod("kcca_correlate") 22 | } 23 | 24 | 25 | #' @export 26 | kcca_correlate.default <- function(x, 27 | y = NULL, 28 | kernel = "rbfdot", 29 | gamma = 0.1, 30 | num_comp = 10, 31 | ...) { 32 | if (is.null(y)) { 33 | y <- x 34 | } 35 | 36 | if (is.data.frame(x)) { 37 | x <- data.matrix(x) 38 | } 39 | 40 | if (is.data.frame(y)) { 41 | y <- data.matrix(y) 42 | } 43 | 44 | 45 | # kcca results -------------------------------------------------------------------- 46 | kcca_res <- 47 | kernlab::kcca( 48 | x = x, 49 | y = y, 50 | kernel = kernel, 51 | gamma = gamma, 52 | ncomps = num_comp, 53 | ... 54 | ) 55 | 56 | # kcor_df ------------------------------------------------------------------------- 57 | 58 | # cleaning up results --- 59 | 60 | # canonical correlations ~~~~~~~~~~~~~~~ 61 | k_cor <- 62 | dplyr::tibble( 63 | component = paste0("component_", seq_along(kcca_res@kcor)), 64 | canonical_correlation = as.numeric(kcca_res@kcor) 65 | ) 66 | 67 | # xcoef: Projection coefficients for x in feature space (n samples x n comps) ~~~~~~~~~~~~~~~ 68 | x_coef <- 69 | as.data.frame(kcca_res@xcoef) |> 70 | dplyr::mutate(sample = dplyr::row_number()) |> 71 | tidyr::pivot_longer(cols = -sample, names_to = "component", values_to = "x_feature_space") |> 72 | dplyr::mutate(component = paste0("component_", as.numeric(substring(component, 2)))) 73 | 74 | # ycoef: Projection coefficients for y in feature space (n samples x ncomps) ~~~~~~~~~~~~~~~ 75 | y_coef <- 76 | as.data.frame(kcca_res@ycoef) |> 77 | dplyr::mutate(sample = dplyr::row_number()) |> 78 | tidyr::pivot_longer(cols = -sample, names_to = "component", values_to = "y_feature_space") |> 79 | dplyr::mutate(component = paste0("component_", as.numeric(substring(component, 2)))) 80 | 81 | # return as one large df instead of correlation table ~~~~~~~~~~~~~~~~~~~ 82 | kcor_df <- 83 | k_cor |> 84 | dplyr::left_join(x_coef, by = "component") |> 85 | dplyr::left_join(y_coef, by = c("component", "sample")) |> 86 | dplyr::mutate(component = as.numeric(gsub("component_", "", component))) |> 87 | dplyr::as_tibble() 88 | 89 | # make it a special _df class ~~~~~~~~~~~~~~~~~~~~ 90 | class(kcor_df) <- c("kcor_df", class(kcor_df)) 91 | 92 | kcor_df 93 | 94 | } 95 | 96 | 97 | #' visualize component x-y feature space 98 | #' 99 | #' @param x a kcor_df object 100 | #' @export 101 | autoplot.kcor_df <- function(x){ 102 | 103 | # x <- kcor_df 104 | 105 | x |> 106 | dplyr::mutate(component = as.factor(component)) |> 107 | ggplot2::ggplot() + 108 | geom_point(ggplot2::aes(x = x_feature_space, y = y_feature_space, color = component)) + 109 | ggplot2::facet_wrap(~component, scales = "free") + 110 | ggplot2::theme_minimal() + 111 | ggplot2::scale_color_viridis_d(option = "G", end = .8) 112 | 113 | } 114 | -------------------------------------------------------------------------------- /R/svm_string_data.R: -------------------------------------------------------------------------------- 1 | 2 | make_svm_string <- function() { 3 | 4 | parsnip::set_new_model("svm_string") 5 | 6 | parsnip::set_model_mode("svm_string", "classification") 7 | 8 | # ------------------------------------------------------------------------------ 9 | 10 | parsnip::set_model_engine("svm_string", "classification", "kernlab") 11 | parsnip::set_dependency("svm_string", "kernlab", "kernlab") 12 | parsnip::set_dependency("svm_string", "kernlab", "maize") 13 | 14 | parsnip::set_model_arg( 15 | model = "svm_string", 16 | eng = "kernlab", 17 | parsnip = "cost", 18 | original = "C", 19 | func = list(pkg = "dials", fun = "cost", range = c(-10, 5)), 20 | has_submodel = FALSE 21 | ) 22 | 23 | parsnip::set_model_arg( 24 | model = "svm_string", 25 | eng = "kernlab", 26 | parsnip = "length", 27 | original = "length", 28 | func = list(pkg = "dials", fun = "length"), 29 | has_submodel = FALSE 30 | ) 31 | 32 | parsnip::set_model_arg( 33 | model = "svm_string", 34 | eng = "kernlab", 35 | parsnip = "lambda", 36 | original = "lambda", 37 | func = list(pkg = "dials", fun = "lambda"), 38 | has_submodel = FALSE 39 | ) 40 | 41 | # TODO there are a few different stringdot 'types' 42 | # consider stringdot(type =...), not the ksvm(type =...) 43 | # parsnip::set_model_arg( 44 | # model = "svm_string", 45 | # eng = "kernlab", 46 | # parsnip = "type", 47 | # original = "type", 48 | # has_submodel = FALSE 49 | # ) 50 | 51 | parsnip::set_model_arg( 52 | model = "svm_string", 53 | eng = "kernlab", 54 | parsnip = "normalized", 55 | original = "normalized", 56 | func = list(pkg = "dials", fun = "normalized"), 57 | has_submodel = FALSE 58 | ) 59 | 60 | parsnip::set_model_arg( 61 | model = "svm_string", 62 | eng = "kernlab", 63 | parsnip = "margin", 64 | original = "epsilon", 65 | func = list(pkg = "dials", fun = "svm_margin"), 66 | has_submodel = FALSE 67 | ) 68 | 69 | 70 | parsnip::set_fit( 71 | model = "svm_string", 72 | eng = "kernlab", 73 | mode = "classification", 74 | value = list( 75 | interface = "data.frame", 76 | protect = c("x", "y"), 77 | func = c(pkg = "maize", fun = "ksvm_stringdot"), 78 | defaults = list(shrinking = TRUE, fit = TRUE) 79 | ) 80 | ) 81 | 82 | parsnip::set_encoding( 83 | model = "svm_string", 84 | eng = "kernlab", 85 | mode = "classification", 86 | options = list( 87 | predictor_indicators = "none", 88 | compute_intercept = FALSE, 89 | remove_intercept = FALSE, 90 | allow_sparse_x = FALSE 91 | ) 92 | ) 93 | 94 | 95 | parsnip::set_pred( 96 | model = "svm_string", 97 | eng = "kernlab", 98 | mode = "classification", 99 | type = "class", 100 | value = list( 101 | pre = NULL, 102 | post = NULL, 103 | func = c(pkg = "maize", fun = "predict_ksvm_stringdot_class"), 104 | args = 105 | list( 106 | object = quote(object), 107 | new_data = quote(new_data) 108 | ) 109 | ) 110 | ) 111 | 112 | parsnip::set_pred( 113 | model = "svm_string", 114 | eng = "kernlab", 115 | mode = "classification", 116 | type = "prob", 117 | value = list( 118 | pre = NULL, 119 | post = function(result, object) as_tibble(result), 120 | func = c(pkg = "maize", fun = "predict_ksvm_stringdot_prob"), 121 | args = 122 | list( 123 | object = quote(object), 124 | new_data = quote(new_data) 125 | ) 126 | ) 127 | ) 128 | 129 | parsnip::set_pred( 130 | model = "svm_string", 131 | eng = "kernlab", 132 | mode = "classification", 133 | type = "raw", 134 | value = list( 135 | pre = NULL, 136 | post = NULL, 137 | func = c(pkg = "maize", fun = "predict_ksvm_stringdot_class"), 138 | args = list(object = quote(object), new_data = quote(new_data)) 139 | ) 140 | ) 141 | } 142 | -------------------------------------------------------------------------------- /R/svm_bessel.R: -------------------------------------------------------------------------------- 1 | #' Bessel Support Vector Machine 2 | #' @description bessel kernel for support vector machines 3 | #' @param mode regression or classification 4 | #' @param engine kernlab ksvm 5 | #' @param cost A positive number for the cost of predicting a sample within 6 | #' or on the wrong side of the margin 7 | #' @param margin A positive number for the epsilon in the SVM insensitive 8 | #' loss function (regression only) 9 | #' @param degree degree parameter for bessel 10 | #' @param order order parameter for bessel 11 | #' @param bessel_sigma sigma parameter for bessel 12 | #' @export 13 | 14 | svm_bessel <- 15 | function(mode = "unknown", engine = "kernlab", 16 | cost = NULL, bessel_sigma = NULL, degree = NULL, order = NULL, margin = NULL) { 17 | 18 | args <- list( 19 | cost = enquo(cost), 20 | bessel_sigma = enquo(bessel_sigma), 21 | degree = enquo(degree), 22 | order = enquo(order), 23 | margin = enquo(margin) 24 | ) 25 | 26 | parsnip::new_model_spec( 27 | "svm_bessel", 28 | args = args, 29 | eng_args = NULL, 30 | mode = mode, 31 | user_specified_mode = !missing(mode), 32 | method = NULL, 33 | engine = engine, 34 | user_specified_engine = !missing(engine) 35 | ) 36 | } 37 | 38 | # ------------------------------------------------------------------------------ 39 | 40 | #' @method update svm_bessel 41 | #' @rdname parsnip_update 42 | #' @export 43 | update.svm_bessel <- 44 | function(object, 45 | parameters = NULL, 46 | cost = NULL, bessel_sigma = NULL, degree = NULL, order = NULL, margin = NULL, 47 | fresh = FALSE, 48 | ...) { 49 | 50 | args <- list( 51 | cost = enquo(cost), 52 | bessel_sigma = enquo(bessel_sigma), 53 | degree = enquo(degree), 54 | order = enquo(order), 55 | margin = enquo(margin) 56 | ) 57 | 58 | parsnip::update_spec( 59 | object = object, 60 | parameters = parameters, 61 | args_enquo_list = args, 62 | fresh = fresh, 63 | cls = "svm_bessel", 64 | ... 65 | ) 66 | } 67 | 68 | # ------------------------------------------------------------------------------ 69 | 70 | #' @export 71 | translate.svm_bessel <- function(x, engine = x$engine, ...) { 72 | x <- parsnip::translate.default(x, engine = engine, ...) 73 | 74 | # slightly cleaner code using 75 | arg_vals <- x$method$fit$args 76 | arg_names <- names(arg_vals) 77 | 78 | # add checks to error trap or change things for this method 79 | if (x$engine == "kernlab") { 80 | 81 | # unless otherwise specified, classification models predict probabilities 82 | if (x$mode == "classification" && !any(arg_names == "prob.model")) 83 | arg_vals$prob.model <- TRUE 84 | if (x$mode == "classification" && any(arg_names == "epsilon")) 85 | arg_vals$epsilon <- NULL 86 | 87 | # convert sigma, degree, order to a `kpar` argument. 88 | if (any(arg_names %in% c("sigma", "degree", "order", "offset"))) { 89 | kpar <- rlang::expr(list()) 90 | if (any(arg_names == "sigma")) { 91 | kpar$sigma <- arg_vals$sigma 92 | arg_vals$sigma <- NULL 93 | } 94 | if (any(arg_names == "degree")) { 95 | kpar$degree <- arg_vals$degree 96 | arg_vals$degree <- NULL 97 | } 98 | if (any(arg_names == "order")) { 99 | kpar$order <- arg_vals$order 100 | arg_vals$order <- NULL 101 | } 102 | if (any(arg_names == "offset")) { 103 | kpar$offset <- arg_vals$offset 104 | arg_vals$offset <- NULL 105 | } 106 | arg_vals$kpar <- kpar 107 | } 108 | 109 | } 110 | x$method$fit$args <- arg_vals 111 | 112 | # worried about people using this to modify the specification 113 | x 114 | } 115 | 116 | # ------------------------------------------------------------------------------ 117 | 118 | #' @export 119 | check_args.svm_bessel <- function(object, call = rlang::caller_env()) { 120 | invisible(object) 121 | } 122 | -------------------------------------------------------------------------------- /R/svm_cossim_data.R: -------------------------------------------------------------------------------- 1 | make_svm_cossim <- function() { 2 | 3 | parsnip::set_new_model("svm_cossim") 4 | 5 | parsnip::set_model_mode("svm_cossim", "classification") 6 | parsnip::set_model_mode("svm_cossim", "regression") 7 | 8 | # ------------------------------------------------------------------------------ 9 | 10 | parsnip::set_model_engine("svm_cossim", "classification", "kernlab") 11 | parsnip::set_model_engine("svm_cossim", "regression", "kernlab") 12 | parsnip::set_dependency("svm_cossim", "kernlab", "kernlab") 13 | parsnip::set_dependency("svm_cossim", "kernlab", "maize") 14 | 15 | parsnip::set_model_arg( 16 | model = "svm_cossim", 17 | eng = "kernlab", 18 | parsnip = "cost", 19 | original = "C", 20 | func = list(pkg = "dials", fun = "cost", range = c(-10, 5)), 21 | has_submodel = FALSE 22 | ) 23 | 24 | parsnip::set_model_arg( 25 | model = "svm_cossim", 26 | eng = "kernlab", 27 | parsnip = "margin", 28 | original = "epsilon", 29 | func = list(pkg = "dials", fun = "svm_margin"), 30 | has_submodel = FALSE 31 | ) 32 | 33 | parsnip::set_fit( 34 | model = "svm_cossim", 35 | eng = "kernlab", 36 | mode = "regression", 37 | value = list( 38 | interface = "formula", 39 | data = c(formula = "x", data = "data"), 40 | protect = c("x", "data"), 41 | func = c(pkg = "kernlab", fun = "ksvm"), 42 | defaults = list(kernel = cossimdot) 43 | ) 44 | ) 45 | 46 | parsnip::set_encoding( 47 | model = "svm_cossim", 48 | eng = "kernlab", 49 | mode = "regression", 50 | options = list( 51 | predictor_indicators = "none", 52 | compute_intercept = FALSE, 53 | remove_intercept = FALSE, 54 | allow_sparse_x = FALSE 55 | ) 56 | ) 57 | 58 | parsnip::set_fit( 59 | model = "svm_cossim", 60 | eng = "kernlab", 61 | mode = "classification", 62 | value = list( 63 | interface = "formula", 64 | data = c(formula = "x", data = "data"), 65 | protect = c("x", "data"), 66 | func = c(pkg = "kernlab", fun = "ksvm"), 67 | defaults = list(kernel = cossimdot) 68 | ) 69 | ) 70 | 71 | parsnip::set_encoding( 72 | model = "svm_cossim", 73 | eng = "kernlab", 74 | mode = "classification", 75 | options = list( 76 | predictor_indicators = "none", 77 | compute_intercept = FALSE, 78 | remove_intercept = FALSE, 79 | allow_sparse_x = FALSE 80 | ) 81 | ) 82 | 83 | parsnip::set_pred( 84 | model = "svm_cossim", 85 | eng = "kernlab", 86 | mode = "regression", 87 | type = "numeric", 88 | value = list( 89 | pre = NULL, 90 | post = \(results, object) { 91 | results[,1] 92 | }, 93 | func = c(pkg = "kernlab", fun = "predict"), 94 | args = 95 | list( 96 | object = quote(object$fit), 97 | newdata = quote(new_data), 98 | type = "response" 99 | ) 100 | ) 101 | ) 102 | 103 | parsnip::set_pred( 104 | model = "svm_cossim", 105 | eng = "kernlab", 106 | mode = "regression", 107 | type = "raw", 108 | value = list( 109 | pre = NULL, 110 | post = NULL, 111 | func = c(pkg = "kernlab", fun = "predict"), 112 | args = list(object = quote(object$fit), newdata = quote(new_data)) 113 | ) 114 | ) 115 | 116 | parsnip::set_pred( 117 | model = "svm_cossim", 118 | eng = "kernlab", 119 | mode = "classification", 120 | type = "class", 121 | value = list( 122 | pre = NULL, 123 | post = NULL, 124 | func = c(pkg = "kernlab", fun = "predict"), 125 | args = 126 | list( 127 | object = quote(object$fit), 128 | newdata = quote(new_data), 129 | type = "response" 130 | ) 131 | ) 132 | ) 133 | 134 | parsnip::set_pred( 135 | model = "svm_cossim", 136 | eng = "kernlab", 137 | mode = "classification", 138 | type = "prob", 139 | value = list( 140 | pre = NULL, 141 | post = function(result, object) as_tibble(result), 142 | func = c(pkg = "kernlab", fun = "predict"), 143 | args = 144 | list( 145 | object = quote(object$fit), 146 | newdata = quote(new_data), 147 | type = "probabilities" 148 | ) 149 | ) 150 | ) 151 | 152 | parsnip::set_pred( 153 | model = "svm_cossim", 154 | eng = "kernlab", 155 | mode = "classification", 156 | type = "raw", 157 | value = list( 158 | pre = NULL, 159 | post = NULL, 160 | func = c(pkg = "kernlab", fun = "predict"), 161 | args = list(object = quote(object$fit), newdata = quote(new_data)) 162 | ) 163 | ) 164 | } 165 | -------------------------------------------------------------------------------- /R/svm_sorensen_data.R: -------------------------------------------------------------------------------- 1 | make_svm_sorensen <- function() { 2 | 3 | parsnip::set_new_model("svm_sorensen") 4 | 5 | parsnip::set_model_mode("svm_sorensen", "classification") 6 | parsnip::set_model_mode("svm_sorensen", "regression") 7 | 8 | # ------------------------------------------------------------------------------ 9 | 10 | parsnip::set_model_engine("svm_sorensen", "classification", "kernlab") 11 | parsnip::set_model_engine("svm_sorensen", "regression", "kernlab") 12 | parsnip::set_dependency("svm_sorensen", "kernlab", "kernlab") 13 | parsnip::set_dependency("svm_sorensen", "kernlab", "maize") 14 | 15 | parsnip::set_model_arg( 16 | model = "svm_sorensen", 17 | eng = "kernlab", 18 | parsnip = "cost", 19 | original = "C", 20 | func = list(pkg = "dials", fun = "cost", range = c(-10, 5)), 21 | has_submodel = FALSE 22 | ) 23 | 24 | parsnip::set_model_arg( 25 | model = "svm_sorensen", 26 | eng = "kernlab", 27 | parsnip = "margin", 28 | original = "epsilon", 29 | func = list(pkg = "dials", fun = "svm_margin"), 30 | has_submodel = FALSE 31 | ) 32 | 33 | parsnip::set_fit( 34 | model = "svm_sorensen", 35 | eng = "kernlab", 36 | mode = "regression", 37 | value = list( 38 | interface = "formula", 39 | data = c(formula = "x", data = "data"), 40 | protect = c("x", "data"), 41 | func = c(pkg = "kernlab", fun = "ksvm"), 42 | defaults = list(kernel = sorsendot) 43 | ) 44 | ) 45 | 46 | parsnip::set_encoding( 47 | model = "svm_sorensen", 48 | eng = "kernlab", 49 | mode = "regression", 50 | options = list( 51 | predictor_indicators = "none", 52 | compute_intercept = FALSE, 53 | remove_intercept = FALSE, 54 | allow_sparse_x = FALSE 55 | ) 56 | ) 57 | 58 | parsnip::set_fit( 59 | model = "svm_sorensen", 60 | eng = "kernlab", 61 | mode = "classification", 62 | value = list( 63 | interface = "formula", 64 | data = c(formula = "x", data = "data"), 65 | protect = c("x", "data"), 66 | func = c(pkg = "kernlab", fun = "ksvm"), 67 | defaults = list(kernel = sorsendot) 68 | ) 69 | ) 70 | 71 | parsnip::set_encoding( 72 | model = "svm_sorensen", 73 | eng = "kernlab", 74 | mode = "classification", 75 | options = list( 76 | predictor_indicators = "none", 77 | compute_intercept = FALSE, 78 | remove_intercept = FALSE, 79 | allow_sparse_x = FALSE 80 | ) 81 | ) 82 | 83 | parsnip::set_pred( 84 | model = "svm_sorensen", 85 | eng = "kernlab", 86 | mode = "regression", 87 | type = "numeric", 88 | value = list( 89 | pre = NULL, 90 | post = \(results, object) { 91 | results[,1] 92 | }, 93 | func = c(pkg = "kernlab", fun = "predict"), 94 | args = 95 | list( 96 | object = quote(object$fit), 97 | newdata = quote(new_data), 98 | type = "response" 99 | ) 100 | ) 101 | ) 102 | 103 | parsnip::set_pred( 104 | model = "svm_sorensen", 105 | eng = "kernlab", 106 | mode = "regression", 107 | type = "raw", 108 | value = list( 109 | pre = NULL, 110 | post = NULL, 111 | func = c(pkg = "kernlab", fun = "predict"), 112 | args = list(object = quote(object$fit), newdata = quote(new_data)) 113 | ) 114 | ) 115 | 116 | parsnip::set_pred( 117 | model = "svm_sorensen", 118 | eng = "kernlab", 119 | mode = "classification", 120 | type = "class", 121 | value = list( 122 | pre = NULL, 123 | post = NULL, 124 | func = c(pkg = "kernlab", fun = "predict"), 125 | args = 126 | list( 127 | object = quote(object$fit), 128 | newdata = quote(new_data), 129 | type = "response" 130 | ) 131 | ) 132 | ) 133 | 134 | parsnip::set_pred( 135 | model = "svm_sorensen", 136 | eng = "kernlab", 137 | mode = "classification", 138 | type = "prob", 139 | value = list( 140 | pre = NULL, 141 | post = function(result, object) as_tibble(result), 142 | func = c(pkg = "kernlab", fun = "predict"), 143 | args = 144 | list( 145 | object = quote(object$fit), 146 | newdata = quote(new_data), 147 | type = "probabilities" 148 | ) 149 | ) 150 | ) 151 | 152 | parsnip::set_pred( 153 | model = "svm_sorensen", 154 | eng = "kernlab", 155 | mode = "classification", 156 | type = "raw", 157 | value = list( 158 | pre = NULL, 159 | post = NULL, 160 | func = c(pkg = "kernlab", fun = "predict"), 161 | args = list(object = quote(object$fit), newdata = quote(new_data)) 162 | ) 163 | ) 164 | } 165 | -------------------------------------------------------------------------------- /R/svm_tanimoto_data.R: -------------------------------------------------------------------------------- 1 | make_svm_tanimoto <- function() { 2 | 3 | parsnip::set_new_model("svm_tanimoto") 4 | 5 | parsnip::set_model_mode("svm_tanimoto", "classification") 6 | parsnip::set_model_mode("svm_tanimoto", "regression") 7 | 8 | # ------------------------------------------------------------------------------ 9 | 10 | parsnip::set_model_engine("svm_tanimoto", "classification", "kernlab") 11 | parsnip::set_model_engine("svm_tanimoto", "regression", "kernlab") 12 | parsnip::set_dependency("svm_tanimoto", "kernlab", "kernlab") 13 | parsnip::set_dependency("svm_tanimoto", "kernlab", "maize") 14 | 15 | parsnip::set_model_arg( 16 | model = "svm_tanimoto", 17 | eng = "kernlab", 18 | parsnip = "cost", 19 | original = "C", 20 | func = list(pkg = "dials", fun = "cost", range = c(-10, 5)), 21 | has_submodel = FALSE 22 | ) 23 | 24 | parsnip::set_model_arg( 25 | model = "svm_tanimoto", 26 | eng = "kernlab", 27 | parsnip = "margin", 28 | original = "epsilon", 29 | func = list(pkg = "dials", fun = "svm_margin"), 30 | has_submodel = FALSE 31 | ) 32 | 33 | parsnip::set_fit( 34 | model = "svm_tanimoto", 35 | eng = "kernlab", 36 | mode = "regression", 37 | value = list( 38 | interface = "formula", 39 | data = c(formula = "x", data = "data"), 40 | protect = c("x", "data"), 41 | func = c(pkg = "kernlab", fun = "ksvm"), 42 | defaults = list(kernel = tanimotodot) 43 | ) 44 | ) 45 | 46 | parsnip::set_encoding( 47 | model = "svm_tanimoto", 48 | eng = "kernlab", 49 | mode = "regression", 50 | options = list( 51 | predictor_indicators = "none", 52 | compute_intercept = FALSE, 53 | remove_intercept = FALSE, 54 | allow_sparse_x = FALSE 55 | ) 56 | ) 57 | 58 | parsnip::set_fit( 59 | model = "svm_tanimoto", 60 | eng = "kernlab", 61 | mode = "classification", 62 | value = list( 63 | interface = "formula", 64 | data = c(formula = "x", data = "data"), 65 | protect = c("x", "data"), 66 | func = c(pkg = "kernlab", fun = "ksvm"), 67 | defaults = list(kernel = tanimotodot) 68 | ) 69 | ) 70 | 71 | parsnip::set_encoding( 72 | model = "svm_tanimoto", 73 | eng = "kernlab", 74 | mode = "classification", 75 | options = list( 76 | predictor_indicators = "none", 77 | compute_intercept = FALSE, 78 | remove_intercept = FALSE, 79 | allow_sparse_x = FALSE 80 | ) 81 | ) 82 | 83 | parsnip::set_pred( 84 | model = "svm_tanimoto", 85 | eng = "kernlab", 86 | mode = "regression", 87 | type = "numeric", 88 | value = list( 89 | pre = NULL, 90 | post = \(results, object) { 91 | results[,1] 92 | }, 93 | func = c(pkg = "kernlab", fun = "predict"), 94 | args = 95 | list( 96 | object = quote(object$fit), 97 | newdata = quote(new_data), 98 | type = "response" 99 | ) 100 | ) 101 | ) 102 | 103 | parsnip::set_pred( 104 | model = "svm_tanimoto", 105 | eng = "kernlab", 106 | mode = "regression", 107 | type = "raw", 108 | value = list( 109 | pre = NULL, 110 | post = NULL, 111 | func = c(pkg = "kernlab", fun = "predict"), 112 | args = list(object = quote(object$fit), newdata = quote(new_data)) 113 | ) 114 | ) 115 | 116 | parsnip::set_pred( 117 | model = "svm_tanimoto", 118 | eng = "kernlab", 119 | mode = "classification", 120 | type = "class", 121 | value = list( 122 | pre = NULL, 123 | post = NULL, 124 | func = c(pkg = "kernlab", fun = "predict"), 125 | args = 126 | list( 127 | object = quote(object$fit), 128 | newdata = quote(new_data), 129 | type = "response" 130 | ) 131 | ) 132 | ) 133 | 134 | parsnip::set_pred( 135 | model = "svm_tanimoto", 136 | eng = "kernlab", 137 | mode = "classification", 138 | type = "prob", 139 | value = list( 140 | pre = NULL, 141 | post = function(result, object) as_tibble(result), 142 | func = c(pkg = "kernlab", fun = "predict"), 143 | args = 144 | list( 145 | object = quote(object$fit), 146 | newdata = quote(new_data), 147 | type = "probabilities" 148 | ) 149 | ) 150 | ) 151 | 152 | parsnip::set_pred( 153 | model = "svm_tanimoto", 154 | eng = "kernlab", 155 | mode = "classification", 156 | type = "raw", 157 | value = list( 158 | pre = NULL, 159 | post = NULL, 160 | func = c(pkg = "kernlab", fun = "predict"), 161 | args = list(object = quote(object$fit), newdata = quote(new_data)) 162 | ) 163 | ) 164 | } 165 | -------------------------------------------------------------------------------- /R/svm_spline_data.R: -------------------------------------------------------------------------------- 1 | make_svm_spline <- function() { 2 | 3 | parsnip::set_new_model("svm_spline") 4 | 5 | parsnip::set_model_mode("svm_spline", "classification") 6 | parsnip::set_model_mode("svm_spline", "regression") 7 | # ------------------------------------------------------------------------------ 8 | 9 | parsnip::set_model_engine("svm_spline", "classification", "kernlab") 10 | parsnip::set_model_engine("svm_spline", "regression", "kernlab") 11 | parsnip::set_dependency("svm_spline", "kernlab", "kernlab") 12 | parsnip::set_dependency("svm_spline", "kernlab", "maize") 13 | 14 | parsnip::set_model_arg( 15 | model = "svm_spline", 16 | eng = "kernlab", 17 | parsnip = "cost", 18 | original = "C", 19 | func = list(pkg = "dials", fun = "cost", range = c(-10, 5)), 20 | has_submodel = FALSE 21 | ) 22 | 23 | parsnip::set_model_arg( 24 | model = "svm_spline", 25 | eng = "kernlab", 26 | parsnip = "margin", 27 | original = "epsilon", 28 | func = list(pkg = "dials", fun = "svm_margin"), 29 | has_submodel = FALSE 30 | ) 31 | 32 | parsnip::set_fit( 33 | model = "svm_spline", 34 | eng = "kernlab", 35 | mode = "regression", 36 | value = list( 37 | interface = "formula", 38 | data = c(formula = "x", data = "data"), 39 | protect = c("x", "data"), 40 | func = c(pkg = "kernlab", fun = "ksvm"), 41 | defaults = list(kernel = "splinedot") 42 | ) 43 | ) 44 | 45 | parsnip::set_fit( 46 | model = "svm_spline", 47 | eng = "kernlab", 48 | mode = "classification", 49 | value = list( 50 | interface = "formula", 51 | data = c(formula = "x", data = "data"), 52 | protect = c("x", "data"), 53 | func = c(pkg = "kernlab", fun = "ksvm"), 54 | defaults = list(kernel = "splinedot") 55 | ) 56 | ) 57 | 58 | parsnip::set_encoding( 59 | model = "svm_spline", 60 | eng = "kernlab", 61 | mode = "regression", 62 | options = list( 63 | predictor_indicators = "none", 64 | compute_intercept = FALSE, 65 | remove_intercept = FALSE, 66 | allow_sparse_x = FALSE 67 | ) 68 | ) 69 | 70 | parsnip::set_pred( 71 | model = "svm_spline", 72 | eng = "kernlab", 73 | mode = "regression", 74 | type = "numeric", 75 | value = list( 76 | pre = NULL, 77 | post = \(results, object) { 78 | results[,1] 79 | }, 80 | func = c(pkg = "kernlab", fun = "predict"), 81 | args = 82 | list( 83 | object = quote(object$fit), 84 | newdata = quote(new_data), 85 | type = "response" 86 | ) 87 | ) 88 | ) 89 | 90 | parsnip::set_pred( 91 | model = "svm_spline", 92 | eng = "kernlab", 93 | mode = "regression", 94 | type = "raw", 95 | value = list( 96 | pre = NULL, 97 | post = NULL, 98 | func = c(pkg = "kernlab", fun = "predict"), 99 | args = list(object = quote(object$fit), newdata = quote(new_data)) 100 | ) 101 | ) 102 | 103 | parsnip::set_encoding( 104 | model = "svm_spline", 105 | eng = "kernlab", 106 | mode = "classification", 107 | options = list( 108 | predictor_indicators = "none", 109 | compute_intercept = FALSE, 110 | remove_intercept = FALSE, 111 | allow_sparse_x = FALSE 112 | ) 113 | ) 114 | 115 | parsnip::set_pred( 116 | model = "svm_spline", 117 | eng = "kernlab", 118 | mode = "classification", 119 | type = "class", 120 | value = list( 121 | pre = NULL, 122 | post = NULL, 123 | func = c(pkg = "kernlab", fun = "predict"), 124 | args = 125 | list( 126 | object = quote(object$fit), 127 | newdata = quote(new_data), 128 | type = "response" 129 | ) 130 | ) 131 | ) 132 | 133 | parsnip::set_pred( 134 | model = "svm_spline", 135 | eng = "kernlab", 136 | mode = "classification", 137 | type = "prob", 138 | value = list( 139 | pre = NULL, 140 | post = function(result, object) as_tibble(result), 141 | func = c(pkg = "kernlab", fun = "predict"), 142 | args = 143 | list( 144 | object = quote(object$fit), 145 | newdata = quote(new_data), 146 | type = "probabilities" 147 | ) 148 | ) 149 | ) 150 | 151 | parsnip::set_pred( 152 | model = "svm_spline", 153 | eng = "kernlab", 154 | mode = "classification", 155 | type = "raw", 156 | value = list( 157 | pre = NULL, 158 | post = NULL, 159 | func = c(pkg = "kernlab", fun = "predict"), 160 | args = list(object = quote(object$fit), newdata = quote(new_data)) 161 | ) 162 | ) 163 | } 164 | -------------------------------------------------------------------------------- /R/svm_cauchy_data.R: -------------------------------------------------------------------------------- 1 | make_svm_cauchy <- function() { 2 | 3 | parsnip::set_new_model("svm_cauchy") 4 | 5 | parsnip::set_model_mode("svm_cauchy", "classification") 6 | parsnip::set_model_mode("svm_cauchy", "regression") 7 | 8 | # ------------------------------------------------------------------------------ 9 | 10 | parsnip::set_model_engine("svm_cauchy", "classification", "kernlab") 11 | parsnip::set_model_engine("svm_cauchy", "regression", "kernlab") 12 | parsnip::set_dependency("svm_cauchy", "kernlab", "kernlab") 13 | parsnip::set_dependency("svm_cauchy", "kernlab", "maize") 14 | 15 | parsnip::set_model_arg( 16 | model = "svm_cauchy", 17 | eng = "kernlab", 18 | parsnip = "cost", 19 | original = "C", 20 | func = list(pkg = "dials", fun = "cost", range = c(-10, 5)), 21 | has_submodel = FALSE 22 | ) 23 | 24 | parsnip::set_model_arg( 25 | model = "svm_cauchy", 26 | eng = "kernlab", 27 | parsnip = "margin", 28 | original = "epsilon", 29 | func = list(pkg = "dials", fun = "svm_margin"), 30 | has_submodel = FALSE 31 | ) 32 | 33 | parsnip::set_model_arg( 34 | model = "svm_cauchy", 35 | eng = "kernlab", 36 | parsnip = "sigma", 37 | original = "sigma", 38 | func = list(pkg = "dials", fun = "sigma"), 39 | has_submodel = FALSE 40 | ) 41 | 42 | parsnip::set_fit( 43 | model = "svm_cauchy", 44 | eng = "kernlab", 45 | mode = "regression", 46 | value = list( 47 | interface = "formula", 48 | data = c(formula = "x", data = "data"), 49 | protect = c("x", "data"), 50 | func = c(pkg = "kernlab", fun = "ksvm"), 51 | defaults = list(kernel = cauchydot) 52 | ) 53 | ) 54 | 55 | parsnip::set_encoding( 56 | model = "svm_cauchy", 57 | eng = "kernlab", 58 | mode = "regression", 59 | options = list( 60 | predictor_indicators = "none", 61 | compute_intercept = FALSE, 62 | remove_intercept = FALSE, 63 | allow_sparse_x = FALSE 64 | ) 65 | ) 66 | 67 | parsnip::set_fit( 68 | model = "svm_cauchy", 69 | eng = "kernlab", 70 | mode = "classification", 71 | value = list( 72 | interface = "formula", 73 | data = c(formula = "x", data = "data"), 74 | protect = c("x", "data"), 75 | func = c(pkg = "kernlab", fun = "ksvm"), 76 | defaults = list(kernel = cauchydot) 77 | ) 78 | ) 79 | 80 | parsnip::set_encoding( 81 | model = "svm_cauchy", 82 | eng = "kernlab", 83 | mode = "classification", 84 | options = list( 85 | predictor_indicators = "none", 86 | compute_intercept = FALSE, 87 | remove_intercept = FALSE, 88 | allow_sparse_x = FALSE 89 | ) 90 | ) 91 | 92 | parsnip::set_pred( 93 | model = "svm_cauchy", 94 | eng = "kernlab", 95 | mode = "regression", 96 | type = "numeric", 97 | value = list( 98 | pre = NULL, 99 | post = \(results, object) { 100 | results[,1] 101 | }, 102 | func = c(pkg = "kernlab", fun = "predict"), 103 | args = 104 | list( 105 | object = quote(object$fit), 106 | newdata = quote(new_data), 107 | type = "response" 108 | ) 109 | ) 110 | ) 111 | 112 | parsnip::set_pred( 113 | model = "svm_cauchy", 114 | eng = "kernlab", 115 | mode = "regression", 116 | type = "raw", 117 | value = list( 118 | pre = NULL, 119 | post = NULL, 120 | func = c(pkg = "kernlab", fun = "predict"), 121 | args = list(object = quote(object$fit), newdata = quote(new_data)) 122 | ) 123 | ) 124 | 125 | parsnip::set_pred( 126 | model = "svm_cauchy", 127 | eng = "kernlab", 128 | mode = "classification", 129 | type = "class", 130 | value = list( 131 | pre = NULL, 132 | post = NULL, 133 | func = c(pkg = "kernlab", fun = "predict"), 134 | args = 135 | list( 136 | object = quote(object$fit), 137 | newdata = quote(new_data), 138 | type = "response" 139 | ) 140 | ) 141 | ) 142 | 143 | parsnip::set_pred( 144 | model = "svm_cauchy", 145 | eng = "kernlab", 146 | mode = "classification", 147 | type = "prob", 148 | value = list( 149 | pre = NULL, 150 | post = function(result, object) as_tibble(result), 151 | func = c(pkg = "kernlab", fun = "predict"), 152 | args = 153 | list( 154 | object = quote(object$fit), 155 | newdata = quote(new_data), 156 | type = "probabilities" 157 | ) 158 | ) 159 | ) 160 | 161 | parsnip::set_pred( 162 | model = "svm_cauchy", 163 | eng = "kernlab", 164 | mode = "classification", 165 | type = "raw", 166 | value = list( 167 | pre = NULL, 168 | post = NULL, 169 | func = c(pkg = "kernlab", fun = "predict"), 170 | args = list(object = quote(object$fit), newdata = quote(new_data)) 171 | ) 172 | ) 173 | } 174 | -------------------------------------------------------------------------------- /R/svm_fourier_data.R: -------------------------------------------------------------------------------- 1 | make_svm_fourier <- function() { 2 | 3 | parsnip::set_new_model("svm_fourier") 4 | 5 | parsnip::set_model_mode("svm_fourier", "classification") 6 | parsnip::set_model_mode("svm_fourier", "regression") 7 | 8 | # ------------------------------------------------------------------------------ 9 | 10 | parsnip::set_model_engine("svm_fourier", "classification", "kernlab") 11 | parsnip::set_model_engine("svm_fourier", "regression", "kernlab") 12 | parsnip::set_dependency("svm_fourier", "kernlab", "kernlab") 13 | parsnip::set_dependency("svm_fourier", "kernlab", "maize") 14 | 15 | parsnip::set_model_arg( 16 | model = "svm_fourier", 17 | eng = "kernlab", 18 | parsnip = "cost", 19 | original = "C", 20 | func = list(pkg = "dials", fun = "cost", range = c(-10, 5)), 21 | has_submodel = FALSE 22 | ) 23 | 24 | parsnip::set_model_arg( 25 | model = "svm_fourier", 26 | eng = "kernlab", 27 | parsnip = "margin", 28 | original = "epsilon", 29 | func = list(pkg = "dials", fun = "svm_margin"), 30 | has_submodel = FALSE 31 | ) 32 | 33 | parsnip::set_model_arg( 34 | model = "svm_fourier", 35 | eng = "kernlab", 36 | parsnip = "sigma", 37 | original = "sigma", 38 | func = list(pkg = "dials", fun = "sigma"), 39 | has_submodel = FALSE 40 | ) 41 | 42 | parsnip::set_fit( 43 | model = "svm_fourier", 44 | eng = "kernlab", 45 | mode = "regression", 46 | value = list( 47 | interface = "formula", 48 | data = c(formula = "x", data = "data"), 49 | protect = c("x", "data"), 50 | func = c(pkg = "kernlab", fun = "ksvm"), 51 | defaults = list(kernel = fourierdot) 52 | ) 53 | ) 54 | 55 | parsnip::set_encoding( 56 | model = "svm_fourier", 57 | eng = "kernlab", 58 | mode = "regression", 59 | options = list( 60 | predictor_indicators = "none", 61 | compute_intercept = FALSE, 62 | remove_intercept = FALSE, 63 | allow_sparse_x = FALSE 64 | ) 65 | ) 66 | 67 | parsnip::set_fit( 68 | model = "svm_fourier", 69 | eng = "kernlab", 70 | mode = "classification", 71 | value = list( 72 | interface = "formula", 73 | data = c(formula = "x", data = "data"), 74 | protect = c("x", "data"), 75 | func = c(pkg = "kernlab", fun = "ksvm"), 76 | defaults = list(kernel = fourierdot) 77 | ) 78 | ) 79 | 80 | parsnip::set_encoding( 81 | model = "svm_fourier", 82 | eng = "kernlab", 83 | mode = "classification", 84 | options = list( 85 | predictor_indicators = "none", 86 | compute_intercept = FALSE, 87 | remove_intercept = FALSE, 88 | allow_sparse_x = FALSE 89 | ) 90 | ) 91 | 92 | parsnip::set_pred( 93 | model = "svm_fourier", 94 | eng = "kernlab", 95 | mode = "regression", 96 | type = "numeric", 97 | value = list( 98 | pre = NULL, 99 | post = \(results, object) { 100 | results[,1] 101 | }, 102 | func = c(pkg = "kernlab", fun = "predict"), 103 | args = 104 | list( 105 | object = quote(object$fit), 106 | newdata = quote(new_data), 107 | type = "response" 108 | ) 109 | ) 110 | ) 111 | 112 | parsnip::set_pred( 113 | model = "svm_fourier", 114 | eng = "kernlab", 115 | mode = "regression", 116 | type = "raw", 117 | value = list( 118 | pre = NULL, 119 | post = NULL, 120 | func = c(pkg = "kernlab", fun = "predict"), 121 | args = list(object = quote(object$fit), newdata = quote(new_data)) 122 | ) 123 | ) 124 | 125 | parsnip::set_pred( 126 | model = "svm_fourier", 127 | eng = "kernlab", 128 | mode = "classification", 129 | type = "class", 130 | value = list( 131 | pre = NULL, 132 | post = NULL, 133 | func = c(pkg = "kernlab", fun = "predict"), 134 | args = 135 | list( 136 | object = quote(object$fit), 137 | newdata = quote(new_data), 138 | type = "response" 139 | ) 140 | ) 141 | ) 142 | 143 | parsnip::set_pred( 144 | model = "svm_fourier", 145 | eng = "kernlab", 146 | mode = "classification", 147 | type = "prob", 148 | value = list( 149 | pre = NULL, 150 | post = function(result, object) as_tibble(result), 151 | func = c(pkg = "kernlab", fun = "predict"), 152 | args = 153 | list( 154 | object = quote(object$fit), 155 | newdata = quote(new_data), 156 | type = "probabilities" 157 | ) 158 | ) 159 | ) 160 | 161 | parsnip::set_pred( 162 | model = "svm_fourier", 163 | eng = "kernlab", 164 | mode = "classification", 165 | type = "raw", 166 | value = list( 167 | pre = NULL, 168 | post = NULL, 169 | func = c(pkg = "kernlab", fun = "predict"), 170 | args = list(object = quote(object$fit), newdata = quote(new_data)) 171 | ) 172 | ) 173 | } 174 | -------------------------------------------------------------------------------- /R/svm_tstudent_data.R: -------------------------------------------------------------------------------- 1 | make_svm_tstudent <- function() { 2 | 3 | parsnip::set_new_model("svm_tstudent") 4 | 5 | parsnip::set_model_mode("svm_tstudent", "classification") 6 | parsnip::set_model_mode("svm_tstudent", "regression") 7 | 8 | # ------------------------------------------------------------------------------ 9 | 10 | parsnip::set_model_engine("svm_tstudent", "classification", "kernlab") 11 | parsnip::set_model_engine("svm_tstudent", "regression", "kernlab") 12 | parsnip::set_dependency("svm_tstudent", "kernlab", "kernlab") 13 | parsnip::set_dependency("svm_tstudent", "kernlab", "maize") 14 | 15 | parsnip::set_model_arg( 16 | model = "svm_tstudent", 17 | eng = "kernlab", 18 | parsnip = "cost", 19 | original = "C", 20 | func = list(pkg = "dials", fun = "cost", range = c(-10, 5)), 21 | has_submodel = FALSE 22 | ) 23 | 24 | parsnip::set_model_arg( 25 | model = "svm_tstudent", 26 | eng = "kernlab", 27 | parsnip = "margin", 28 | original = "epsilon", 29 | func = list(pkg = "dials", fun = "svm_margin"), 30 | has_submodel = FALSE 31 | ) 32 | 33 | parsnip::set_model_arg( 34 | model = "svm_tstudent", 35 | eng = "kernlab", 36 | parsnip = "degree", 37 | original = "degree", 38 | func = list(pkg = "dials", fun = "degree"), 39 | has_submodel = FALSE 40 | ) 41 | 42 | parsnip::set_fit( 43 | model = "svm_tstudent", 44 | eng = "kernlab", 45 | mode = "regression", 46 | value = list( 47 | interface = "formula", 48 | data = c(formula = "x", data = "data"), 49 | protect = c("x", "data"), 50 | func = c(pkg = "kernlab", fun = "ksvm"), 51 | defaults = list(kernel = tstudentdot) 52 | ) 53 | ) 54 | 55 | parsnip::set_encoding( 56 | model = "svm_tstudent", 57 | eng = "kernlab", 58 | mode = "regression", 59 | options = list( 60 | predictor_indicators = "none", 61 | compute_intercept = FALSE, 62 | remove_intercept = FALSE, 63 | allow_sparse_x = FALSE 64 | ) 65 | ) 66 | 67 | parsnip::set_fit( 68 | model = "svm_tstudent", 69 | eng = "kernlab", 70 | mode = "classification", 71 | value = list( 72 | interface = "formula", 73 | data = c(formula = "x", data = "data"), 74 | protect = c("x", "data"), 75 | func = c(pkg = "kernlab", fun = "ksvm"), 76 | defaults = list(kernel = tstudentdot) 77 | ) 78 | ) 79 | 80 | parsnip::set_encoding( 81 | model = "svm_tstudent", 82 | eng = "kernlab", 83 | mode = "classification", 84 | options = list( 85 | predictor_indicators = "none", 86 | compute_intercept = FALSE, 87 | remove_intercept = FALSE, 88 | allow_sparse_x = FALSE 89 | ) 90 | ) 91 | 92 | parsnip::set_pred( 93 | model = "svm_tstudent", 94 | eng = "kernlab", 95 | mode = "regression", 96 | type = "numeric", 97 | value = list( 98 | pre = NULL, 99 | post = \(results, object) { 100 | results[,1] 101 | }, 102 | func = c(pkg = "kernlab", fun = "predict"), 103 | args = 104 | list( 105 | object = quote(object$fit), 106 | newdata = quote(new_data), 107 | type = "response" 108 | ) 109 | ) 110 | ) 111 | 112 | parsnip::set_pred( 113 | model = "svm_tstudent", 114 | eng = "kernlab", 115 | mode = "regression", 116 | type = "raw", 117 | value = list( 118 | pre = NULL, 119 | post = NULL, 120 | func = c(pkg = "kernlab", fun = "predict"), 121 | args = list(object = quote(object$fit), newdata = quote(new_data)) 122 | ) 123 | ) 124 | 125 | parsnip::set_pred( 126 | model = "svm_tstudent", 127 | eng = "kernlab", 128 | mode = "classification", 129 | type = "class", 130 | value = list( 131 | pre = NULL, 132 | post = NULL, 133 | func = c(pkg = "kernlab", fun = "predict"), 134 | args = 135 | list( 136 | object = quote(object$fit), 137 | newdata = quote(new_data), 138 | type = "response" 139 | ) 140 | ) 141 | ) 142 | 143 | parsnip::set_pred( 144 | model = "svm_tstudent", 145 | eng = "kernlab", 146 | mode = "classification", 147 | type = "prob", 148 | value = list( 149 | pre = NULL, 150 | post = function(result, object) as_tibble(result), 151 | func = c(pkg = "kernlab", fun = "predict"), 152 | args = 153 | list( 154 | object = quote(object$fit), 155 | newdata = quote(new_data), 156 | type = "probabilities" 157 | ) 158 | ) 159 | ) 160 | 161 | parsnip::set_pred( 162 | model = "svm_tstudent", 163 | eng = "kernlab", 164 | mode = "classification", 165 | type = "raw", 166 | value = list( 167 | pre = NULL, 168 | post = NULL, 169 | func = c(pkg = "kernlab", fun = "predict"), 170 | args = list(object = quote(object$fit), newdata = quote(new_data)) 171 | ) 172 | ) 173 | } 174 | -------------------------------------------------------------------------------- /R/svm_tanh_data.R: -------------------------------------------------------------------------------- 1 | make_svm_tanh <- function() { 2 | 3 | parsnip::set_new_model("svm_tanh") 4 | 5 | parsnip::set_model_mode("svm_tanh", "classification") 6 | parsnip::set_model_mode("svm_tanh", "regression") 7 | 8 | # ------------------------------------------------------------------------------ 9 | 10 | parsnip::set_model_engine("svm_tanh", "classification", "kernlab") 11 | parsnip::set_model_engine("svm_tanh", "regression", "kernlab") 12 | parsnip::set_dependency("svm_tanh", "kernlab", "kernlab") 13 | parsnip::set_dependency("svm_tanh", "kernlab", "maize") 14 | 15 | parsnip::set_model_arg( 16 | model = "svm_tanh", 17 | eng = "kernlab", 18 | parsnip = "cost", 19 | original = "C", 20 | func = list(pkg = "dials", fun = "cost", range = c(-10, 5)), 21 | has_submodel = FALSE 22 | ) 23 | 24 | parsnip::set_model_arg( 25 | model = "svm_tanh", 26 | eng = "kernlab", 27 | parsnip = "scale_factor", 28 | original = "scale", 29 | func = list(pkg = "dials", fun = "scale_factor"), 30 | has_submodel = FALSE 31 | ) 32 | 33 | parsnip::set_model_arg( 34 | model = "svm_tanh", 35 | eng = "kernlab", 36 | parsnip = "margin", 37 | original = "epsilon", 38 | func = list(pkg = "dials", fun = "svm_margin"), 39 | has_submodel = FALSE 40 | ) 41 | 42 | parsnip::set_fit( 43 | model = "svm_tanh", 44 | eng = "kernlab", 45 | mode = "regression", 46 | value = list( 47 | interface = "formula", 48 | data = c(formula = "x", data = "data"), 49 | protect = c("x", "data"), 50 | func = c(pkg = "kernlab", fun = "ksvm"), 51 | defaults = list(kernel = "tanhdot") 52 | ) 53 | ) 54 | 55 | parsnip::set_encoding( 56 | model = "svm_tanh", 57 | eng = "kernlab", 58 | mode = "regression", 59 | options = list( 60 | predictor_indicators = "none", 61 | compute_intercept = FALSE, 62 | remove_intercept = FALSE, 63 | allow_sparse_x = FALSE 64 | ) 65 | ) 66 | 67 | parsnip::set_fit( 68 | model = "svm_tanh", 69 | eng = "kernlab", 70 | mode = "classification", 71 | value = list( 72 | interface = "formula", 73 | data = c(formula = "x", data = "data"), 74 | protect = c("x", "data"), 75 | func = c(pkg = "kernlab", fun = "ksvm"), 76 | defaults = list(kernel = "tanhdot") 77 | ) 78 | ) 79 | 80 | parsnip::set_encoding( 81 | model = "svm_tanh", 82 | eng = "kernlab", 83 | mode = "classification", 84 | options = list( 85 | predictor_indicators = "none", 86 | compute_intercept = FALSE, 87 | remove_intercept = FALSE, 88 | allow_sparse_x = FALSE 89 | ) 90 | ) 91 | 92 | parsnip::set_pred( 93 | model = "svm_tanh", 94 | eng = "kernlab", 95 | mode = "regression", 96 | type = "numeric", 97 | value = list( 98 | pre = NULL, 99 | post = \(results, object) { 100 | results[,1] 101 | }, 102 | func = c(pkg = "kernlab", fun = "predict"), 103 | args = 104 | list( 105 | object = quote(object$fit), 106 | newdata = quote(new_data), 107 | type = "response" 108 | ) 109 | ) 110 | ) 111 | 112 | parsnip::set_pred( 113 | model = "svm_tanh", 114 | eng = "kernlab", 115 | mode = "regression", 116 | type = "raw", 117 | value = list( 118 | pre = NULL, 119 | post = NULL, 120 | func = c(pkg = "kernlab", fun = "predict"), 121 | args = list(object = quote(object$fit), newdata = quote(new_data)) 122 | ) 123 | ) 124 | 125 | parsnip::set_pred( 126 | model = "svm_tanh", 127 | eng = "kernlab", 128 | mode = "classification", 129 | type = "class", 130 | value = list( 131 | pre = NULL, 132 | post = NULL, 133 | func = c(pkg = "kernlab", fun = "predict"), 134 | args = 135 | list( 136 | object = quote(object$fit), 137 | newdata = quote(new_data), 138 | type = "response" 139 | ) 140 | ) 141 | ) 142 | 143 | parsnip::set_pred( 144 | model = "svm_tanh", 145 | eng = "kernlab", 146 | mode = "classification", 147 | type = "prob", 148 | value = list( 149 | pre = NULL, 150 | post = function(result, object) as_tibble(result), 151 | func = c(pkg = "kernlab", fun = "predict"), 152 | args = 153 | list( 154 | object = quote(object$fit), 155 | newdata = quote(new_data), 156 | type = "probabilities" 157 | ) 158 | ) 159 | ) 160 | 161 | parsnip::set_pred( 162 | model = "svm_tanh", 163 | eng = "kernlab", 164 | mode = "classification", 165 | type = "raw", 166 | value = list( 167 | pre = NULL, 168 | post = NULL, 169 | func = c(pkg = "kernlab", fun = "predict"), 170 | args = list(object = quote(object$fit), newdata = quote(new_data)) 171 | ) 172 | ) 173 | } 174 | -------------------------------------------------------------------------------- /vignettes/pre-processors-in-maize.qmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "pre-processors in maize" 3 | vignette: > 4 | %\VignetteIndexEntry{pre-processors-in-maize} 5 | %\VignetteEngine{quarto::html} 6 | %\VignetteEncoding{UTF-8} 7 | knitr: 8 | opts_chunk: 9 | collapse: true 10 | comment: '#>' 11 | --- 12 | 13 | ### kernel recipes 14 | 15 | {recipes} is a package within the tidymodels ecosystem. The main goal of recipes is to create *steps* which are pre-processors for datasets. These steps can be useful for simplifying large datasets into key components, dropping NA values, creating lags, and more. These steps create new features that can then be used for a regression or classification model. Recipes for {maize} focus on specialty kernel methods for extracting components/features, thanks to {kernlab} and {mildsvm} packages. 16 | 17 | Below we will apply the recipe steps in maize directly to corn data. 18 | 19 | ```{r} 20 | #| label: setup 21 | #| echo: false 22 | #| message: false 23 | #| include: false 24 | 25 | library(kernlab) 26 | library(mildsvm) 27 | 28 | library(maize) 29 | library(recipes) 30 | 31 | library(ggplot2) 32 | library(tidyr) 33 | 34 | corn_data <- maize::corn_data 35 | 36 | ``` 37 | 38 | #### Corn Dataset 39 | 40 | a glimpse at the original dataset. The corn dataset has three types of corn that vary in height and kernel size. 41 | ```{r} 42 | corn_data |> 43 | ggplot(aes(x = kernel_size, y = height, color = type, shape = type)) + 44 | geom_point(size = 3) + 45 | theme_minimal() + 46 | labs(title = 'Original Unprocessed Data', 47 | subtitle = "corn dataset") + 48 | scale_color_viridis_d(end = .9) + 49 | theme(legend.position = "top") 50 | ``` 51 | 52 | 53 | #### Kernel PCA 54 | 55 | Kernel Principal Components Analysis is a nonlinear form of principal component analysis. Laplacian and tanh kernels are supported with `step_kpca_*`. 56 | 57 | ##### Laplace PCA 58 | ```{r} 59 | kpca_data <- 60 | recipe(type ~ ., corn_data) |> 61 | step_kpca_laplace(height, num_comp = 6) |> 62 | prep() |> 63 | bake(new_data = corn_data) 64 | 65 | kpca_data |> 66 | tidyr::pivot_longer(-c(kernel_size, type)) |> 67 | ggplot(aes(x = kernel_size, y = value, color = type, shape = type)) + 68 | geom_point(size = 3) + 69 | facet_wrap(~name, scales = 'free') + 70 | theme_minimal() + 71 | labs(title = 'Laplace kPCA Processed Data', 72 | subtitle = "corn dataset") + 73 | scale_color_viridis_d(end = .9) + 74 | theme(legend.position = "top") 75 | ``` 76 | 77 | #### Kernel Hebbian Algorithm 78 | 79 | Kernel Hebbian Algorithm is a nonlinear iterative algorithm for principal component analysis. Laplacian and tanh kernels are supported with `step_kha_*`. 80 | 81 | ##### Laplace Hebbian 82 | ```{r} 83 | kha_data <- 84 | recipe(type ~ ., corn_data) |> 85 | step_kha_laplace(height, num_comp = 3) |> 86 | prep() |> 87 | bake(new_data = corn_data) 88 | 89 | kha_data |> 90 | tidyr::pivot_longer(-c(kernel_size, type)) |> 91 | ggplot(aes(x = kernel_size, y = value, color = type, shape = type)) + 92 | geom_point(size = 3, alpha = .5) + 93 | facet_wrap(~name, scales = 'free') + 94 | theme_minimal() + 95 | labs(title = 'Laplace kha Processed Data', 96 | subtitle = "corn dataset") + 97 | scale_color_viridis_d(end = .9) + 98 | theme(legend.position = "top") 99 | ``` 100 | 101 | 102 | #### Kernel Feature Analysis 103 | 104 | The Kernel Feature Analysis algorithm is an algorithm for extracting structure from possibly high-dimensional data sets. Similar to kpca a new basis for the data is found. The data can then be projected on the new basis. The laplacian kernel is supported with `step_kfa_laplace`. 105 | 106 | ##### Laplace Feature Analysis 107 | ```{r} 108 | kfa_data <- 109 | recipe(type ~ ., corn_data) |> 110 | step_kfa_laplace(height, kernel_size, num_comp = 9, sigma = .87) |> 111 | prep() |> 112 | bake(new_data = corn_data) 113 | 114 | kfa_data |> 115 | tidyr::pivot_longer(-c(kFA1, type)) |> 116 | ggplot(aes(x = kFA1, y = value, color = type, shape = type)) + 117 | geom_point(size = 3, alpha = .5) + 118 | facet_wrap(~name, scales = 'free') + 119 | theme_minimal() + 120 | labs(title = 'Laplace kfa Processed Data', 121 | subtitle = "corn dataset") + 122 | scale_color_viridis_d(end = .9) + 123 | theme(legend.position = "top") 124 | ``` 125 | 126 | 127 | #### Nystrom Kernel Feature Map 128 | 129 | Use Nyström method, `step_kfm_nystrom` to fit a feature map that approximates the 'radial' kernel. 130 | 131 | ```{r} 132 | kfm_data <- 133 | recipe(type ~ ., corn_data) |> 134 | step_kfm_nystrom(height, kernel_size, r = 10, sigma = .01) |> 135 | prep() |> 136 | bake(new_data = corn_data) 137 | 138 | kfm_data |> 139 | tidyr::pivot_longer(-c(kFM01, type)) |> 140 | ggplot(aes(x = kFM01, y = value, color = type, shape = type)) + 141 | geom_point(size = 3, alpha = .5) + 142 | facet_wrap(~name, scales = 'free') + 143 | theme_minimal() + 144 | labs(title = 'Nystrom KFM RBF Processed Data', 145 | subtitle = "corn dataset") + 146 | scale_color_viridis_d(end = .9) + 147 | theme(legend.position = "top") 148 | ``` 149 | 150 | -------------------------------------------------------------------------------- /R/svm_laplace_data.R: -------------------------------------------------------------------------------- 1 | make_svm_laplace <- function() { 2 | 3 | parsnip::set_new_model("svm_laplace") 4 | 5 | parsnip::set_model_mode("svm_laplace", "classification") 6 | parsnip::set_model_mode("svm_laplace", "regression") 7 | 8 | # ------------------------------------------------------------------------------ 9 | 10 | parsnip::set_model_engine("svm_laplace", "classification", "kernlab") 11 | parsnip::set_model_engine("svm_laplace", "regression", "kernlab") 12 | parsnip::set_dependency("svm_laplace", "kernlab", "kernlab") 13 | parsnip::set_dependency("svm_laplace", "kernlab", "maize") 14 | 15 | parsnip::set_model_arg( 16 | model = "svm_laplace", 17 | eng = "kernlab", 18 | parsnip = "cost", 19 | original = "C", 20 | func = list(pkg = "dials", fun = "cost", range = c(-10, 5)), 21 | has_submodel = FALSE 22 | ) 23 | 24 | parsnip::set_model_arg( 25 | model = "svm_laplace", 26 | eng = "kernlab", 27 | parsnip = "margin", 28 | original = "epsilon", 29 | func = list(pkg = "dials", fun = "svm_margin"), 30 | has_submodel = FALSE 31 | ) 32 | 33 | parsnip::set_model_arg( 34 | model = "svm_laplace", 35 | eng = "kernlab", 36 | parsnip = "laplace_sigma", 37 | original = "sigma", 38 | func = list(pkg = "dials", fun = "svm_margin"), 39 | has_submodel = FALSE 40 | ) 41 | 42 | parsnip::set_fit( 43 | model = "svm_laplace", 44 | eng = "kernlab", 45 | mode = "regression", 46 | value = list( 47 | interface = "formula", 48 | data = c(formula = "x", data = "data"), 49 | protect = c("x", "data"), 50 | func = c(pkg = "kernlab", fun = "ksvm"), 51 | defaults = list(kernel = "laplacedot") 52 | ) 53 | ) 54 | 55 | parsnip::set_fit( 56 | model = "svm_laplace", 57 | eng = "kernlab", 58 | mode = "classification", 59 | value = list( 60 | interface = "formula", 61 | data = c(formula = "x", data = "data"), 62 | protect = c("x", "data"), 63 | func = c(pkg = "kernlab", fun = "ksvm"), 64 | defaults = list(kernel = "laplacedot") 65 | ) 66 | ) 67 | 68 | parsnip::set_encoding( 69 | model = "svm_laplace", 70 | eng = "kernlab", 71 | mode = "regression", 72 | options = list( 73 | predictor_indicators = "none", 74 | compute_intercept = FALSE, 75 | remove_intercept = FALSE, 76 | allow_sparse_x = FALSE 77 | ) 78 | ) 79 | 80 | svm_reg_post <- function(results, object) { 81 | results[,1] 82 | } 83 | parsnip::set_pred( 84 | model = "svm_laplace", 85 | eng = "kernlab", 86 | mode = "regression", 87 | type = "numeric", 88 | value = list( 89 | pre = NULL, 90 | post = svm_reg_post, 91 | func = c(pkg = "kernlab", fun = "predict"), 92 | args = 93 | list( 94 | object = quote(object$fit), 95 | newdata = quote(new_data), 96 | type = "response" 97 | ) 98 | ) 99 | ) 100 | 101 | parsnip::set_pred( 102 | model = "svm_laplace", 103 | eng = "kernlab", 104 | mode = "regression", 105 | type = "raw", 106 | value = list( 107 | pre = NULL, 108 | post = NULL, 109 | func = c(pkg = "kernlab", fun = "predict"), 110 | args = list(object = quote(object$fit), newdata = quote(new_data)) 111 | ) 112 | ) 113 | 114 | parsnip::set_encoding( 115 | model = "svm_laplace", 116 | eng = "kernlab", 117 | mode = "classification", 118 | options = list( 119 | predictor_indicators = "none", 120 | compute_intercept = FALSE, 121 | remove_intercept = FALSE, 122 | allow_sparse_x = FALSE 123 | ) 124 | ) 125 | 126 | parsnip::set_pred( 127 | model = "svm_laplace", 128 | eng = "kernlab", 129 | mode = "classification", 130 | type = "class", 131 | value = list( 132 | pre = NULL, 133 | post = NULL, 134 | func = c(pkg = "kernlab", fun = "predict"), 135 | args = 136 | list( 137 | object = quote(object$fit), 138 | newdata = quote(new_data), 139 | type = "response" 140 | ) 141 | ) 142 | ) 143 | 144 | parsnip::set_pred( 145 | model = "svm_laplace", 146 | eng = "kernlab", 147 | mode = "classification", 148 | type = "prob", 149 | value = list( 150 | pre = NULL, 151 | post = function(result, object) as_tibble(result), 152 | func = c(pkg = "kernlab", fun = "predict"), 153 | args = 154 | list( 155 | object = quote(object$fit), 156 | newdata = quote(new_data), 157 | type = "probabilities" 158 | ) 159 | ) 160 | ) 161 | 162 | parsnip::set_pred( 163 | model = "svm_laplace", 164 | eng = "kernlab", 165 | mode = "classification", 166 | type = "raw", 167 | value = list( 168 | pre = NULL, 169 | post = NULL, 170 | func = c(pkg = "kernlab", fun = "predict"), 171 | args = list(object = quote(object$fit), newdata = quote(new_data)) 172 | ) 173 | ) 174 | } 175 | -------------------------------------------------------------------------------- /vignettes/glass-gem.qmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "glass-gem" 3 | vignette: > 4 | %\VignetteIndexEntry{glass-gem} 5 | %\VignetteEngine{quarto::html} 6 | %\VignetteEncoding{UTF-8} 7 | knitr: 8 | opts_chunk: 9 | collapse: true 10 | comment: '#>' 11 | --- 12 | 13 | ```{r} 14 | #| label: setup 15 | #| message: false 16 | #| warning: false 17 | 18 | options(repos = c(CRAN = "https://cloud.r-project.org")) 19 | install.packages("devtools") 20 | devtools::install_github("tidymodels/applicable@develop") 21 | devtools::install_cran("isotree") 22 | 23 | library(applicable) 24 | library(ggplot2) 25 | library(kernlab) 26 | library(maize) 27 | library(dplyr) 28 | 29 | # heldout novel/anomaly type 30 | corn_glass_gem <- data.frame( 31 | height = c(85, 75, 95), 32 | kernel_size = c(4., 3., 3), 33 | type = c("Rainbow") 34 | ) 35 | 36 | set.seed(31415) 37 | corn_train <- corn_data |> dplyr::sample_frac(.90) 38 | corn_test <- corn_data |> dplyr::anti_join(corn_train) 39 | 40 | # add the novel type to the test data 41 | corn_test <- corn_test |> dplyr::bind_rows(corn_glass_gem) 42 | ``` 43 | 44 | 45 | # {maize} for novelty detection 46 | 47 | An extension of {applicable}: 48 | *"There are times when a model’s prediction should be taken with some skepticism. For example, if a new data point is substantially different from the training set, its predicted value may be suspect. In chemistry, it is not uncommon to create an “applicability domain” model that measures the amount of potential extrapolation new samples have from the training set. applicable contains different methods to measure how much a new data point is an extrapolation from the original data (if at all)."* 49 | 50 | Within {applicable}, there lies an isolation forest technique for anomaly detection. SVMs are also known for novelty detection and outlier selection. 51 | 52 | 53 | Below both the isolation forest and one-SVC methods are shown. Note that the corn testing data has a new type added to it. "Rainbow" corn has been added and differs from the other three corn types. Below will showcase how both methods pick up on the new type. 54 | 55 | #### isolation forest score method 56 | 57 | The isolation forest method is based on isotree and is the base code used for the SVM method shown in the next section. 58 | 59 | ```{r} 60 | if_mod <- apd_isolation(corn_train |> dplyr::select(-type), ntrees = 10, nthreads = 1) 61 | if_mod 62 | 63 | isolation_scores <- score(if_mod, corn_test |> dplyr::select(-type)) 64 | head(isolation_scores) 65 | ``` 66 | 67 | 68 | ```{r} 69 | corn_test |> 70 | bind_cols(isolation_scores) |> 71 | ggplot() + 72 | geom_point(aes(x = kernel_size, y = height, color = score, shape = type)) + 73 | theme_minimal() + 74 | scale_color_viridis_c(option = "B", end = .8) + 75 | labs(title = "isolation forest method") 76 | ``` 77 | 78 | #### SVM novelty detection score method 79 | 80 | This implementation is based on one-class-svc (novelty) detection method found in kernlab. 81 | 82 | ```{r} 83 | 84 | svm_mod <- apd_svm_novel_detection(corn_train |> dplyr::select(-type), kernel = "rbfdot", nu = .1) 85 | svm_mod 86 | 87 | novel_scores <- score(svm_mod, corn_test |> dplyr::select(-type)) 88 | head(novel_scores) 89 | ``` 90 | 91 | 92 | ```{r} 93 | corn_test |> 94 | bind_cols(novel_scores) |> 95 | ggplot() + 96 | geom_point(aes(x = kernel_size, y = height, color = score, shape = type)) + 97 | theme_minimal() + 98 | scale_color_viridis_c(option = "B", end = .8, direction = -1) + 99 | labs(title = "SVM novelty detection method") 100 | ``` 101 | 102 | # {maize} for kernel canonical correlation analysis 103 | 104 | Based on the tidymodels' {corrr} package: 105 | *"corrr is a package for exploring correlations in R. It focuses on creating and working with data frames of correlations (instead of matrices) that can be easily explored via corrr functions or by leveraging tools like those in the tidyverse."* 106 | 107 | {maize} aims to do a similar approach but for kernlab's kcca function. This method is an non-linear extension of canonical correlation analysis and differs from CCA. Instead of finding linear combinations of variables that maximize correlation between two sets, KCCA maps data to a high-dimensional feature space using a kernel function and then applies CCA in that space. 108 | 109 | #### kernel canonical correlation analysis 110 | 111 | Kernel Canonical Correlation Analysis (KCCA) is a non-linear extension of CCA and will handle comparisons between two variables or datasets, (x, y). 112 | 113 | The analysis workflow is shown below: 114 | 115 | ```{r} 116 | corn_set_one <- corn_data |> dplyr::sample_frac(.50) 117 | corn_set_two <- corn_data |> dplyr::anti_join(corn_set_one) 118 | 119 | maize_kcca <- 120 | kcca_correlate(x = corn_set_one, 121 | y = corn_set_two, 122 | num_comp = 6) 123 | 124 | maize_kcca |> 125 | str() 126 | ``` 127 | 128 | kernel canonical correlations: 129 | ```{r} 130 | maize_kcca |> 131 | dplyr::group_by(component) |> 132 | dplyr::slice(1) |> 133 | dplyr::select(component, canonical_correlation) 134 | ``` 135 | 136 | visualizing the feature space of the KCCA feature space: 137 | ```{r} 138 | maize_kcca |> autoplot() 139 | ``` 140 | 141 | -------------------------------------------------------------------------------- /R/svm_anova_rbf_data.R: -------------------------------------------------------------------------------- 1 | make_svm_anova_rbf <- function() { 2 | 3 | parsnip::set_new_model("svm_anova_rbf") 4 | 5 | parsnip::set_model_mode("svm_anova_rbf", "classification") 6 | parsnip::set_model_mode("svm_anova_rbf", "regression") 7 | 8 | # ------------------------------------------------------------------------------ 9 | 10 | parsnip::set_model_engine("svm_anova_rbf", "classification", "kernlab") 11 | parsnip::set_model_engine("svm_anova_rbf", "regression", "kernlab") 12 | parsnip::set_dependency("svm_anova_rbf", "kernlab", "kernlab") 13 | parsnip::set_dependency("svm_anova_rbf", "kernlab", "maize") 14 | 15 | parsnip::set_model_arg( 16 | model = "svm_anova_rbf", 17 | eng = "kernlab", 18 | parsnip = "cost", 19 | original = "C", 20 | func = list(pkg = "dials", fun = "cost", range = c(-10, 5)), 21 | has_submodel = FALSE 22 | ) 23 | 24 | parsnip::set_model_arg( 25 | model = "svm_anova_rbf", 26 | eng = "kernlab", 27 | parsnip = "degree", 28 | original = "degree", 29 | func = list(pkg = "dials", fun = "degree_int"), 30 | has_submodel = FALSE 31 | ) 32 | 33 | parsnip::set_model_arg( 34 | model = "svm_anova_rbf", 35 | eng = "kernlab", 36 | parsnip = "rbf_sigma", 37 | original = "sigma", 38 | func = list(pkg = "dials", fun = "rbf_sigma"), 39 | has_submodel = FALSE 40 | ) 41 | 42 | parsnip::set_model_arg( 43 | model = "svm_anova_rbf", 44 | eng = "kernlab", 45 | parsnip = "margin", 46 | original = "epsilon", 47 | func = list(pkg = "dials", fun = "svm_margin"), 48 | has_submodel = FALSE 49 | ) 50 | 51 | parsnip::set_fit( 52 | model = "svm_anova_rbf", 53 | eng = "kernlab", 54 | mode = "regression", 55 | value = list( 56 | interface = "formula", 57 | data = c(formula = "x", data = "data"), 58 | protect = c("x", "data"), 59 | func = c(pkg = "kernlab", fun = "ksvm"), 60 | defaults = list(kernel = "anovadot") 61 | ) 62 | ) 63 | 64 | parsnip::set_encoding( 65 | model = "svm_anova_rbf", 66 | eng = "kernlab", 67 | mode = "regression", 68 | options = list( 69 | predictor_indicators = "none", 70 | compute_intercept = FALSE, 71 | remove_intercept = FALSE, 72 | allow_sparse_x = FALSE 73 | ) 74 | ) 75 | 76 | parsnip::set_fit( 77 | model = "svm_anova_rbf", 78 | eng = "kernlab", 79 | mode = "classification", 80 | value = list( 81 | interface = "formula", 82 | data = c(formula = "x", data = "data"), 83 | protect = c("x", "data"), 84 | func = c(pkg = "kernlab", fun = "ksvm"), 85 | defaults = list(kernel = "anovadot") 86 | ) 87 | ) 88 | 89 | parsnip::set_encoding( 90 | model = "svm_anova_rbf", 91 | eng = "kernlab", 92 | mode = "classification", 93 | options = list( 94 | predictor_indicators = "none", 95 | compute_intercept = FALSE, 96 | remove_intercept = FALSE, 97 | allow_sparse_x = FALSE 98 | ) 99 | ) 100 | 101 | parsnip::set_pred( 102 | model = "svm_anova_rbf", 103 | eng = "kernlab", 104 | mode = "regression", 105 | type = "numeric", 106 | value = list( 107 | pre = NULL, 108 | post = \(results, object) { 109 | results[,1] 110 | }, 111 | func = c(pkg = "kernlab", fun = "predict"), 112 | args = 113 | list( 114 | object = quote(object$fit), 115 | newdata = quote(new_data), 116 | type = "response" 117 | ) 118 | ) 119 | ) 120 | 121 | parsnip::set_pred( 122 | model = "svm_anova_rbf", 123 | eng = "kernlab", 124 | mode = "regression", 125 | type = "raw", 126 | value = list( 127 | pre = NULL, 128 | post = NULL, 129 | func = c(pkg = "kernlab", fun = "predict"), 130 | args = list(object = quote(object$fit), newdata = quote(new_data)) 131 | ) 132 | ) 133 | 134 | parsnip::set_pred( 135 | model = "svm_anova_rbf", 136 | eng = "kernlab", 137 | mode = "classification", 138 | type = "class", 139 | value = list( 140 | pre = NULL, 141 | post = NULL, 142 | func = c(pkg = "kernlab", fun = "predict"), 143 | args = 144 | list( 145 | object = quote(object$fit), 146 | newdata = quote(new_data), 147 | type = "response" 148 | ) 149 | ) 150 | ) 151 | 152 | parsnip::set_pred( 153 | model = "svm_anova_rbf", 154 | eng = "kernlab", 155 | mode = "classification", 156 | type = "prob", 157 | value = list( 158 | pre = NULL, 159 | post = function(result, object) as_tibble(result), 160 | func = c(pkg = "kernlab", fun = "predict"), 161 | args = 162 | list( 163 | object = quote(object$fit), 164 | newdata = quote(new_data), 165 | type = "probabilities" 166 | ) 167 | ) 168 | ) 169 | 170 | parsnip::set_pred( 171 | model = "svm_anova_rbf", 172 | eng = "kernlab", 173 | mode = "classification", 174 | type = "raw", 175 | value = list( 176 | pre = NULL, 177 | post = NULL, 178 | func = c(pkg = "kernlab", fun = "predict"), 179 | args = list(object = quote(object$fit), newdata = quote(new_data)) 180 | ) 181 | ) 182 | } 183 | -------------------------------------------------------------------------------- /vignettes/ensembling_weak_learners.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "ensembling weak learners in maize" 3 | output: rmarkdown::html_vignette 4 | vignette: > 5 | %\VignetteIndexEntry{ensembling weak learners in maize} 6 | %\VignetteEngine{knitr::rmarkdown} 7 | %\VignetteEncoding{UTF-8} 8 | --- 9 | 10 | ```{r, include = FALSE} 11 | knitr::opts_chunk$set( 12 | collapse = TRUE, 13 | comment = "#>" 14 | ) 15 | ``` 16 | 17 | ```{r setup} 18 | library(ggplot2) 19 | library(parsnip) 20 | library(maize) 21 | 22 | # binary-classification [1,0] 23 | corn_df <- corn_data |> 24 | dplyr::mutate(type = as.character(type)) |> 25 | dplyr::filter(type %in% c("Sweet", "Popcorn")) |> 26 | dplyr::mutate(type = factor(type)) |> 27 | dplyr::mutate( 28 | type = factor(type, levels = c("Sweet", "Popcorn"), labels = c("0", "1")) 29 | ) 30 | 31 | set.seed(31415) 32 | corn_train <- corn_df |> dplyr::sample_frac(.80) |> as.data.frame() 33 | corn_test <- corn_df |> dplyr::anti_join(corn_train) |> as.data.frame() 34 | 35 | 36 | # use this later to create a classification field 37 | # ----------------------------------------------- 38 | kernel_min <- corn_df$kernel_size |> min() 39 | kernel_max <- corn_df$kernel_size |> max() 40 | kernel_vec <- seq(kernel_min, kernel_max, by = 1) 41 | height_min <- corn_df$height |> min() 42 | height_max <- corn_df$height |> max() 43 | height_vec <- seq(height_min, height_max, by = 1) 44 | corn_grid <- expand.grid(kernel_size = kernel_vec, height = height_vec) 45 | # ----------------------------------------------- 46 | 47 | 48 | ``` 49 | 50 | 51 | ### Boosting & Bagging with {ebmc} & {maize} 52 | 53 | The {ebmc} package supports **binary classification** tasks, where the response variable is encoded as **[1, 0]**. The package provides implementations of several boosting and bagging methods tailored for imbalanced datasets. A handful of these have been ported to {maize} including: `ebmc::ub()`, `ebmc::rus()`, and `ebmc::adam2()`, which are configured to use `e1071::svm()` as the base learning algorithm for ensemble modeling. In {maize}, the current kernel supported is a **radial basis function**. 54 | 55 | *Extending {baguette}'s implementation for SVM's may be added at a later date but note the backend is currently {ebmc} and the technique differs.* 56 | 57 | #### Random Under Sampling (RUS) Bagging with SVMs 58 | 59 | ```{r} 60 | # model params -- 61 | bagged_svm_spec <- 62 | bag_svm_rbf(num_learners = 50, 63 | imb_ratio = 1) |> 64 | set_mode("classification") |> 65 | set_engine("ebmc") 66 | 67 | # fit -- 68 | bag_svm_class_fit <- bagged_svm_spec |> fit(type ~ ., data = corn_train) 69 | 70 | # predictions -- 71 | preds <- predict(bag_svm_class_fit, corn_grid, "class") 72 | pred_grid <- corn_grid |> cbind(preds) 73 | 74 | # plot -- 75 | corn_test |> 76 | ggplot() + 77 | geom_tile(inherit.aes = FALSE, 78 | data = pred_grid, 79 | aes(x = kernel_size, y = height, fill = .pred_class), 80 | alpha = .8) + 81 | geom_point(aes(x = kernel_size, y = height, color = type, shape = type), size = 3) + 82 | theme_minimal() + 83 | labs(title = "RUS Bagging with RBF SVMs", 84 | subtitle = "corn class prediction") + 85 | scale_fill_viridis_d() + 86 | scale_color_manual(values = c("violet", "orange")) 87 | 88 | ``` 89 | 90 | #### Random Under Sampling with Boosting, RUSBoost with SVMs 91 | 92 | ```{r} 93 | # model params -- 94 | boost_svm_spec <- 95 | rus_boost_svm_rbf(num_learners = 50, 96 | imb_ratio = 1) |> 97 | set_mode("classification") |> 98 | set_engine("ebmc") 99 | 100 | # fit -- 101 | boost_svm_class_fit <- boost_svm_spec |> fit(type ~ ., data = corn_train) 102 | 103 | # predictions -- 104 | preds <- predict(boost_svm_class_fit, corn_grid, "class") 105 | pred_grid <- corn_grid |> cbind(preds) 106 | 107 | # plot -- 108 | corn_test |> 109 | ggplot() + 110 | geom_tile(inherit.aes = FALSE, 111 | data = pred_grid, 112 | aes(x = kernel_size, y = height, fill = .pred_class), 113 | alpha = .8) + 114 | geom_point(aes(x = kernel_size, y = height, color = type, shape = type), size = 3) + 115 | theme_minimal() + 116 | labs(title = "RUS Boosting with RBF SVMs", 117 | subtitle = "corn class prediction") + 118 | scale_fill_viridis_d() + 119 | scale_color_manual(values = c("violet", "orange")) 120 | 121 | ``` 122 | 123 | #### Adaptive Boosting, AdaBoost with SVMs 124 | 125 | ```{r} 126 | # model params -- 127 | adaboost_svm_spec <- 128 | ada_boost_svm_rbf(num_learners = 50, 129 | imb_ratio = 1) |> 130 | set_mode("classification") |> 131 | set_engine("ebmc") 132 | 133 | # fit -- 134 | adaboost_svm_class_fit <- adaboost_svm_spec |> fit(type ~ ., data = corn_train) 135 | 136 | # predictions -- 137 | preds <- predict(adaboost_svm_class_fit, corn_grid, "class") 138 | pred_grid <- corn_grid |> cbind(preds) 139 | 140 | # plot -- 141 | corn_test |> 142 | ggplot() + 143 | geom_tile(inherit.aes = FALSE, 144 | data = pred_grid, 145 | aes(x = kernel_size, y = height, fill = .pred_class), 146 | alpha = .8) + 147 | geom_point(aes(x = kernel_size, y = height, color = type, shape = type), size = 3) + 148 | theme_minimal() + 149 | labs(title = "AdaBoosting with RBF SVMs", 150 | subtitle = "corn class prediction") + 151 | scale_fill_viridis_d() + 152 | scale_color_manual(values = c("violet", "orange")) 153 | 154 | ``` 155 | -------------------------------------------------------------------------------- /R/step_kpca_tanh.R: -------------------------------------------------------------------------------- 1 | #' Laplacian function kernel PCA signal extraction 2 | #' 3 | #' `step_kpca_tanh()` creates a *specification* of a recipe step that will 4 | #' convert numeric data into one or more principal components using a tanh 5 | #' kernel basis expansion. 6 | #' @inheritParams step_kpca_laplace 7 | #' @param scale_factor A numeric value for the tanh function parameter. 8 | #' @param res An S4 [kernlab::kpca()] object is stored 9 | #' here once this preprocessing step has be trained by 10 | #' [prep()]. 11 | #' @family multivariate transformation steps 12 | #' @export 13 | step_kpca_tanh <- 14 | function(recipe, 15 | ..., 16 | role = "predictor", 17 | trained = FALSE, 18 | num_comp = 5, 19 | res = NULL, 20 | columns = NULL, 21 | scale_factor = 0.2, 22 | offset = 0, 23 | prefix = "kPC", 24 | keep_original_cols = FALSE, 25 | skip = FALSE, 26 | id = rand_id("kpca_tanh")) { 27 | recipes_pkg_check(required_pkgs.step_kpca_tanh()) 28 | 29 | add_step( 30 | recipe, 31 | step_kpca_tanh_new( 32 | terms = enquos(...), 33 | role = role, 34 | trained = trained, 35 | num_comp = num_comp, 36 | res = res, 37 | columns = columns, 38 | scale_factor = scale_factor, 39 | offset = offset, 40 | prefix = prefix, 41 | keep_original_cols = keep_original_cols, 42 | skip = skip, 43 | id = id 44 | ) 45 | ) 46 | } 47 | 48 | step_kpca_tanh_new <- 49 | function(terms, role, trained, num_comp, res, columns, scale_factor, offset, prefix, 50 | keep_original_cols, skip, id) { 51 | step( 52 | subclass = "kpca_tanh", 53 | terms = terms, 54 | role = role, 55 | trained = trained, 56 | num_comp = num_comp, 57 | res = res, 58 | columns = columns, 59 | scale_factor = scale_factor, 60 | offset = offset, 61 | prefix = prefix, 62 | keep_original_cols = keep_original_cols, 63 | skip = skip, 64 | id = id 65 | ) 66 | } 67 | 68 | #' @export 69 | prep.step_kpca_tanh <- function(x, training, info = NULL, ...) { 70 | col_names <- recipes_eval_select(x$terms, training, info) 71 | check_type(training[, col_names], types = c("double", "integer")) 72 | 73 | if (x$num_comp > 0 && length(col_names) > 0) { 74 | cl <- 75 | rlang::call2( 76 | "kpca", 77 | .ns = "kernlab", 78 | x = rlang::expr(as.matrix(training[, col_names])), 79 | features = x$num_comp, 80 | kernel = "tanhdot", 81 | th = 1e-20, # req low thres for tanh 82 | kpar = list(scale = x$scale_factor, offset = x$offset) 83 | ) 84 | kprc <- try(rlang::eval_tidy(cl), silent = TRUE) 85 | if (inherits(kprc, "try-error")) { 86 | cli::cli_abort(c( 87 | x = "Failed with error:", 88 | i = as.character(kprc) 89 | )) 90 | } 91 | } else { 92 | kprc <- NULL 93 | } 94 | 95 | step_kpca_tanh_new( 96 | terms = x$terms, 97 | role = x$role, 98 | trained = TRUE, 99 | num_comp = x$num_comp, 100 | scale_factor = x$scale_factor, 101 | offset = x$offset, 102 | res = kprc, 103 | columns = col_names, 104 | prefix = x$prefix, 105 | keep_original_cols = get_keep_original_cols(x), 106 | skip = x$skip, 107 | id = x$id 108 | ) 109 | } 110 | 111 | #' @export 112 | bake.step_kpca_tanh <- function(object, new_data, ...) { 113 | uses_dim_red(object) 114 | col_names <- names(object$columns) 115 | check_new_data(col_names, object, new_data) 116 | 117 | keep_going <- object$num_comp > 0 && length(col_names) > 0 118 | if (!keep_going) { 119 | return(new_data) 120 | } 121 | 122 | cl <- 123 | rlang::call2( 124 | "predict", 125 | .ns = "kernlab", 126 | object = object$res, 127 | rlang::expr(as.matrix(new_data[, col_names])) 128 | ) 129 | comps <- rlang::eval_tidy(cl) 130 | comps <- comps[, seq_len(object$num_comp), drop = FALSE] 131 | colnames(comps) <- names0(ncol(comps), object$prefix) 132 | comps <- as_tibble(comps) 133 | comps <- check_name(comps, new_data, object) 134 | new_data <- vec_cbind(new_data, comps) 135 | new_data <- remove_original_cols(new_data, object, col_names) 136 | new_data 137 | } 138 | 139 | #' @export 140 | print.step_kpca_tanh <- function(x, width = max(20, options()$width - 40), ...) { 141 | title <- "tanh kernel PCA extraction with " 142 | print_step(x$columns, x$terms, x$trained, title, width) 143 | invisible(x) 144 | } 145 | 146 | 147 | #' @rdname tidy.recipe 148 | #' @export 149 | tidy.step_kpca_tanh <- function(x, ...) { 150 | uses_dim_red(x) 151 | if (is_trained(x)) { 152 | res <- tibble(terms = unname(x$columns)) 153 | } else { 154 | term_names <- sel2char(x$terms) 155 | res <- tibble(terms = term_names) 156 | } 157 | res$id <- x$id 158 | res 159 | } 160 | 161 | #' @export 162 | tunable.step_kpca_tanh <- function(x, ...) { 163 | tibble::tibble( 164 | name = c("num_comp", "scale_factor"), 165 | call_info = list( 166 | list(pkg = "dials", fun = "num_comp", range = c(1L, 4L)), 167 | list(pkg = "dials", fun = "scale_factor"), 168 | list(pkg = "dials", fun = "offset") 169 | ), 170 | source = "recipe", 171 | component = "step_kpca_tanh", 172 | component_id = x$id 173 | ) 174 | } 175 | 176 | #' @rdname required_pkgs.recipe 177 | #' @export 178 | required_pkgs.step_kpca_tanh <- function(x, ...) { 179 | c("kernlab") 180 | } 181 | --------------------------------------------------------------------------------