R/to_bnlearn.R

Defines functions toBNLearn.igraph toBNLearn.bn toBNLearn.matrix toBNLearn.default toBNLearn

Documented in toBNLearn

#' Convert to bnlearn object.
#' 
#' @param x An object that represents a DAG.
#' @returns bn_obj A bn object.
#' 
#' @examples
#' adj <- UniformlySampleDAG(c('A', 'B', 'C'))
#' toBNLearn(adj)
#' 
#' @export
toBNLearn <- function(x) UseMethod('toBNLearn')

#' @export
toBNLearn.default <- function(x) { 
  return(toBNLearn.matrix(x)) 
}

#' @export
toBNLearn.matrix <- function(x) {
  
  names <- colnames(x)
  bn_obj <- bnlearn::empty.graph(names)
  bnlearn::amat(bn_obj) <- x
  
  return(bn_obj)
}

#' @export
toBNLearn.bn <- function(x) {
  return(x)
}

#' @export
toBNLearn.igraph <- function(x) {
  bn_obj <- x |>
    methods::as('matrix') |> 
    toBNLearn.matrix()
  
  return(bn_obj)
}

Try the cia package in your browser

Any scripts or data that you put into this service are public.

cia documentation built on April 4, 2025, 5:23 a.m.