我正在 R 中使用 bnlearn
包来处理贝叶斯网络
中的大量数据。这些变量是离散的,并且有超过 300 万个观测值。
使用 bn.fit 函数我可以轻松获得条件概率分布。然而,有些变量具有未观察到的值(即 NA 或 NaN)。
在一些测试中,我得到了这个:
nw.fit <-bn.fit (nw, date, method = 'bayes')
Error in check.data (date): the data set contains NULL/NaN/NA values.
所以, 我如何处理数据并获得条件概率分布?
有人可以帮助我吗?
谢谢!
最佳答案
catnet包可以处理缺失数据,专为离散数据而设计
您可以使用 cnProb(bnet,which) 获得条件概率
这是一个例子
rm(list=ls())
### generate random data (not simply independent binomials)
set.seed(123)
n.obs <- 10
a1 <- rbinom(n.obs,1,.3)
a2 <- runif(n.obs)
a3 <- floor(-3*log(.25+3*a2/4))
a3[a3>=2] <- NA
a2 <- floor(2*a2)
my.data <- data.frame(a1,a2,a3 )
### discretize data into proper categories
my.data <- cnDiscretize(my.data,numCategories=2)
my.data
## a1 a2 a3
## 1 1 2 1
## 2 2 1 2
## 3 1 2 1
## 4 2 2 2
## 5 2 1 NA
## 6 1 2 1
## 7 1 1 NA
## 8 2 1 NA
## 9 1 1 NA
## 10 1 2 1
## say we want a2 conditional on a1,a3
## first generate a network with a1,a3 ->a2
cnet <- cnNew(
nodes = c("a1", "a2", "a3"),
cats = list(c("1","2"), c("1","2"), c("1","2")),
parents = list(NULL, c(1,3), NULL)
)
## set the empirical probabilities from data=my.data
cnet2 <- cnSetProb(cnet,data=my.data)
## to get the conditional probability table
cnProb(cnet2,which='a2')
##$a2
## a1 a3 0 1
## A 0.0000000 0.0000000 0.0000000 1.0000000
## B 0.0000000 1.0000000 0.5712826 0.4287174
## A 1.0000000 0.0000000 0.0000000 1.0000000
## B 1.0000000 1.0000000 0.5685786 0.4314214
关于r - bnlearn 包 R 中的 NA/NaN 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24112535/