使用 rjags 定义条件线性高斯网络
Define conditional linear Gaussian network using rjags
我正在努力使用 rjags
定义条件线性高斯贝叶斯网络。
(clg BN 由具有连续正态和离散父节点(预测变量)的连续子节点(结果)定义)
对于下面的网络,A 是离散的,D 和 E 是连续的:
对于rjags
模型,我想我想要的是在值节点A
上定义节点E
的参数采用:伪代码
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
My question: How can i define this model succinctly please?
数据来自
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"))
您只需要将变量 A 用作索引参数:
library('rjags')
model <- "
model {
A ~ dcat(c(0.0948, 0.9052 ))
D ~ dnorm(11.87054, 1/1.503111^2)
ints <- c(6.558366, 3.370021)
int <- ints[A]
slopes <- c(1.180965, 1.532289)
slope <- slopes[A]
sigs <- c(2.960002, 6.554402)
sig <- sigs[A]
E ~ dnorm(int + slope*D, 1/sig^2)
}
"
jg <- jags.model(textConnection(model), n.adapt = 1000)
顺便说一下,由于模型中有很多固定量,因此在 R 中定义这些量然后将它们作为数据传递给 JAGS 可能更有意义。通过这种方式,您可以调整向量的值和长度(只要 catprobs、ints、slopes 和 sigs 的长度匹配),而无需修改 JAGS 代码。例如(为了方便使用 runjags 虽然也可以使用 jags):
library("runjags")
model <- "
model {
A ~ dcat(catprobs)
D ~ dnorm(Dmu, Dprec)
int <- ints[A]
slope <- slopes[A]
sig <- sigs[A]
E ~ dnorm(int + slope*D, 1/sig^2)
#data# catprobs, Dmu, Dprec, ints, slopes, sigs
#monitor# A, D, E
}
"
catprobs <- c(0.0948, 0.9052)
Dmu <- 11.87054
Dprec <- 1/1.503111^2
ints <- c(6.558366, 3.370021)
slopes <- c(1.180965, 1.532289)
sigs <- c(2.960002, 6.554402)
results <- run.jags(model)
results
马特
我正在努力使用 rjags
定义条件线性高斯贝叶斯网络。
(clg BN 由具有连续正态和离散父节点(预测变量)的连续子节点(结果)定义)
对于下面的网络,A 是离散的,D 和 E 是连续的:
对于rjags
模型,我想我想要的是在值节点A
上定义节点E
的参数采用:伪代码
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
My question: How can i define this model succinctly please?
数据来自
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"))
您只需要将变量 A 用作索引参数:
library('rjags')
model <- "
model {
A ~ dcat(c(0.0948, 0.9052 ))
D ~ dnorm(11.87054, 1/1.503111^2)
ints <- c(6.558366, 3.370021)
int <- ints[A]
slopes <- c(1.180965, 1.532289)
slope <- slopes[A]
sigs <- c(2.960002, 6.554402)
sig <- sigs[A]
E ~ dnorm(int + slope*D, 1/sig^2)
}
"
jg <- jags.model(textConnection(model), n.adapt = 1000)
顺便说一下,由于模型中有很多固定量,因此在 R 中定义这些量然后将它们作为数据传递给 JAGS 可能更有意义。通过这种方式,您可以调整向量的值和长度(只要 catprobs、ints、slopes 和 sigs 的长度匹配),而无需修改 JAGS 代码。例如(为了方便使用 runjags 虽然也可以使用 jags):
library("runjags")
model <- "
model {
A ~ dcat(catprobs)
D ~ dnorm(Dmu, Dprec)
int <- ints[A]
slope <- slopes[A]
sig <- sigs[A]
E ~ dnorm(int + slope*D, 1/sig^2)
#data# catprobs, Dmu, Dprec, ints, slopes, sigs
#monitor# A, D, E
}
"
catprobs <- c(0.0948, 0.9052)
Dmu <- 11.87054
Dprec <- 1/1.503111^2
ints <- c(6.558366, 3.370021)
slopes <- c(1.180965, 1.532289)
sigs <- c(2.960002, 6.554402)
results <- run.jags(model)
results
马特