【发布时间】:2018-01-05 00:56:15
【问题描述】:
我正在尝试使用 bnlearn 包 做一个预测模型,但我收到错误指示:“check.data(data) 中的错误:数据丢失” . 这是我用来构建预测模型的示例数据集和代码行:
dat <- read.table(text = " category birds wolfs snakes
yes 3 9 7
no 3 8 4
no 1 2 8
yes 1 2 3
yes 1 8 3
no 6 1 2
yes 6 7 1
no 6 1 5
yes 5 9 7
no 3 8 7
no 4 2 7
notsure 1 2 3
notsure 7 6 3
no 6 1 1
notsure 6 3 9
no 6 1 1 ",header = TRUE)
以下是我用来获得预测的代码行:
dat$birds<-as.numeric(dat$birds)
dat$wolfs<-as.numeric(dat$wolfs)
dat$snakes<-as.numeric(dat$snakes)
training.set = dat[1:8,2:4 ]
demo.set = dat[8:16,2:4 ]
res <- hc(training.set)
fitted = bn.fit(res, training.set)
pred = predict(fitted, demo.set) # I get an error: "Error in check.data(data) : the data are missing."
有什么办法解决吗?
【问题讨论】:
-
您需要指定要对其进行预测的节点。即
predict(fitted, node="snakes", data=demo.set)。要跨所有节点进行预测,您可以使用sapply(names(fitted), function(i) predict(fitted, node=i, demo.set))。查看?bnlearn:::predict.bn.fit了解如何指定参数。 [我认为您已经解决了这个问题(鉴于您随后的,现在已删除的问题) - 如果是这样,请您写下答案并接受它(以便它在 SO 上看起来已解决)。 -
感谢 user20650 的帮助。我通过这行代码得到了我想要的东西:predict(fitted, node="snakes", data=demo.set,method = "bayes-lw" )。该方法在这里很关键,如果没有它,我对所有观察结果都得到了相同的预测值。请写下您的评论作为答案,我会为它投票。
标签: r bayesian-networks