Why do I get "Error in check.data(data, allow.levels = TRUE)" when using predict in bnlearn

huangapple go评论152阅读模式
英文:

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:

Why do I get "Error in check.data(data, allow.levels = TRUE)" when using predict in bnlearn

With R I need to then make 2 predictions (Buy Computer: YES/NO) based on these features
Why do I get "Error in check.data(data, allow.levels = TRUE)" when using predict in bnlearn

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)

所需的最小参数是objectnodedata。(cluster是可选的,methodprobdebug有默认值)

您的代码:

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 &#39;bn.fit&#39;
predict(object, node, data, cluster, method = &quot;parents&quot;, ...,
  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 &lt;- predict(learned.network, newdata=data_computer_test, node=&quot;Buy.Computer&quot;, method=&quot;bayes-lw&quot;)

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 &lt;- predict(
    object = learned.network, 
    data = data_computer_test, 
    node = &quot;Buy.Computer&quot;, 
    method = &quot;bayes-lw&quot;)

huangapple
  • 本文由 发表于 2023年8月11日 04:50:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76879225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定