BNlearn R error “variable Variable1 must have at least two levels.”

后端 未结 1 1685
予麋鹿
予麋鹿 2021-01-20 16:04

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         


        
相关标签:
1条回答
  • 2021-01-20 16:47

    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.

    0 讨论(0)
提交回复
热议问题