{"id":13400521,"url":"https://github.com/tidymodels/themis","last_synced_at":"2025-05-16T01:05:07.476Z","repository":{"id":37105607,"uuid":"214698193","full_name":"tidymodels/themis","owner":"tidymodels","description":"Extra recipes steps for dealing with unbalanced data","archived":false,"fork":false,"pushed_at":"2025-04-24T18:06:54.000Z","size":77436,"stargazers_count":141,"open_issues_count":21,"forks_count":12,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-05-09T18:08:31.231Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://themis.tidymodels.org/","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tidymodels.png","metadata":{"files":{"readme":"README.Rmd","changelog":"NEWS.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-12T18:46:35.000Z","updated_at":"2025-04-24T18:04:35.000Z","dependencies_parsed_at":"2024-06-21T13:01:51.368Z","dependency_job_id":"edf50ad2-d486-440e-81e5-8657a066541d","html_url":"https://github.com/tidymodels/themis","commit_stats":{"total_commits":334,"total_committers":11,"mean_commits":"30.363636363636363","dds":0.08982035928143717,"last_synced_commit":"c9bdc843f3c299e668f1b41dee1d1f11deceb4ba"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidymodels%2Fthemis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidymodels%2Fthemis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidymodels%2Fthemis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidymodels%2Fthemis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tidymodels","download_url":"https://codeload.github.com/tidymodels/themis/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254448579,"owners_count":22072764,"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":[],"created_at":"2024-07-30T19:00:52.906Z","updated_at":"2025-05-16T01:05:07.450Z","avatar_url":"https://github.com/tidymodels.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r}\n#| include: false\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n\n# themis \u003ca href=\"https://themis.tidymodels.org\"\u003e\u003cimg src=\"man/figures/logo.png\" align=\"right\" height=\"138\" /\u003e\u003c/a\u003e\n\n\u003c!-- badges: start --\u003e\n[![R-CMD-check](https://github.com/tidymodels/themis/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/tidymodels/themis/actions/workflows/R-CMD-check.yaml)\n[![Codecov test coverage](https://codecov.io/gh/tidymodels/themis/branch/main/graph/badge.svg)](https://app.codecov.io/gh/tidymodels/themis?branch=main)\n[![CRAN status](https://www.r-pkg.org/badges/version/themis)](https://CRAN.R-project.org/package=themis)\n[![Downloads](http://cranlogs.r-pkg.org/badges/themis)](https://CRAN.R-project.org/package=themis)\n[![Lifecycle: maturing](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html)\n\u003c!-- badges: end --\u003e\n\n**themis** contains extra steps for the\n[`recipes`](https://CRAN.R-project.org/package=recipes) package for\ndealing with unbalanced data. The name **themis** is that of the [ancient Greek god](https://thishollowearth.wordpress.com/2012/07/02/god-of-the-week-themis/) who is typically  depicted with a balance.\n\n## Installation\n\nYou can install the released version of themis from [CRAN](https://CRAN.R-project.org) with:\n\n``` r\ninstall.packages(\"themis\")\n```\n\nInstall the development version from GitHub with:\n\n``` r\n# install.packages(\"pak\")\npak::pak(\"tidymodels/themis\")\n```\n\n## Example\n\nFollowing is a example of using the [SMOTE](https://jair.org/index.php/jair/article/view/10302/24590) algorithm to deal with unbalanced data\n\n```{r}\n#| label: example\n#| message: false\nlibrary(recipes)\nlibrary(modeldata)\nlibrary(themis)\n\ndata(\"credit_data\", package = \"modeldata\")\n\ncredit_data0 \u003c- credit_data |\u003e\n  filter(!is.na(Job))\n\ncount(credit_data0, Job)\n\nds_rec \u003c- recipe(Job ~ Time + Age + Expenses, data = credit_data0) |\u003e\n  step_impute_mean(all_predictors()) |\u003e\n  step_smote(Job, over_ratio = 0.25) |\u003e\n  prep()\n\nds_rec |\u003e\n  bake(new_data = NULL) |\u003e\n  count(Job)\n```\n\n## Methods\n\nBelow is some unbalanced data. Used for examples latter.\n\n```{r}\n#| fig-alt: \"Bar chart with 5 columns. class on the x-axis and count on the y-axis. Class a has height 10, b has 20, c has 30, d has 40, and e has 50.\"\nexample_data \u003c- data.frame(class = letters[rep(1:5, 1:5 * 10)],\n                           x = rnorm(150))\n\nlibrary(ggplot2)\n\nexample_data |\u003e\n  ggplot(aes(class)) +\n  geom_bar()\n```\n\n### Upsample / Over-sampling\n\nThe following methods all share the tuning parameter `over_ratio`, which is the ratio of the minority-to-majority frequencies.\n\n| name | function | Multi-class |\n|---|---|---|\n| Random minority over-sampling with replacement | `step_upsample()` | :heavy_check_mark: |\n| Synthetic Minority Over-sampling Technique | `step_smote()` | :heavy_check_mark: |\n| Borderline SMOTE-1 | `step_bsmote(method = 1)` | :heavy_check_mark: |\n| Borderline SMOTE-2 | `step_bsmote(method = 2)` | :heavy_check_mark: |\n| Adaptive synthetic sampling approach for imbalanced learning | `step_adasyn()` | :heavy_check_mark: |\n| Generation of synthetic data by Randomly Over Sampling Examples| `step_rose()` | |\n\nBy setting `over_ratio = 1` you bring the number of samples of all minority classes equal to 100% of the majority class.\n\n```{r}\n#| fig-alt: \"Bar chart with 5 columns. class on the x-axis and count on the y-axis. class a, b, c, d, and e all have a height of 50.\"\nrecipe(~., example_data) |\u003e\n  step_upsample(class, over_ratio = 1) |\u003e\n  prep() |\u003e\n  bake(new_data = NULL) |\u003e\n  ggplot(aes(class)) +\n  geom_bar()\n```\n\nand by setting `over_ratio = 0.5` we upsample any minority class with less samples then 50% of the majority up to have 50% of the majority.\n\n```{r}\n#| fig-alt: \"Bar chart with 5 columns. class on the x-axis and count on the y-axis. Class a has height 25, b has 25, c has 30, d has 40, and e has 50.\"\nrecipe(~., example_data) |\u003e\n  step_upsample(class, over_ratio = 0.5) |\u003e\n  prep() |\u003e\n  bake(new_data = NULL) |\u003e\n  ggplot(aes(class)) +\n  geom_bar()\n```\n\n### Downsample / Under-sampling\n\nMost of the the following methods all share the tuning parameter `under_ratio`, which is the ratio of the majority-to-minority frequencies.\n\n| name | function | Multi-class | under_ratio |\n|---|---|---|---|\n| Random majority under-sampling with replacement | `step_downsample()` | :heavy_check_mark: | :heavy_check_mark: | \n| NearMiss-1 | `step_nearmiss()` | :heavy_check_mark: |:heavy_check_mark: | \n| Extraction of majority-minority Tomek links | `step_tomek()` | | |\n\nBy setting `under_ratio = 1` you bring the number of samples of all majority classes equal to 100% of the minority class.\n\n```{r}\n#| fig-alt: \"Bar chart with 5 columns. class on the x-axis and count on the y-axis. Class a, b, c, d, and e all have a height of 10.\"\nrecipe(~., example_data) |\u003e\n  step_downsample(class, under_ratio = 1) |\u003e\n  prep() |\u003e\n  bake(new_data = NULL) |\u003e\n  ggplot(aes(class)) +\n  geom_bar()\n```\n\nand by setting `under_ratio = 2` we downsample any majority class with more then 200% samples of the minority class down to have to 200% samples of the minority.\n\n```{r}\n#| fig-alt: \"Bar chart with 5 columns. class on the x-axis and count on the y-axis. Class a has height 10, b, c, d, and e have ha height of 20.\"\nrecipe(~., example_data) |\u003e\n  step_downsample(class, under_ratio = 2) |\u003e\n  prep() |\u003e\n  bake(new_data = NULL) |\u003e\n  ggplot(aes(class)) +\n  geom_bar()\n```\n\n## Contributing\n\nThis project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.\n\n- For questions and discussions about tidymodels packages, modeling, and machine learning, [join us on RStudio Community](https://forum.posit.co/new-topic?category_id=15\u0026tags=tidymodels,question).\n\n- If you think you have encountered a bug, please [submit an issue](https://github.com/tidymodels/themis/issues).\n\n- Either way, learn how to create and share a [reprex](https://reprex.tidyverse.org/articles/articles/learn-reprex.html) (a minimal, reproducible example), to clearly communicate about your code.\n\n- Check out further details on [contributing guidelines for tidymodels packages](https://www.tidymodels.org/contribute/) and [how to get help](https://www.tidymodels.org/help/).\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftidymodels%2Fthemis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftidymodels%2Fthemis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftidymodels%2Fthemis/lists"}