## A -> B <- C
am1 <- matrix(c(0,1,0, 0,0,0, 0,1,0), 3,3)
colnames(am1) <- rownames(am1) <- LETTERS[1:3]
g1 <- as(t(am1), "graphNEL") ## convert to graph
cpdag1 <- dag2cpdag(g1)
if(requireNamespace("Rgraphviz")) {
par(mfrow = c(1,2))
plot(g1)
plot(cpdag1)
}
## A -> B -> C
am2 <- matrix(c(0,1,0, 0,0,1, 0,0,0), 3,3)
colnames(am2) <- rownames(am2) <- LETTERS[1:3]
g2 <- as(t(am2), "graphNEL") ## convert to graph
cpdag2 <- dag2cpdag(g2)
if(requireNamespace("Rgraphviz")) {
par(mfrow = c(1,2))
plot(g2)
plot(cpdag2)
}
Run the code above in your browser using DataLab