为R指定JAGS或BUGS中的离散weibull分布 [英] Specify a discrete weibull distribution in JAGS or BUGS for R

查看:244
本文介绍了为R指定JAGS或BUGS中的离散weibull分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在R中使用JAGS将Weibull模型拟合为离散值.将weibull拟合为连续数据没有问题,但是当我切换到离散值时遇到麻烦.

I am fitting a weibull model to discrete values using JAGS in R. I have no problem fitting a weibull to continuous data, but I run in to trouble when I switch to discrete values.

这里有一些数据,以及适合JAGS中的weibull模型的代码:

Here is some data, and code to fit a weibull model in JAGS:

#draw data from a weibull distribution
y <- rweibull(200, shape = 1, scale = 0.9)
#y <- round(y)

#load jags, specify a jags model.
library(runjags)

j.model ="
model{
for (i in 1:N){
y[i] ~ dweib(shape[i], scale[i])

shape[i] <- b1
scale[i] <- b2
}

#priors
b1 ~ dnorm(0, .0001) I(0, )
b2 ~ dnorm(0, .0001) I(0, )
}
"

#load data as list
data <- list(y=y, N = length(y))

#run jags model.
jags.out <-     run.jags(j.model,
                         data=data,
                         n.chains=3,
                         monitor=c('b1','b2')
                        )
summary(jags.out)

此型号非常合适.但是,如果我使用y <- round(y)将y值转换为离散值,并运行相同的模型,它将失败,并显示错误Error in node y[7], Node inconsistent with parents.每次尝试时,节点的特定数量都会更改,但是它始终是一个很小的数字.

This model fits fine. However, if I transform y values to discrete values using y <- round(y), and run the same model, it fails with the error Error in node y[7], Node inconsistent with parents. The particular number of the node changes every time I try, but its always a low number.

我知道我可以通过在所有值中添加一个很小的数字来进行此操作,但是,这不能说明数据是离散的这一事实.我知道存在离散的weibull分布,但是如何在JAGS中实现一个呢?

I know I can make this run by adding a very small number to all of my values, however, this does not account for the fact that the data are discrete. I know discrete weibull distributions exists, but how can I implement one in JAGS?

推荐答案

您可以使用一个把戏"在JAGS中实现离散的weibull分布.使用pmf 此处,我们可以创建一个函数来生成一些数据:

You can use the 'ones trick' to implement a discrete weibull distribution in JAGS. Using the pmf here we can make a function to generate some data:

pmf_weib <- function(x, scale, shape){

  exp(-(x/scale)^shape) - exp(-((x+1)/scale)^shape)
}

# probability of getting 0 through 200 with scale = 7 and shape = 4
probs <- pmf_weib(seq(0,200), 7, 4) 

y <- sample(0:200, 100, TRUE, probs ) # sample from those probabilities

要使惯用技巧"起作用,通常必须将新的pmf除以某个大常数,以确保概率在0到1之间.虽然看起来离散的weibull的pmf已经确保了这一点,但我们有仍然在模型中添加了一些大常量.所以,这是该模型现在的样子:

For the 'ones trick' to work you generally have to divide your new pmf by some large constant to ensure that the probability is between 0 and 1. While it appears that the pmf of the discrete weibull already ensures this, we have still added some large constant in the model anyways. So, here is what the model looks like now:

j.model ="
data{ 
C <- 10000
for(i in 1:N){
ones[i] <- 1
}
}
model{
for (i in 1:N){
discrete_weib[i] <- exp(-(y[i]/scale)^shape) - exp(-((y[i]+1)/scale)^shape)

ones[i] ~ dbern(discrete_weib[i]/C)
}

#priors
scale ~ dnorm(0, .0001) I(0, )
shape ~ dnorm(0, .0001) I(0, )
}
"

请注意,我们在数据参数中添加了1)1的向量和一个大常数,2)离散weibull的pmf,3)通过伯努利试验对该概率进行了计算.

Note that we added 1) a vector of ones and a large constant in the data argument, 2) the pmf of the discrete weibull, and 3) we run that probability through a Bernoulli trial.

您可以使用与上面相同的代码来拟合模型,这是摘要,表明该模型成功恢复了参数值(比例= 7和形状= 4).

You can fit the model with the same code you have above, here is the summary which shows that the model successfully recovered the parameter values (scale = 7 and shape = 4).

       Lower95   Median  Upper95     Mean        SD Mode       MCerr MC%ofSD SSeff
scale 6.968277 7.289216 7.629413 7.290810 0.1695400   NA 0.001364831     0.8 15431
shape 3.843055 4.599420 5.357713 4.611583 0.3842862   NA 0.003124576     0.8 15126

这篇关于为R指定JAGS或BUGS中的离散weibull分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆