训练数据:
使用 R,我需要根据这些特征做出 2 个预测(购买计算机:是/否)
本质上是说两者中的每一个都是“是”还是“否”。 我尝试了下面的代码并收到错误
check.data(data,allow.levels = TRUE)中的错误:数据丢失。
> 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")
check.data(data,allow.levels = TRUE)中的错误:数据丢失。
为什么我会收到此错误?
来自
predict()
的文档(链接):
Usage
## S3 method for class 'bn.fit'
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")