我在R中有一个bnlearn模型,它是用包含4个分类变量和8个数值变量的gs函数学习的。当我试图用测试集验证我的模型时,当我试图预测一些节点时,我会得到这个错误:
Check.fit.vs.data中的错误(拟合=对象,数据=数据,子集= object[node]$parents): ‘'Keyword’在节点和数据中有不同的级别数。
在bnlearn中不能同时使用数值变量和范畴变量吗?如果有可能,我做错了什么?
mydata$A <- as.factor(mydata$A)
mydata$B <- as.numeric(mydata$B)
mydata$C <- as.numeric(mydata$C)
mydata$D <- as.numeric(mydata$D)
mydata$E <- as.factor(mydata$E)
mydata$F <- as.numeric(mydata$F)
mydata$G <- as.numeric(mydata$G)
mydata$H <- as.numeric(mydata$H)
mydata$I <- as.numeric(mydata$I)
mydata$J <- as.numeric(mydata$J)
mydata$K <- as.numeric(mydata$K)
mydata$L <- as.numeric(mydata$L)
mydata$M <- as.numeric(mydata$M)
mydata$N <- as.numeric(mydata$N)
mydata$O <- as.numeric(mydata$O)
mydata$P <- as.numeric(mydata$P)
mydata$Q <- as.numeric(mydata$Q)
#create vector of black arcs
temp1=vector(mode = "character", length = 0)
for (i in 1:length(varnames)){
for (j in 1:length(varnames)){
temp1 <- c(temp1,varnames[i])
}
}
temp2=vector(mode = "character", length = 0)
for (i in 1:length(varnames)){
temp2 <- c(temp2,varnames)
}
#creat to arcs of the model
arcdata = read.csv("C:/users/asaf/desktop/in progress/whitearcs.csv", header = T)
wfrom=arcdata[,1]
wto=arcdata[,2]
whitelist = data.frame(from = wfrom,to =wto)
#block unwanted arcs
blacklist = data.frame(from = temp1, to = temp2)
#fit and plot the model
#gaussian method
model = gs(mydata, whitelist = whitelist, blacklist = blacklist)
#inference procedure
learntmodel = bn.fit(model,mydata,method = "mle",debug = F)
graphviz.plot(learntmodel)
myvalidation=read.csv("C:/users/asaf/desktop/in progress/val.csv", header = T)
#predicate A
pred = predict(learntmodel, node="A", myvalidation)
myvalidation$A <- pred
#predicate B
pred = predict(learntmodel, node="B", myvalidation)
myvalidation$B <- pred此时,它抛出以下错误:
Check.fit.vs.data中的错误(拟合=对象,数据=数据,子集= object[node]$parents): ‘’在节点和数据中有不同的级别数。
发布于 2016-03-26 13:02:10
bnlearn不能同时处理混合变量(定性和定量),我认为在deal包中是可能的。
另一种可能是使用discretize将连续变量转换为离散变量:
dmydata <- discretize(mydata, breaks = 2, method = "interval")
model <- gs(dmydata, whitelist = whitelist, blacklist = blacklist)..。继续你的代码。
发布于 2016-05-29 16:40:27
实际上,我今天也遇到了同样的问题,我解决了这个问题,我确保连接到这个节点的其他节点.即$A,也有相同的水平。
https://stackoverflow.com/questions/32501233
复制相似问题