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

2

7

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.

Grotto answered 14/9, 2017 at 20:0 Comment(2)
The data type must be either numeric or factor. If your data are 0 / 1 's, they are likely categories. so if your data.frame is called dat use dat[] <- lapply(dat, as.factor)Nanon
This works, should put it as answer.Legging
E
2

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))
Earsplitting answered 1/4, 2020 at 17:18 Comment(0)
W
0

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

Worrisome answered 15/11, 2021 at 9:11 Comment(2)
This is not correctNanon
... and to give a counter example; the variables in the alarm dataset that comes with bnlearn have levels with more than one character (library(bnlearn); data("alarm"); str(alarm))Nanon

© 2022 - 2024 — McMap. All rights reserved.