gpt4 book ai didi

r - 将 WinBUGS 模型转换为 JAGS(使用 R)

转载 作者:行者123 更新时间:2023-12-02 01:19:46 27 4
gpt4 key购买 nike

我正在尝试在 JAGS 中实现为 WinBUGS 编写的以下模型:

model {
for (i in 1:N) {
wtp[i] ~ dweib(r[G[i]], mu[i])I(lower[i], upper[i])
mu[i] <- exp(beta[G[i]])
G[i] ~ dcat(P[])
}
P[1] ~ dunif(0.01, 0.99)
P[2] <- 1 - P[1]
r[1] ~ dunif(1, 10)
r[2] ~ dunif(0.1, 10)
beta[1] ~ dunif(0, 1000)
beta[2] ~ dunif(-1000, 0)
weibmed[1] <- pow(log(2) * exp(-beta[1]), 1 / r[1])
weibmed[2] <- pow(log(2) * exp(-beta[2]), 1 / r[2])
weibmed[3] <- pow(log(1 / (1 - 0.5 + P[1])) * exp(-beta[2]), 1 / r[2])
weibmean[1] <- pow(exp(-beta[1]), 1 / r[1]) * exp(loggam((1 + r[1]) / r[1]))
weibmean[2] <- pow(exp(-beta[2]), 1 / r[2]) * exp(loggam((1 + r[2]) / r[2]))
weibmean[3] <- P[1] * weibmean[1] + P[2] * weibmean[2]
}

我认为在 JAGS 中使用它会很简单:

library(rjags)

txt <- 'model {
for (i in 1:N) {
wtp[i] ~ dweib(r[G[i]], mu[i])T(lower[i], upper[i])
mu[i] <- exp(beta[G[i]])
G[i] ~ dcat(P[])
}
P[1] ~ dunif(0.01, 0.99)
P[2] <- 1 - P[1]
r[1] ~ dunif(1, 10)
r[2] ~ dunif(0.1, 10)
beta[1] ~ dunif(0, 1000)
beta[2] ~ dunif(-1000, 0)
weibmed[1] <- pow(log(2) * exp(-beta[1]), 1 / r[1])
weibmed[2] <- pow(log(2) * exp(-beta[2]), 1 / r[2])
weibmed[3] <- pow(log(1 / (1 - 0.5 + P[1])) * exp(-beta[2]), 1 / r[2])
weibmean[1] <- pow(exp(-beta[1]), 1 / r[1]) * exp(loggam((1 + r[1]) / r[1]))
weibmean[2] <- pow(exp(-beta[2]), 1 / r[2]) * exp(loggam((1 + r[2]) / r[2]))
weibmean[3] <- P[1] * weibmean[1] + P[2] * weibmean[2]
}'

set.seed(3.14159)
dat <- list(N = 1000, lower = rep(0, 1000), upper = runif(1000, 5, 200000))
ini <- list(P = c(0.4, NA), r = c(8.2, 1.2), beta = c(3.8, -6.5))

mod <- jags.model(
file = textConnection(txt),
data = dat,
inits = c(ini, .RNG.name = 'base::Mersenne-Twister', .RNG.seed = 314159),
n.chains = 1,
n.adapt = 100
)

sam.jags <- coda.samples(
model = mod,
variable.names = c('P', 'r', 'beta', 'weibmed', 'weibmean'),
n.iter = 400,
n.thin = 1
)

只需将 I() 替换为 T()。这会产生 coda.samples() 错误:

Error: Error in node weibmed[3]
Invalid parent values

如果我忽略对 weibmedweibmean 的监控,那么 coda.samples() 可以工作,但参数估计:

                Mean          SD    Naive SE Time-series SE
P[1] 0.4840704 0.2769491 0.01384746 0.01384746
P[2] 0.5159296 0.2769491 0.01384746 0.01384746
beta[1] 509.3614647 295.0860473 14.75430237 14.75430237
beta[2] -487.5362940 285.4126899 14.27063449 14.27063449
r[1] 5.2054730 2.6330434 0.13165217 0.13165217
r[2] 5.0478143 2.9480476 0.14740238 0.14740238

与我在使用 WinBUGS 时得到的结果不可比:

library(R2WinBUGS)

sam.bugs <- bugs(
model.file = 'model.bug',
data = dat,
inits = list(ini),
parameters.to.save = c('P', 'r', 'beta'), #, 'weibmed', 'weibmean'),
n.chains = 1,
n.burnin = 100,
n.iter = 500,
n.thin = 1,
debug = F,
DIC = F,
bugs.seed = 314159
)

Inference for Bugs model at "3mixout2.bug", fit using WinBUGS,
1 chains, each with 500 iterations (first 100 discarded)
n.sims = 400 iterations saved
mean sd 2.5% 25% 50% 75% 97.5%
P[1] 0.4 0.0 0.3 0.4 0.4 0.4 0.4
P[2] 0.6 0.0 0.6 0.6 0.6 0.6 0.7
r[1] 7.2 0.8 6.2 6.6 6.9 7.7 9.3
r[2] 1.5 0.0 1.4 1.4 1.5 1.5 1.6
beta[1] 5.5 0.5 4.6 5.0 5.4 5.8 6.6
beta[2] -7.2 0.2 -7.5 -7.3 -7.2 -7.1 -6.9

有什么想法或建议吗?

最佳答案

JAGS手册说;

pow(x, z) || Power function || Real || If x < 0 then z is integer

0.5 < P[1] , log(1 / (1 - 0.5 + P[1])) * exp(-beta[2]) < 0 , 所以 weibmed[3] , pow(negative, non_integer) , 变成 NaN .据我所知,coda.samples()不允许监控变量取 NaN .

如果您使用 P[1] ~ dunif(0.01, 0.5)weibmed[3] 除外通过更改其名称从受监视的变量列表中删除,例如 weibmed3 <- pow(log(1 / (1 - 0.5 + P[1])) * exp(-beta[2]), 1 / r[2]) ,您的代码运行。

关于r - 将 WinBUGS 模型转换为 JAGS(使用 R),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40776739/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com