简体   繁体   中英

Bnlearn: Predicting multiple node values

I'm using an r-package "bnlearn" to work with a bayes net I have constructed:

bn.gs <- gs(x = dat, cluster = NULL, whitelist = wl, blacklist = bl, test = NULL, alpha = 0.05, B = NULL, debug = FALSE, optimized = TRUE, strict = FALSE, undirected = FALSE)

It gives me a nice plot and everything seems to work well. All the variables are continuous and between -1 and 1. The feeding variables (those with no parents) were generated as below (N = 1000):

A <- runif(N, min=-1, max=1)

Let's assume that my variables are A, B, ... Z, and I know the values of C, G and M. Now I would like to predict the values of the rest of the nodes (A, B, D, ...) given C, G and M. As far as I am concerned, predict() works for one node at a time.

Is there a method to predict multiple nodes simultaneously, or should I end up getting right values by applying predict() to each node at time? I already tried to predict the value for node "A", given the value of "C":

predict(bn.gs, node = "A", testdata, debug = TRUE)

where testdata is a data frame in this form:

A    B    C    D    E    ...
0.0  0.0  0.7  0.0  0.0  ...

but I get this:

* predicting values for node A.
  > prediction for observation 1 is nan with predictor:
    (0.000000) + (0.000000) * (nan) + (0.000000) * (nan)
[1] NA

I'm positive that I am doing something wrong here. In my network there are arcs C -> S -> A. Also "nan"s are weird, since my network should be well defined.

Thank you already :).

Just in case others may look for the answer as well, I did solve this problem by using rbmn package. After creating a bn object using bnlearn, I did following:

my.bn.par <- bn.fit(x = bn.gs, data = dat)

library("rbmn")

my.bn.par.rbmn <- bnfit2nbn(my.bn.par)
print8nbn(my.bn.par.rbmn)
my.bn.par.mn <- nbn2mn(my.bn.par.rbmn)

# This is the names vector for my nodes:
names <- c("A","B","C","D","E")

# Names of given variables
obsnames <- c("C","E")

# Values for given variables
obsval <- c(0.51,-0.24)

# Names of variables to be predicted
prednames <- setdiff(names, obsnames)

# Then predict all the unknown variables
print8mn(condi4joint(my.bn.par.mn, par = prednames, pour = obsnames, x2 = obsval))

The method was presented in Marco Scutari's book "Bayesian Networks".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM