尝试使用 BNlearn 创建 BN,但我不断收到错误消息;
Error in check.data(data, allowed.types = discrete.data.types) : variable Variable1 must have at least two levels.
它给了我每个变量的错误,即使它们都是因素并且有超过 1 个级别,如您所见 - 在这种情况下,我的变量“模型”有 4 个级别
由于我无法共享变量和数据集,因此我为数据集创建了一个小集合和所属代码。我遇到同样的问题。我知道我只共享了 2 个变量,但所有变量都出现相同的错误。
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 个或更多级别:
字符串(数据)
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