{"id":32612834,"url":"https://github.com/uofuepibio/defm","last_synced_at":"2026-04-18T19:31:13.954Z","repository":{"id":144994487,"uuid":"477880742","full_name":"UofUEpiBio/defm","owner":"UofUEpiBio","description":"Estimation and simulation of Multi-binary response models","archived":false,"fork":false,"pushed_at":"2026-02-10T19:04:58.000Z","size":855,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-02-14T13:46:20.830Z","etag":null,"topics":["ergm","mle","modeling","r","rpackage","simulation","statistics"],"latest_commit_sha":null,"homepage":"https://uofuepibio.github.io/defm/","language":"C++","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/UofUEpiBio.png","metadata":{"files":{"readme":"README.md","changelog":"NEWS.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2022-04-04T21:33:13.000Z","updated_at":"2025-12-01T21:14:49.000Z","dependencies_parsed_at":"2025-07-03T01:21:05.147Z","dependency_job_id":"3ce267a1-6488-4f79-80dd-c5caedcdadaa","html_url":"https://github.com/UofUEpiBio/defm","commit_stats":{"total_commits":79,"total_committers":1,"mean_commits":79.0,"dds":0.0,"last_synced_commit":"f6928e4c63e33a57cbc89e0b8e660b77d5a57752"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/UofUEpiBio/defm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UofUEpiBio%2Fdefm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UofUEpiBio%2Fdefm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UofUEpiBio%2Fdefm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UofUEpiBio%2Fdefm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/UofUEpiBio","download_url":"https://codeload.github.com/UofUEpiBio/defm/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UofUEpiBio%2Fdefm/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31982441,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-18T17:30:12.329Z","status":"ssl_error","status_checked_at":"2026-04-18T17:29:59.069Z","response_time":103,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["ergm","mle","modeling","r","rpackage","simulation","statistics"],"created_at":"2025-10-30T14:57:24.520Z","updated_at":"2026-04-18T19:31:13.937Z","avatar_url":"https://github.com/UofUEpiBio.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n# defm: Fit and simulate discrete binary exponential family models\n\n\u003c!-- badges: start --\u003e\n\n[![CRAN\nstatus](https://www.r-pkg.org/badges/version/defm)](https://CRAN.R-project.org/package=defm)\n[![Grand\ntotal](https://cranlogs.r-pkg.org/badges/grand-total/defm)](https://CRAN.R-project.org/package=defm)\n[![R-CMD-check](https://github.com/UofUEpiBio/defm/actions/workflows/r.yml/badge.svg)](https://github.com/UofUEpiBio/defm/actions/workflows/r.yml)\n[![defm status\nbadge](https://uofuepibio.r-universe.dev/defm/badges/version)](https://uofuepibio.r-universe.dev/defm)\n\u003c!-- badges: end --\u003e\n\nDiscrete exponential family models (DEFM) have a long tradition with\nextensive development rooted in exponential random graph models (ERGMs.)\nApplicable to any form o data that can be represented as binary arrays,\nDEFMs provide a way to model jointly distributed binary variables.\n\nThis package, built on top of the C++ library\n[`barry`](https://github.com/USCbiostats/barry), provides a\ncomputationally efficient implementation of this family of models.\n\n# Installation\n\nYou can install `defm` using the\n\u003ca href=\"https://cran.r-project.org/package=remotes\"\ntarget=\"_blank\"\u003e\u003ccode\u003eremotes\u003c/code\u003e\u003c/a\u003e R package:\n\n``` r\nremotes::install_github(\"UofUEpi/defm\")\n```\n\nOr from our r-universe repository:\n\n    install.packages(\n      'defm',\n      repos = c(\n        'https://uofuepibio.r-universe.dev',\n        'https://cloud.r-project.org'\n      )\n    )\n\n# Examples\n\n## Example 1: Four outcomes\n\nIn this example, we will simulate a dataset that contains 1,000\nindividuals with four different outcomes. The outcomes, 0/1 vectors,\nwill be modeled as Markov processes of order one. Future states of this\n0/1 vector are a function of the previous point in time. The following\nlines simulate the baseline data:\n\n``` r\nlibrary(defm)\n#\u003e Loading required package: stats4\n\n# Simulation parameters\nset.seed(1231)\nn   \u003c- 5000L # Count of individuals\nn_y \u003c- 3L    # Number of dependent variables\nn_x \u003c- 2L    # Number of independent variables\n\n# Simulating how many observations we will have per individuals\nn_reps \u003c- sample(3:10, n, replace = TRUE)\n\n# Final number of rows in the data\nn_t \u003c- sum(n_reps)\n\n# Simulating the data\nY \u003c- matrix(as.integer(runif(n_y * n_t) \u003c .1), ncol = n_y)\ncolnames(Y) \u003c- paste0(\"y\", 1:n_y - 1)\nX \u003c- matrix(rnorm(n_x * n_t), ncol = n_x)\ncolnames(X) \u003c- paste0(\"x\", 1:n_x - 1L)\nid \u003c- rep(1L:n, n_reps)\ntime \u003c- unlist(sapply(n_reps, \\(x) 1:x))\n```\n\nHere is a brief look at the data structure. Remember, we still have not\nactually simulated data **WITH THE MODEL**.\n\n|  id | time |  y0 |  y1 |  y2 |    x0 |    x1 |\n|----:|-----:|----:|----:|----:|------:|------:|\n|   1 |    1 |   0 |   0 |   0 |  0.51 |  0.95 |\n|   1 |    2 |   0 |   0 |   0 |  0.16 |  0.25 |\n|   1 |    3 |   0 |   0 |   0 |  1.20 | -1.72 |\n|   1 |    4 |   0 |   1 |   0 | -0.20 |  1.55 |\n|   2 |    1 |   0 |   1 |   0 | -0.15 | -0.68 |\n|   2 |    2 |   1 |   0 |   0 |  1.19 |  0.92 |\n|   2 |    3 |   0 |   0 |   1 | -0.65 |  1.16 |\n|   2 |    4 |   0 |   0 |   0 | -0.99 |  0.21 |\n|   2 |    5 |   0 |   0 |   0 |  0.76 |  1.45 |\n|   2 |    6 |   0 |   0 |   0 | -0.68 | -1.34 |\n\nFor this example, we will simulate a model with the following features:\n\n- **Ones**: Baseline density (prevalence of ones),\n  ![\\sum\\_{itk}y\\_{itk}](https://latex.codecogs.com/svg.latex?%5Csum_%7Bitk%7Dy_%7Bitk%7D \"\\sum_{itk}y_{itk}\")\n\n- **Ones x Attr 2**: Same as before, but weighted by one of the\n  covariates. (simil to fixed effect)\n  ![\\sum\\_{itk}y\\_{itk}x\\_{it}](https://latex.codecogs.com/svg.latex?%5Csum_%7Bitk%7Dy_%7Bitk%7Dx_%7Bit%7D \"\\sum_{itk}y_{itk}x_{it}\")\n\n- **Transition** : And a transition structure, in particular\n  `y0 -\u003e (y0, y1)`,\n  ![y\\_{i0}^0y\\_{i0}^{t+1} y\\_{i1}^{t+1}](https://latex.codecogs.com/svg.latex?y_%7Bi0%7D%5E0y_%7Bi0%7D%5E%7Bt%2B1%7D%20y_%7Bi1%7D%5E%7Bt%2B1%7D \"y_{i0}^0y_{i0}^{t+1} y_{i1}^{t+1}\").\n\nIn `defm`, transition statistics can be represented using matrices. In\nthis case, the transition can be written as:\n\n![\\begin{array}{c}t\\\\t+1\\end{array}\\left\\[\\begin{array}{ccc}1 \u0026 \\cdot \u0026 \\cdot \\\\ 1 \u0026 1 \u0026 \\cdot\\end{array}\\right\\]](https://latex.codecogs.com/svg.latex?%5Cbegin%7Barray%7D%7Bc%7Dt%5C%5Ct%2B1%5Cend%7Barray%7D%5Cleft%5B%5Cbegin%7Barray%7D%7Bccc%7D1%20%26%20%5Ccdot%20%26%20%5Ccdot%20%5C%5C%201%20%26%201%20%26%20%5Ccdot%5Cend%7Barray%7D%5Cright%5D \"\\begin{array}{c}t\\\\t+1\\end{array}\\left[\\begin{array}{ccc}1 \u0026 \\cdot \u0026 \\cdot \\\\ 1 \u0026 1 \u0026 \\cdot\\end{array}\\right]\")\n\nwhich in `R` is\n\n``` r\n\u003e matrix(c(1,NA_integer_,NA_integer_,1,1,NA_integer_), nrow = 2, byrow = TRUE)\n#      [,1] [,2] [,3]\n# [1,]    1   NA   NA\n# [2,]    1    1   NA\n```\n\nThe `NA` entries in the matrix mean that those can be either zero or\none. In other words, only values different from `NA` will be considered\nfor specifying the terms. Here is the factory function:\n\n``` r\n# Creating the model and adding a couple of terms\nbuild_model \u003c- function(id., Y., X., order. = 1, par. = par.) {\n  \n  # Mapping the data to the C++ wrapper\n  d_model. \u003c- new_defm(id., Y., X., order = order.)\n\n  # Adding the model terms\n  td_ones(d_model.)\n  td_ones(d_model., covar = \"x1\")\n  \n  transition \u003c- matrix(NA_integer_, nrow = order. + 1, ncol = ncol(Y.))\n  transition[c(1,2,4)] \u003c- 1\n  \n  td_generic(d_model., transition)\n  \n  # Initializing the model\n  init_defm(d_model.)\n  \n  # Returning\n  d_model.\n  \n}\n```\n\nWith this factory function, we will use it to simulate some data with\nthe same dimensions of the original dataset. In this case, the\nparameters used for the simulation will be:\n\n- **Ones**: -2, i.e., low density,\n- **Ones x Attr 2**: 2, yet correlated with covariate \\# 2,\n- **Transition** : 5, And a high chance of observing the transition\n  `y0 -\u003e (y0, y1)`\n\n``` r\nsim_par \u003c- c(-2, 2, 5)\nd_model \u003c- build_model(id, Y, X, order = 1L, par. = sim_par)\nsimulated_Y \u003c- sim_defm(d_model, sim_par)\nhead(cbind(id, simulated_Y))\n#\u003e      id      \n#\u003e [1,]  1 0 0 0\n#\u003e [2,]  1 0 0 1\n#\u003e [3,]  1 0 0 0\n#\u003e [4,]  1 1 1 1\n#\u003e [5,]  2 0 1 0\n#\u003e [6,]  2 1 1 0\n```\n\nNow, let’s see if we can recover the parameters using MLE:\n\n``` r\ncolnames(simulated_Y) \u003c- paste0(\"y\", 1:n_y - 1)\nd_model_sim \u003c- build_model(id, simulated_Y, X, order = 1, par. = sim_par)\nans \u003c- defm_mle(d_model_sim)\n\nsummary(ans)\n#\u003e Maximum likelihood estimation\n#\u003e \n#\u003e Call:\n#\u003e stats4::mle(minuslogl = minuslog, start = start, method = \"L-BFGS-B\", \n#\u003e     nobs = nrow_defm(object) + ifelse(morder_defm(object) \u003e 0, \n#\u003e         -nobs_defm(object), 0L), lower = lower, upper = upper)\n#\u003e \n#\u003e Coefficients:\n#\u003e                         Estimate Std. Error\n#\u003e Num. of ones           -2.009506 0.01435063\n#\u003e Num. of ones x x1       2.020209 0.01592262\n#\u003e Motif {y0+}⇨{y0+, y1+}  5.051076 0.04649922\n#\u003e \n#\u003e -2 log L: 54960.47\n```\n\nOr better, we can use `texreg` to generate a pretty output:\n\n\u003ctable class=\"texreg\" style=\"margin: 10px auto;border-collapse: collapse;border-spacing: 0px;caption-side: bottom;color: #000000;border-top: 2px solid #000000;\"\u003e\n\n\u003ccaption\u003e\n\nStatistical models\n\u003c/caption\u003e\n\n\u003cthead\u003e\n\n\u003ctr\u003e\n\n\u003cth style=\"padding-left: 5px;padding-right: 5px;\"\u003e\n\n \n\u003c/th\u003e\n\n\u003cth style=\"padding-left: 5px;padding-right: 5px;\"\u003e\n\nModel 1\n\u003c/th\u003e\n\n\u003c/tr\u003e\n\n\u003c/thead\u003e\n\n\u003ctbody\u003e\n\n\u003ctr style=\"border-top: 1px solid #000000;\"\u003e\n\n\u003ctd style=\"padding-left: 5px;padding-right: 5px;\"\u003e\n\nNum. of ones\n\u003c/td\u003e\n\n\u003ctd style=\"padding-left: 5px;padding-right: 5px;\"\u003e\n\n-2.01 (0.01)\u003csup\u003e\\*\\*\\*\u003c/sup\u003e\n\u003c/td\u003e\n\n\u003c/tr\u003e\n\n\u003ctr\u003e\n\n\u003ctd style=\"padding-left: 5px;padding-right: 5px;\"\u003e\n\nNum. of ones x x1\n\u003c/td\u003e\n\n\u003ctd style=\"padding-left: 5px;padding-right: 5px;\"\u003e\n\n2.02 (0.02)\u003csup\u003e\\*\\*\\*\u003c/sup\u003e\n\u003c/td\u003e\n\n\u003c/tr\u003e\n\n\u003ctr\u003e\n\n\u003ctd style=\"padding-left: 5px;padding-right: 5px;\"\u003e\n\nMotif {y0+}⇨{y0+, y1+}\n\u003c/td\u003e\n\n\u003ctd style=\"padding-left: 5px;padding-right: 5px;\"\u003e\n\n5.05 (0.05)\u003csup\u003e\\*\\*\\*\u003c/sup\u003e\n\u003c/td\u003e\n\n\u003c/tr\u003e\n\n\u003ctr style=\"border-top: 1px solid #000000;\"\u003e\n\n\u003ctd style=\"padding-left: 5px;padding-right: 5px;\"\u003e\n\nAIC\n\u003c/td\u003e\n\n\u003ctd style=\"padding-left: 5px;padding-right: 5px;\"\u003e\n\n54966.47\n\u003c/td\u003e\n\n\u003c/tr\u003e\n\n\u003ctr\u003e\n\n\u003ctd style=\"padding-left: 5px;padding-right: 5px;\"\u003e\n\nBIC\n\u003c/td\u003e\n\n\u003ctd style=\"padding-left: 5px;padding-right: 5px;\"\u003e\n\n54991.16\n\u003c/td\u003e\n\n\u003c/tr\u003e\n\n\u003ctr style=\"border-bottom: 2px solid #000000;\"\u003e\n\n\u003ctd style=\"padding-left: 5px;padding-right: 5px;\"\u003e\n\nN\n\u003c/td\u003e\n\n\u003ctd style=\"padding-left: 5px;padding-right: 5px;\"\u003e\n\n27777\n\u003c/td\u003e\n\n\u003c/tr\u003e\n\n\u003c/tbody\u003e\n\n\u003ctfoot\u003e\n\n\u003ctr\u003e\n\n\u003ctd style=\"font-size: 0.8em;\" colspan=\"2\"\u003e\n\n\u003csup\u003e\\*\\*\\*\u003c/sup\u003ep \\\u003c 0.001; \u003csup\u003e\\*\\*\u003c/sup\u003ep \\\u003c 0.01; \u003csup\u003e\\*\u003c/sup\u003ep \\\u003c\n0.05\n\u003c/td\u003e\n\n\u003c/tr\u003e\n\n\u003c/tfoot\u003e\n\n\u003c/table\u003e\n\nWe can also see the counts\n\n| id | y0 | y1 | y2 | x0 | x1 | Num. of ones | Num. of ones x x1 | Motif {y0+}⇨{y0+, y1+} |\n|---:|---:|---:|---:|---:|---:|---:|---:|---:|\n| 1 | 0 | 0 | 0 | 0.51 | 0.95 | NA | NA | NA |\n| 1 | 0 | 0 | 1 | 0.16 | 0.25 | 1 | 0.25 | 0 |\n| 1 | 0 | 0 | 0 | 1.20 | -1.72 | 0 | 0.00 | 0 |\n| 1 | 1 | 1 | 1 | -0.20 | 1.55 | 3 | 4.66 | 0 |\n| 2 | 0 | 1 | 0 | -0.15 | -0.68 | NA | NA | NA |\n| 2 | 1 | 1 | 0 | 1.19 | 0.92 | 2 | 1.84 | 0 |\n| 2 | 1 | 1 | 1 | -0.65 | 1.16 | 3 | 3.48 | 1 |\n| 2 | 1 | 1 | 0 | -0.99 | 0.21 | 2 | 0.43 | 1 |\n| 2 | 1 | 1 | 1 | 0.76 | 1.45 | 3 | 4.35 | 1 |\n| 2 | 0 | 0 | 0 | -0.68 | -1.34 | 0 | 0.00 | 0 |\n\nFinally, we can also take a look at the distribution of the log odds. We\ncalculate this by looking at changes in a single entry of the array. For\nexample, the log-odds of having\n![y\\_{12}: 0\\to 1](https://latex.codecogs.com/svg.latex?y_%7B12%7D%3A%200%5Cto%201 \"y_{12}: 0\\to 1\"),\nwhich are equivalent to\n\n![\\log{\\frac{\\mbox{Pr}\\left(\\left.y\\_{12} = 1\\vphantom{\\mathbf{y}\\_{-12}}\\\\\\right\\|\\mathbf{y}\\_{-12}\\vphantom{y\\_{12} = 1}\\right)}{\\mbox{Pr}\\left(\\left.y\\_{12} = 0\\vphantom{\\mathbf{y}\\_{-12}}\\\\\\right\\|\\mathbf{y}\\_{-12}\\vphantom{y\\_{12} = 0}\\right)}} = \\mbox{logit}\\left(\\mbox{Pr}\\left(\\left.y\\_{12} = 1\\vphantom{\\mathbf{y}\\_{-12}}\\\\\\right\\|\\mathbf{y}\\_{-12}\\vphantom{y\\_{12} = 1}\\right)\\right)](https://latex.codecogs.com/svg.latex?%5Clog%7B%5Cfrac%7B%5Cmbox%7BPr%7D%5Cleft%28%5Cleft.y_%7B12%7D%20%3D%201%5Cvphantom%7B%5Cmathbf%7By%7D_%7B-12%7D%7D%5C%3B%5Cright%7C%5Cmathbf%7By%7D_%7B-12%7D%5Cvphantom%7By_%7B12%7D%20%3D%201%7D%5Cright%29%7D%7B%5Cmbox%7BPr%7D%5Cleft%28%5Cleft.y_%7B12%7D%20%3D%200%5Cvphantom%7B%5Cmathbf%7By%7D_%7B-12%7D%7D%5C%3B%5Cright%7C%5Cmathbf%7By%7D_%7B-12%7D%5Cvphantom%7By_%7B12%7D%20%3D%200%7D%5Cright%29%7D%7D%20%3D%20%5Cmbox%7Blogit%7D%5Cleft%28%5Cmbox%7BPr%7D%5Cleft%28%5Cleft.y_%7B12%7D%20%3D%201%5Cvphantom%7B%5Cmathbf%7By%7D_%7B-12%7D%7D%5C%3B%5Cright%7C%5Cmathbf%7By%7D_%7B-12%7D%5Cvphantom%7By_%7B12%7D%20%3D%201%7D%5Cright%29%5Cright%29 \"\\log{\\frac{\\mbox{Pr}\\left(\\left.y_{12} = 1\\vphantom{\\mathbf{y}_{-12}}\\;\\right|\\mathbf{y}_{-12}\\vphantom{y_{12} = 1}\\right)}{\\mbox{Pr}\\left(\\left.y_{12} = 0\\vphantom{\\mathbf{y}_{-12}}\\;\\right|\\mathbf{y}_{-12}\\vphantom{y_{12} = 0}\\right)}} = \\mbox{logit}\\left(\\mbox{Pr}\\left(\\left.y_{12} = 1\\vphantom{\\mathbf{y}_{-12}}\\;\\right|\\mathbf{y}_{-12}\\vphantom{y_{12} = 1}\\right)\\right)\")\n\nWe can use the `logodds` function for this:\n\n``` r\nlo \u003c- logodds(d_model_sim, coef(ans), 1,  2) \nhist(lo, main = \"Distribution of Log-odds for term Y(1,2)\",\n     col=\"tomato\", border = \"transparent\", breaks = 100)\nabline(v=0, lwd = 2, lty = 2, col = \"steelblue\")\n```\n\n\u003cimg src=\"man/figures/README-logodds-1.png\" style=\"width:100.0%\" /\u003e\n\n## Example 2: A fun model\n\nFor fun, imagine that we want to describe a process in which an\nindividual moves sequentially through a set of states. In this example,\nthere are ten different `y` variables, but the person can only have one\nof them as active (equal to one.) We can simulate such data using DEFM.\n\nWe first need to generate the baseline data for the simulation. This\ninvolves creating a matrix of size 20x10 (so we have 20 time points,)\nfilled with zeros in all but the first entry:\n\n``` r\nn   \u003c- 20L\nn_y \u003c- 10L\nid \u003c- rep(1L, n)\nY \u003c- matrix(0L, nrow = n, ncol = n_y)\nY[1] \u003c- 1L\nX \u003c- matrix(0.0, nrow = n, ncol = 1)\n```\n\nWith the data in hand, we can now simulate the process. First, we need\nto build the model. The key component of the model will be the\ntransition matrices:\n\n![\\left\\[\\begin{array}{cccc}\n1 \u0026 0 \u0026 NA \u0026 \\dots \\\\\n0 \u0026 1 \u0026 NA \u0026 \\dots\n\\end{array}\\right\\] \\dots \n\\left\\[\\begin{array}{cccccc}\n\\dots \u0026 NA \u0026 1 \u0026 0 \u0026 NA \u0026 \\dots  \\\\\n\\dots \u0026 NA \u0026 0 \u0026 1 \u0026 NA \u0026 \\dots  \n\\end{array}\\right\\] \\dots \n\\left\\[\\begin{array}{cccc}\n\\dots \u0026 NA \u0026 1 \u0026 0 \\\\\n\\dots \u0026 NA \u0026 0 \u0026 1 \n\\end{array}\\right\\]](https://latex.codecogs.com/svg.latex?%5Cleft%5B%5Cbegin%7Barray%7D%7Bcccc%7D%0A1%20%26%200%20%26%20NA%20%26%20%5Cdots%20%5C%5C%0A0%20%26%201%20%26%20NA%20%26%20%5Cdots%0A%5Cend%7Barray%7D%5Cright%5D%20%5Cdots%20%0A%5Cleft%5B%5Cbegin%7Barray%7D%7Bcccccc%7D%0A%5Cdots%20%26%20NA%20%26%201%20%26%200%20%26%20NA%20%26%20%5Cdots%20%20%5C%5C%0A%5Cdots%20%26%20NA%20%26%200%20%26%201%20%26%20NA%20%26%20%5Cdots%20%20%0A%5Cend%7Barray%7D%5Cright%5D%20%5Cdots%20%0A%5Cleft%5B%5Cbegin%7Barray%7D%7Bcccc%7D%0A%5Cdots%20%26%20NA%20%26%201%20%26%200%20%5C%5C%0A%5Cdots%20%26%20NA%20%26%200%20%26%201%20%0A%5Cend%7Barray%7D%5Cright%5D \"\\left[\\begin{array}{cccc}\n1 \u0026 0 \u0026 NA \u0026 \\dots \\\\\n0 \u0026 1 \u0026 NA \u0026 \\dots\n\\end{array}\\right] \\dots \n\\left[\\begin{array}{cccccc}\n\\dots \u0026 NA \u0026 1 \u0026 0 \u0026 NA \u0026 \\dots  \\\\\n\\dots \u0026 NA \u0026 0 \u0026 1 \u0026 NA \u0026 \\dots  \n\\end{array}\\right] \\dots \n\\left[\\begin{array}{cccc}\n\\dots \u0026 NA \u0026 1 \u0026 0 \\\\\n\\dots \u0026 NA \u0026 0 \u0026 1 \n\\end{array}\\right]\")\n\nLet’s take a look at the process:\n\n``` r\n# Creating a new instance of a DEFM object\ncolnames(Y) \u003c- paste0(\"y\", 0:(n_y - 1))\ncolnames(X) \u003c- \"x0\"\nd_model \u003c- new_defm(id = id, Y = Y, X = X, order = 1)\n\n# Creating the transition terms, these\nfor (i in (1:(n_y - 1) - 1)) {\n  transition \u003c- matrix(NA_integer_, nrow = 2, ncol = n_y)\n  transition[c(1:4) + 2 * i] \u003c- c(1,0,0,1)\n  td_generic(d_model, transition)\n}\n\n# Here is the last transition term\ntransition \u003c- matrix(NA_integer_, nrow = 2, ncol = n_y)\ntransition[c(n_y * 2 - 1, n_y * 2, 1, 2)] \u003c- c(1,0,0,1)\ntd_generic(d_model, transition)\n\n# Adding a term of ones\ntd_ones(d_model)\n\n# Initializing and simulating\ninit_defm(d_model)\nset.seed(33)\n(Y_sim \u003c-sim_defm(d_model, par = c(rep(200, n_y), -5)))\n#\u003e       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n#\u003e  [1,]    1    0    0    0    0    0    0    0    0     0\n#\u003e  [2,]    0    1    0    0    0    0    0    0    0     0\n#\u003e  [3,]    0    0    1    0    0    0    0    0    0     0\n#\u003e  [4,]    0    0    0    1    0    0    0    0    0     0\n#\u003e  [5,]    0    0    0    0    1    0    0    0    0     0\n#\u003e  [6,]    0    0    0    0    0    1    0    0    0     0\n#\u003e  [7,]    0    0    0    0    0    0    1    0    0     0\n#\u003e  [8,]    0    0    0    0    0    0    0    1    0     0\n#\u003e  [9,]    0    0    0    0    0    0    0    0    1     0\n#\u003e [10,]    0    0    0    0    0    0    0    0    0     1\n#\u003e [11,]    1    0    0    0    0    0    0    0    0     0\n#\u003e [12,]    0    1    0    0    0    0    0    0    0     0\n#\u003e [13,]    0    0    0    0    0    0    0    0    0     0\n#\u003e [14,]    0    0    0    0    0    0    0    0    0     0\n#\u003e [15,]    0    0    0    0    0    0    0    0    0     0\n#\u003e [16,]    0    0    0    0    0    0    0    0    0     0\n#\u003e [17,]    0    0    0    0    0    0    0    0    0     0\n#\u003e [18,]    0    0    0    0    0    0    0    0    0     0\n#\u003e [19,]    0    0    0    0    0    0    1    0    0     0\n#\u003e [20,]    0    0    0    0    0    0    0    0    0     0\n```\n\nThe simulation should produce a nice-looking figure:\n\n\u003cimg src=\"man/figures/README-chain1fig-1.png\" style=\"width:100.0%\" /\u003e\n\n## Example 3: Using formulas for transitions\n\nIn this example, we will redo the previous model, but now use formulas\nfor specifying the transitions:\n\n``` r\nd_model_formula \u003c- new_defm(id = id, Y = Y, X = X, order = 1)\n\n# We can use text formulas to add transition terms\nd_model_formula \u003c- d_model_formula + \n  \"{y0, 0y1} \u003e {0y0, y1}\" +\n  \"{y1, 0y2} \u003e {0y1, y2}\" +\n  \"{y2, 0y3} \u003e {0y2, y3}\" +\n  \"{y3, 0y4} \u003e {0y3, y4}\" +\n  \"{y4, 0y5} \u003e {0y4, y5}\" +\n  \"{y5, 0y6} \u003e {0y5, y6}\" +\n  \"{y6, 0y7} \u003e {0y6, y7}\" +\n  \"{y7, 0y8} \u003e {0y7, y8}\" +\n  \"{y8, 0y9} \u003e {0y8, y9}\" +\n  \"{0y0, y9} \u003e {y0, 0y9}\" \n\nd_model_formula |\u003e\n  td_ones()\n\ninit_defm(d_model_formula)\n\n# Inspecting\nd_model_formula\n#\u003e Num. of Arrays       : 19\n#\u003e Support size         : 2\n#\u003e Support size range   : [11, 20]\n#\u003e Arrays in powerset   : 2048\n#\u003e Transform. Fun.      : no\n#\u003e Model terms (11)    :\n#\u003e  - Motif {y0+, y1-}⇨{y0-, y1+}\n#\u003e  - Motif {y1+, y2-}⇨{y1-, y2+}\n#\u003e  - Motif {y2+, y3-}⇨{y2-, y3+}\n#\u003e  - Motif {y3+, y4-}⇨{y3-, y4+}\n#\u003e  - Motif {y4+, y5-}⇨{y4-, y5+}\n#\u003e  - Motif {y5+, y6-}⇨{y5-, y6+}\n#\u003e  - Motif {y6+, y7-}⇨{y6-, y7+}\n#\u003e  - Motif {y7+, y8-}⇨{y7-, y8+}\n#\u003e  - Motif {y8+, y9-}⇨{y8-, y9+}\n#\u003e  - Motif {y0-, y9+}⇨{y0+, y9-}\n#\u003e  - Num. of ones\n#\u003e Model rules (1)     :\n#\u003e  - Markov model of order 1\n#\u003e Model Y variables (10):\n#\u003e   0) y0\n#\u003e   1) y1\n#\u003e   2) y2\n#\u003e   3) y3\n#\u003e   4) y4\n#\u003e   5) y5\n#\u003e   6) y6\n#\u003e   7) y7\n#\u003e   8) y8\n#\u003e   9) y9\n\n# Simulating\nset.seed(33)\n(Y_sim_formula \u003c- sim_defm(d_model_formula, par = c(rep(200, n_y), -5)))\n#\u003e       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n#\u003e  [1,]    1    0    0    0    0    0    0    0    0     0\n#\u003e  [2,]    0    1    0    0    0    0    0    0    0     0\n#\u003e  [3,]    0    0    1    0    0    0    0    0    0     0\n#\u003e  [4,]    0    0    0    1    0    0    0    0    0     0\n#\u003e  [5,]    0    0    0    0    1    0    0    0    0     0\n#\u003e  [6,]    0    0    0    0    0    1    0    0    0     0\n#\u003e  [7,]    0    0    0    0    0    0    1    0    0     0\n#\u003e  [8,]    0    0    0    0    0    0    0    1    0     0\n#\u003e  [9,]    0    0    0    0    0    0    0    0    1     0\n#\u003e [10,]    0    0    0    0    0    0    0    0    0     1\n#\u003e [11,]    1    0    0    0    0    0    0    0    0     0\n#\u003e [12,]    0    1    0    0    0    0    0    0    0     0\n#\u003e [13,]    0    0    0    0    0    0    0    0    0     0\n#\u003e [14,]    0    0    0    0    0    0    0    0    0     0\n#\u003e [15,]    0    0    0    0    0    0    0    0    0     0\n#\u003e [16,]    0    0    0    0    0    0    0    0    0     0\n#\u003e [17,]    0    0    0    0    0    0    0    0    0     0\n#\u003e [18,]    0    0    0    0    0    0    0    0    0     0\n#\u003e [19,]    0    0    0    0    0    0    1    0    0     0\n#\u003e [20,]    0    0    0    0    0    0    0    0    0     0\n```\n\nThe new simulation…\n\n\u003cimg src=\"man/figures/README-chain2fig-1.png\" style=\"width:100.0%\" /\u003e\n\n# Acknowledgement\n\nThis work was supported by the Assistant Secretary of Defense for Health\nAffairs endorsed by the Department of Defense, through the Psychological\nHealth/Traumatic Brain Injury Research Program Long-Term Impact of\nMilitary-Relevant Brain Injury Consortium (LIMBIC)\nAward/W81XWH-18-PH/TBIRP-LIMBIC under Award No. I01 RX003443. The U.S.\nArmy Medical Research Acquisition Activity, 839 Chandler Street, Fort\nDetrick MD 21702-5014 is the awarding and administering acquisition\noffice. Opinions, interpretations, conclusions and recommendations are\nthose of the author and are not necessarily endorsed by the Department\nof Defense. Any opinions, findings, conclusions, or recommendations\nexpressed in this publication are those of the author(s) and do not\nnecessarily reflect the views of the U.S. Government, the U.S.\nDepartment of Veterans Affairs or the Department of Defense and no\nofficial endorsement should be inferred.\n\n# Code of Conduct\n\nPlease note that the `defm` project is released with a [Contributor Code\nof\nConduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html).\nBy contributing to this project, you agree to abide by its terms.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuofuepibio%2Fdefm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuofuepibio%2Fdefm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuofuepibio%2Fdefm/lists"}