"bnlearn" usage in R for prediction of discrete variables

558 Views Asked by At

I am working on "bnlearn" in R to construct a probabilistic network on discrete variables. I need to predict one variable based on the constructed BN. Let's take iris data as an example, I wrote R codes referring to another post in CrossValidated. The codes are as below:

> data(iris)
> set.seed(1)
> tr_idx <- sample(seq_len(nrow(iris)), size = 100) 
> tr <- iris[tr_idx,]
> te <- iris[-tr_idx,]
> res <- hc(tr)
> fit <- bn.fit(res, tr)
> pred <- predict(fit, "Species", te)
> cb <- cbind(pred, te[, "Species"])
> accuracy(f = pred, x = te[, "Species"])

Am I right? My questions are: 1. Do I have to discretise each variables? 2. If my data include both discrete and continuous variables, which bn structure learning function I might use? 3. Function accuracy did not work in terms of the above example, I got an error as below:

accuracy(f = pred, x = te[, "Species"])
Error in accuracy(f = pred, x = te[, "Species"]) : First argument should be a forecast object or a time series.

So, Would you please help on this? Many thanks in advance.

Regards, Xuemei

0

There are 0 best solutions below