I am starting to use the pcalg package, and I am having some doubts regarding the functions pdag2allDags and addBgKnowledge:
I am using the sample data gmG provided by the package
library(pcalg)
library(Rgraphviz)
data("gmG")
df<-gmG$x
suffStat <- list(C = cor(df), n = nrow(df))
fci.fit<-fci(suffStat, indepTest = gaussCItest, p = ncol(df),alpha = 0.01)
plot(fci.fit)
I would like to get the all equivalent DAGs. From documentation, it should work using the function pdag2allDags (from here). We should need just to get the amat (adjacent matrix) data.
From the specifications on the documentation, I thought the following should work...
plotAllDags <- function(res) {
require(graph)
p <- sqrt(ncol(res$dags))
nDags <- ceiling(sqrt(nrow(res$dags)))
par(mfrow = c(nDags, nDags))
for (i in 1:nrow(res$dags)) {
tmp <- matrix(res$dags[i,],p,p)
colnames(tmp) <- rownames(tmp) <- res$nodeNms
plot(as(tmp, "graphNEL"))
}
}
res1<-pdag2allDags(as(fci.fit,"amat"))
plotAllDags(res1)
But, instead, it returns:
Error in sqrt(ncol(res$dags)) : non-numeric argument to mathematical function
We see also the amat in the fci's object. So, I tried:
res2<-pdag2allDags(fci.fit@amat)
plotAllDags(res2)
It also returns the same:
Error in sqrt(ncol(res$dags)) : non-numeric argument to mathematical function
But if I use the pc algorithm, it works:
pc.fit<-pc(suffStat, indepTest = gaussCItest, p = ncol(df),alpha = 0.01)
plot(pc.fit)
res0<-pdag2allDags(as(pc.fit,"amat"))
plotAllDags(res0)
What is going on? Is not pdag2allDags intended to deal with all amat objects (pc, fci, rfci, etc.)?
I was not able to find any other ...allDags function in the documentation. How to get the all equivalent DAGs from the output of the fci function?
The same happens for the function addBgKnowledge. It works for pc:
pc.amat2<-addBgKnowledge(gInput = pc.fit@graph,x=1,y=2)
plot(pc.amat2)
but not for fci, even the documentation saying it uses an amat
fci.amat2<-addBgKnowledge(gInput = as(fci.fit,"amat"),x=1,y=2)
plot(as(t(fci.amat2),"graphNEL"))
It delivers:
Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'plot': argument is not a matrix