## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
# Display the formula of a dosearch object as a LaTeX equation. If variants
# is TRUE, the variants of the formula for the values of the variables of
# case-by-case reasoning are displayed instead, each followed by the values.
show_formula <- function(x, variants = FALSE) {
  f <- get_formula(x, variants = variants)
  values <- names(f)
  if (is.null(values)) values <- ""
  values <- ifelse(nzchar(values), paste0("\\qquad (", values, ")"), "")
  cat(paste("$$", f, values, "$$\n"), sep = "")
}
# Print the steps of a derivation as "from -> to (rule)"
print_steps <- function(x) {
  lines <- strsplit(x$derivation, "\n")[[1L]]
  nodes <- regmatches(
    lines,
    regexec("^(n[0-9]+)\\[.*label=\"(.*)\"\\]$", lines)
  )
  nodes <- do.call(rbind, nodes[lengths(nodes) == 3L])
  labels <- stats::setNames(nodes[, 3L], nodes[, 2L])
  edges <- regmatches(
    lines,
    regexec("^(n[0-9]+) -> (n[0-9]+)\\[label=\"(.*)\"\\]$", lines)
  )
  edges <- do.call(rbind, edges[lengths(edges) == 4L])
  cat(
    sprintf(
      "%-18s -> %-18s (%s)",
      labels[edges[, 2L]],
      labels[edges[, 3L]],
      edges[, 4L]
    ),
    sep = "\n"
  )
}

## ----setup--------------------------------------------------------------------
library("dosearch")

## ----performance-figure, echo = FALSE, fig.width = 8, fig.height = 4, fig.align = "center", fig.cap = "(a) Search time per run for each graph size $n$ with versions 1.0.12 (grey) and 1.1.0 (blue), on a logarithmic scale. The 54 runs that ended before the search, with a search time of zero in both versions, are omitted. (b) Ratio of the search times of the two versions for the runs that took at least 1 ms with version 1.0.12, on a logarithmic scale. The boxes show the quartiles and the whiskers the 5th and 95th percentiles."----
# Base-10 logarithms of the 5th, 25th, 50th, 75th and 95th percentiles,
# one column per graph size n = 5, 6, 7, 8
time_old <- matrix(
  c(
    -0.6053, -0.0149, 0.3613, 0.5747, 0.7541,
    0.4441, 0.9877, 1.3535, 1.5618, 1.7147,
    0.7880, 1.7858, 2.1233, 2.3931, 2.6203,
    0.8767, 2.4692, 2.9795, 3.3002, 3.5294
  ),
  nrow = 5L
)
time_new <- matrix(
  c(
    -1.2669, -0.5648, -0.1400, 0.0974, 0.3096,
    -0.4142, 0.2671, 0.6073, 0.8599, 1.0480,
    -0.4734, 0.7980, 1.1909, 1.5161, 1.9036,
    -0.5640, 1.3301, 1.8792, 2.2879, 2.5874
  ),
  nrow = 5L
)
speedup <- matrix(
  c(
    0.3126, 0.4418, 0.5090, 0.5465, 0.6086,
    0.5642, 0.6486, 0.7267, 0.7951, 0.9972,
    0.6767, 0.8458, 0.9215, 1.0312, 1.2343,
    0.8460, 0.9874, 1.0740, 1.1849, 1.4527
  ),
  nrow = 5L
)
fill <- c("grey85", "#D2E0F0")
border <- c("grey40", "#1F64B4")
op <- par(mfrow = c(1L, 2L), mar = c(4.1, 4.6, 2.1, 1.1))
stats <- matrix(0, 5L, 8L)
stats[, c(1L, 3L, 5L, 7L)] <- time_old
stats[, c(2L, 4L, 6L, 8L)] <- time_new
bxp(
  list(stats = stats, n = rep(1L, 8L)),
  at = rep(1:4, each = 2L) + c(-0.19, 0.19),
  boxwex = 0.32,
  boxfill = rep(fill, 4L),
  border = rep(border, 4L),
  whisklty = 1L,
  staplelty = 1L,
  ylim = c(-1.5, 4),
  xlim = c(0.45, 4.55),
  axes = FALSE,
  main = "(a) Search time per run",
  cex.main = 0.9,
  font.main = 1
)
abline(h = -1:4, col = "grey92")
bxp(
  list(stats = stats, n = rep(1L, 8L)),
  at = rep(1:4, each = 2L) + c(-0.19, 0.19),
  boxwex = 0.32,
  boxfill = rep(fill, 4L),
  border = rep(border, 4L),
  whisklty = 1L,
  staplelty = 1L,
  axes = FALSE,
  add = TRUE
)
axis(1L, at = 1:4, labels = 5:8)
axis(2L, at = -1:4, labels = c("0.1", "1", "10", "100", "1000", "10000"), las = 1L)
box()
title(xlab = "Number of vertices n", ylab = "Search time (ms)")
legend(
  "topleft",
  legend = c("1.0.12", "1.1.0"),
  fill = fill,
  border = border,
  bty = "n"
)
bxp(
  list(stats = speedup, n = rep(1L, 4L)),
  boxwex = 0.32,
  boxfill = fill[2L],
  border = border[2L],
  whisklty = 1L,
  staplelty = 1L,
  ylim = c(0, 1.6),
  xlim = c(0.45, 4.55),
  axes = FALSE,
  main = "(b) Speedup per run",
  cex.main = 0.9,
  font.main = 1
)
abline(h = log10(c(1, 2, 5, 10, 20)), col = "grey92")
bxp(
  list(stats = speedup, n = rep(1L, 4L)),
  boxwex = 0.32,
  boxfill = fill[2L],
  border = border[2L],
  whisklty = 1L,
  staplelty = 1L,
  axes = FALSE,
  add = TRUE
)
axis(1L, at = 1:4, labels = 5:8)
axis(2L, at = log10(c(1, 2, 5, 10, 20)), labels = c(1, 2, 5, 10, 20), las = 1L)
box()
title(xlab = "Number of vertices n", ylab = "Speedup")
par(op)

## -----------------------------------------------------------------------------
dosearch(
  "p(y,z)",
  "p(y|do(x))",
  "x -> y \n z -> y",
  control = list(verbose = TRUE)
)

## -----------------------------------------------------------------------------
dosearch("p(y)", "p(y|do(x))", "y -> x")

## -----------------------------------------------------------------------------
dosearch(
  "p(y|x,z) \n p(x)",
  "p(y|do(x))",
  "z -> y \n x -> y",
  control = list(verbose = TRUE)
)

## -----------------------------------------------------------------------------
dosearch(
  "p(x,y|s)",
  "p(y|do(x))",
  "x -> y \n y -> s",
  selection_bias = "s",
  control = list(verbose = TRUE)
)

## -----------------------------------------------------------------------------
dosearch(
  "p(y|do(x),t)",
  "p(y|do(x))",
  "x -> y \n t -> y",
  transportability = "t",
  control = list(verbose = TRUE)
)

## -----------------------------------------------------------------------------
dosearch("p(x,y|s) \n p(y)", "p(y|do(x))", "x -> y \n y -> s", selection_bias = "s")

## -----------------------------------------------------------------------------
derived <- dosearch(
  "p(b,c,d|a) \n p(a,d|c)",
  "p(b|c,d)",
  "b -> a \n b -> d \n c -> d \n c <-> d"
)

## ----echo = FALSE, results = "asis"-------------------------------------------
show_formula(derived)

## -----------------------------------------------------------------------------
backdoor <- dosearch("p(x,y,z)", "p(y|do(x))", "x -> y \n z -> x \n z -> y")

## ----echo = FALSE, results = "asis"-------------------------------------------
show_formula(backdoor)

## -----------------------------------------------------------------------------
frontdoor <- dosearch("p(x,y,z)", "p(y|do(x))", "x -> z \n z -> y \n x <-> y")
frontdoor

## ----echo = FALSE, results = "asis"-------------------------------------------
show_formula(frontdoor)

## -----------------------------------------------------------------------------
data <- "
  p(x)
  p(v,w|x,do(y))
  p(y|v,w,do(x,z))
"
graph <- "
  v -> x
  x -> y
  w -> y
  v -> y
  x -> z
  w -> z
  y <-> v
  w <-> v
"
siblings <- dosearch(data, "p(y|do(x))", graph)

## ----echo = FALSE, results = "asis"-------------------------------------------
show_formula(siblings)

## -----------------------------------------------------------------------------
nested <- dosearch(
  "p(x) \n p(y|x) \n p(z|x,y)",
  "p(y|do(x))",
  "x -> y \n z -> x \n z -> y"
)

## ----echo = FALSE, results = "asis"-------------------------------------------
show_formula(nested)

## -----------------------------------------------------------------------------
casecontrol <- dosearch(
  "p(x*,y*,r_x,r_y) \n p(y)",
  "p(y|do(x))",
  "x -> y \n y -> r_y \n r_y -> r_x",
  missing_data = "r_x : x, r_y : y"
)

## ----echo = FALSE, results = "asis"-------------------------------------------
show_formula(casecontrol)

## -----------------------------------------------------------------------------
proxies <- dosearch(
  "p(x*,y*,r_x,r_y) \n p(y)",
  "p(x)",
  "x -> y \n y -> r_x \n y -> r_y",
  missing_data = "r_x : x, r_y : y",
  control = list(heuristic = TRUE)
)

## ----echo = FALSE, results = "asis"-------------------------------------------
show_formula(proxies)

## -----------------------------------------------------------------------------
observed_proxy <- dosearch(
  "p(x) \n p(y|x*,r_x = 1)",
  "p(x,y)",
  "x -> r_x",
  missing_data = "r_x : x"
)

## ----echo = FALSE, results = "asis"-------------------------------------------
show_formula(observed_proxy)

## -----------------------------------------------------------------------------
missing_proxy <- dosearch(
  "p(x) \n p(y|x*,r_x = 0)",
  "p(x,y)",
  "x -> r_x",
  missing_data = "r_x : x"
)

## ----echo = FALSE, results = "asis"-------------------------------------------
show_formula(missing_proxy)

## -----------------------------------------------------------------------------
data <- "
  p(y*,r_y)
  p(y|r_y = 0)
"
dosearch(data, "p(y)", "y -> r_y", missing_data = "r_y : y")
followup <- dosearch(
  data,
  "p(y)",
  "y -> r_y",
  missing_data = "r_y : y",
  case_reasoning = TRUE,
  control = list(draw_derivation = TRUE)
)
followup

## -----------------------------------------------------------------------------
print_steps(followup)

## -----------------------------------------------------------------------------
data <- "
  p(y*,r_y)
  p(y)
"
dosearch(data, "p(r_y = 1|y)", "y -> r_y", missing_data = "r_y : y")
dosearch(
  data,
  "p(r_y = 1|y)",
  "y -> r_y",
  missing_data = "r_y : y",
  case_reasoning = TRUE
)

## -----------------------------------------------------------------------------
missing_x <- dosearch(
  "p(x*,y*,r_x,r_y) \n p(y)",
  "p(y|r_x = 0)",
  "x -> y \n y -> r_y \n r_y -> r_x",
  missing_data = "r_x : x, r_y : y",
  case_reasoning = TRUE
)

## ----echo = FALSE, results = "asis"-------------------------------------------
show_formula(missing_x)

## -----------------------------------------------------------------------------
cases <- dosearch(
  "p(y,r_x = 1|z) \n p(y,r_x = 0|z)",
  "p(y,r_x|do(z))",
  "z -> y \n x -> r_x",
  missing_data = "r_x : x",
  case_reasoning = TRUE
)

## ----echo = FALSE, results = "asis"-------------------------------------------
show_formula(cases)

## -----------------------------------------------------------------------------
difference <- dosearch(
  "p(y,r_x = 1|z) \n p(y|z)",
  "p(y,r_x = 0|do(z))",
  "z -> y \n x -> r_x",
  missing_data = "r_x : x",
  case_reasoning = TRUE
)

## ----echo = FALSE, results = "asis"-------------------------------------------
show_formula(difference)

## ----echo = FALSE, results = "asis"-------------------------------------------
show_formula(cases, variants = TRUE)

## ----echo = FALSE, results = "asis"-------------------------------------------
show_formula(followup, variants = TRUE)
show_formula(missing_x, variants = TRUE)

## -----------------------------------------------------------------------------
cases
get_formula(cases, variants = TRUE)

## ----fig.width = 7, fig.height = 1.2, fig.align = "center"--------------------
plot_formula(missing_x)

## -----------------------------------------------------------------------------
data <- "
  p(x*,y*,r_x = 1,r_y = 1)
  p(x,y,r_x = 0,r_y = 0)
  p(x,y,r_x = 0,r_y = 1)
  p(x,y,r_x = 1,r_y = 0)
"
strata <- dosearch(
  data,
  "p(y|x,r_x,r_y)",
  "x -> y \n x -> r_x \n y -> r_y",
  missing_data = "r_x : x, r_y : y",
  case_reasoning = TRUE
)

## ----fig.width = 7, fig.height = 1.2, fig.align = "center"--------------------
plot_formula(strata)

## ----fig.width = 7, fig.height = 2, fig.align = "center"----------------------
plot_formula(strata, variants = TRUE)

