p <- 10 ## number of random variables
s <- 0.4 ## sparseness of the graph
## generate a random DAG
set.seed(42)
g <- randomDAG(p, s)
g01 <- 1*(as(g,"matrix") > 0) # 0/1-version of adjacency matrix
print.table(g01, zero.=".")
## compute its unique CPDAG
system.time(
res <- dag2cpdag(g)
)
## res has some bidirected edges
## ==> adj.matrix: no longer upper triangular, but {0,1}
print.table(as(res, "matrix"), zero.=".")
dm <- as(res, "matrix") - g01 ## difference: 2 entries in lower tri.
print.table(dm, zero.=".")
stopifnot(all(dm %in% 0:1), sum(dm == 1) == 2)
## Find CPDAG with PC algorithm:
## As dependence oracle, we use the true correlation matrix in
## gaussCItest() with a large "virtual sample size" and a large alpha:
system.time(
rpc <- pc(suffStat = list(C = cov2cor(trueCov(g)), n = 10^9),
indepTest = gaussCItest, alpha = 0.9999, p = p)
)
## confirm that it coincides with dag2cpdag()'s result:
stopifnot(all.equal(as( res, "matrix"),
as(rpc@graph,"matrix"), tol=0))
Run the code above in your browser using DataLab