Package {dosearch}


Title: Causal Effect Identification from Multiple Incomplete Data Sources
Version: 1.1.0
Description: Identification of causal effects from arbitrary observational and experimental probability distributions via do-calculus and standard probability manipulations using a search-based algorithm by Tikka, Hyttinen and Karvanen (2021) <doi:10.18637/jss.v099.i05>. Allows for the presence of mechanisms related to selection bias (Bareinboim and Tian, 2015) <doi:10.1609/aaai.v29i1.9679>, transportability (Bareinboim and Pearl, 2014) http://ftp.cs.ucla.edu/pub/stat_ser/r443.pdf, missing data (Mohan, Pearl, and Tian, 2013) http://ftp.cs.ucla.edu/pub/stat_ser/r410.pdf) and arbitrary combinations of these. Also supports identification in the presence of context-specific independence (CSI) relations through labeled directed acyclic graphs (LDAG). For details on CSIs see (Corander et al., 2019) <doi:10.1016/j.apal.2019.04.004>.
License: GPL (≥ 3)
Depends: R (≥ 4.0)
Suggests: covr, dagitty, DiagrammeR, igraph, knitr, mockr, rmarkdown, testthat (≥ 3.0.0)
Imports: graphics, Rcpp
LinkingTo: Rcpp
VignetteBuilder: knitr
BugReports: https://github.com/santikka/dosearch/issues
URL: https://github.com/santikka/dosearch
Encoding: UTF-8
NeedsCompilation: yes
Config/testthat/edition: 3
LazyData: true
Config/roxygen2/version: 8.1.1
Packaged: 2026-11-29 07:42:34 UTC; Santtu
Author: Santtu Tikka ORCID iD [aut, cre], Antti Hyttinen ORCID iD [ctb], Juha Karvanen ORCID iD [ctb]
Maintainer: Santtu Tikka <santtuth@gmail.com>
Repository: CRAN
Date/Publication: 2026-11-29 08:10:02 UTC

Causal Effect Identification from Multiple Incomplete Data Sources

Description

Solves causal effect identifiability problems from arbitrary observational and experimental distributions using a heuristic search. Allows for the presence of advanced data-generating mechanisms. See Tikka et al. (2021) https://doi.org/10.18637/jss.v099.i05 for further details.

See also

Author(s)

Santtu Tikka, Antti Hyttinen, Juha Karvanen

References

S. Tikka, A. Hyttinen and J. Karvanen. "Causal effect identification from multiple incomplete data sources: a general search-based approach." Journal of Statistical Software, 99(5):1–40, 2021.

See Also

Useful links:


Systematic Analysis of Bivariate Missing Data Problems

Description

This data set contains the results of a systematic analysis of all missing data problems of two variables. Each problem is associated with a graph containing two vertices, X and Y, and their response indicators, R_X and R_Y.

Usage

data(bivariate_missingness)

Format

A data frame with 6144 rows and 8 variables:

graph

the graph of the instance.

nedges

number of edges in the graph (directed and bidirected).

arrowXtoY

whether the graph contains an arrow from X to Y or not.

jointXY

identifiability of the joint distribution of X and Y

marginX

identifiability of the marginal distribution of X.

marginY

identifiability of the marginal distribution of Y.

YcondX

identifiability of the conditional distribution of Y given X.

YdoX

identifiability of the causal effect of X on Y.

Source

Tikka et al. https://arxiv.org/abs/1902.01073


Identify a Causal Effect from Arbitrary Experiments And Observations

Description

Identify a causal query from available data in a semi-Markovian causal model described by a graph that is a directed acyclic graph (DAG) or a labeled directed acyclic graph (LDAG). In a semi-Markovian causal model, each unobserved variable has exactly two children and they are denoted by bidirected edges. For DAGs, special mechanisms related to transportability of causal effects, recoverability from selection bias and identifiability under missing data can also be included. See 'Details' for the syntax of each argument. Note that all character type arguments are case-sensitive.

is_identifiable returns the a logical value describing the identifiability of a causal query of an object of class dosearch.

get_formula returns the identifying formula describing a causal query of an object of class dosearch. If no formula is available, returns NULL.

get_derivation returns the derivation of a causal query of an object of class dosearch. If no derivation is available, returns NULL.

get_benchmark returns the benchmarking information of an object of class dosearch. If no benchmark is available, returns NULL.

Usage

dosearch(
  data,
  query,
  graph,
  transportability = NULL,
  selection_bias = NULL,
  missing_data = NULL,
  control = list(),
  case_reasoning = FALSE
)

## S3 method for class 'dosearch'
summary(object, ...)

## S3 method for class 'dosearch'
plot(x, ...)

## S3 method for class 'dosearch'
print(x, max_chars = 300L, ...)

is_identifiable(x)

get_formula(x, run_again = FALSE, variants = FALSE)

get_derivation(x, run_again = FALSE, draw_all = FALSE)

get_benchmark(x, run_again = FALSE, include_rules = FALSE)

Arguments

data

A character string describing the available distributions in the package syntax. Alternatively, a list of character vectors.

query

A character string describing the target distribution in the package syntax. Alternatively, a character vector.

graph

A character string describing either a DAG or an LDAG in the package syntax. Alternatively, an igraph graph as used in the causaleffect package or a DAG constructed using the dagitty package.

transportability

A character string describing the transportability nodes of the model in the package syntax (for DAGs only).

selection_bias

A character string describing the selection bias nodes of the model in the package syntax (for DAGs only).

missing_data

A character string describing the missing data mechanisms of the model in the package syntax (for DAGs only).

control

A list of control parameters.

case_reasoning

A logical value. If TRUE, the search also uses the rules for case-by-general, case-by-case and general-by-case reasoning on the values of the missing data mechanisms, i.e., rules -9, 11, -11, 12, 13 and -13 (see rules of control). These rules are added to the default rules or to the rules given via control. The default is FALSE (for DAGs with missing data mechanisms only).

object

An object of class dosearch.

...

Additional arguments passed to base::format().

x

An object of class dosearch.

max_chars

Maximum number of characters of the formula to display. The default is 300.

run_again

If TRUE, runs the search again in an attempt to obtain the formula, for example if control$formula was FALSE in the call to dosearch(), but the query itself is identifiable.

variants

If TRUE, returns the variants of the formula for the values of the variables of case-by-case reasoning instead (see variants in the value of dosearch()). If case-by-case reasoning was not used, the formula itself is returned.

draw_all

A logical value. If TRUE, the derivation will contain every step taken by the search. If FALSE, only steps that resulted in identification are returned.

include_rules

A logical value. If TRUE, also benchmarks the time taken by each inference rule separately.

Details

Argument data is used to list the available input distributions. When graph is a DAG the distributions should be of the form

P(A_i|do(B_i),C_i)

Individual variables within sets should be separated by a comma. For example, three input distributions:

P(Z|do(X)), P(W,Y|do(Z,X)), P(W,Y,X|Z)

should be given as follows:

> data <- "
+  P(Z|do(X))
+  P(W,Y|do(Z,X))
+  P(W,Y,X|Z)
+"

The use of multiple do-operators is not permitted. Furthermore, when both conditioning variables and a do-operator are present, every conditioning variable must either precede the do-operator or follow it. When graph is an LDAG, the do-operation is represented by an intervention node, i.e.,

P(Y|do(X),Z) = P(Y|X,Z,I_X = 1)

For example, in the case of the previous example in an LDAG, the three input distributions become:

> data <- "
+  P(Z|X,I_X = 1)
+  P(W,Y|Z,X,I_X=1,I_Z=1)
+  P(W,Y,X|Z)
+"

The intervention nodes I_X and I_Z must be explicitly defined in the graph along with the relevant labels for the edges.

Argument query is the target distribution of the search. It has the same syntax as data, but only a single distribution should be given. Multiple simultaneous target distributions are not supported.

Argument graph is a description of a directed acyclic graph where directed edges are denoted by ⁠->⁠ and bidirected arcs corresponding to unobserved confounders are denoted by ⁠<->⁠ (or by ⁠--⁠). As an example, a DAG with two directed edges and one bidirected edge is constructed as follows:

> graph <- "
+  X -> Z
+  Z -> Y
+  X <-> Y
+"

Some alternative formats for DAGs are supported as well. Graphs created using the igraph package in the causal.effect package syntax can be used for dosearch as well. DAGs created using the dagitty package are also supported. Note that both time and space complexity of the underlying search algorithm are exponential in the number of vertices, but instances with up to ten nodes are routinely solved in under a second.

LDAGs are constructed similarly with the addition of labels and with the omission bidirected edges (latent variables must be explicitly defined). As an example, an LDAG with two labeled edges can be constructed as follows:

> graph <- "
+  X -> Z : A = 0
+  Z -> Y : A = 1
+  A -> Z
+  A -> Y
+"

Here the labels indicate that the edge from X to Z vanishes when A has the value 0 and the edge from Z to Y vanishes when A has the value 1. Multiple labels on the same edge should be separated by a semi-colon, and individual assignments within each label should be separated by a comma.

Argument transportability enumerates the nodes that should be understood as transportability nodes responsible for discrepancies between domains. Individual variables should be separated by a comma. See e.g., (Bareinboim and Pearl, 2014) for details on transportability.

Argument selection_bias enumerates the nodes that should be understood as selection bias nodes responsible for bias in the input data sets. Individual variables should be separated by a comma. See e.g., (Bareinboim and Tian, 2015) for details on selection bias recoverability.

Argument missing_data enumerates the missingness mechanisms of the model. The syntax for a single mechanism is M_X : X where MX is the mechanism for X. Individual mechanisms should be separated by a comma. If MX or X is not present in the graph, it is added to the graph with no edges other than the edge to the proxy variable of X, i.e., it is independent of the other variables of the graph, and a warning is issued by default. Proxy variables should not be included in the graph, since they are automatically generated based on missing_data. By default, a warning is issued if a proxy variable is present in an input distribution but its corresponding mechanism is not present in any input. See e.g., (Mohan, Pearl and Tian, 2013) for details on missing data as a causal inference problem. Note that dosearch is not complete for missing data problems, meaning that if dosearch is not able to identify the query, it might still be identifiable via some other means.

Missing data mechanisms can be assigned values in data and query. The value 1 denotes an enabled mechanism, i.e., the variable is observed and its proxy variable is equal to the true variable, and the value 0 denotes a disabled mechanism, i.e., the variable is missing. For example, ⁠P(Y*,R_Y = 1)⁠ is the distribution of the observed values of Y, and ⁠P(Y|R_Y = 0)⁠ is the distribution of Y among the units for which Y is missing, as obtained from a follow-up study of non-respondents. No other values are permitted. In addition to the rules of Tikka et al. (2021), the search can use rules analogous to the case-by-case, general-by-case and case-by-general reasoning of Tikka et al. (2019) for the values of the mechanisms by setting case_reasoning = TRUE. These rules are typically needed when mechanisms are disabled in the input or target distributions, and they can also identify queries that are not identifiable otherwise when the inputs originate from different sources. However, they increase the size of the search space, so they are not used by default.

Before the search is started, the target is checked against criteria that can show it to be non-identifiable directly from the inputs, in which case the search is not carried out (when improve = TRUE in control). The target is non-identifiable by do-search when a variable on the left-hand side of the target does not appear on the left-hand side of any input distribution (Tikka et al., 2021). For DAGs without missing data mechanisms, two additional criteria are used. The first applies when a variable on the right-hand side of the target does not appear in any input distribution but the target depends on it, and the second applies when every input distribution of some variable of the left-hand side of the target also conditions on, or intervenes on, a neighbor of the variable in the graph that the rules of the search can never remove, but the target does not. See the vignette "New features in dosearch 1.1.0" for the precise criteria and their proofs. With verbose = TRUE in control, the criterion that applies is reported.

The control argument is a list that can supply any of the following components:

Value

dosearch returns an object of class dosearch which is a list with the following components by default. See the control options on how to obtain a graphical representation of the derivation or how to benchmark the search.

summary returns a summary.dosearch object.

plot returns a htmlwidget object or NULL (invisibly)

print returns x invisibly.

is_identifiable returns a logical value. If TRUE, the target distribution was identifiable from the available inputs.

get_formula returns a character string representing the query in terms of the input data or NULL if the query is not identifiable. If variants is TRUE, the variants are returned as a named character vector when they are available.

get_derivation returns a graphical representation of the derivation steps that resulted in identification. The return object is a character string in DOT syntax.

get_benchmark returns a list with one or two elements or NULL. The first element of the list is always a numeric value of the total time taken by the search in milliseconds. The second is a numeric vector of the time taken by each inference rule (in the internal C++ implementation) of the search in milliseconds if include_rules is TRUE.

References

S. Tikka, A. Hyttinen, J. Karvanen. "Causal Effect Identification from Multiple Incomplete Data Sources: A General Search-based Approach." Journal of Statistical Software, 99(5):1–40, 2021.

E. Bareinboim, J. Pearl. "Transportability from Multiple Environments with Limited Experiments: Completeness Results." In Proceedings of the 27th Annual Conference on Neural Information Processing Systems, 280–288, 2014.

E. Bareinboim, J. Tian. "Recovering Causal Effects from Selection Bias " In Proceedings of the 29th AAAI Conference on Artificial Intelligence, 3475–3481, 2015.

K. Mohan, J. Pearl, J. Tian. "Graphical Models for Inference with Missing Data." In Proceedings of the 26th International Conference on Neural Information Processing Systems, 1277–1285, 2013.

S. Tikka, A. Hyttinen, J. Karvanen. "Identifying Causal Effects via Context-Specific Independence Relations." In Proceedings of the 33rd Annual Conference on Neural Information Processing Systems, 2019.

Examples

# A simple back-door formula
data1 <- "P(x,y,z)"
query1 <- "P(y|do(x))"
graph1 <- "
  x -> y
  z -> x
  z -> y
"
dosearch(data1, query1, graph1)

# A simple front-door formula
data2 <- "P(x,y,z)"
query2 <- "P(y|do(x))"
graph2 <- "
  x -> z
  z -> y
  x <-> y
"
dosearch(data2, query2, graph2)

# A scenario with combined transportability and selection bias
data <- "
  p(x,z,y|s)
  p(y,z|t,do(x))
"
query <- "p(y|do(x))"
graph <- "
  x -> z
  z -> y
  x -> s
  t -> z
  x <-> y
"
dosearch(
  data,
  query,
  graph,
  transportability = "t",
  selection_bias = "s",
  control = list(heuristic = TRUE, improve = FALSE)
)

# A simple case-control design
data <- "
  p(x*,y*,r_x,r_y)
  p(y)
"
graph <- "
  x -> y
  y -> r_y
  r_y -> r_x
"
md <- "r_x : x, r_y : y"
dosearch(data, query, graph, missing_data = md)

# The value 0 denotes a disabled missing data mechanism. Here y is
# missing not at random, but a follow-up study of the non-respondents
# provides the distribution of y when it is missing in the original data.
# Combining the cases requires case-by-case reasoning.
data <- "
  p(y*,r_y)
  p(y|r_y = 0)
"
dosearch(
  data,
  "p(y)",
  "y -> r_y",
  missing_data = "r_y : y",
  case_reasoning = TRUE
)

# Graph input using 'igraph' in the 'causaleffect' syntax
if (requireNamespace("igraph", quietly = TRUE)) {
  g_igraph <- igraph::graph_from_literal(
    x -+ z, z -+ y, x -+ y, y -+ x,
    simplify = FALSE
  )
  g_igraph <- igraph::set.edge.attribute(g_igraph, "description", 3:4, "U")
  dosearch(data2, query2, g_igraph)
}

# Graph input with 'dagitty'
if (requireNamespace("dagitty", quietly = TRUE)) {
  g_dagitty <- dagitty::dagitty("dag{x -> z -> y; x <-> y}")
  dosearch(data2, query2, g_dagitty)
}

# Alternative distribution input style using lists and vectors:
# Each element of the list describes a single distribution
# Each element is a character vector that describes the role
# of each variable in the distribution as follows:
# For a variable V and a distribution P(A|do(B),C) we have
#   V = 0, if V is in A
#   V = 1, if V is in B
#   V = 2, if V is in C
data_alt <- list(
  c(x = 0, y = 0, z = 0) # = P(x,y,z)
)
query_alt <- c(x = 1, y = 0) # = P(y|do(x))
dosearch(data_alt, query_alt, graph2)

## Not run: 
# Additional examples
# Multiple input distributions (both observational and interventional)
data3 <- "
  p(z_2,x_2|do(x_1))
  p(z_1|x_2,do(x_1,y))
  p(x_1|w,do(x_2))
  p(y|z_1,z_2,x_1,do(x_2))
  p(w|y,x_1,do(x_2))
"
query3 <- "p(y,x_1|w,do(x_2))"
graph3 <- "
  x_1 -> z_2
  x_1 -> z_1
  x_2 -> z_1
  x_2 -> z_2
  z_1 -> y
  z_2 -> y
  x_1 -> w
  x_2 -> w
  z_1 -> w
  z_2 -> w
"
dosearch(data3, query3, graph3)

# Selection bias
data4 <- "
  p(x,y,z_1,z_2|s)
  p(z_1,z_2)
"
query4 <- "p(y|do(x))"
graph4 <- "
  x   -> z_1
  z_1 -> z_2
  x   -> y
  y   -- z_2
  z_2 -> s
"
dosearch(data4, query4, graph4, selection_bias = "s")

# Transportability
data5 <- "
  p(x,y,z_1,z_2)
  p(x,y,z_1|t_1,t_2,do(z_2))
  p(x,y,z_2|t_3,do(z_1))
"
query5 <- "p(y|do(x))"
graph5 <- "
  z_1 -> x
  x   -> z_2
  z_2 -> y
  z_1 <-> x
  z_1 <-> z_2
  z_1 <-> y
  t_1 -> z_1
  t_2 -> z_2
  t_3 -> y
"
dosearch(data5, query5, graph5, transportability = "t_1, t_2, t_3")

# Missing data
# Proxy variables are denoted by an asterisk (*)
data6 <- "
  p(x*,y*,z*,m_x,m_y,m_z)
"
query6 <- "p(x,y,z)"
graph6 <- "
  z -> x
  x -> y
  x -> m_z
  y -> m_z
  y -> m_x
  z <-> y
"
# The mechanism m_y is independent of the other variables, so it is not
# present in the graph, which would produce a warning by default
dosearch(
  data6,
  query6,
  graph6,
  missing_data = "m_x : x, m_y : y, m_z : z",
  control = list(warn = FALSE)
)

# An LDAG
data7 <- "P(X,Y,Z)"
query7 <- "P(Y|X,I_X=1)"
graph7 <- "
  X -> Y : Z = 1
  Z -> Y
  Z -> X : I_X = 1
  I_X -> X
  H -> X : I_X = 1
  H -> Z
  Q -> Z
  Q -> Y : Z = 0
"
dosearch(data7, query7, graph7)

# A more complicated LDAG
# with multiple assignments for the edge X -> Z

data8 <- "P(X,Y,Z,A,W)"
query8 <- "P(Y|X,I_X=1)"
graph8 <- "
  I_X -> X
  I_Z -> Z
  A -> W
  Z -> Y
  A -> Z
  X -> Z : I_Z = 1; A = 1
  X -> Y : A = 0
  W -> X : I_X = 1
  W -> Y : A = 0
  A -> Y
  U -> X : I_X = 1
  U -> Y : A = 1
"
dosearch(data8, query8, graph8)


## End(Not run)

data <- "p(x,y,z)"
query <- "p(y|do(x))"
graph <- "
  x -> y
  z -> x
  z -> y
"
x <- dosearch(data, query, graph)
y <- summary(x)

## Not run: 
out <- dosearch(
  "p(x,y,z, w)",
  "p(y|do(x))",
  "x -> y \n z -> x \n w -> z \n x <-> w \n w <-> y",
  control = list(draw_derivation = TRUE)
)
if (requireNamespace("DiagrammeR", quietly = TRUE)) {
  plot(out)
}

## End(Not run)

data <- "p(x,y,z)"
query <- "p(y|do(x))"
graph <- "
  x -> z
  z -> y
  x <-> y
"
x <- dosearch(data, query, graph)
print(x)

data <- "P(x,y,z)"
query <- "P(y|do(x))"
graph <- "
  x -> y
  z -> x
  z -> y
"
x <- dosearch(data, query, graph)
is_identifiable(x)
# TRUE

data <- "P(x,y,z)"
query <- "P(y|do(x))"
graph <- "
  x -> y
  z -> x
  z -> y
"
x <- dosearch(data, query, graph, control = list(formula = FALSE))
get_formula(x, run_again = TRUE)

# The variants of a formula derived by case-by-case reasoning
x <- dosearch(
  "p(y*,r_y = 1) \n p(y,r_y = 0)",
  "p(y|r_y)",
  "y -> r_y",
  missing_data = "r_y : y",
  case_reasoning = TRUE
)
get_formula(x, variants = TRUE)

data <- "P(x,y,z)"
query <- "P(y|do(x))"
graph <- "
  x -> y
  z -> x
  z -> y
"
x <- dosearch(data, query, graph, control = list(draw_derivation = FALSE))
get_derivation(x, run_again = TRUE)
data <- "P(x,y,z)"
query <- "P(y|do(x))"
graph <- "
  x -> y
  z -> x
  z -> y
"
x <- dosearch(data, query, graph, control = list(benchmark = FALSE))
get_benchmark(x, run_again = TRUE)


Plot the Formula of a Causal Query

Description

plot_formula draws the formula of an object of class dosearch on the current graphics device with the mathematics rendered, e.g., summations with their limits, fractions, subscripts, superscripts and primes. The formula is converted to a grDevices::plotmath expression, so no LaTeX installation is needed.

Usage

plot_formula(x, variants = FALSE, max_chars = 500L, cex = 1.5, ...)

Arguments

x

An object of class dosearch.

variants

A logical value. If TRUE, the variants of the formula for the values of the variables of case-by-case reasoning (see variants in the value of dosearch()) are drawn one below the other, each preceded by the values of the variables. If the formula has no variants, the formula itself is drawn. The default is FALSE.

max_chars

The maximum number of characters of a formula to plot. The default is 500.

cex

A numeric value giving the character expansion of the text. The text is shrunk further if it does not fit the plot region. The default is 1.5.

...

Additional arguments passed to graphics::text(), e.g., col or family. The variants are drawn as a left-aligned block, so adj cannot be given.

Details

The formula is drawn on a single line without line breaks, and the text is shrunk to fit the plot region, so the function is intended for formulas of moderate length. Formulas longer than max_chars characters (as given by get_formula()) are not plotted.

The name of a variable is split at its first underscore into a name and a subscript, e.g., r_x is drawn as r_x. Proxy variables are drawn with an asterisk as a superscript, and primes as prime marks, e.g., ⁠y^{\ast\prime}⁠ is drawn as y^{*\prime}. A variable name that contains other characters than ASCII letters, digits, periods and underscores, or that begins or ends with an underscore, is cleaned for plotting: non-ASCII characters are transliterated, the other characters are replaced by periods, and the underscores at the beginning and the end are removed. A warning lists the names that were changed. The formula itself is not changed.

The mathematical symbols are drawn with the symbol font of the graphics device. The grDevices::pdf() device does not embed this font in the file, so PDF viewers may substitute another font, which can affect the spacing around the symbols. The grDevices::cairo_pdf() device embeds the fonts.

Value

The plotted formulas as an expression vector (invisibly), which can also be drawn by other functions that support grDevices::plotmath, or NULL (invisibly) if no formula is available.

Examples

data <- "p(x,y,z)"
query <- "p(y|do(x))"
graph <- "
  x -> z
  z -> y
  x <-> y
"
x <- dosearch(data, query, graph)
plot_formula(x)

# The variants of a formula derived by case-by-case reasoning
x <- dosearch(
  "p(y*,r_y = 1) \n p(y,r_y = 0)",
  "p(y|r_y)",
  "y -> r_y",
  missing_data = "r_y : y",
  case_reasoning = TRUE
)
plot_formula(x, variants = TRUE)


Print the Summary of a dosearch Object

Description

Print the Summary of a dosearch Object

Usage

## S3 method for class 'summary.dosearch'
print(x, max_chars = 300L, ...)

Arguments

x

An object of class summary.dosearch.

max_chars

Maximum number of characters of the formula to display. The default is 300.

...

Not used.

Value

x (invisibly)

Examples

data <- "p(x,y,z)"
query <- "p(y|do(x))"
graph <- "
  x -> y
  z -> x
  z -> y
"
x <- dosearch(data, query, graph)
y <- summary(x)
print(y)