如何使用 `R` 中的 `pcalg`、`igraph` 和 `gRim` 包向 DAG 的边添加偏相关

问题描述 投票:0回答:1

使用

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)

谢谢!

r igraph directed-acyclic-graphs
1个回答
0
投票

获得偏相关的一种方法是使用 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     
© www.soinside.com 2019 - 2024. All rights reserved.