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 ha
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)
Example
library(tibble)
library (bnlearn)
d <- as_tibble(learning.test)
hc(d)
Error in check.data(x) : variable A must have at least two levels.
In particular, it is the line from bnlearn:::check.data
if (nlevels(x[, col]) < 2)
stop("variable ", col, " must have at least two levels.")
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.