大家早上好,我是意大利的里卡多。这是我第一次在StackOverflow的问题上表演,如果我做错了什么,很抱歉。
我对R中的编码也不是很精通,我正在努力解决以下问题:我试图从遗传表达式数据(文件carm.csv的链接)开始,使用一个平均MaxMin爬山的模型来学习一个健壮的结构和一个用于参数学习的Bayes方法来构建一个贝叶斯网络。
我使用函数bn.fit {bnlearn}进行参数学习,因为出现了一个错误消息:"Error in bn.fit(.):图只有部分有向。“。的确,输入中的图不是有向的,但这是大学的作业,而它不应该找到无向图(而且,奇怪的只有一个拱,顺便说一句)。
我试图搜索,也许在R,O‘’Reilly,2013年,第35页中找到了这个有用的东西--贝叶斯网络
“bnlearn 3.2及更高版本在设置弧线方向方面比较挑剔;因此,bn.gs是一个无向图,必须扩展到一个带有cextend()的DAG中,才能结束这个示例。”对于我来说,强制在DAG中使用函数的图形并不那么有趣:)
你能帮我一下吗?你看到我的密码出什么问题了吗?
我附上代码如下:
library(bnlearn)
library(Rgraphviz)
###BAYESIAN NETWORKS
#Use a Bayesian network to analyze genetic data (in the file carm.csv) on gene
#expression measured in a series of cytokines in order to assess
#their association with CARM protein expression on a sample of 42 subjects.
#Discretize the data using Hartemink's method (considering 3 final levels
# and 8 initial levels) and proceed with network structure learning
#using a hybrid-type algorithm (Max-Min Hill-Climbing)
#Load:
data <- as.data.frame(read.csv("carm.csv", sep = ";",))
#Discretization:
discret.data<-discretize(data, method='hartemink', breaks=3, ibreaks=8, idisc='quantile')
#Use the model averaging approach to obtain a robust final network
#(again with the mmhc algorithm), using 200 bootstrap samples
ddat<-discret.data
set.seed(123)
boot<-boot.strength(data=ddat,R=200,algorithm="mmhc")
print(boot)
plot(boot)
#Use a threshold of 70% to obtain a network using the averaged.network command,
#and then use the bayes method to do parameter learning:
#Average model:
avg.boot<-averaged.network(boot, threshold=0.70)
print(avg.boot)
plot(avg.boot)
#Parameter learning via Bayes Method:
dag.fit<-bn.fit(avg.boot, ddat, method="bayes")发布于 2022-06-13 23:28:09
在某些情况下,当弧的两个可能方向的概率恰好为0.5时,averaged.network可能返回部分有向图。您可以尝试的一件事是将引导复制的数量从200增加到500。这意味着将从引导数据中创建500个DAG,以便找到最有可能的弧线和方向。引导过程中的DAG越多,就越有可能“打破领带”并引导弧线。
如果这不起作用,你仍然有一个没有方向的弧线,你需要处理它。有很多方法可以做到这一点,包括试验其他算法。解决方案实际上取决于您的用例和域。但是,一般来说,对于许多域和用例,您可以简单地忽略该弧线,并且仍然可以使用有效的DAG来对变量的联合概率进行编码。这段代码会做到这一点,如果你觉得无视这条弧线,你肯定会让你的代码工作起来:
avg.boot<-averaged.network(boot, threshold=0.70)
arcs(avg.boot) <- directed.arcs(avg.boot) # ignore undirected arcs您的bn.fit肯定会工作,因为您已经删除了无定向弧。
https://stackoverflow.com/questions/72354419
复制相似问题