4.参数学习
得到贝叶斯网的网络结构之后,可以对局部分布的参数进行参数估计了,这称作参数学习。
4.1参数学习的基本方法
bnlearn包的参数学习函数是bn.fit,其参数method给出了两种具体的方法:“mle”为极大似然估计;"bayes"为贝叶斯后验估计(采用无信息先验分布)。
4.2对marks数据集的参数学习
marks是个连续数据集,所以参数采用的是回归系数的形式。下面对其中的一个节点计算参数的极大似然估计:
library(bnlearn)
## Warning: package 'bnlearn' was built under R version 3.0.1
data(marks)
marks.bn <- gs(marks, undirected = FALSE)
marks.bn1 <- hc(marks)
marks.fit <- bn.fit(marks.bn1, data = marks)
marks.fit <- bn.fit(marks.bn, data = marks)
## Error: the graph is only partially directed.
marks.fit$ALG
##
## Parameters of node ALG (Gaussian distribution)
##
## Conditional density: ALG | MECH + VECT
## Coefficients:
## (Intercept) MECH VECT
## 25.3620 0.1834 0.3577
## Standard deviation of the residuals: 7.987
## Pearson's Linear Correlation
##
## data: ALG ~ STAT | ANL
## cor = 0.4172, df = 85, p-value = 5.827e-05
## alternative hypothesis: true value is not equal to 0
#在参数估计的时候,由gs算法得到的mark.bn是个部分有向图,会报错。
参数估计bn.fit的结果得到一个bn.fit对象。利用这个可以修改局部分布。
marks.fit$ALG <- list(coef = c(`(Intercept)` = 26, MECH = 0.18, VECT = 0.36),
sd = 7.99)
marks.fit$ALG
##
## Parameters of node ALG (Gaussian distribution)
##
## Conditional density: ALG | MECH + VECT
## Coefficients:
## (Intercept) MECH VECT
## 26.00 0.18 0.36
## Standard deviation of the residuals: 7.99
在marks这个连续数据例子中,生成的网络是高斯贝叶斯网,在上面对参数的修改中,包括了一个回归系数的完整集合(就是coef),以及残差的标准差(sd)
另外,利用custom.fit函数也可以使用上面的语法生成bn.fit对象。
5.离散化
下面考虑在保持相依结构的前提下把marks数据集进行离散化。这种离散化会如何改变网络的形式呢?discretize可以实现离散化的功能。下面是discretize采用interval方法,按照中位数分为两个区间(breaks=2),数据按相应的区间归到对应的类中。 离散化的数据也可以采用结构学习的方法,由结构学习算法得到相应的网络结构。
marks.d <- discretize(marks, method = "interval", breaks = 2)
marks.dgs <- gs(marks.d)
得到贝叶斯网的网络结构之后,可以对局部分布的参数进行参数估计了,这称作参数学习。
4.1参数学习的基本方法
bnlearn包的参数学习函数是bn.fit,其参数method给出了两种具体的方法:“mle”为极大似然估计;"bayes"为贝叶斯后验估计(采用无信息先验分布)。
4.2对marks数据集的参数学习
marks是个连续数据集,所以参数采用的是回归系数的形式。下面对其中的一个节点计算参数的极大似然估计:
library(bnlearn)
## Warning: package 'bnlearn' was built under R version 3.0.1
data(marks)
marks.bn <- gs(marks, undirected = FALSE)
marks.bn1 <- hc(marks)
marks.fit <- bn.fit(marks.bn1, data = marks)
marks.fit <- bn.fit(marks.bn, data = marks)
## Error: the graph is only partially directed.
marks.fit$ALG
##
## Parameters of node ALG (Gaussian distribution)
##
## Conditional density: ALG | MECH + VECT
## Coefficients:
## (Intercept) MECH VECT
## 25.3620 0.1834 0.3577
## Standard deviation of the residuals: 7.987
## Pearson's Linear Correlation
##
## data: ALG ~ STAT | ANL
## cor = 0.4172, df = 85, p-value = 5.827e-05
## alternative hypothesis: true value is not equal to 0
#在参数估计的时候,由gs算法得到的mark.bn是个部分有向图,会报错。
参数估计bn.fit的结果得到一个bn.fit对象。利用这个可以修改局部分布。
marks.fit$ALG <- list(coef = c(`(Intercept)` = 26, MECH = 0.18, VECT = 0.36),
sd = 7.99)
marks.fit$ALG
##
## Parameters of node ALG (Gaussian distribution)
##
## Conditional density: ALG | MECH + VECT
## Coefficients:
## (Intercept) MECH VECT
## 26.00 0.18 0.36
## Standard deviation of the residuals: 7.99
在marks这个连续数据例子中,生成的网络是高斯贝叶斯网,在上面对参数的修改中,包括了一个回归系数的完整集合(就是coef),以及残差的标准差(sd)
另外,利用custom.fit函数也可以使用上面的语法生成bn.fit对象。
5.离散化
下面考虑在保持相依结构的前提下把marks数据集进行离散化。这种离散化会如何改变网络的形式呢?discretize可以实现离散化的功能。下面是discretize采用interval方法,按照中位数分为两个区间(breaks=2),数据按相应的区间归到对应的类中。 离散化的数据也可以采用结构学习的方法,由结构学习算法得到相应的网络结构。
marks.d <- discretize(marks, method = "interval", breaks = 2)
marks.dgs <- gs(marks.d)
plot(marks.dgs, radius = 200, arrow = 30)
marks.dhc <- hc(marks.d)
plot(marks.dhc, radius = 160, arrow = 40)
all.equal(cpdag(marks.dgs), cpdag(marks.dhc))
## [1] TRUE
可以看到离散化之后的网络结构依然保持着连续数据marks生成的部分网络结构。
离散化的网络参数就构成了CPT(条件概率表)。下面是参数学习的结果:
marks.fit2 <- bn.fit(marks.dhc, data = marks.d)
marks.fit2$ALG
##
## Parameters of node ALG (multinomial distribution)
##
## Conditional probability table:
##
## VECT
## ALG [8.93,45.5] (45.5,82.1]
## [14.9,47.5] 0.5806 0.2281
## (47.5,80.1] 0.4194 0.7719