BnlearnSearch: R6 Interface to bnlearn Search Algorithms

BnlearnSearchR Documentation

R6 Interface to bnlearn Search Algorithms

Description

A wrapper that lets you drive bnlearn algorithms within the causalDisco framework. For arguments to the test, score, and algorithm, see the bnlearn documentation.

Value

An R6 object with the methods documented below.

Public fields

data

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

score

Character scalar naming the score function used in bnlearn. Can be set with $set_score(). Kebab-case score names (as used in bnlearn, e.g. "pred-loglik") are also accepted and automatically translated to snake_case. Recognised values are:

Continuous - Gaussian

  • "aic_g", "bic_g", "ebic_g", "loglik_g", "pred_loglik_g" - gaussian versions of the respective scores for discrete data.

  • "bge" - Gaussian posterior density.

  • "nal_g" - node-average log-likelihood.

  • "pnal_g" - penalised node-average log-likelihood.

Discrete – categorical

  • "aic" - Akaike Information Criterion.

  • "bdla" - locally averaged BDE.

  • "bde" - Bayesian Dirichlet equivalent (uniform).

  • "bds" - Bayesian Dirichlet score.

  • "bic" - Bayesian Information Criterion.

  • "ebic" - Extended BIC.

  • "fnml" - factorised NML.

  • "k2" - K2 score.

  • "loglik" - log-likelihood.

  • "mbde" - modified BDE.

  • "nal" - node-average log-likelihood.

  • "pnal" - penalised node-average log-likelihood.

  • "pred_loglik" - predictive log-likelihood.

  • "qnml" - quotient NML.

Mixed Discrete/Gaussian

  • "aic_cg", "bic_cg", "ebic_cg", "loglik_cg", "nal_cg", "pnal_cg", "pred_loglik_cg" - conditional Gaussian versions of the respective scores for discrete data.

test

Character scalar naming the conditional-independence test passed to bnlearn. Can be set with $set_score(). Kebab-case test names (as used in bnlearn, e.g. "mi-adf") are also accepted and automatically translated to snake_case. Recognised values are:

Continuous - Gaussian

  • "cor" – Pearson correlation

  • "fisher_z" / "zf" – Fisher Z test

  • "mc_cor" – Monte Carlo Pearson correlation

  • "mc_mi_g" – Monte Carlo mutual information (Gaussian)

  • "mc_zf" – Monte Carlo Fisher Z

  • "mi_g" – mutual information (Gaussian)

  • "mi_g_sh" – mutual information (Gaussian, shrinkage)

  • "smc_cor" – sequential Monte Carlo Pearson correlation

  • "smc_mi_g" – sequential Monte Carlo mutual information (Gaussian)

  • "smc_zf" – sequential Monte Carlo Fisher Z

Discrete – categorical

  • "mc_mi" – Monte Carlo mutual information

  • "mc_x2" – Monte Carlo chi-squared

  • "mi" – mutual information

  • "mi_adf" – mutual information with adjusted d.f.

  • "mi_sh" – mutual information (shrinkage)

  • "smc_mi" – sequential Monte Carlo mutual information

  • "smc_x2" – sequential Monte Carlo chi-squared

  • "sp_mi" – semi-parametric mutual information

  • "sp_x2" – semi-parametric chi-squared

  • "x2" – chi-squared

  • "x2_adf" – chi-squared with adjusted d.f.

Discrete – ordered factors

  • "jt" – Jonckheere–Terpstra

  • "mc_jt" – Monte Carlo Jonckheere–Terpstra

  • "smc_jt" – sequential Monte Carlo Jonckheere–Terpstra

Mixed Discrete/Gaussian

  • "mi_cg" – mutual information (conditional Gaussian)

For Monte Carlo tests, set the number of permutations using the B argument.

alg

Function generated by $set_alg() that runs a structure-learning algorithm from bnlearn. Period.case alg names (as used in bnlearn, e.g. "fast.iamb") are also accepted and automatically translated to snake_case. Recognised values are:

Constraint-based

  • "fast_iamb" – Fast-IAMB algorithm. See fast_iamb() and the underlying bnlearn::fast.iamb().

  • "gs" – Grow-Shrink algorithm. See gs() and the underlying bnlearn::gs().

  • "iamb" – Incremental Association Markov Blanket algorithm. See iamb() and the underlying bnlearn::iamb().

  • "iamb_fdr" – IAMB with FDR control algorithm. See iamb_fdr() and the underlying bnlearn::iamb.fdr().

  • "inter_iamb" – Interleaved-IAMB algorithm. See inter_iamb() and the underlying bnlearn::inter.iamb().

  • "pc" – PC-stable algorithm. See pc() and the underlying bnlearn::pc.stable().

params

A list of extra tuning parameters stored by set_params() and spliced into the learner call.

knowledge

A list with elements whitelist and blacklist containing prior-knowledge constraints added via set_knowledge().

Methods

Public methods


Method new()

Constructor for the BnlearnSearch class.

Usage
BnlearnSearch$new()

Method set_params()

Set the parameters for the search algorithm.

Usage
BnlearnSearch$set_params(params)
Arguments
params

A parameter to set.


Method set_data()

Set the data for the search algorithm.

Usage
BnlearnSearch$set_data(data)
Arguments
data

A data frame containing the data to use for the search.


Method set_test()

Set the conditional-independence test to use in the search algorithm.

Usage
BnlearnSearch$set_test(method, alpha = 0.05)
Arguments
method

[Experimental]

A string specifying the type of test to use. Can also be a user-defined function with signature ⁠function(x, y, z, data, args)⁠, where x and y are the variables being tested for independence, z is the conditioning set, data is the dataset, and args is a list of additional arguments. The function should return the test statistic and the p-value. See bnlearn::ci.test() for more details.

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

alpha

Significance level for the test.


Method set_score()

Set the score function for the search algorithm.

Usage
BnlearnSearch$set_score(method)
Arguments
method

Character naming the score function to use.


Method set_alg()

Set the causal discovery algorithm to use.

Usage
BnlearnSearch$set_alg(method, args = NULL)
Arguments
method

Character naming the algorithm to use.

args

A list of additional arguments to pass to the algorithm.


Method set_knowledge()

Set the prior knowledge for the search algorithm using a Knowledge object.

Usage
BnlearnSearch$set_knowledge(knowledge_obj)
Arguments
knowledge_obj

A Knowledge object containing prior knowledge.


Method run_search()

Run the search algorithm on the currently set data.

Usage
BnlearnSearch$run_search(data = NULL)
Arguments
data

A data frame containing the data to use for the search. If NULL, the currently set data will be used, i.e. self$data.


Method clone()

The objects of this class are cloneable with this method.

Usage
BnlearnSearch$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples

### bnlearn_search R6 class examples ###

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

# Load data
data(num_data)

# Recommended:
my_pc <- pc(engine = "bnlearn", test = "fisher_z", alpha = 0.05)
result <- my_pc(num_data)

# or
result <- disco(data = num_data, method = my_pc)

plot(result)

# Example with detailed settings:
my_pc2 <- pc(
  engine = "bnlearn",
  test = "mi_g",
  alpha = 0.01
)
disco(data = num_data, method = my_pc2)

# With knowledge

kn <- knowledge(
  num_data,
  starts_with("X") %-->% Y
)

disco(data = num_data, method = my_pc2, knowledge = kn)

# Using additional test args (bootstrap samples)

my_iamb <- iamb(
  engine = "bnlearn",
  test = "mc_zf",
  alpha = 0.05,
  B = 100
)

disco(data = num_data, method = my_iamb)

# Using R6 class:
s <- BnlearnSearch$new()

s$set_data(num_data)
s$set_test(method = "fisher_z", alpha = 0.05)
s$set_alg("pc")

g <- s$run_search()

plot(g)

causalDisco documentation built on April 13, 2026, 5:06 p.m.