I tried to create bayesian network with function iamb(x) and gs(x). But it showed "Error in check.data(x) : variable MFYield must have at least two levels."
Here is my code,
bn[sapply(bn, is.character)] <- lapply(bn[sapply(bn, is.character)], as.factor)
attach(bn)
this is sample of my data "bn":
str(bn)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 59 obs. of 42 variables:
$ MFYield : Factor w/ 2 levels "0","1": 1 1 1 1 2 1 1 1 1 1 ...
$ MWtA : Factor w/ 2 levels "0","1": 2 2 2 2 2 2 2 2 2 2 ...
$ MWtT : Factor w/ 2 levels "0","1": 2 2 2 2 1 2 2 2 2 2 ...
$ MClb : Factor w/ 2 levels "0","1": 2 2 2 2 2 1 1 1 1 1 ...
$ MPS : Factor w/ 2 levels "0","1": 2 2 2 2 2 2 2 2 2 2 ...
$ MTwU : Factor w/ 2 levels "0","1": 2 2 2 2 2 2 2 2 2 2 ...
$ MTwD : Factor w/ 2 levels "0","1": 1 1 1 1 1 2 2 2 2 2 ...
$ MTwDAgent: Factor w/ 2 levels "0","1": 1 1 1 1 1 2 2 2 2 2 ...
bn.iamb = iamb(bn)
The error begun here, when I try to create network. Error in check.data(x) : variable MFYield must have at least two levels.
I am not sure that because my data is tibble? A tibble: 59 x 42 When I checked, it said:
nlevels("bn$MFYield")
[1] 0
class(bn)
[1] "tbl_df" "tbl" "data.frame"
is.factor(bn$MFYield)
[1] TRUE
So, I think my data is already a factor, but R cannot detect it is already have two level. And I do not understand, why?
How can I fix it?. I quite a new beginner for R please help me to get through this.
Thank you.