R/B3_bn_to_graphNEL.R

Defines functions bn_to_graphNEL

Documented in bn_to_graphNEL

#' Convert a bn object to graphNEL object
#'
#' Convert a bn object to graphNEL object while removing isolated nodes
#'
#' @param graph_bn a \code{bn} object of Bayesian network
#' @return a \code{graphNEL} object
#' 
#' @author Han Yu
#'
#' @importFrom igraph igraph.from.graphNEL igraph.to.graphNEL induced_subgraph degree
#' @importFrom bnlearn as.graphNEL
#' 
#' @export

bn_to_graphNEL <- function(graph_bn) {
  
  graph.graphNEL <- bnlearn::as.graphNEL(graph_bn)
  graph.igraph <- igraph.from.graphNEL(graph.graphNEL)
  
  # remove isolated nodes
  deg <- igraph::degree(graph.igraph)
  nodes <- names(deg)[deg>0] 
  graph.igraph.sub <- induced_subgraph(graph.igraph, nodes)
  graph.graphNEL.sub <- igraph.to.graphNEL(graph.igraph.sub)
  
  return(graph.graphNEL.sub)
  
}

Try the BayesNetBP package in your browser

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

BayesNetBP documentation built on May 9, 2022, 1:05 a.m.