【发布时间】:2022-06-14 07:28:09
【问题描述】:
大家早上好, 这是来自意大利的里卡多。 这是我关于 StackOverflow 问题的第一次牛仔竞技表演,如果我做错了什么,请见谅。
我也不是 R 编码方面的专家,我正在努力解决以下问题: 我尝试使用平均 MMHC 模型(MaxMin Hill-Climbing 上的引导程序)从遗传表达数据(文件 carm.csv 的链接)开始在 R 中构建贝叶斯网络,以学习稳健的结构和贝叶斯方法进行参数学习。
All is good util 我使用 function bn.fit {bnlearn} 进行参数学习,因为出现错误消息:"Error in bn.fit(...) : the图只是部分有向的。”。确实,输入中的图不是有向图,但这是大学作业,不应该找到无向图(另外,奇怪的是只有一个拱门,顺便说一句)。
我尝试搜索并可能发现了这些有用的东西 R 中的贝叶斯网络,O'Reilly 2013,p。 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")
【问题讨论】:
-
来自
help(averaged.network): "averaged.network() 通常返回一个完全有向图;当且仅当其每个方向的概率恰好为 0.5 时,弧才能是无向的。”。因此,值得检查您的boot对象以查看包含哪些边缘(高于阈值)并且方向 = 0.5。该怎么办?mmhc返回一个有向图,所以我认为使用奇数个复制可以对此进行排序;但不是。快速查看帮助页面显示cpdag=TRUE默认返回等价类。将其设置为FALSE应该会有所帮助(在这种情况下)。 -
... 或者你可以尝试使用
?cextend;但不能保证有可能获得完全定向的 graoh。
标签: r bayesian-networks bnlearn