r - BNLearn:如何将高斯贝叶斯网络的估计参数与其条件结构合并?
问题描述
我使用该函数定义了高斯贝叶斯网络的结构,iamb
然后使用 估计了节点的系数bn.fit
。
图书馆
library(bnlearn)
数据
{ C E G N V W
48.83 51.48 42.64 54.1 42.96 41.96
48.85 73.43 40.97 60.07 65.29 48.96
67.01 71.1 52.52 51.64 63.22 62.03
37.83 49.33 56.15 49.01 47.75 38.77
55.3 49.27 63.55 54.62 60.57 56.66
56.12 48.72 66.02 43.95 55.54 52.39}
代码
# Definition of mandatory and forbidden nodes - here the white list
wl = data.frame(from = c("E","G","V","W","N"), to = c("V", "V","W","C","C"))
# Definition of the constrained network
network <- iamb(Data, test = "cor", whitelist = wl)
# Estimation of the coefficients according to the structure of the network
est.para <- bn.fit(network, data = Data)
问题是这est.para
是一个列表而不是可以绘制的 GBN 等。 我想知道如何合并网络和估计的参数?
解决方案
如果你想要一些网络图来显示除了连接之外的一些额外信息,你可以使用strength.plot
. 按照你的例子:
library(Rgraphviz)
strength <- arc.strength(network, Data)
strength.plot(network, strength, shape = "ellipse")
如果绝对需要使用 GBN 参数的结果est.para
,您可以使用graphviz.plot
参数来突出边缘和节点(可以使用edgeRenderInfo
and来完成nodeRenderInfo
)。举个例子,您可以使用参数来选择边缘的宽度:
library(data.table)
plot <- graphviz.plot(network, shape = "ellipse")
arc.sizes <- data.table(network$arcs)
arc.sizes[, edge.name := paste0(arc.sizes$from, "~", arc.sizes$to)]
arc.sizes[, param := abs(est.para[[to]]$coefficients[[from]]), by = .(from, to)]
arc.sizes[, lwd := 5*((param - min(param))/(max(param) - min(param)))]
lwd <- as.vector(arc.sizes$lwd)
names(lwd) <- arc.sizes$edge.name
edgeRenderInfo(plot) <- list(lwd = lwd)
renderGraph(plot)
推荐阅读
- azure-data-factory - 用于在 azure 数据工厂中排除数组项的集合
- graphql - 更改相关字段而不改变原始字段
- reactjs - 为什么我不能用 Typescript 传播道具?
- python - 如何在使用 PyTorch 'nn.Sequential' 时访问网络权重?
- python - 由于不同的字符编码,查找唯一元素的代码会给出重复的元素
- scala - 尾递归和按名称/值调用
- python - 在递归排列代码中将输出列表作为参数传递是否很好
- wordpress - 我需要帮助来获取外部 url 来显示,就好像它是我网站的一部分一样
- javascript - 启动自己的网站时如何隐藏切换侧边栏?
- php - 如何修复致命错误:未捕获的错误:调用字符串上的成员函数 children()