1

I'm a first-time poster, but a long-time learner from this site. This is the first time an answer to my question hasn't been reverse-engineer-able from previous posts, so I'm hoping someone can help me solve it.

I'm trying to plot the results of a hill-climbing search algorithm (hc from the bnlearn package in R) run on a correlation matrix as a Reingold-Tilford tree graph.

Say I run:

    hc.obj<-hc(corr.matrix)
    hc.plot<-qgraph(hc.obj, directed = "TRUE", layout = "spring")

I get a Fruchterman-Reingold layout of the directed relationships no problem.

But I can't get a tree layout to work no matter how I format the information in hc.obj. For example, running the code above with "layout = tree" I get the error: "Error in l[, 1] : incorrect number of dimensions".

Here's a reproducible example:

    require("bnlearn")
    require("qgraph")
    cm <- matrix(runif(100), ncol=10)
    cm <- (cm * lower.tri(cm)) + t(cm * lower.tri(cm))
    diag(cm) <- 1 
    cm.df<-as.data.frame(cm)
    hc.obj<-hc(cm.df)
    hc.plot<-qgraph(hc.obj, directed = "TRUE", layout = "tree")

Similarly, if I try to run through igraph as:

    layout_as_tree(hc.obj)

I get the error "Error in layout_as_tree(hc.obj) : Not a graph object"

Miri
  • 13
  • 3

1 Answers1

1

It appears that you're trying use the plotting functions to do something they're not intended to.

hc.obj is a model object of class bn whereas qplot() for instance supports the following:

...either a weights matrix or an edgelist. Can also be an object of class "sem" (sem), "mod" (sem), "lavaan" (lavaan), "principal" (psych), "loadings" (stats), "factanal" (stats), "graphNEL" (Rgraphviz), "pcAlgo" (pcalg), "huge" (huge), "select" (huge) or the output of glasso".

You can, however plot a bn object. One way is to simply use plot().

if(!require(pacman)) install.packages("pacman")
pacman::p_load(qgraph,bnlearn,Rgraphviz)
cm <- matrix(runif(100), ncol=10)
cm <- (cm * lower.tri(cm)) + t(cm * lower.tri(cm))
diag(cm) <- 1 
cm.df<-as.data.frame(cm)
hc.obj<-hc(cm.df)
plot(hc.obj)

enter image description here

Another way is to use graphviz.plot().

bnlearn::graphviz.plot(hc.obj)

enter image description here

If you do a search or review the relevant CRAN task view you can probably find more packages that support bn class objects.

Hack-R
  • 22,422
  • 14
  • 75
  • 131
  • Thanks that's a good start - I'll try to find an alternative package that will let me plot with a tree layout. A paper that included what I'm trying to reproduce came out today: [link](http://www.sciencedirect.com/science/article/pii/S0005796716301103), but it doesn't give details on how they got there. I thought it might help to transform the hc.plot output to a matrix of present/absent weights (1s and 0s), but I get the same error. It seems like it must be something specific to do with the tree layout because the spring layout in qgraph seems to work fine with this kind of output. – Miri Jul 06 '16 at 01:17
  • @Miri I can't see the paper you're linking to because it requires $20 or special access privileges, but the 2nd plot looks close to a dendrogram. – Hack-R Jul 06 '16 at 01:20
  • 1
    You're totally right! Just ran with the graphviz.plot you suggested and it's perfect. Thanks so much!! – Miri Jul 06 '16 at 01:22
  • @Miri You're very welcome! Glad to help. BTW there may be more options here https://cran.r-project.org/web/views/Bayesian.html If this was helpful please mark it as the answer or upvote :) I work for points lol. – Hack-R Jul 06 '16 at 01:28