R/read-bif.R

Defines functions read_bif

read_bif <- function(file, debug = FALSE) {
  bn <- bnlearn::read.bif(file, debug)
  map <- dplyr::data_frame(old_nm = names(bn),
                           new_nm = sprintf('X%d', 0:(length(bn) - 1)))
  if ('factor' %in% names(bn)) stop('tem um factor como nome de variavel.')
  if ('Freq' %in% names(bn)) stop('tem um Freq como nome de variavel.')
  bn2 <- lapply(bn, function(x) {
    x$cpt <- as.data.frame(x$prob, stringsAsFactors = FALSE)
    if(length(x$cpt) == 2) {
      names(x$cpt) <- c(map[map$old_nm == x$node, ][['new_nm']], 'factor')
    } else {
      for (i in seq_len(length(x$cpt) - 1)) {
        names(x$cpt)[i] <- map[map$old_nm == names(x$cpt)[i], ][['new_nm']]
      }
      names(x$cpt)[length(x$cpt)] <- 'factor'
    }
    x$parents <- map[map$old_nm %in% x$parents, ][['new_nm']]
    x$children <- map[map$old_nm %in% x$children, ][['new_nm']]
    x$node <- map[map$old_nm == x$node, ][['new_nm']]
    x
  })
  names(bn2) <- sprintf('X%d', 0:(length(bn2) - 1))
  class(bn2) <- class(bn)
  bn2
}
jtrecenti/ea2 documentation built on May 20, 2019, 3:17 a.m.