Search code examples
frequency-distributionwinbugswinbugs14r2winbugs

WinBUGS - Defining a discrete stepwise distribution


I want to define a distribution in my model of the form: P(x=10)=0.10, P(x=15)=0.20, P(x=20)=0.70

The WinBUGS FAQ says it is possible construct my own discrete uniform distribution as a categorical variable with a uniform prior and which can take on the necessary integer values. See the blockerht example in the first part of the manual.

I looked the example up, I think it is this one: "A hierarchical t-distribution with unknown degrees of freedom"

At the model specification they do something like:

for (n in 1:Nbins) {
   prior[n] <- 1/Nbins;   # Uniform prior on v
}
 k ~ dcat(prior[]);

Which does define a discrete uniform. But I don't know how to get to the form I need. Can anyone help me?


Solution

  • If I understand your question correctly, you do not need the loop...

    #BUGS script to obtain distribution
    m1<-"model{
      ind ~ dcat(p[])
      pmix <- x[ind]
    }"
    writeLines(m1,"m1.txt")
    
    #simulate from the distribution    
    library("R2OpenBUGS")
    m1.bug<-bugs(data = list(x=c(10, 15, 20), p=c(0.1,0.2,0.7)),
                 inits = NULL,
                 param = "pmix",
                 model = "m1.txt", 
                 n.iter = 1100, n.burnin = 100, n.chains = 1, n.thin=1, DIC=FALSE)
    
    hist(m1.bug$sims.list$pmix)
    

    should work...

    enter image description here