BNlearn R错误“变量Variable1必须至少具有两个级别." [英] BNlearn R error “variable Variable1 must have at least two levels.”

查看:267
本文介绍了BNlearn R错误“变量Variable1必须至少具有两个级别."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用BNlearn创建BN,但我不断收到错误消息;

Trying to create a BN using BNlearn, but I keep getting an error;

Error in check.data(data, allowed.types = discrete.data.types) : variable Variable1 must have at least two levels.

这给我每个变量带来的错误,即使它们都是因素,并且具有多个级别,如您所见-在这种情况下,我的变量模型"具有4个级别

It gives me that error for every of my variable, even though they're all factors and has more than 1 levels, As you can see - in this case my variable "model" has 4 levels

由于我无法共享变量和数据集,因此我创建了一个小集合并为该数据集添加了相关代码.我遇到同样的问题.我知道我只共享2个变量,但是所有变量都得到相同的错误.

As I can't share the variables and dataset, I've created a small set and belonging code to the data set. I get the same problem. I know I've only shared 2 variables, but I get the same error for all the variables.

library(tidyverse)
library (bnlearn)
library(openxlsx)

DataFull <- read.xlsx("(.....)/test.xlsx", sheet = 1, startRow = 1, colNames = TRUE)
set.seed(600)
DataFull <- as_tibble(DataFull)

DataFull$Variable1 <- as.factor(DataFull$Variable1)
DataFull$TargetVar <- as.factor(DataFull$TargetVar)

DataFull <- na.omit(DataFull)
DataFull <- droplevels(DataFull)

DataFull <- DataFull[sample(nrow(DataFull)),]
Data <- DataFull[1:as.integer(nrow(DataFull)*0.70)-1,]
Datatest <- DataFull[as.integer(nrow(DataFull)*0.70):nrow(DataFull),]
nrow(Data)+nrow(Datatest)==nrow(DataFull)

FocusVar <- as.character("TargetVar")
BN.naive <- naive.bayes(Data, FocusVar) 

使用str(data),我可以看到该变量已经具有2个或更多级别:

Using str(data), I can see that the variable has 2 or more levels already:

str(数据)

str(Data)

Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   27586 obs. of  2 variables:
 $ Variable1: Factor w/ 3 levels "Small","Medium",..: 2 2 3 3 3 3 3 3 3 3 ...
 $ TargetVar: Factor w/ 2 levels "Yes","No": 1 1 1 1 1 1 2 1 1 1 ...

链接到数据集: https://drive.google.com/open?id= 1VX2xkPdeHKdyYqEsD0FSm1BLu1UCtOj9eVIVfA_KJ3g

推荐答案

bnlearn期望data.frame:不适用于tibbles,因此,通过省略行data.frame >

bnlearn expects a data.frame : doesn't work with tibbles, So keep your data as a data.frame by omitting the line DataFull <- as_tibble(DataFull)

示例

library(tibble)
library (bnlearn)

d <- as_tibble(learning.test)
hc(d)

check.data(x)中的错误:变量A必须至少具有两个级别.

Error in check.data(x) : variable A must have at least two levels.

尤其是从bnlearn:::check.data

if (nlevels(x[, col]) < 2) 
      stop("variable ", col, " must have at least two levels.")

在标准data.frame中,learning.test[,"A"]返回一个向量,因此nlevels(learning.test[,"A"])可以按预期工作,但是,根据设计,您不能从tibbles提取这样的向量:d[,"A"])仍然是tbl_df而不是向量,因此nlevels(d[,"A"])不能按预期工作,并返回零.

In a standard data.frame,learning.test[,"A"] returns a vector and so nlevels(learning.test[,"A"]) works as expected, however, by design, you cannot extract vectors like this from tibbles : d[,"A"]) is still a tbl_df and not a vector hence nlevels(d[,"A"]) doesn't work as expected, and returns zero.

这篇关于BNlearn R错误“变量Variable1必须至少具有两个级别."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆