Data type error with bn.fit() in R: variable is not supported in bnlearn (type: integer)

5.1k views Asked by At

Based on a given network structure, I created a data frame of 100 instances for six binary variables(x1 to x6). So it's a 100 x 6 data frame of 0/1 values stored in a variable 'input_params'. Created an empty graph using statements:

library(bnlearn) bn_graph = empty.graph(names(input_params))

But when I try fitting above parameters('input_params') in the network using

bn_nw <- bn.fit(bn_graph, input_params)

I get an error saying
Error in data.type(x) : variable x1 is not supported in bnlearn (type: integer).

What data type conversion should I do to avoid this error? Right now its 0 or 1 in the values.

2

There are 2 answers

0
hard On

The function bn.fit() in the package bnlearn calculates a local conditional probability distribution for each variable.

In the discrete case we expect categorical (factor function) parameters (in the columns "fac1","fac2","fac3") :
fac_cols <- c("fac1","fac2","fac3")

Is is the data continous (e.g. measurements from a sensor) the data needs to be of type numeric (numeric function):
num_cols <- c("num1","num2","num3")

Assuming input_params is a data.frame, we need to transform both sets of columns (fac_cols, num_cols) by either:

input_params[,fac_cols] <- lapply(input_params[,fac_cols], as.factor)
input_params[,num_cols] <- lapply(input_params[,num_cols], as.numeric)

or with dplyr

input_params <- input_params %>% mutate_at(vars(fac_cols), funs(as.factor)) %>% mutate_at(vars(num_cols), funs(as.numeric))
2
Parviz Shahmirzalou On

you need to change qualitative variable names to one character, for example you have a variable name Age with categories young, adult, old. Now change names of subgroups to y,a,o I have a problem like you and solved