{"id":25109959,"url":"https://github.com/smac-group/swag","last_synced_at":"2026-01-08T19:47:42.261Z","repository":{"id":56935478,"uuid":"257943348","full_name":"SMAC-Group/swag","owner":"SMAC-Group","description":"swag is a R package that trains a meta-learning procedure which combines screening and wrapper methods to find a set of extremely low-dimensional attribute combinations.","archived":false,"fork":false,"pushed_at":"2023-08-22T08:14:42.000Z","size":312,"stargazers_count":2,"open_issues_count":1,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-08T00:33:28.983Z","etag":null,"topics":["machine-learning"],"latest_commit_sha":null,"homepage":"","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SMAC-Group.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-22T15:33:04.000Z","updated_at":"2024-03-08T16:51:45.000Z","dependencies_parsed_at":"2022-08-21T05:50:14.062Z","dependency_job_id":null,"html_url":"https://github.com/SMAC-Group/swag","commit_stats":null,"previous_names":["smac-group/swag-r-package"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SMAC-Group%2Fswag","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SMAC-Group%2Fswag/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SMAC-Group%2Fswag/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SMAC-Group%2Fswag/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SMAC-Group","download_url":"https://codeload.github.com/SMAC-Group/swag/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246786697,"owners_count":20833731,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["machine-learning"],"created_at":"2025-02-08T00:33:48.001Z","updated_at":"2025-12-12T02:23:11.630Z","avatar_url":"https://github.com/SMAC-Group.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\ntitle: \"\"\noutput: github_document\n---\n\n[![Travis-CI Build Status](https://travis-ci.com/SMAC-Group/SWAG-R-Package.svg?branch=master)](https://travis-ci.com/github/SMAC-Group/SWAG-R-Package)\n[![Last-changedate](https://img.shields.io/badge/last%20change-`r gsub('-', '--', Sys.Date())`-green.svg)](https://github.com/SMAC-Group/SWAG-R-Package)\n[![license](https://img.shields.io/badge/license-GPL--3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0.en.html)\n\n```{r setup, include=FALSE}\nknitr::opts_chunk$set(echo = TRUE)\n```\n\n# `swag` package\n\n**swag** is a package that trains a meta-learning procedure which combines screening and wrapper methods to find a set of extremely low-dimensional attribute combinations.\n\n## Installing the package with GitHub \n\nFirst install the **devtools** package. Then **swag** with the following code:\n\n\n```{r,echo=FALSE,include=FALSE,eval=TRUE}\n# devtools::install_github(\"SMAC-Group/SWAG-R-Package\")\n\nlibrary(swag) #load the new package\n```\n\n\n```{r, eval=F,echo=TRUE}\n## if not installed\n## install.packages(\"remotes\")\n\nremotes::install_github(\"SMAC-Group/SWAG-R-Package\")\n\nlibrary(swag) #load the new package\n```\n\n## Quick start\n\nWe propose to use the **breastcancer** dataset readily available from the package **mlbench** to give an overview of **swag**.\n\n```{r BreastCancer, eval=T}\n# After having installed the mlbench package\n\ndata(BreastCancer, package = \"mlbench\")\n\n# Pre-processing of the data\ny \u003c- BreastCancer$Class # response variable\nx \u003c- as.matrix(BreastCancer[setdiff(names(BreastCancer),c(\"Id\",\"Class\"))]) # features\n\n# remove missing values and change to 'numeric'\nid \u003c- which(apply(x,1,function(x) sum(is.na(x)))\u003e0)\ny \u003c- y[-id]\nx \u003c- x[-id,]\nx \u003c- apply(x,2,as.numeric)\n\n# Training and test set\nset.seed(180) # for replication\nind \u003c- sample(1:dim(x)[1],dim(x)[1]*0.2)  \ny_test \u003c- y[ind]\ny_train \u003c- y[-ind]\nx_test \u003c- x[ind,]\nx_train \u003c-x[-ind,]\n```\n\nNow we are ready to train with **swag**! The first step is to define the meta-parameters of the **swag** procedure:  $p_{max}$ the maximum dimension of attributes, $\\alpha$ a performance quantile which represents the percentage of learners which are selected at each dimension and $m$, the maximum numbers of learners trained at each dimension. We can set all these meta-parameters, together with a seed for replicability purposes and `verbose = TRUE` to get a message as each dimension is completed, thanks to the *swagcontrol()* function which behaves similarly to the `trControl = ` argument of **caret**.\n\n\n```{r control-swag, eval=T}\n# Meta-parameters chosen for the breast cancer dataset\nswagcon \u003c- swagControl(pmax = 4L, \n                       alpha = 0.5, \n                       m = 20L,\n                       seed = 163L, #for replicability\n                       verbose = T #keeps track of completed dimensions\n                       )\n\n# Given the low dimensional dataset, we can afford a wider search \n# by fixing alpha = 0.5 as a smaller alpha may also stop the \n# training procedure earlier than expected.\n```\n\nHaving set-up the meta-parameters as explained above, we are now ready to train the **swag**. We start with the linear Support Vector Machine learner:  \n\n```{r, eval=FALSE, message=FALSE,warning=FALSE,echo=FALSE}\nlibrary(caret) # swag is build around caret and uses it to train each learner\n```\n\n\n```{r SVM, eval=TRUE, warning=FALSE,message=FALSE}\n### SVM Linear Learner ###\ntrain_swag_svml \u003c- swag(\n  # arguments for swag\n  x = x_train, \n  y = y_train, \n  control = swagcon,\n  auto_control = FALSE,\n  # arguments for caret\n  trControl = caret::trainControl(method = \"repeatedcv\", number = 10, repeats = 1, allowParallel = F),\n  metric = \"Accuracy\",\n  method = \"svmLinear\",  # Use method = \"svmRadial\" to train this alternative learner\n  preProcess = c(\"center\", \"scale\")\n)\n```\n\nThe only difference with respect to the classic **caret** train function, is the specification of the **swag** arguments which have been explained previously. In the above chunk for the *svmLinear* learner, we define the estimator of the out-of-sample accuracy as 10-fold cross-validation repeated 1 time. For this specific case, we have chosen to center and rescale the data, as usually done for SVMs, and, the parameter that controls the margin in SVMs is automatically fixed at unitary value (i.e. $c=1$).\n\nLet's have a look at the typical output of a **swag** training object for the *svmLinear* learner:\n\n```{r CVs, eval=T}\ntrain_swag_svml$CVs  \n\n# A list which contains the cv training errors of each learner explored in a given dimension\n```\n\n```{r VarMat, eval=T}\ntrain_swag_svml$VarMat \n\n# A list which contrains a matrix, for each dimension, with the attributes tested at that step \n```\n\n```{r cv-alpha, eval= T}\ntrain_swag_svml$cv_alpha \n\n# The cut-off cv training error, at each dimension, determined by the choice of alpha\n```\n\nThe other two learners that we have implemented on **swag** are: lasso (**glmnet** package required) and random forest (**party** package required). The training phase for these learners, differs a little with respect to the SVM one. We can look at the random forest for a practical example:\n\n```{r random-forest, eval=TRUE}\n### Random Forest Learner ###\ntrain_swag_rf \u003c- swag(\n  # arguments for swag\n  x = x, \n  y = y, \n  control = swagcon,\n  auto_control = FALSE,\n  # arguments for caret\n  trControl = caret::trainControl(method = \"repeatedcv\", number = 10, repeats = 1, allowParallel = F),\n  metric = \"Accuracy\",\n  method = \"rf\",\n  # dynamically modify arguments for caret\n  caret_args_dyn = function(list_arg,iter){\n    list_arg$tuneGrid = expand.grid(.mtry=sqrt(iter))\n    list_arg\n  }\n)\n```\n\nThe newly introduced argument `caret_args_dyn` enables the user to modify the hyper-parameters related to a given learner in a dynamic way since they can change as the dimension grows up to the desired $p_{max}$. This allows to adapt the *mtry* hyper-parameter as the dimension grows. In the example above, we have fixed *mtry* to the square root of the number of attributes at each step as it is usually done in practice.  \n \nYou can tailor the learning arguments of *swag()* as you like, introducing for example grids for the hyper-parameters specific of a given learner or update these grids as the dimension increases similarly to what is usually done for the **caret** package. This gives you a wide range of possibilities and a lot of flexibility in the training phase.\n\nTo conclude this brief introduction, we present the usual *predict()* function which can be applied to a **swag** trained object similarly to many other packages in R. We pick the random forest learner for this purpose.  \n\n```{r predictions, eval=T}\n# best learner predictions \n# if `newdata` is not specified, then predict gives predictions based on the training \n# sample\n\nsapply(predict(object = train_swag_rf), function(x) head(x))\n\n# best learner predictions \nbest_pred \u003c- predict(object = train_swag_rf, \n                     newdata = x_test)\n\nsapply(best_pred, function(x) head(x))\n\n# predictions for a given dimension \n\ndim_pred \u003c-  predict(\n  object = train_swag_rf, \n  newdata = x_test, \n  type = \"attribute\",\n  attribute = 4L)\n\n\nsapply(dim_pred,function(x) head(x))\n\n# predictions below a given CV error\n\ncv_pred \u003c-  predict(\n  object = train_swag_rf, \n  newdata = x_test, \n  type = \"cv_performance\",\n  cv_performance = 0.04)\n\nsapply(cv_pred,function(x) head(x))\n\n```\n\n\nNow we can evaluate the performance of the best learner selected by **swag** thanks to the *confusionMatrix()* function of **caret**. \n\n```{r confusion-matrix, eval=T}\n# transform predictions into a data.frame of factors with levels of `y_test`\nbest_learn \u003c- factor(levels(y_test)[best_pred$predictions])\ncaret::confusionMatrix(best_learn,y_test)\n```\n\nThanks for the attention. You can  definitely say that you worked with **swag** !!!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmac-group%2Fswag","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmac-group%2Fswag","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmac-group%2Fswag/lists"}