{"id":24805323,"url":"https://github.com/viadee/anchorsonr","last_synced_at":"2025-10-13T06:32:19.843Z","repository":{"id":67262824,"uuid":"192675270","full_name":"viadee/anchorsOnR","owner":"viadee","description":"Implementation of the Anchors algorithm: Explain black-box ML models","archived":false,"fork":false,"pushed_at":"2019-11-21T15:20:33.000Z","size":13609,"stargazers_count":15,"open_issues_count":12,"forks_count":5,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-11-22T15:40:55.147Z","etag":null,"topics":["anchors","explainable-ai","iml","machine-learning","r","rules","xai"],"latest_commit_sha":null,"homepage":null,"language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/viadee.png","metadata":{"files":{"readme":"README.md","changelog":"NEWS.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-19T06:49:11.000Z","updated_at":"2024-04-09T12:10:53.000Z","dependencies_parsed_at":"2023-06-02T11:31:07.583Z","dependency_job_id":null,"html_url":"https://github.com/viadee/anchorsOnR","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viadee%2FanchorsOnR","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viadee%2FanchorsOnR/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viadee%2FanchorsOnR/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viadee%2FanchorsOnR/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/viadee","download_url":"https://codeload.github.com/viadee/anchorsOnR/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":236309796,"owners_count":19128390,"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":["anchors","explainable-ai","iml","machine-learning","r","rules","xai"],"created_at":"2025-01-30T07:18:02.890Z","updated_at":"2025-10-13T06:32:14.249Z","avatar_url":"https://github.com/viadee.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/viadee/anchorsOnR.svg?branch=master)](https://travis-ci.org/viadee/anchorsOnR)\n[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)\n\n# anchorsOnR  \n\nThis package implements the Anchors XAI algorithm as proposed by Marco Tulio Ribeiro (2018). The original paper *\"Anchors: High-Precision Model-Agnostic Explanations\"* can be found [*here*](https://homes.cs.washington.edu/~marcotcr/aaai18.pdf). It provides a short characterization of anchors, which reads as follows: \n\n\u003e An anchor explanation is a rule that sufficiently “anchors” the prediction locally – such that changes to the rest of the feature values of the instance do not matter. In other words, for instances on which the anchor holds, the prediction is (almost) always the same.\n\n\u003e The anchor method is able to explain any black box classifier, with two or more classes. All we require is that the classifier implements a function that takes [a data instance] and outputs [an integer] prediction.\n\nThus, anchors are highly precise explanations in form of human readable IF-THEN rules, that describe which values caused the model's outcome for one specific instance. They prodive a clear coverage, i.e., state exactly to which other instances the apply. \n\nThis R package interfaces the [*anchorJ Java Implementation*](https://github.com/viadee/javaAnchorExplainer).\n\n## Getting Started\n\nThese instructions will get you a copy of the project up and running on your local machine for development and testing purposes.\n\n### Prerequisites\n\nThe R package requires a Java SE Runtime Environment (JRE) with [Java version 8](https://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html) or higher.\n\nIf you want to fiddle around with the anchorsOnR source code, make sure to have the `devtools` R-package installed.\n\n```{r}\ninstall.packages(\"devtools\")\n```\n\n### Installing anchorsOnR\n\nNow, install the *anchors* package directly from github as follows:\n```{r}\ndevtools::install_github(\"viadee/anchorsOnR\")\n```\nThe following dependencies are required to use this package (unmodified, distributed and maintained by their respective authors through the established channels such as CRAN): \n* checkmate (BSD 3 clause)\n* jsonlite (MIT)\n* BBmisc (BSD 3 clause)\n* uuid (MIT)\n* magrittr (MIT)\n\n### Using the Algorithm\n\nThe anchors API was designed in the style of the [lime R package](https://github.com/thomasp85/lime). The best way to illustrate the process of model-agnostic explanation in *anchors* is by example. Assume we aim to understand predictions made on the iris dataset. \n\n#### Obtaining a Model\n\nTowards that goal, we first train an *mlr* learner as a black box model to explain\n\n```{r}\nlibrary(anchors)\nlibrary(mlr)\n\ndata(iris)\n\n# our goal is to predict the species\ntask = makeClassifTask(data = iris, target = \"Species\", id = \"iris\")\n\n# setting up a learner\nlrn = makeLearner(\"classif.rpart\")\n\n# train the learner on the training set\nmodel = train(learner = lrn, task = task)\n```\n\nThe created decision tree can easily be visualized and thus the algorithm's results be compared and validated. Nonetheless, the approach and is *model agnostic*, which means any other model could be explained. This also includes such  that are not inherently visualizable. \n\n[^1]As mentioned before, explaining a decision tree is of little use in practice as it includes explainability in its structure. Therefore, we consider this model a *black box*.\n\n![Iris decision tree visualized](iris_decision_tree.png)\n\n#### Calling anchorsOnR\n\nHaving created a model whose behavior is to be explained, we can obtain the explanations by first creating an *explainer* and using it on a specific instance (or multiple instances):\n```{r}\nexplainer = anchors(iris, model, target = \"Species\")\n\nexplanations = explain(iris[100,], explainer)\n```\nThe explain function spins up and eventually closes a background JVM in which the anchor server is tasked with determining the anchors in your dataset.\n\nThe explanation can be printed and looks similar to the following output:\n```{r}\nprintExplanations(explainer, explanations)\n\n# ====Explained Instance 100 ====\n# Sepal.Length = 5.7\n# Sepal.Width = 2.8\n# Petal.Length = 4.1\n# Petal.Width = 1.3\n# WITH LABEL  = 'versicolor'\n# ====Result====\n# IF Petal.Length = 4.1 (ADDED PRECISION: 0.1736, ADDED COVERAGE: -0.085) AND\n# Petal.Width = 1.3 (ADDED PRECISION: 0.8263, ADDED COVERAGE: -0.913)\n# THEN PREDICT 'versicolor'\n# WITH PRECISION 1 AND COVERAGE 0.002\n```\nIt becomes obvious why this approach is called Anchors: its result are rules, that describes the decision making of a machine learning model anchored around a particular instance of interest, while generalizing to as many other instances as possible. \n\nWe can check the result with the visualized decision tree and see that the anchor in fact explains the model locally. \n\n#### Discretization\n\nThe previous example shows one of anchors' disadvantages. Rules get very specific for numeric values and thus, coverage is low. Discretization helps to group multiple values into one class that gets used as a proxy feature by anchors. This way, we can obtain anchors that generalize superiorly.\n\nWe can simply define the cut points for each feature and pass it to anchors:\n```{r}\nbins = list()\nbins[[1]] = list(cuts = c(4.3, 5.4, 6.3, 7.9))\nbins[[2]] = list(cuts = c(2.0, 2.9, 3.2, 4.4))\nbins[[3]] = list(cuts = c(1, 2.6333, 4.9, 6.9))\nbins[[4]] = list(cuts = c(0.1, 0.8666, 1.6, 2.5))\n\nexplainer = anchors(iris, model, target = \"Species\", bins = bins)\n\nexplanations = explain(iris[100,], explainer)\n```\n\nThe output looks different now. Being less specific and having a higher coverage, this rule applies to more instances than before and is more easy to interpret.\n\n```{r}\nprintExplanations(explainer, explanations)\n\n# ====Result====\n# IF Petal.Length IN [2.6333,4.9) (ADDED PRECISION: 0.1676, ADDED COVERAGE: -0.251) AND\n# Petal.Width IN [0.8666,1.6) (ADDED PRECISION: 0.8323, ADDED COVERAGE: -0.635)\n# THEN PREDICT 'versicolor'\n# WITH PRECISION 1 AND COVERAGE 0.114\n```\n\n### Extending Model Support\nBy default, *anchors* supports a variety of machine learning packets and model classes, such as:\n\n* lda\n* mlr\n* keras\n* h2o\n\nHowever, the prefered model of your choice might not be included in this list. In order to explain an arbitraty machine learning model, *anchors* needs to be able to retrieve predictions from that model in a standardised way. Furthermore, it requires information as to whether it is a classification or regression model. To cater the former, *anchors* calls the `predict_model()` generic which the user is free to supply methods for. For the latter, the model must respond to the `model_type()` generic. See `models.R`for examples on how to do write corresponding methods.\n\n## Authors\n\n* **Thorben Hellweg** [thllwg](https://github.com/thllwg)\n* **Tobias Goerke** [TobiasGoerke](https://github.com/TobiasGoerke)\n* **Magdalena Lang** [MagdalenaLang1](https://github.com/MagdalenaLang1)\n* **Ronja Köhling**\n\n## License\n\nBSD 3-Clause License\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fviadee%2Fanchorsonr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fviadee%2Fanchorsonr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fviadee%2Fanchorsonr/lists"}