" bnlearn"在R中用于预测离散变量

时间:2016-12-14 16:54:32

标签: prediction

我正在研究" bnlearn"在R中构建离散变量的概率网络。我需要根据构造的BN预测一个变量。让我们以虹膜数据为例,我写了R代码,指的是CrossValidated中的另一篇文章。代码如下:

> 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"])

我是对的吗?我的问题是: 1.我是否必须对每个变量进行分类? 2.如果我的数据包括离散变量和连续变量,我可能会使用哪种结构学习函数? 3.根据上面的例子,功能准确性不起作用,我得到如下错误:

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.

那么,请你帮忙吗?非常感谢提前。

此致 雪梅

0 个答案:

没有答案