## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4.2)
library(silentema)

## ----first--------------------------------------------------------------------
g <- dm_graph("M0")
g
g$edges[1:6, ]

## ----motifs, fig.height = 3.6-------------------------------------------------
for (m in c("M1", "M2", "M3")) plot(dm_graph(m))

## ----motifs2, fig.height = 3.6------------------------------------------------
for (m in c("M4", "M6")) plot(dm_graph(m))
plot(dm_graph("M5", context = "observed"))

## ----combine, fig.height = 4.6------------------------------------------------
g <- dm_graph(c("M2", "M4"), sensor = TRUE, probe = TRUE)
g
plot(g)

## ----dsep---------------------------------------------------------------------
dsep(dm_graph("M1"), "R3", "X3", c("X2", "eta"))   # lagged-state dependence: yes
dsep(dm_graph("M2"), "R3", "X3", c("X2", "eta"))   # self-censoring: no
dsep(dm_graph("M4"), "R3", "X3", c("X2", "eta"))   # the person intercept blocks zeta -> R and U -> eta
dsep(dm_graph("M4"), "R3", "X3", "X2")             # without it, R and X are associated through U

## ----dsep2--------------------------------------------------------------------
chain <- list(nodes = c("A", "B", "C"), edges = cbind(from = c("A", "B"), to = c("B", "C")))
c(marginal = dsep(chain, "A", "C"), given_B = dsep(chain, "A", "C", "B"))
collider <- list(nodes = c("A", "B", "C"), edges = cbind(from = c("A", "C"), to = c("B", "B")))
c(marginal = dsep(collider, "A", "C"), given_B = dsep(collider, "A", "C", "B"))

## ----rec1---------------------------------------------------------------------
recoverability(dm_graph("M1"))

## ----table--------------------------------------------------------------------
decls <- list("M0", "M1", "M2", "M3", "M4", "M5", "M6", c("M1", "M3"), c("M1", "M4"),
              c("M3", "M4"), c("M2", "M4"), c("M2", "M3"))
tab <- do.call(rbind, lapply(decls, function(m) {
  r <- recoverability(dm_graph(m))
  data.frame(motifs = paste(m, collapse = "+"),
             kernel = r$recoverable[1], mean_obs = r$recoverable[2], mean_dyn = r$recoverable[3],
             law_person = r$recoverable[4], law_prompt = r$recoverable[5],
             silence_null = attr(r, "silence_null"))
}))
tab

## ----m1m3---------------------------------------------------------------------
r13 <- recoverability(dm_graph(c("M1", "M3")))
r13$estimator[grepl("silence", r13$estimand)]

## ----context------------------------------------------------------------------
recoverability(dm_graph("M5", context = "observed"))[1, c("recoverable", "estimator")]

## ----sensor-------------------------------------------------------------------
rs <- recoverability(dm_graph(c("M1", "M3"), sensor = TRUE))
rs$estimator[grepl("sensor", rs$estimand)]

## ----probe--------------------------------------------------------------------
rp <- recoverability(dm_graph("M2", probe = TRUE))
rp[rp$estimand == "transition kernel from probe prompts", c("recoverable", "estimator")]

