r - R : variable is not supported in bnlearn (type: integer) 中 bn.fit() 的数据类型错误

标签 r bayesian bnlearn

基于给定的网络结构,我为六个二进制变量(x1 到 x6)创建了一个包含 100 个实例的数据框。所以它是一个 0/​​1 值的 100 x 6 数据帧,存储在变量“input_params”中。使用语句创建了一个空图:
library(bnlearn) bn_graph = empty.graph(names(input_params))
但是当我尝试在网络中使用上面的参数('input_params')拟合时
bn_nw <- bn.fit(bn_graph, input_params)
我收到一条错误消息Error in data.type(x) : variable x1 is not supported in bnlearn (type: integer).
我应该做什么数据类型转换来避免这个错误?现在它的值是 0 或 1。

最佳答案

函数bn.fit()包裹内bnlearn计算每个变量的局部条件概率分布。

在离散情况下,我们期望 categorical ( factor function ) 参数(在 "fac1","fac2","fac3" 列中):fac_cols <- c("fac1","fac2","fac3")
是连续数据(例如来自传感器的测量)数据需要是类型 numeric ( numeric function ):
num_cols <- c("num1","num2","num3")
假设 input_params是一个 data.frame,我们需要通过以下任一方式转换两组列( fac_colsnum_cols ):

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

或与 dplyr
input_params <- input_params %>% mutate_at(vars(fac_cols), funs(as.factor)) %>% mutate_at(vars(num_cols), funs(as.numeric))

关于r - R : variable is not supported in bnlearn (type: integer) 中 bn.fit() 的数据类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46227336/

相关文章:

r - 在 R 中拆分图形网络字符串

r - bnlearn + Rgraphviz : double arrows instead of undirected edges when customizing plots

r - 在 Rstudio 的查看器中显示 Rd 文件

r - 找到一对列中的最大值/最小值

java - 使用 weka API 对高维 int vector 进行分类的最佳方法是什么?

rstan MCMC : Different squence of data resulting in different results, 为什么?

r - 在 R 中使用 Fortran 子例程? undefined symbol

r - 无法绘制直方图, 'x' 必须是数字

machine-learning - 除了贝叶斯方法之外,机器学习中的其他主要方法/范式是什么?

r - 如何使用 R 中的 bnlearn 增加贝叶斯网络图中文本的大小