Although the dround "distribution" seems conceptually simple, I get an initialization error message that makes me wonder if I'm using it right. I'll illustrate with a minimal example: given an observed integer x we want to infer the real value truescore which is know to be in the interval 0-100.
truescore ~ dunif(0, 100)
x ~ dround(truescore, 0)
This works for x=50 (or more generally for x in the middle of dunif) but not for any other values (e.g. 51, 2, 65 etc.). It says Error in node x, Observed node inconsistent with unobserved parents at initialization. It seems strange that initialization values is necessary in such a simple case. Am I doing it wrong?
Furthermore, for x=50, the posterior on "truescore" behaves as if the observation was rounded to nearest integer for digits parameter below 2. (E.g. for 1, 0, -1, -100 etc. the posterior is x +/- 0.5). I would expect the posterior to broaden as the measurement resolution decreases and for digits=1 to give an estimate of +/- 0.05 rather than +/- 0.5.
Best,
Jonas
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I think I might have run into the same or maybe a similar issue here, using JAGS 4.2.0.
For my case, I have a normally distributed data variable 1-5, mean 3, SD 0.5 or so, but it is rounded to 1/2/3/4/5. I thought it would be nice to include the rounding and saw that the JAGS manual says:
Rounded data: dround
The dround distribution represents rounded data. It has two parameters: t the original continuous variable and d, the number of decimal places to which the measurements are rounded. Thus if t = 1.2345 and d = 2 then the rounded value is 1.23. Note that d can be negative: if d = −2 then the data are rounded to the nearest 100.
There aren't any examples in the manual or online anywhere that I could find, but my assumption is that I should be able to just do this normally as dround(x[i], 0) etc. But this doesn't seem to work for me, even when an almost identical use of dinterval() does:
data_rounded <-round(rnorm(100, mean=3, sd=0.5)); data_rounded
# [1] 2 3 3 3 3 3 3 3 4 4 4 3 2 3 4 3 2 3 3 4 3 3 2 3 3 2 3 3 2 3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 3 3 2 2 3 3 3 2 3 3 3 3 2 2 4 3 3 3 3 3 3 4 4 3 2 2 3 3 3 4 2 2 3 3 3 3 3 3 3 3 2 3 3 3 3 2# [93] 3 4 4 3 2 3 2 3## Model treating it as rounded:library(runjags)
model1 <-"model { for (i in 1:n) { y[i] ~ dround(true[i], 1) true[i] ~ dnorm(mu, tau) } mu ~ dnorm(3, pow(1, -2)) tau ~ dgamma(0.001, 0.001) }"
data <-list(y=data_rounded, n=length(data_rounded))
params <-c("mu","tau")
j1 <- run.jags(model=model1, monitor=params, data=data); j1
# Compiling rjags model...# Error: The following error occured when compiling and adapting the model using rjags:# Error in rjags::jags.model(model, data = dataenv, n.chains = length(runjags.object$end.state), : # Error in node y[1]# Node inconsistent with parents# # # # It may help to use failed.jags(c('model','data','inits')) to see model/data/inits syntax with line numbers# In addition: Warning messages:# 1: No initial value blocks found and n.chains not specified: 2 chains were used # 2: No initial values were provided - JAGS will use the same initial values for all chains
failed.jags(c('model','data','inits'))# # JAGS model syntax:# # 1 | model{# 2 | for (i in 1:n) {# 3 | y[i] ~ dround(true[i], 1)# 4 | true[i] ~ dnorm(mu, tau)# 5 | }# 6 | mu ~ dnorm(3, pow(1, -2))# 7 | tau ~ dgamma(0.001, 0.001)# 8 | }# # # JAGS data:# # 1 | "y" <- c(2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 2, 3, 4, 3, 2, 3, 3, 4, 3, 3, 2, 3, 3, 2, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 3, 2, 3, 3, 3, 3, 2, 2, 4, 3, 3, 3, 3, 3, 3, 4, 4, 3, 2, 2, 3, 3, 3, 4, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 2, 3, 4, 4, 3, 2, 3, 2, 3)# 2 | "n" <- 100# 3 | # # # JAGS chains initial values / end states:# # Chain 1:# # # # Chain 2:## Model using the dinterval to express discretizing:
breaks <-1:5
model2 <-"model { for (i in 1:n) { y[i] ~ dinterval(true[i], breaks) true[i] ~ dnorm(mu, tau) } mu ~ dnorm(3, pow(1, -2)) tau ~ dgamma(0.001, 0.001) }"
data <-list(breaks=breaks, y=data_rounded, n=length(data_rounded))
j2 <- run.jags(model=model2, monitor=params, data=data); j2
# Compiling rjags model...# Calling the simulation using the rjags method...# Note: the model did not require adaptation# Burning in the model for 4000 iterations...# |**************************************************| 100%# Running the model for 10000 iterations...# |**************************************************| 100%# Simulation complete# Calculating summary statistics...# Calculating the Gelman-Rubin statistic for 2 variables....# Finished running the simulation# Warning messages:# 1: No initial value blocks found and n.chains not specified: 2 chains were used # 2: No initial values were provided - JAGS will use the same initial values for all chains # # JAGS model summary statistics from 20000 samples (chains = 2; adapt+burnin = 5000):# # Lower95 Median Upper95 Mean SD Mode MCerr MC%ofSD SSeff AC.10 psrf# mu 3.2999 3.4059 3.5188 3.4063 0.055658 3.4078 0.00050657 0.9 12072 0.0064681 1.0001# tau 2.9369 4.3424 5.9953 4.3968 0.79776 4.2138 0.0085467 1.1 8713 0.0046073 1# # Total time taken: 1.2 seconds
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Observable functions like dround impose strong a posteriori constraints on the parameters. JAGS requires you to start the chain in a state where those constraints are already satisfied. One way to do this is to set true to be equal to the observed value of y at initialization,
A more refined approach is to set true to be equal to y plus a small random perturbation eps so that true + eps still rounds to to y to the desired accuracy.
inits <-list(true=y+runif(length(y),-0.49,0.49))
Another remark is that your data are rounded to 0 digits, so in your model you should have
y[i] ~ dround(true[i], 0)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I think I have run into a similar issue with ~dround. In this example, I have binomially distributed data y[i]. We have an estimate for binomial distribution parameter p = 0.8, and we consider this as data in the model. We would like to estimate n[i] , and we assume that n[i] is distributed with a Pareto distribution with shape parameter alpha = alpha to be estimated and scale parameter c = 1 as data. The Pareto distribution has support [c, ∞). Thus, this seems like an appropriate place to use ~dround. I placed constraints on the initial values for n[i] and tmp[i]. However, I still get the error “Failed check for discrete-valued parameters in distribution dbin” with or without providing initial values as shown in the script below. An example in R is below:``
Although the dround "distribution" seems conceptually simple, I get an initialization error message that makes me wonder if I'm using it right. I'll illustrate with a minimal example: given an observed integer x we want to infer the real value truescore which is know to be in the interval 0-100.
This works for x=50 (or more generally for x in the middle of dunif) but not for any other values (e.g. 51, 2, 65 etc.). It says Error in node x, Observed node inconsistent with unobserved parents at initialization. It seems strange that initialization values is necessary in such a simple case. Am I doing it wrong?
Furthermore, for x=50, the posterior on "truescore" behaves as if the observation was rounded to nearest integer for digits parameter below 2. (E.g. for 1, 0, -1, -100 etc. the posterior is x +/- 0.5). I would expect the posterior to broaden as the measurement resolution decreases and for digits=1 to give an estimate of +/- 0.05 rather than +/- 0.5.
Best,
Jonas
I think I might have run into the same or maybe a similar issue here, using JAGS 4.2.0.
For my case, I have a normally distributed data variable 1-5, mean 3, SD 0.5 or so, but it is rounded to 1/2/3/4/5. I thought it would be nice to include the rounding and saw that the JAGS manual says:
There aren't any examples in the manual or online anywhere that I could find, but my assumption is that I should be able to just do this normally as
dround(x[i], 0)
etc. But this doesn't seem to work for me, even when an almost identical use ofdinterval()
does:Observable functions like
dround
impose strong a posteriori constraints on the parameters. JAGS requires you to start the chain in a state where those constraints are already satisfied. One way to do this is to settrue
to be equal to the observed value ofy
at initialization,A more refined approach is to set
true
to be equal toy
plus a small random perturbationeps
so thattrue + eps
still rounds to toy
to the desired accuracy.Another remark is that your data are rounded to 0 digits, so in your model you should have
For reference, here's a simple
dround
example on my system:I think I have run into a similar issue with ~dround. In this example, I have binomially distributed data y[i]. We have an estimate for binomial distribution parameter p = 0.8, and we consider this as data in the model. We would like to estimate n[i] , and we assume that n[i] is distributed with a Pareto distribution with shape parameter alpha = alpha to be estimated and scale parameter c = 1 as data. The Pareto distribution has support [c, ∞). Thus, this seems like an appropriate place to use ~dround. I placed constraints on the initial values for n[i] and tmp[i]. However, I still get the error “Failed check for discrete-valued parameters in distribution dbin” with or without providing initial values as shown in the script below. An example in R is below:``