## generate and draw random DAG
## this example is taken from Zhang (2008), Fig. 6, p.1882 (see references)
amat <- t(matrix(c(0,1,0,0,1, 0,0,1,0,0, 0,0,0,1,0, 0,0,0,0,0, 0,0,0,1,0),5,5))
colnames(amat) <- rownames(amat) <- as.character(1:5)
V <- as.character(1:5)
edL <- vector("list",length=5)
names(edL) <- V
edL[[1]] <- list(edges=c(2,4),weights=c(1,1))
edL[[2]] <- list(edges=3,weights=c(1))
edL[[3]] <- list(edges=5,weights=c(1))
edL[[4]] <- list(edges=5,weights=c(1))
g <- new("graphNEL", nodes=V, edgeL=edL,edgemode="directed")
if (require(Rgraphviz)) {
plot(g)
}
## define the latent variable
L <- 1
## generate 100000 samples of DAG using standard normal error distribution
n <- 100000
d.mat <- rmvDAG(n, g, errDist = "normal")
## delete rows and columns corresponding to the latent variable
d.mat <- d.mat[-L,-L]
## estimate the skeleton of given data using psepset=TRUE
(resD <- pcAlgo(d.mat, alpha=0.05, psepset=TRUE))
## extend the pcalgo-object into a PAG using all 10 rules
rules <- rep(TRUE,10)
resP <- udag2pag(resD, rules=rules, verbose=1)
colnames(resP) <- rownames(resP) <- graph::nodes(g)[-L]
if (require(Rgraphviz)) {
## plot the original DAG and the PAG
op <- par(mfrow=c(1,2))
plot(g, main="original (random) DAG")
plotAG(resP)
par(op)
}
Run the code above in your browser using DataLab