# Prediction of continuous variable using "bnlearn" package in R

I use bnlearn package in R to learn the structure of my Bayesian Network and its parameters. What I want to do is to "predict" the value of a node given the value of other nodes as evidence (obviously, with the exception of the node whose values we are predicting).

I have continuous variables.

library(bnlearn)                       # Load the package in R
data(gaussian.test)
training.set = gaussian.test[1:4000, ] # This is training set to learn the parameters
test.set = gaussian.test[4001:4010, ]  # This is test set to give as evidence
res = hc(training.set)                 # learn BN structure on training set data
fitted = bn.fit(res, training.set)     # learning of parameters
pred = predict(fitted$C, test.set) # predicts the value of node C given test set table(pred, test.set[, "C"]) # compares the predicted value as original  Now, this code works well and gives a table in which you can see that predicted values for node C are exactly same as the original value of node C in test set. I do not understand the reason for that, could someone please explain it? I know, I am providing entire df of test set that already has the value of node C in it. But if I give the data of other columns, it gives an error. So, I tried an alternative of putting other values to 0. test.set$C = 0                     # To not give the original value of node C as evidence
pred = predict(fitted$C, test.set) # predicts the value of node C given test set table(pred, test.set[, "C"]) # compares the predicted value as original  Is this approach wrong? (Using "NA" is not allowed.) ## 3 Answers Why are you using table to compare the output? Using cbind to put the actual and predicted values side by side shows that the predictions are not the same as the actual, and you can compute standard accuracy metrics to quantify the degree to which they diverge. library(bnlearn) # Load the package in R library(forecast) data(gaussian.test) training.set = gaussian.test[1:4000, ] # This is training set to learn the parameters test.set = gaussian.test[4001:4010, ] # This is test set to give as evidence res = hc(training.set) # learn BN structure on training set data fitted = bn.fit(res, training.set) # learning of parameters pred = predict(fitted, "C", test.set) # predicts the value of node C given test set cbind(pred, test.set[, "C"]) # compare the actual and predicted accuracy(f = pred, x = test.set[, "C"])  Comparing the actual and predicted: > cbind(predicted = pred, actual = test.set[, "C"]) predicted actual [1,] 3.5749952 3.952410 [2,] 0.7434548 1.443177 [3,] 5.1731669 5.924198 [4,] 10.0840800 10.296560 [5,] 12.3966908 12.268170 [6,] 9.1834888 9.725431 [7,] 6.8067145 5.625797 [8,] 9.9246630 9.597326 [9,] 5.9426798 6.503896 [10,] 16.0056136 16.037176  Measuring accuracy of prediction: > accuracy(f = pred, x = test.set[, "C"]) ME RMSE MAE MPE MAPE Test set 0.1538594 0.5804431 0.4812143 6.172352 11.26223  • I get an error Error in is.constant(y) : (list) object cannot be coerced to type 'double' In pred = predict(fitted$C, test.set) Any idea, why? Commented May 8, 2015 at 9:33
• @lovedynasty What line do you get that error on? Commented May 8, 2015 at 10:33
• In line, predict(fitted$C, test.set) Commented May 8, 2015 at 10:51 • @lovedynasty You will have to post a complete reproducbile example with your data to diagnose. I am assuming that the above example runs fine. Commented May 8, 2015 at 11:23 • @lovedynasty The interface of bnlearn:::predict.bn.fit appears to have changed. I have updated my code to account for that change. Commented May 8, 2015 at 20:49 For both the predicted sets you proposed (with both the Original values and zeros) I found the same output in R. [1] 3.5749952 0.7434548 5.1731669 10.0840800 12.3966908 9.1834888 6.8067145 [8] 9.9246630 5.9426798 16.0056136  This shows that the values of C are irrelevant. Furthermore, test.set$c provides you with:

[1]  3.952410  1.443177  5.924198 10.296560 12.268170  9.725431  5.625797  9.597326
[9]  6.503896 16.037176


which is inherently different from the predicted output. This leads me to believe that your code is in fact correct.

The equivalent for the discrete case occurs (inability to set the target variable to zero). In this case do the following:

test.set\$TARGET<-as.factor(0) levels(test.set\$TARGET) <- c(level1,level2,level3...)