if/else statement for defining a distribution in JAGS
Asked Answered
P

2

6

In JAGS I'd like to define a Poisson distribution for parameter w[i] which is also truncated (greater than or equal to 2) if another parameter, e[i], is greater than 0.

Essentially I want it to represent:

w[i] ~ ifelse( e[i] > 0, dpois(mu) T(2,) , dpois(mu) )

I've tried using the step function by adapting the code that was given in response to someone else's post which was requesting something similar: Choosing Different Distributions based on if - else condition in WinBugs/JAGS

But this doesn't seem to work?

Thank you

Palla answered 13/10, 2017 at 12:43 Comment(0)
S
5

Maybe something like this?

pois1 ~ dpois(mu) T(2,)
pois2 ~ dpois(mu)
for(i in 1:N){
indicator1[i] <- ifelse(e[i] > 0, 1, 0)
indicator2[i] <- ifelse(e[i] <= 0, 1, 0)
w[i] <- (pois1 * indicator1[i]) + (pois2 * indicator2[i])
}

When e[i] is greater than 1 w[i] takes the value from pois1. If it is not w[i] takes the value from pois2.

EDIT: Or, you could define only one indicator variable and do it like this.

pois1 ~ dpois(mu) T(2,)
pois2 ~ dpois(mu)
for(i in 1:N){
indicator[i] <- ifelse(e[i] > 0, 1, 0)
w[i] <- (pois1 * indicator[i]) + (pois2 * (1 - indicator[i]))
}
Summary answered 13/10, 2017 at 20:10 Comment(2)
Thanks! This works. Now realise the problem I had was actually that e[i] was circularly defined, but all sorted now.Palla
Great. I added another way to do with for defining only one indicator variable in the answer as well.Summary
D
0

You could try this

w[i] ~ dpois(mu) T(ifelse(e[i] > 0), 2, 0), )

A lower bound of 0 on the Poisson distribution is equivalent to not having a lower bound.

Diseased answered 30/10, 2022 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.