我正在努力使用rjags
. (clg BN 由具有连续法线和离散父节点(预测变量)的连续子节点(结果)定义)
对于下面的网络,A 是离散的,D 和 E 是连续的:
对于rjags
模型,我想我想要的是将节点的参数E
定义在值节点上A
:伪代码
model {
A ~ dcat(c(0.0948, 0.9052 ))
D ~ dnorm(11.87054, 1/1.503111^2)
if A==a then E ~ dnorm(6.558366 + 1.180965*D, 1/2.960002^2)
if A==b then E ~ dnorm(3.370021 + 1.532289*D, 1/6.554402^2)
}
我可以通过使用下面的代码来获得一些工作,但它会随着更多的预测变量和分类级别而很快变得混乱。
library(rjags)
model <- textConnection("model {
A ~ dcat(c(0.0948, 0.9052 ))
D ~ dnorm(11.87054, 1/1.503111^2)
int = 6.558366 - (A==2)*(6.558366 - 3.370021)
slope = 1.180965 - (A==2)*(1.180965 - 1.532289)
sig = 2.960002 - (A==2)*(2.960002 - 6.554402)
E ~ dnorm(int + slope*D, 1/sig^2)
}")
jg <- jags.model(model, n.adapt = 1000
我的问题:我该如何简洁地定义这个模型?
数据来自
library(bnlearn)
net = model2network("[A][D][E|A:D]")
ft = bn.fit(net, clgaussian.test[c("A", "D", "E")])
coef(ft)
structure(list(A = structure(c(0.0948, 0.9052), class = "table", .Dim = 2L, .Dimnames = list(
c("a", "b"))), D = structure(11.8705363469396, .Names = "(Intercept)"),
E = structure(c(6.55836552742708, 1.18096500477159, 3.37002124328838,
1.53228891423418), .Dim = c(2L, 2L), .Dimnames = list(c("(Intercept)",
"D"), c("0", "1")))), .Names = c("A", "D", "E"))
sigma(ft)
structure(list(A = NA, D = 1.50311121682603, E = structure(c(2.96000206596326,
6.55440224877698), .Names = c("0", "1"))), .Names = c("A", "D",
"E"))