-1

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?

user20650
  • 24,654
  • 5
  • 56
  • 91
  • 3
    Welcome to SO, Naim Assis Ismail! What other packages are you using? I cannot find `check.data`, is it not in `bnlearn` afaict. Also, please do not post (only) an image of code/data/errors: it breaks screen-readers and it cannot be copied or searched (ref: https://meta.stackoverflow.com/a/285557 and https://xkcd.com/2116/). Please include the code, console output, or data (e.g., `data.frame(...)` or the output from `dput(head(x))`) directly into a [code block]. – r2evans Aug 10 '23 at 21:02
  • agree with all of the above, except `check.data` which is an unexported function from `bnlearn`. But this is a typo by using `newdata` argument instead of `data` – user20650 Aug 11 '23 at 15:44

1 Answers1

2

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")
Paul Stafford Allen
  • 1,840
  • 1
  • 5
  • 16