0

尝试在“bnlearn”包中使用结构性.em 时出现错误这是代码:

cut.learn<- structural.em(cut.df, maximize = "hc", 
+               maximize.args = "restart",
+               fit="mle", fit.args = list(),
+               impute = "parents", impute.args = list(), return.all = FALSE,
+               max.iter = 5, debug = FALSE)

check.data(x, allow.levels = TRUE, allow.missing = TRUE, warn.if.no.missing = TRUE, : 至少一个变量没有观察值。

有没有人遇到同样的问题,请告诉我如何解决它。谢谢你。

4

1 回答 1

1

我得到了structural.em 的工作。我目前正在研究我称之为 pybnl 的 bnlearn 的 python 接口。我也遇到了你上面描述的问题。

这是一个 jupyter 笔记本,展示了如何从 python标记中使用 StructuralEM 。

它的要点在第 135 页的slides-bnshort.pdf “The MARKS Example, Revisited”中进行了描述。

您必须手动使用初始估算数据框创建初始拟合,然后像这样向结构.em 提供参数(ldmarks 是潜在离散标记数据框,其中 LAT 列仅包含缺失/NA 值):

library(bnlearn)
data('marks')
dmarks = discretize(marks, breaks = 2, method = "interval")
ldmarks = data.frame(dmarks, LAT = factor(rep(NA, nrow(dmarks)), levels = c("A", "B")))

imputed = ldmarks
# Randomly set values of the unobserved variable in the imputed data.frame
imputed$LAT = sample(factor(c("A", "B")), nrow(dmarks2), replace = TRUE)

# Fit the parameters over an empty graph
dag = empty.graph(nodes = names(ldmarks))
fitted = bn.fit(dag, imputed)
# Although we've set imputed values randomly, nonetheless override them with a uniform distribution
fitted$LAT = array(c(0.5, 0.5), dim = 2, dimnames = list(c("A", "B")))

# Use whitelist to enforce arcs from the latent node to all others
r = structural.em(ldmarks, fit = "bayes", impute="bayes-lw", start=fitted, maximize.args=list(whitelist = data.frame(from = "LAT", to = names(dmarks))), return.all = TRUE)  

您必须使用 bnlearn 4.4-20180620 或更高版本,因为它修复了底层 impute 函数中的错误。

于 2018-06-22T11:00:27.453 回答