大学课程的简单 R 代码问题

问题描述 投票:0回答:1

我不太擅长编码,但最终不得不学习这个模块,而且我真的很挣扎。 ChatGPT 无法在 RStudio 上帮助我解决这一问题。

我就开门见山吧。训练数据:

使用 R,我需要根据这些特征做出 2 个预测(购买计算机:是/否)

本质上是说两者中的每一个都是“是”还是“否”。 我尝试了下面的代码并收到错误“check.data(data,allow.levels = TRUE) 中的错误:数据丢失。”

  Bayesian network parameters

  Parameters of node Income (multinomial distribution)

Conditional probability table:
 High  Low 
 0.4  0.6 

  Parameters of node Student (multinomial distribution)

Conditional probability table:
 FALSE  TRUE 
  0.4   0.6 

  Parameters of node Credit.Rating (multinomial distribution)

Conditional probability table:
 Excellent      Fair 
0.4666667 0.5333333 

  Parameters of node Buy.Computer (multinomial distribution)

Conditional probability table:
 
, , Student = FALSE, Credit.Rating = Excellent

            Income
Buy.Computer      High       Low
         No  0.5000000 0.3333333
         Yes 0.5000000 0.6666667

, , Student = TRUE, Credit.Rating = Excellent

            Income
Buy.Computer      High       Low
         No  0.6666667 0.5000000
         Yes 0.3333333 0.5000000

, , Student = FALSE, Credit.Rating = Fair

            Income
Buy.Computer      High       Low
         No  0.5000000 0.3333333
         Yes 0.5000000 0.6666667

, , Student = TRUE, Credit.Rating = Fair

            Income
Buy.Computer      High       Low
         No  0.6666667 0.6250000
         Yes 0.3333333 0.3750000


Error in check.data(data, allow.levels = TRUE) : the data are missing.
Show in New Window
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)
> 
> print(learned.network)

  Bayesian network parameters

  Parameters of node Income (multinomial distribution)

Conditional probability table:
 High  Low 
 0.4  0.6 

  Parameters of node Student (multinomial distribution)

Conditional probability table:
 FALSE  TRUE 
  0.4   0.6 

  Parameters of node Credit.Rating (multinomial distribution)

Conditional probability table:
 Excellent      Fair 
0.4666667 0.5333333 

  Parameters of node Buy.Computer (multinomial distribution)

Conditional probability table:
 
, , Student = FALSE, Credit.Rating = Excellent

            Income
Buy.Computer      High       Low
         No  0.5000000 0.3333333
         Yes 0.5000000 0.6666667

, , Student = TRUE, Credit.Rating = Excellent

            Income
Buy.Computer      High       Low
         No  0.6666667 0.5000000
         Yes 0.3333333 0.5000000

, , Student = FALSE, Credit.Rating = Fair

            Income
Buy.Computer      High       Low
         No  0.5000000 0.3333333
         Yes 0.5000000 0.6666667

, , Student = TRUE, Credit.Rating = Fair

            Income
Buy.Computer      High       Low
         No  0.6666667 0.6250000
         Yes 0.3333333 0.3750000


> 
> 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.
> 

我不知道该怎么办,差点把我的头发拔掉!

非常感谢!

r bayesian bayesian-networks
1个回答
0
投票

来自

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")
© www.soinside.com 2019 - 2024. All rights reserved.