# Odd results from Bayesian network in R

Related to question here.

I've been trying to teach myself about Network Analysis, and developing DAG charts in R. Let's say that I have the following data.

dat=data.frame(sold=c(0,0,0,1,0,1), won=c(1,0,0,1,0,1), bid=c(5,3,2,5,3,4))
dat


Given what I'm trying to analyze, I know that the DAG plot should be as follows:

bid => won => sold


However, when I utilize the bnlearn package to generate the plot, it comes out as follows. It just can't be correct, and should be in the opposite direction.

library("bnlearn")
library("Rgraphviz")

bn.hc <- hc(dat, score = "bic")
graphviz.plot(bn.hc)


Now, I know that's just the data that I provided it to learn on, but I've messed around with the variable values, and it never turns our the way it should. Basically, a bid should determine whether you win, and whether you win should determine whether you can sell it. Just doesn't make sense.

Isn't there some way to specify what variable is the response variable? In my case, the response variable should be sold, and there should be no arcs from sold to another node.

Can anyone help with diagnosing the problem in R? Is there something I'm missing in the code? or of my understanding of BN's? is this an issue w/ what I pass as the algorithm to use in 'score'?

• IMO the main problem is that you have so little data here that any model makes (no) sense; remember that in contrary to you R doesn't understand variable names and can't infer from that. BTW you can get "right" result simply by reversing the order of columns -- this even better shows that you are torturing this poor algorithm stranded in a no-information regime. – user88 Sep 26 '12 at 23:25
• In any case, the two dependence structures are identical (as long as you do not have interventions). Also, I think you are confusing the direction of reasoning with the directions of the arrows. Finally, all variables in a Bayesian network can be a response variable and can have arcs to other variables. – Neil G Sep 27 '12 at 0:01

Actually, the structure you learned and the structure you proposed are "Markov equivalent". This basically means you can use Bayes theorem to go from one to another.

Proof

P(bid)P(won|bid)P(sold|won) =
P(won, bid) P(won, sold)/P(won) =
P(bid|won)P(won)P(won|sold)P(sold)/P(won) =
P(bid|won)P(won|sold)P(sold)


which is what you want.

To show this in R, try this

bn.2 <- empty.graph(nodes(bn.hc))
arcs(bn.2) <- matrix(c("bid", "won", "won", "sold"), ncol = 2, byrow = T)
score(bn.hc, dat) == score(bn.2, dat)
> TRUE


Also checkout ?cpdag

Basically, you can change the direction of any arc in the network and get a graph of the same equivalence class, so long as that arc is not in a v-structure.

I think what you're asking for is the whitelist parameter. Use this to indicate which edges the network should include.

hc(x, start = NULL, whitelist = NULL, blacklist = NULL, score = NULL, ..., debug = FALSE, restart = 0, perturb = 1, max.iter = Inf, maxp = Inf, optimized = TRUE)

whitelist
a data frame with two columns (optionally labeled "from" and "to"), containing a set of arcs to be included in the graph.