贝叶斯网络R语言学习笔记1

2021年7月19日19:54:12

一、创建贝叶斯网络结构

贝叶斯网络的图结构存储在bn对象中,可以通过三种表示来创建bn对象,即the arc set of the graph, its adjacency matrix or a model formula(边集、邻接矩阵、模型公式)。此外,可以通过empty.graph()random.graph()函数创建空网络结构或随机网络结构。

1、创建空网络

空网络应该是最简单的形式了,先从简单的入门。

# 产生空网络
library(bnlearn)
e = empty.graph(LETTERS[1:6])
e
class(e)
# 结果Random/Generated Bayesian networkmodel:[A][B][C][D][E][F] nodes:                                 6 arcs:                                  0 undirected arcs:                     0 directed arcs:                       0 average markov blanket size:           0.00 average neighbourhood size:            0.00 average branching factor:              0.00 generation algorithm:                  Empty > class(e)
[1] "bn"

通常empty.graph()默认只产生一个bn对象,可以指定参数num,使之产生一批网络对象。

empty.graph(LETTERS[1:6], num = 2) #此处指定num = 2,产生两个bn对象

2、创建特定网络结构

①特定的边集

要产生边集,首先需要指定一个边集的矩阵。以下代码产生一个矩阵,两列,按行填充,行名为from,to。

arc.set = matrix(c("A", "C", "B", "F", "C", "F"),ncol = 2, byrow = TRUE,dimnames = list(NULL, c("from", "to")))
arc.setfrom to
[1,] "A"  "C"
[2,] "B"  "F"
[3,] "C"  "F"

将产生的边集arc.set传给bn对象使用arcs()函数

arcs(e) = arc.set
eRandom/Generated Bayesian networkmodel:[A][B][D][E][C|A][F|B:C] nodes:                                 6 arcs:                                  3 undirected arcs:                     0 directed arcs:                       3 average markov blanket size:           1.33 average neighbourhood size:            1.00 average branching factor:              0.50 generation algorithm:                  Empty

对于arcs()函数,其会对边集做一定的检查,来看是否符合上下条件。

  • 边的标签要和图里的标签一致
bogus = matrix(c("X", "Y", "W", "Z"),ncol = 2, byrow = TRUE,dimnames = list(NULL, c("from", "to")))
bogusfrom to
[1,] "X"  "Y"
[2,] "W"  "Z"

bogus传给bn对象e,会出现报错

arcs(e) = bogusError in check.arcs(value, nodes = names(x$nodes)) : node(s) 'X' 'W' 'Y' 'Z' not present in the graph.
  • 不能引入环(e.g. A → B → C → A) ,除非我们设置ignore.cycles = TURE
cycle = matrix(c("A", "C", "C", "B", "B", "A"),ncol = 2, byrow = TRUE,dimnames = list(NULL, c("from", "to")))
cyclefrom to
[1,] "A"  "C"
[2,] "C"  "B"
[3,] "B"  "A"arcs(e) = cycleError in `arcs<-`(`*tmp*`, value = c("A", "C", "B", "C", "B", "A")) : the specified network contains cycles.arcs(e,ignore.cycles = TRUE) = cycleError in `arcs<-`(`*tmp*`, ignore.cycles = TRUE, value = c("A", "C", "B",  : 参数没有用(ignore.cycles = TRUE)> acyclic(e) # check whether the graph is acyclic/completely directed.检测是否无环
[1] TRUE
  • 不能引入圈 (e.g. A → A).
> loops = matrix(c("A", "A", "B", "B", "C", "D"),
-                ncol = 2, byrow = TRUE,
-                dimnames = list(NULL, c("from", "to")))
> loopsfrom to
[1,] "A"  "A"
[2,] "B"  "B"
[3,] "C"  "D"
> arcs(e) = loops
Error in check.arcs(value, nodes = names(x$nodes)) : invalid arcs that are actually loops:A -> A B -> B
  • 可以通过在边集中包括双向边引入非直接边(e.g. A → B and B → A),保证双向均未引入环,设置ignore.cycles会覆盖这个检查。
> edges = matrix(c("A", "B", "B", "A", "C", "D"),
+                  ncol = 2, byrow = TRUE,
+                  dimnames = list(NULL, c("from", "to")))
> edgesfrom to
[1,] "A"  "B"
[2,] "B"  "A"
[3,] "C"  "D"
> arcs(e) = edges
> eRandom/Generated Bayesian networkmodel:[partially directed graph]nodes:                                 6 arcs:                                  2 undirected arcs:                     1 #这里有个非直接环弧 directed arcs:                       1 average markov blanket size:           0.67 average neighbourhood size:            0.67 average branching factor:              0.17 generation algorithm:                  Empty

②特定的的邻接矩阵

使用amat()函数可以用矩阵来产生图结构。
我们首先需要做的是产生一个矩阵,矩阵的元素为0/1整数(注意这里使用0L1L,0和1也是可以的,但是会占更多的内存,这在大数据集尤为重要),1L就代表存在一条弧。

> adj = matrix(0L, ncol = 6, nrow = 6,
+              dimnames = list(LETTERS[1:6], LETTERS[1:6]))
> adjA B C D E F
A 0 0 0 0 0 0
B 0 0 0 0 0 0
C 0 0 0 0 0 0
D 0 0 0 0 0 0
E 0 0 0 0 0 0
F 0 0 0 0 0 0
> adj["A", "C"] = 1L
> adj["B", "F"] = 1L
> adj["C", "F"] = 1L
> adj["D", "E"] = 1L
> adj["A", "E"] = 1L
> adjA B C D E F
A 0 0 1 0 1 0
B 0 0 0 0 0 1
C 0 0 0 0 0 1
D 0 0 0 0 1 0
E 0 0 0 0 0 0
F 0 0 0 0 0 0

接着使用amat()函数将弧(arcs)引入到图中

> amat(e) = adj
> eRandom/Generated Bayesian networkmodel:[A][B][D][C|A][E|A:D][F|B:C] nodes:                                 6 arcs:                                  5 undirected arcs:                     0 directed arcs:                       5 average markov blanket size:           2.33 average neighbourhood size:            1.67 average branching factor:              0.83 generation algorithm:                  Empty

注意:amat()函数和arcs()函数一样,都会检查错误的图结构

③特定的模型公式

模型公式的格式来源于deal包,定义如下

  • 每个节点在方括号中
  • 每个节点的父节点在列在管道符后(级竖杆符号|),父节点间用冒号:隔开,但是都是在一个方括号中
    可以采用两种方式来定义图结构
使用model2network()函数,不用先产生空图
> model2network("[A][C][B|A][D|C][F|A:B:C][E|F]")Random/Generated Bayesian networkmodel:[A][C][B|A][D|C][F|A:B:C][E|F] nodes:                                 6 arcs:                                  6 undirected arcs:                     0 directed arcs:                       6 average markov blanket size:           2.67 average neighbourhood size:            2.00 average branching factor:              1.00 generation algorithm:                  Empty
使用modelstring()函数和一个已存在的bn对象,类似之前的arcs()amat()
> modelstring(e) = "[A][C][B|A][D|C][F|A:B:C][E|F]"
> eRandom/Generated Bayesian networkmodel:[A][C][B|A][D|C][F|A:B:C][E|F] nodes:                                 6 arcs:                                  6 undirected arcs:                     0 directed arcs:                       6 average markov blanket size:           2.67 average neighbourhood size:            2.00 average branching factor:              1.00 generation algorithm:                  Empty 

注意:model2network()modelstring()函数同amat()函数、arcs()函数一样,都会检查错误的图结构。

3、创建一个或多个随机结构

①特定节点顺序

默认情况下,random.graph()函数产生的图和提供的节点顺序一致;在每个弧中,尾部节点排在头部节点的前面。弧是独立的样本,其包含概率由prob参数指定,prob参数的默认值设置为使图中平均有与节点一样多的弧。

> random.graph(LETTERS[1:6], prob = 0.1) #顺序是从A到FRandom/Generated Bayesian networkmodel:[A][B][D][E][C|B][F|C]   #这里可以看出,[B]是排在[C|B]前面的nodes:                                 6 arcs:                                  2 undirected arcs:                     0 directed arcs:                       2 average markov blanket size:           0.67 average neighbourhood size:            0.67 average branching factor:              0.33 generation algorithm:                  Full Ordering arc sampling probability:              0.1

empty.graph()函数一样,我们也可以指定num 参数来产生一批图,这样就会产生一个列表,可以使用lapply() 来很好地批量计算。

②以均匀概率从连通的有向无环图空间中抽样

除了使用默认的方法method = "ordered",还可以使用method = "ic-dag"来从具有均匀概率的连通的有向无环图空间中抽样,使用 Ide & Cozman MCMC sampler
请注意,建议的老化长度(length of the burn-in scales ?)与节点数量成二次函数关系,因此使用num批量生成多个图要比一次生成一个图高效得多。

> random.graph(LETTERS[1:6], num = 2, method = "ic-dag")
[[1]]Random/Generated Bayesian networkmodel:[C][E][A|E][B|A:E][D|B:E][F|A:C:D] nodes:                                 6 arcs:                                  8 undirected arcs:                     0 directed arcs:                       8 average markov blanket size:           3.67 average neighbourhood size:            2.67 average branching factor:              1.33 generation algorithm:                  Ide & Cozman's Multiconnected DAGs burn in length:                        216 maximum in-degree:                     Inf maximum out-degree:                    Inf maximum degree:                        Inf [[2]]Random/Generated Bayesian networkmodel:[E][A|E][B|A:E][C|A][D|B:E][F|A:C:D] nodes:                                 6 arcs:                                  9 undirected arcs:                     0 directed arcs:                       9 average markov blanket size:           3.67 average neighbourhood size:            3.00 average branching factor:              1.50 generation algorithm:                  Ide & Cozman's Multiconnected DAGs burn in length:                        216 maximum in-degree:                     Inf maximum out-degree:                    Inf maximum degree:                        Inf 

此方法对生成的图的结构接受几个可选的约束,如下面的参数所指定:

  • burn.in: the number of iterations for the algorithm to converge to a stationary (and uniform) probability distribution.收敛迭代的次数
  • every: return only one graph every number of steps instead of all the generated graphs. High values of every result in a more diverse set.每个步骤只返回一个图,而不是所有生成的图。every 的高值会导致更多样化的集合。
  • max.degree: the maximum degree of a node.
  • max.in.degree: the maximum in-degree.
  • max.out.degree: the maximum out-degree.
    举例
random.graph(LETTERS[1:6], num = 2, method = "ic-dag", burn.in = 10^4,
+              every = 50, max.degree = 3)
[[1]]Random/Generated Bayesian networkmodel:[B][F|B][A|F][C|A:F][D|A:B][E|B:C] nodes:                                 6 arcs:                                  8 undirected arcs:                     0 directed arcs:                       8 average markov blanket size:           3.33 average neighbourhood size:            2.67 average branching factor:              1.33 generation algorithm:                  Ide & Cozman's Multiconnected DAGs burn in length:                        10000 maximum in-degree:                     Inf maximum out-degree:                    Inf maximum degree:                        3 [[2]]Random/Generated Bayesian networkmodel:[A][C|A][F|C][D|F][B|D:F][E|B:C] nodes:                                 6 arcs:                                  7 undirected arcs:                     0 directed arcs:                       7 average markov blanket size:           2.67 average neighbourhood size:            2.33 average branching factor:              1.17 generation algorithm:                  Ide & Cozman's Multiconnected DAGs burn in length:                        10000 maximum in-degree:                     Inf maximum out-degree:                    Inf maximum degree:                        3

③以均匀概率从有向无环图空间抽样

Melançon的 MCMC算法以均匀概率从有向无环图空间抽样(不需要连接not necessarily connected),指定method = "melancon"即可。

> random.graph(LETTERS[1:6], method = "melancon")Random/Generated Bayesian networkmodel:[A][B|A][F|A][C|A:B][E|B][D|B:C:E] nodes:                                 6 arcs:                                  8 undirected arcs:                     0 directed arcs:                       8 average markov blanket size:           3.00 average neighbourhood size:            2.67 average branching factor:              1.33 generation algorithm:                  Melancon's Uniform Probability DAGs burn in length:                        216 maximum in-degree:                     Inf maximum out-degree:                    Inf maximum degree:                        Inf 

至于可选的参数,它与Ide & Cozman算法是相同的。

4、总结

  1. 创建三种网络:空网络、特定网络、随机网络
  2. 创建特定网络网络的三种方法:边集、邻接矩阵、模型公式
  3. 存在的问题:对随机网络不太懂,为什么要创建随机网络,是给我们看看有这些节点可以产生什么样的结构吗?
  4. 随机网络的取样是啥意思?

贝叶斯网络R语言学习笔记1相关推荐

  1. R语言学习笔记(八)--读写文件与网络爬虫

    R语言学习笔记(八) 1 工作路径 2 保存R对象 3 Scan函数 3-1 从控制台读取数据 3-2 从txt文件读取数据 3-3 从url读取数据 4 按行读写文本文件 5 读取文本文件(txt. ...

  2. R语言学习笔记——入门篇:第一章-R语言介绍

    R语言 R语言学习笔记--入门篇:第一章-R语言介绍 文章目录 R语言 一.R语言简介 1.1.R语言的应用方向 1.2.R语言的特点 二.R软件的安装 2.1.Windows/Mac 2.2.Lin ...

  3. R语言学习笔记(1~3)

    R语言学习笔记(1~3) 一.R语言介绍 x <- rnorm(5) 创建了一个名为x的向量对象,它包含5个来自标准正态分布的随机偏差. 1.1 注释 由符号#开头. #函数c()以向量的形式输 ...

  4. r语言c函数怎么用,R语言学习笔记——C#中如何使用R语言setwd()函数

    在R语言编译器中,设置当前工作文件夹可以用setwd()函数. > setwd("e://桌面//") > setwd("e:\桌面\") > ...

  5. R语言学习笔记 07 Probit、Logistic回归

    R语言学习笔记 文章目录 R语言学习笔记 probit回归 factor()和as.factor() relevel() 案例11.4复刻 glm函数 整理变量 回归:Logistic和Probit- ...

  6. R语言学习笔记——高级篇:第十四章-主成分分析和因子分析

    R语言 R语言学习笔记--高级篇:第十四章-主成分分析和因子分析 文章目录 R语言 前言 一.R中的主成分和因子分析 二.主成分分析 2.1.判断主成分的个数 2.2.提取主成分 2.3.主成分旋转 ...

  7. R语言学习笔记——入门篇:第三章-图形初阶

    R语言 R语言学习笔记--入门篇:第三章-图形初阶 文章目录 R语言 一.使用图形 1.1.基础绘图函数:plot( ) 1.2.图形控制函数:dev( ) 补充--直方图函数:hist( ) 补充- ...

  8. R语言学习笔记 06 岭回归、lasso回归

    R语言学习笔记 文章目录 R语言学习笔记 比较lm.ridge和glmnet函数 画岭迹图 图6-4 <统计学习导论 基于R语言的应用>P182 图6-6<统计学习导论 基于R语言的 ...

  9. R语言学习笔记(三)多元数据的数据特征、相关分析与图形表示

    文章目录 写在前面 独立性检验 χ2\chi^2χ2独立性检验 Fisher独立性检验 Cochran-Mantel-Haenszel χ2\chi^2χ2独立性检验 相关性分析 相关性检验 相关性检 ...

  10. R语言学习笔记-机器学习1-3章

    在折腾完爬虫还有一些感兴趣的内容后,我最近在看用R语言进行简单机器学习的知识,主要参考了<机器学习-实用案例解析>这本书. 这本书是目前市面少有的,纯粹以R语言为基础讲解的机器学习知识,书 ...

最新文章

  1. SharePoint基础之六- SharePoint基础架构中涉及的ASP.NET架构
  2. php安装问题_PHP安装十大经典问题
  3. unity字符串换行符_Unity中Text中首行缩进两个字符和换行的代码
  4. Storm介绍及安装部署
  5. 179. 最大数---LeetCode---JAVA
  6. U盘:对于目标文件系统,文件过大放不到u盘里
  7. 计算机应用的时间地点意义,计算机应用在教学中的作用
  8. CCF数图焦点 | 数据治理
  9. Python入门篇-生成器函数
  10. Mac OS X下MySql卸载方法
  11. 制图折断线_制图规范
  12. Windows 10 下,强制关闭端口
  13. H5页面调用微信支付
  14. 高考首日,为梦想加油!
  15. 学习linux压缩命令压缩文档
  16. TCP与UDP的区别:
  17. EventLoop(回顾)
  18. java解析xml文件并写入Excel表
  19. 最基本的顺序表(经典顺序表)
  20. python excel写入日期变数字_解决python 读取excel时 日期变成数字并加.0的问题

热门文章

  1. php 图片上加文字,PHP语言之给图片添加文字(支持中文)//PHP函数
  2. 易基因表观技术 | m6A常量/微量RNA甲基化测序及案例文献
  3. origin数据平滑_Origin平滑曲线的使用方法
  4. python中右对齐_python右对齐的实例方法
  5. 混沌大学--喜茶模式拷贝指南
  6. 如何将HTML与win10桌面壁纸,如何在Windows 10中将页面设置为桌面背景 | MOS86
  7. Java学习到什么程度可以找第一份工作?
  8. 【VBA宏编程】——Excel操作
  9. adb shell 小米手机_【ADB命令实战】免ROOT停用小米手机系统应用
  10. Python实现简单的层次聚类算法以及可视化