CausalDiscoSearch: R6 Interface to causalDisco Search Algorithms

CausalDiscoSearchR Documentation

R6 Interface to causalDisco Search Algorithms

Description

This class implements the search algorithms from the causalDisco package, which wraps and adds temporal order to pcalg algorithms. It allows to set the data, sufficient statistics, test, score, and algorithm.

Public fields

data

A data.frame holding the data set currently attached to the search object. Can be set with set_data().

score

A function that will be used to build the score, when data is set. Can be set with $set_score(). Recognized values are:

  • "tbic" - Temporal BIC score for Gaussian data. See TemporalBIC.

  • "tbdeu" - Temporal BDeu score for discrete data. See TemporalBDeu.

test

A function that will be used to test independence. Can be set with $set_test(). Recognized values are:

  • "fisher_z" - Fisher Z test for Gaussian data. See cor_test().

  • "fisher_z_twd" - Fisher Z test for Gaussian data with test-wise deletion. See micd::gaussCItwd().

  • "fisher_z_mi" - Fisher Z test for Gaussian data with multiple imputation. See micd::gaussCItestMI().

  • "reg" - Regression test for discrete or binary data. See reg_test().

  • "g_square" - G square test for discrete data. See pcalg::binCItest() and pcalg::disCItest().

  • "g_square_twd" - G square test for discrete data with test-wise deletion. See micd::disCItwd().

  • "g_square_mi" - G square test for discrete data with multiple imputation. See micd::disMItest().

  • "conditional_gaussian" - Test for conditional independence in mixed data. See micd::mixCItest().

  • "conditional_gaussian_twd" - Test for conditional independence in mixed data with test-wise deletion. See micd::mixCItwd().

  • "conditional_gaussian_mi" - Test for conditional independence in mixed data with multiple imputation. See micd::mixMItest().

alg

A function that will be used to run the search algorithm. Can be set with $set_alg(). Recognized values are:

  • "tfci" - TFCI algorithm. See tfci().

  • "tges" - TGES algorithm. See tges().

  • "tpc" - TPC algorithm. See tpc().

Can also be a user-defined function; see $set_alg() for details.

params

A list of parameters for the test and algorithm. Can be set with $set_params(). TODO: not secure yet in terms of distributing arguments. Use with caution.

suff_stat

Sufficient statistic. The format and contents of the sufficient statistic depends on which test is being used.

knowledge

A Knowledge object holding background knowledge.

Methods

Public methods


CausalDiscoSearch$new()

Constructor for the CausalDiscoSearch class.

Usage
CausalDiscoSearch$new()

CausalDiscoSearch$set_params()

Sets the parameters for the test and algorithm.

Usage
CausalDiscoSearch$set_params(params)
Arguments
params

A list of parameters to set.


CausalDiscoSearch$set_data()

Sets the data for the search algorithm.

Usage
CausalDiscoSearch$set_data(data, set_suff_stat = TRUE)
Arguments
data

A data.frame or a matrix containing the data.

set_suff_stat

Logical; whether to set the sufficient statistic.


CausalDiscoSearch$set_suff_stat()

Sets the sufficient statistic for the data.

Usage
CausalDiscoSearch$set_suff_stat()

CausalDiscoSearch$set_test()

Sets the test for the search algorithm.

Usage
CausalDiscoSearch$set_test(
  method,
  alpha = 0.05,
  suff_stat_fun = NULL,
  args = NULL
)
Arguments
method

[Experimental]

A string specifying the type of test to use.

Can also be a user-defined function with signature ⁠function(x, y, conditioning_set, suff_stat)⁠, where x and y are the variables being tested for independence, conditioning_set is the conditioning set, and suff_stat is the sufficient statistic for the test. If a user-defined function is provided, then suff_stat_fun must also be provided, which is a function that should take the data as input and returns a sufficient statistic for the test. Optionally, the signature of the user-defined test function can also include an args parameter, which is a list of additional arguments to pass to the test function. If args is provided, then the test function should have the signature ⁠function(x, y, conditioning_set, suff_stat, args)⁠, and the args parameter will be passed to the test function.

EXPERIMENTAL: user-defined tests syntax are subject to change.

alpha

Significance level for the test.

suff_stat_fun

A function that takes the data as input and returns a sufficient statistic for the test. Only needed if method is a user-defined function.

args

A list of additional arguments to pass to the test. Only needed if method is a user-defined function with an args parameter in its signature.


CausalDiscoSearch$set_score()

Sets the score for the search algorithm.

Usage
CausalDiscoSearch$set_score(method, params = list())
Arguments
method

A string specifying the type of score to use.

params

A list of parameters to pass to the score function.


CausalDiscoSearch$set_alg()

Sets the algorithm for the search.

Usage
CausalDiscoSearch$set_alg(method, type = c("constraint", "score"), args = NULL)
Arguments
method

[Experimental]

A string specifying the type of algorithm to use.

Can also be a user-defined function implementing a full search algorithm. Its required signature depends on type:

  • type = "constraint" (default) - signature function(data, knowledge, suff_stat, ...), analogous to tpc_run() and tfci_run().

  • type = "score" - signature function(score, ...), analogous to tges_run().

Optionally, the function's signature can also include an args parameter, which is a list of additional arguments to pass to the algorithm function. If args is provided, the function should have the signature function(data, knowledge, suff_stat, args) (or function(score, args) for type = "score"), and the args parameter will be passed to the algorithm function.

EXPERIMENTAL: user-defined algorithm syntax is subject to change.

type

Only used if method is a user-defined function. One of "constraint" (default) or "score", specifying whether the algorithm is constraint-based (called with data/knowledge/suff_stat) or score-based (called with score).

args

A list of additional arguments to pass to the algorithm. Only needed if method is a user-defined function with an args parameter in its signature.


CausalDiscoSearch$set_knowledge()

Sets the background knowledge for the search with a Knowledge object.

Usage
CausalDiscoSearch$set_knowledge(
  kn,
  directed_as_undirected = lifecycle::deprecated()
)
Arguments
kn

A Knowledge object.

directed_as_undirected

[Deprecated] This argument no longer has any effect and will be removed in a future release. Directed forbidden edges are now resolved using tier information, or must be specified in both directions.


CausalDiscoSearch$run_search()

Runs the search algorithm on the data.

Usage
CausalDiscoSearch$run_search(data = NULL, set_suff_stat = TRUE)
Arguments
data

A data.frame or a matrix containing the data.

set_suff_stat

Logical; whether to set the sufficient statistic


CausalDiscoSearch$clone()

The objects of this class are cloneable with this method.

Usage
CausalDiscoSearch$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

See Also

knowledge().

Examples

# Generally, we do not recommend using the R6 classes directly, but rather
# use the disco() or any method function, for example pc(), instead.

data(tpc_example)

# background knowledge (tiered knowledge)
kn <- knowledge(
  tpc_example,
  tier(
    child ~ starts_with("child"),
    youth ~ starts_with("youth"),
    old ~ starts_with("oldage")
  )
)

# Recommended (TPC example):
my_tpc <- tpc(engine = "causalDisco", test = "fisher_z", alpha = 0.05)
result <- disco(data = tpc_example, method = my_tpc, knowledge = kn)
plot(result)

# or
my_tpc <- my_tpc |>
  set_knowledge(kn)
result <- my_tpc(tpc_example)
plot(result)

# Using R6 class:

# --- Constraint-based: TPC ----------------------------------------------------
s_tpc <- CausalDiscoSearch$new()
s_tpc$set_params(list(verbose = FALSE))
s_tpc$set_test("fisher_z", alpha = 0.2)
s_tpc$set_alg("tpc")
s_tpc$set_knowledge(kn)
s_tpc$set_data(tpc_example)
res_tpc <- s_tpc$run_search()
print(res_tpc)

# Switch to TFCI on the same object (reuses suff_stat/test)
s_tpc$set_alg("tfci")
res_tfci <- s_tpc$run_search()
print(res_tfci)

# --- Score-based: TGES --------------------------------------------------------
s_tges <- CausalDiscoSearch$new()
s_tges$set_score("tbic") # Gaussian temporal score
s_tges$set_alg("tges")
s_tges$set_data(tpc_example, set_suff_stat = FALSE) # suff stat not used for TGES
s_tges$set_knowledge(kn)
res_tges <- s_tges$run_search()
print(res_tges)

# --- Custom (user-defined) algorithm: constraint-based -----------------------
my_alg <- function(data, knowledge, suff_stat) {
  tpc_run(
    data = data,
    knowledge = knowledge,
    suff_stat = suff_stat,
    test = reg_test
  )
}

s_custom <- CausalDiscoSearch$new()
s_custom$set_test("reg")
s_custom$set_knowledge(kn)
s_custom$set_alg(my_alg) # type = "constraint" by default
s_custom$set_data(tpc_example)
res_custom <- s_custom$run_search()
print(res_custom)

# --- Intentional error demonstrations ----------------------------------------

# run_search() without setting an algorithm
try(CausalDiscoSearch$new()$run_search(tpc_example))

# set_suff_stat() requires data and test first
s_err <- CausalDiscoSearch$new()
try(s_err$set_suff_stat()) # no data & no test
s_err$set_data(tpc_example, set_suff_stat = FALSE)
try(s_err$set_suff_stat()) # no test

# unknown test / score / algorithm
try(CausalDiscoSearch$new()$set_test("not_a_test"))
try(CausalDiscoSearch$new()$set_score("not_a_score"))
try(CausalDiscoSearch$new()$set_alg("not_an_alg"))

# set_knowledge() requires a `Knowledge` object
try(CausalDiscoSearch$new()$set_knowledge(list(not = "Knowledge")))

causalDisco documentation built on Nov. 17, 2026, 5:08 p.m.