Choosing different distributions based on a certain condition

  • Sheeraz Ahmad

    Sheeraz Ahmad - 2014-06-04

    I am trying to define a node sat, such that it is 0 if another node con=0 and sat comes from a normal distribution if con≠0. Does anyone has any suggestion on how to make that work? Thanks!

    Here are the things I have tried (along with error message), none of which work:

    temp[i,1] ~ dcat(1)
    temp[i,2] ~ dnorm(g[i],taus)
    ind <- con[i]+1
    sat[i] ~ temp[i,ind]

    Error in last line near "temp"

    I know for this case sat=1 when con=0, but that works too.

    sat[i] ~ ifelse(con[i]==0, 0, dnorm(g[i],taus))

    Incorrect number of parameters in function dnorm

    Possibly because ifelse doesn't work with distributions

    temp[i] ~ dnorm(g[i],taus)
    sat[i] <- con[i]*temp[i]

    sat[1] is a logical node and cannot be observed

    sat is observed in my model and logical nodes can't be observed

  • Matt Denwood

    Matt Denwood - 2014-06-04

    What you're trying to fit is a mixture distribution I think, but it sounds a bit strange. Do you really have a mixture of integers (0) and non-integers (numbers on the real line from a normal distribution) in your observed data? If so, surely you know a-priori which observations are truly 0 and which aren't? If not, you may not want to use a normal distribution but perhaps an integer distribution such as a Poisson (in which case you would have a zero-inflated Poisson distribution, which is fairly straightforward to code).

    The general way that you can code a mixture distribution when the distributional form is the same (but parameters differ) is as follows:

    sat[i] ~ dnorm(mu[con[i]+1], tau[con[i]+1])
    con[i] ~ dbern(prob)

    This would work with any distribution, not just normal.

    Or if you have a zero-inflated Poisson mixture you can use this code:

    sat[i] ~ dpois(lambda[i])
    lambda[i] <- mean * con[i]
    con[i] ~ dbern(prob)

    Note that for these types of code you will have to be careful about specifying sensible initial values for con.

    Hope that helps,


    Last edit: Matt Denwood 2014-06-04
  • Sheeraz Ahmad

    Sheeraz Ahmad - 2014-06-05

    Thanks Matt! I responded yesterday with a detailed explanation for my choice of random variables, but for some reason I don't see that anymore :/ Anyways, I decided on using the same family of distributions after all to make things work. Thanks again!

  • Martyn Plummer

    Martyn Plummer - 2014-06-05

    That happens to me to sometimes. You have to be careful to press the "post" button and not just the "preview" button.


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks