英文:
Why do I get "Error in check.data(data, allow.levels = TRUE)" when using predict in bnlearn
问题
以下是代码部分的翻译:
训练数据:
![](https://i.stack.imgur.com/FWqwD.png)
使用 R 时,我需要基于以下特征进行两次预测(购买计算机:是/否)
![](https://i.stack.imgur.com/HDdXz.png)
基本上是要判断每个特征的答案是否为“是”或“否”。
我尝试了以下代码,但出现了错误
> Error in check.data(data, allow.levels = TRUE) : the data are missing.(数据丢失。)
为什么会出现此错误?
英文:
The training data:
With R I need to then make 2 predictions (Buy Computer: YES/NO) based on these features
Essentially to say whether it would be Yes or No for each of the two.
I've tried the code below and get the error
> Error in check.data(data, allow.levels = TRUE) : the data are missing.
> library(bnlearn)
>
> data_computer <- data.frame(predictions.table)
> data_computer$Income <- as.factor(data_computer$Income)
> data_computer$Student <- as.factor(data_computer$Student)
> data_computer$Credit.Rating <- as.factor(data_computer$Credit.Rating)
> data_computer$Buy.Computer <- as.factor(data_computer$Buy.Computer)
>
> network_structure <- empty.graph(nodes = c("Income","Student","Credit.Rating","Buy.Computer"))
>
> network_structure <- set.arc(network_structure,"Income","Buy.Computer")
> network_structure <- set.arc(network_structure,"Student","Buy.Computer")
> network_structure <- set.arc(network_structure,"Credit.Rating","Buy.Computer")
>
> learned.network <- bn.fit(network_structure, data_computer)
>
> data_computer_test <- data.frame(
+ Income = c("High", "Low"),
+ Student = c("FALSE", "FALSE"),
+ Credit.Rating = c("Fair", "Excellent")
+ )
>
> data_computer_test$Income <- as.factor(data_computer_test$Income)
> data_computer_test$Student <- as.factor(data_computer_test$Student)
> data_computer_test$Credit.Rating <- as.factor(data_computer_test$Credit.Rating)
>
> new_predictions <- predict(learned.network, newdata=data_computer_test, node="Buy.Computer", method="bayes-lw")
> Error in check.data(data, allow.levels = TRUE) : the data are missing.
Why do I get this error?
答案1
得分: 2
来自predict()
文档的说明:
用法
bn.fit
类的S3方法
predict(object, node, data, cluster, method = "parents", ...,
prob = FALSE, debug = FALSE)
所需的最小参数是object
、node
和data
。(cluster
是可选的,method
、prob
和debug
有默认值)
您的代码:
new_predictions <- predict(learned.network, newdata=data_computer_test, node="Buy.Computer", method="bayes-lw")
R将正确地假设未命名的第一个参数是object
。
其他所有参数都已命名,因此将分配给具有匹配名称的参数。
没有名为newdata
的预期参数,因此这会传递给...
,因此您没有data
,因此会出现错误消息。
尝试这个:
new_predictions <- predict(
object = learned.network,
data = data_computer_test,
node = "Buy.Computer",
method = "bayes-lw")
英文:
From the documentation for predict()
(link):
Usage
## S3 method for class 'bn.fit'
predict(object, node, data, cluster, method = "parents", ...,
prob = FALSE, debug = FALSE)
The minimum required arguments are object
, node
, and data
. (cluster
is optional, and method
, prob
and debug
have default values)
Your code:
new_predictions <- predict(learned.network, newdata=data_computer_test, node="Buy.Computer", method="bayes-lw")
R will correctly assume that the unnamed first argument is the object
.
All the others are named so will be assigned to arguments with matching names.
There is no expected argument named newdata
so this is passed to the ...
and you are left with no data
, hence the error message.
Try this:
new_predictions <- predict(
object = learned.network,
data = data_computer_test,
node = "Buy.Computer",
method = "bayes-lw")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论