{"id":27152538,"url":"https://github.com/santikka/cfid","last_synced_at":"2025-10-05T00:46:58.968Z","repository":{"id":56934860,"uuid":"374587717","full_name":"santikka/cfid","owner":"santikka","description":"cfid:  R package for identifying counterfactuals.","archived":false,"fork":false,"pushed_at":"2024-07-13T08:45:16.000Z","size":223,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-19T12:44:50.244Z","etag":null,"topics":["causal-inference","causal-models","causality-algorithms","counterfactual","counterfactuals","directed-acyclic-graph","identifiability"],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/santikka.png","metadata":{"files":{"readme":"README.Rmd","changelog":"NEWS.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":"codemeta.json"}},"created_at":"2021-06-07T08:17:03.000Z","updated_at":"2024-07-13T08:45:20.000Z","dependencies_parsed_at":"2023-01-22T13:46:00.966Z","dependency_job_id":"f0f4ca6a-4acd-4f8f-b67f-370fb4d1b7a0","html_url":"https://github.com/santikka/cfid","commit_stats":{"total_commits":48,"total_committers":3,"mean_commits":16.0,"dds":0.08333333333333337,"last_synced_commit":"d6598defe71bccdfd962ab0bee42103ba4dda07f"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/santikka%2Fcfid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/santikka%2Fcfid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/santikka%2Fcfid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/santikka%2Fcfid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/santikka","download_url":"https://codeload.github.com/santikka/cfid/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247873168,"owners_count":21010403,"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":["causal-inference","causal-models","causality-algorithms","counterfactual","counterfactuals","directed-acyclic-graph","identifiability"],"created_at":"2025-04-08T15:39:11.799Z","updated_at":"2025-10-05T00:46:53.924Z","avatar_url":"https://github.com/santikka.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n```{r setup, include=FALSE}\nknitr::opts_chunk$set(\n  echo = TRUE,\n  collapse = TRUE,\n  comment = \"#\u003e\"\n)\n```\n\n# cfid: An R Package for Identification of Counterfactual Queries in Causal Models\n\n\u003c!-- badges: start --\u003e\n[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)\n[![R-CMD-check](https://github.com/santikka/cfid/workflows/R-CMD-check/badge.svg)](https://github.com/santikka/cfid/actions)\n[![Codecov test coverage](https://codecov.io/gh/santikka/cfid/branch/main/graph/badge.svg)](https://app.codecov.io/gh/santikka/cfid?branch=main)\n[![CRAN version](https://www.r-pkg.org/badges/version/cfid)](https://CRAN.R-project.org/package=cfid)\n\u003c!-- badges::end --\u003e\n\n```{r, echo = FALSE, warning=FALSE}\nlibrary(\"cfid\")\n```\n\n## Overview\n\nThis package facilitates the identification of counterfactual queries in structural causal \nmodels via the ID* and IDC* algorithms by Shpitser, I. and Pearl, J. (2007, 2008) \nhttps://arxiv.org/abs/1206.5294,\nhttps://jmlr.org/papers/v9/shpitser08a.html. A simple interface is provided for \ndefining causal graphs and counterfactual conjunctions. Construction of parallel\nworlds graphs and counterfactual graphs is done automatically based on the \ncounterfactual query and the causal graph.\n\nFor further information, see the tutorial paper on this package published in *The R Journal*: https://doi.org/10.32614/RJ-2023-053\n\n## Installation\n\nYou can install the latest development version by using the devtools package:\n```{r, eval = FALSE}\n# install.packages(\"devtools\")\ndevtools::install_github(\"santikka/cfid\")\n```\n\n## Graphs\n\nDirected acyclic graphs (DAG) can be defined using the function `dag` in a syntax\nsimilar to the [`dagitty`](https://cran.r-project.org/package=dagitty) package.\nThis function accepts\nedges of the form `X -\u003e Y`, `X \u003c- Y`, and `X \u003c-\u003e Y`, where the last variant is \na shorthand for a latent confounder affecting both `X` and `Y` (a so-called\nbidirected edge). Subgraphs can be defined using curly braces `{...}`. Edges\nto and from subgraphs connect to all vertices present in the subgraph. Subgraphs\ncan also be nested. Some examples of valid constructs include:\n```{r, eval = FALSE}\ndag(\"X -\u003e Y \u003c- Z \u003c-\u003e W\")\ndag(\"{X Y Z} -\u003e {A B}\")\ndag(\"X -\u003e {Z \u003c-\u003e {Y W}}\")\n```\nwhich define the following DAGs:\n```mermaid\nflowchart LR;\n  X((X))--\u003eY((Y));\n  Z((Z))--\u003eY;\n  W((W))\u003c-.-\u003eZ;\n```\n```mermaid\nflowchart LR;\n  X((X))--\u003eA((A));\n  Y((Y))--\u003eA;\n  Z((Z))--\u003eA;\n  X--\u003eB((B));\n  Y--\u003eB;\n  Z--\u003eB;\n```\n```mermaid\nflowchart LR;\n  X((X))--\u003eZ((Z));\n  X--\u003eY((Y));\n  X--\u003eW((W));\n  Z\u003c-.-\u003eY;\n  Z\u003c-.-\u003eW;\n```\n\n## Counterfactual variables and conjunctions\n\nA counterfactual variable is defined by its name, value, and the submodel that\nit originated from (a set of interventions). For example, $y_x$ is a counterfactual\nvariable named $Y$ with the value assignment $y$ that originated from a submodel\nwhere the intervention $do(X = x)$ took place.\n\nThe function `counterfactual_variable`\nand its shorthand alias `cf` can be used to construct counterfactual variables.\nThis function takes three arguments: `var`, `obs`, and `sub` that correspond\nto the variable name, observed value assignment and subscript (the submodel).\nFor example, $y_x$ is defined as follows:\n```{r}\ncf(var = \"Y\", obs = 0, sub = c(X = 0))\n```\nby default, the value 0 is the \"default\" or baseline level, and integer values\ndifferent from 0 are denoted by primes. For example $y'_x$ is a similar counterfactual\nvariable to $y_x$, except that it was observed to take the value $y'$ instead of $y$\nThis can be accomplished by changing the `obs` argument:\n```{r}\ncf(var = \"Y\", obs = 1, sub = c(X = 0))\n```\nPurely observational counterfactual variables (of the original causal model)\ncan be defined by omitting the `sub` argument.\n\nConjunctions of multiple counterfactual variables can be constructed using the\nfunction `counterfactual_conjunction` or its shorthand alias `conj`. This\nfunction simply takes an arbitrary number of `\"counterfacual_variable\"` objects\nas its argument. For example, the counterfactual conjunction \n$y \\wedge y'_x$ can be defined as follows:\n```{r}\nv1 \u003c- cf(\"Y\", 0)\nv2 \u003c- cf(\"Y\", 1, c(\"X\" = 0))\nconj(v1, v2)\n```\n\n## Identification\n\nIdentifiability of (conditional) counterfactual conjunctions can be determined \nvia the function `identifiable`. This function takes the conjunction `gamma` to \nbe identified from the set of all interventional distributions $P_*$ of the causal\nmodel represented by the `\"dag\"` object `g`. An optional conditioning conjunction\n`delta` can also be provided. The solution is provided in LaTeX syntax if the\nquery is identifiable. For instance, we can consider the identifiability\nof $P(y_x|x' \\wedge z_d \\wedge d)$ in the DAG shown below as follows:\n```mermaid\nflowchart TB;\n  X((X))--\u003eW((W));\n  W--\u003eY((Y));\n  D((D))--\u003eZ((Z));\n  Z--\u003eY;\n  X\u003c-.-\u003eY;\n```\n```{r}\ng1 \u003c- dag(\"X -\u003e W -\u003e Y \u003c- Z \u003c- D X \u003c-\u003e Y\")\nv1 \u003c- cf(\"Y\", 0, c(X = 0))\nv2 \u003c- cf(\"X\", 1)\nv3 \u003c- cf(\"Z\", 0, c(D = 0))\nv4 \u003c- cf(\"D\", 0)\nc1 \u003c- conj(v1)\nc2 \u003c- conj(v2, v3, v4)\nidentifiable(g = g1, gamma = c1, delta = c2)\n```\nFor more information and examples, please see the package documentation.\n\n## Related packages\n\n- The [`causaleffect`](https://cran.r-project.org/package=causaleffect) \n  package provides the ID and IDC algorithms for\n  the identification of causal effects (among other algorithms).\n- The [`dosearch`](https://cran.r-project.org/package=dosearch) \n  package provides a heuristic search algorithm that uses\n  do-calculus to identify causal effects from an arbitrary combination of\n  input distributions.\n- The [`dagitty`](https://cran.r-project.org/package=dagitty) \n  package provides various tools for causal modeling, such as finding \n  adjustment sets and instrumental variables.\n- The [`R6causal`](https://cran.r-project.org/package=R6causal)\n  package implements an R6 class for structural causal models, and provides\n  tools to simulate counterfactual scenarios for discrete variables.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsantikka%2Fcfid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsantikka%2Fcfid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsantikka%2Fcfid/lists"}