{"id":32200928,"url":"https://github.com/osofr/simcausal","last_synced_at":"2025-10-22T03:57:27.890Z","repository":{"id":32464776,"uuid":"36044556","full_name":"osofr/simcausal","owner":"osofr","description":"Simulating Longitudinal and Network Data with Causal Inference Applications","archived":false,"fork":false,"pushed_at":"2024-07-18T16:08:38.000Z","size":746,"stargazers_count":70,"open_issues_count":15,"forks_count":11,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-10-22T03:57:23.220Z","etag":null,"topics":["counterfactual-data","sem","simulated-network","simulating-data","structural-equations"],"latest_commit_sha":null,"homepage":"","language":"R","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/osofr.png","metadata":{"files":{"readme":"README.md","changelog":"NEWS","contributing":null,"funding":null,"license":null,"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":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2015-05-22T00:54:16.000Z","updated_at":"2025-07-30T07:44:58.000Z","dependencies_parsed_at":"2025-09-08T16:36:25.149Z","dependency_job_id":null,"html_url":"https://github.com/osofr/simcausal","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/osofr/simcausal","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osofr%2Fsimcausal","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osofr%2Fsimcausal/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osofr%2Fsimcausal/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osofr%2Fsimcausal/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/osofr","download_url":"https://codeload.github.com/osofr/simcausal/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osofr%2Fsimcausal/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280376547,"owners_count":26320275,"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","status":"online","status_checked_at":"2025-10-22T02:00:06.515Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["counterfactual-data","sem","simulated-network","simulating-data","structural-equations"],"created_at":"2025-10-22T03:57:26.775Z","updated_at":"2025-10-22T03:57:27.881Z","avatar_url":"https://github.com/osofr.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"simcausal\n==========\n\n[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/simcausal)](https://CRAN.R-project.org/package=simcausal)\n[![](http://cranlogs.r-pkg.org/badges/simcausal)](https://CRAN.R-project.org/package=simcausal) [![](http://cranlogs.r-pkg.org/badges/grand-total/simcausal)](https://CRAN.R-project.org/package=simcausal)\n[![Travis-CI Build Status](https://travis-ci.org/osofr/simcausal.svg?branch=master)](https://travis-ci.org/osofr/simcausal)\n[![Coverage Status](https://coveralls.io/repos/osofr/simcausal/badge.svg?branch=master\u0026service=github)](https://coveralls.io/github/osofr/simcausal?branch=master)\n\nThe `simcausal` R package is a tool for specification and simulation of complex longitudinal data structures that are based on structural equation models (SEMs). The emphasis is on the types of simulations frequently encountered in causal inference problems, such as, observational data with time-dependent confounding, selection bias, and random monitoring processes. The interface allows for quick expression of dependencies between a large number of time-varying nodes. \n\n### Installation\n\nTo install the CRAN release version of `simcausal`: \n\n```R\ninstall.packages('simcausal')\n```\n\nTo install the development version (requires the `devtools` package):\n\n```R\ndevtools::install_github('osofr/simcausal', build_vignettes = FALSE)\n```\n\n### Documentation\n\nOnce the package is installed, see the [vignette](https://CRAN.R-project.org/package=simcausal/vignettes/simcausal_vignette.pdf), consult the internal package documentation and examples. \n\n* To see the vignette in R:\n\n```R\nvignette(\"simcausal_vignette\", package=\"simcausal\")\n```\n\n* To see all available package documentation:\n\n```R\n?simcausal\nhelp(package = 'simcausal')\n```\n\n* To see the latest updates for the currently installed version of the package:\n\n```r\nnews(package = \"simcausal\")\n```\n\n### Brief overview\n\nBelow is an example simulating data with 4 covariates specified by 4 structural equations (nodes). New equations are added by using successive calls to `+ node()` function and data are simulated by calling `sim` function:\n\n```R\nlibrary(\"simcausal\")\nD \u003c- DAG.empty() + \n  node(\"CVD\", distr=\"rcat.b1\", probs = c(0.5, 0.25, 0.25)) +\n  node(\"A1C\", distr=\"rnorm\", mean = 5 + (CVD \u003e 1)*10 + (CVD \u003e 2)*5) +\n  node(\"TI\", distr=\"rbern\", prob = plogis(-0.5 - 0.3*CVD + 0.2*A1C)) +\n  node(\"Y\", distr=\"rbern\", prob = plogis(-3 + 1.2*TI + 0.1*CVD + 0.3*A1C))\nD \u003c- set.DAG(D)\ndat \u003c- sim(D,n=200)\n```\n\nTo display the above SEM object as a directed acyclic graph:\n\n```R\nplotDAG(D)\n```\n\nTo allow the above nodes `A1C`, `TI` and `Y` to change over time, for time points t = 0,...,7, and keeping `CVD` the same, simply add `t` argument to `node` function and use the square bracket `[...]` vector indexing to reference time-varying nodes inside the `node` function expressions:\n\n```R\nlibrary(\"simcausal\")\nD \u003c- DAG.empty() + \n  node(\"CVD\", distr=\"rcat.b1\", probs = c(0.5, 0.25, 0.25)) +\n  node(\"A1C\", t=0, distr=\"rnorm\", mean=5 + (CVD \u003e 1)*10 + (CVD \u003e 2)*5) + \n  node(\"TI\", t=0, distr=\"rbern\", prob=plogis(-5 - 0.3*CVD + 0.5*A1C[t])) +\n\n  node(\"A1C\", t=1:7, distr=\"rnorm\", mean=-TI[t-1]*10 + 5 + (CVD \u003e 1)*10 + (CVD \u003e 2)*5) +\n  node(\"TI\", t=1:7, distr=\"rbern\", prob=plogis(-5 - 0.3*CVD + 0.5*A1C[t] + 1.5*TI[t-1])) +\n  node(\"Y\", t=0:7, distr=\"rbern\", prob=plogis(-6 - 1.2*TI[t] + 0.1*CVD + 0.3*A1C[t]), EFU=TRUE)\nD \u003c- set.DAG(D)\ndat.long \u003c- sim(D,n=200)\n```\n\nThe `+ action` function allows defining counterfactual data under various interventions (e.g., static, dynamic, deterministic, or stochastic), which can be then simulated by calling `sim` function. In particular, the interventions may represent exposures to treatment regimens, the occurrence or non-occurrence of right-censoring events, or of clinical monitoring events.\n\nIn addition, the functions `set.targetE`, `set.targetMSM` and `eval.target` provide tools for defining and computing a few selected features of the distribution of the counterfactual data that represent common causal quantities of interest, such as, treatment-specific means, the average treatment effects and coefficients from working marginal structural models. \n\n\n### Using networks in SEMs\n\nFunction `network` provies support for networks simulations, in particular it enables defining and simulating SEM for dependent data. For example, a network sampling function like `rnet.gnm` (provided by the package, see `?rnet.gnm`) can be used to specify and simulate dependent data from a network-based SEM.  Start defining a SEM that uses the this network, with a `+network` syntax and providing \"`rnet.gnm`\" as a \"`netfun`\" argument to `network` function:\n\n```R\nlibrary(\"simcausal\")\nlibrary(\"magrittr\")\nD \u003c- DAG.empty() + network(\"ER.net\", netfun = \"rnet.gnm\", m_pn = 50)\n```\n\nFirst define two IDD nodes `W1` (categorical) and `W2` (Bernoulli):\n\n```R\nD \u003c- D + \n  node(\"W1\", distr = \"rcat.b1\", probs = c(0.0494, 0.1823, 0.2806, 0.2680, 0.1651, 0.0546)) + \n  node(\"W2\", distr = \"rbern\", prob = plogis(-0.2 + W1/3))\n```\n\nNew nodes (structural equations) can now be specified conditional on the past node values of observations connected to each unit `i` (*friends* of `i`). The friends are defined by the network matrix that is returned by the above network generator `rnet.gnm`. Double square bracket syntax \"`[[...]]`\" allows referencing the node values of connected friends. Two special variables, \"`Kmax`\" and \"`nF`\" can be used along-side indexing \"`[[...]]`\". `Kmax`  defines the maximal number of friends (maximal friend index) for all observation. When `kth` friend referenced in \"`Var[[k]]`\" doesn't exist, the default is to set that value to \"`NA`\". Adding the argument \"`replaceNAw0=TRUE`\" to `node` function changes such values from `NA` to `0`. `nF` is another special variable, which is a vector of length `n` and each `nF[i]` is equal to the current number of friends for unit `i`. Any kind of summary function that can be applied to multiple time-varying nodes can be similarly applied to network-indexed nodes. For additional details, see the package documentation for the network function (`?network`) and the package vignette on conducting network simulations.\n\nDefine network variable \"`netW1`\" as the `W1` values of the first friend and define binary exposure \"`A`\" so that probability of success for each unit 'i' for `A` is a logit-linear function of:\n1. `W1[i]`,\n2. Sum of `W1` values among all friends of `i`,\n3. Mean value of `W2` among all friends of `i`.\n\n```R\ndat.net \u003c- {\n  D + node(\"netW1.F1\", distr = \"rconst\", const = W1[[1]]) +\n  node(\"A\", distr = \"rbern\",\n              prob = plogis(2 + -0.5 * W1 +\n                            -0.1 * sum(W1[[1:Kmax]]) +\n                            -0.7 * ifelse(nF \u003e 0, sum(W2[[1:Kmax]])/nF, 0)),\n              replaceNAw0 = TRUE)} %\u003e%\nset.DAG() %\u003e%\nsim(n=1000)\n```\n\nThe simulated data frame returned by `sim()` also contains the simulated network object, saved as a separate attribute. The network is saved as an `R6` object of class `NetIndClass`, under attribute called \"`netind_cl`\". The field \"`NetInd`\" contains the network matrix, the field \"`Kmax`\" contains the maximum number of friends (number of columns in `NetInd`) and the field \"`nF`\" contains the vector for total number of friends for each observation (see `?NetIndClass` for more information).\n\n```{r}\n(Kmax \u003c- attributes(dat.net)$netind_cl$Kmax)\nNetInd_mat \u003c- attributes(dat.net)$netind_cl$NetInd\nhead(NetInd_mat)\nnF \u003c- attributes(dat.net)$netind_cl$nF\nhead(nF)\n```\n\n### Citation\nTo cite `simcausal` in publications, please use:\n\u003e Sofrygin O, van der Laan MJ, Neugebauer R (2015). *simcausal: Simulating Longitudinal Data with Causal Inference Applications.* R package version 0.5.\n\n### Funding\nThe development of this package was partially funded through internal operational funds provided by the Kaiser Permanente Center for Effectiveness \u0026 Safety Research (CESR). This work was also partially supported through a Patient-Centered Outcomes Research Institute (PCORI) Award (ME-1403-12506) and an NIH grant (R01 AI074345-07).\n\n### Copyright\nThis software is distributed under the GPL-2 license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosofr%2Fsimcausal","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fosofr%2Fsimcausal","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosofr%2Fsimcausal/lists"}