使用pcalg
包本身的示例,我想知道是否以及如何在DAG中添加部分相关性?下面是一个例子:
require(pcalg)
require(igraph)
require(gRim)
data(gmG)
n <- nrow(gmG8$x)
V <- colnames(gmG8$x) # labels aka node names
## estimate CPDAG
pc.fit <- pc(suffStat=list(C=cor(gmG8$x), n=n),
indepTest=gaussCItest, ## indep.test: partial correlations
alpha=0.01, labels=V, verbose=FALSE)
gr = as(pc.fit@graph, 'igraph')
plot.igraph(gr, layout=layout_in_circle,
vertex.color='grey',
vertex.shape='circle', vertex.size=25,
vertex.label.cex=0.6, vertex.label.color='blue',
edge.arrow.size=0.5)
谢谢!
获得部分相关性的一种方法是使用pcalg的pcorOrder()
。对于上面的例子,像这样:
corMatrix <- cor(gmG8$x)
pcormtx = matrix(0, nrow=8, ncol=8)
allVars = 1:8
for (i in seq(from=1, to=7, by=1)) {
for (j in seq(from=i+1, to=8, by=1)) {
S = allVars[!allVars %in% c(i,j)]
pcormtx[j,i] = pcorOrder(i, j, S, corMatrix)
}
}
pcormtx = round(pcormtx, 3)
pcormtx[upper.tri(pcormtx, diag=TRUE)] = NA
print(pcormtx, na.print='')
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
# [1,]
# [2,] 0.201
# [3,] -0.011 0.610
# [4,] -0.006 -0.002 0.004
# [5,] -0.213 0.106 -0.009 0.006
# [6,] 0.412 -0.008 -0.020 0.010 0.129
# [7,] 0.003 0.001 0.003 -0.015 0.002 0.346
# [8,] 0.218 -0.012 0.016 -0.019 0.695 -0.009 0.001