Hi Chris,
Thanks for the quick reply. Good to know that I am not missing something obvious in the manual.
I am trying the user-defined route, as that seems cleaner. Here is my attempt:
# integer-valued uniform distribution
dUnif <- nimbleFunction(
run = function(x = integer(),
min = integer(0,default=0),
max = integer(0,default=1),
log=integer(0,default=FALSE))
{
returnType(double())
if (x < min | x > max) {
p <- 0
} else {
p <- 1 / (max - min + 1)
}
if (log) return (log(p)) else return (p)
}
)
rUnif <- nimbleFunction(
run = function(n = integer(),
min = integer(0,default=0),
max = integer(0,default=1))
{
returnType(integer())
if (n != 1) stop('only n=1 is supported')
s <- runif(n=n,min=min,max=max+1)
s <- floor(s)
s <- as.integer(s)
return (s)
}
)
registerDistributions(list(
dUnif=list(BUGSdist="dUnif(min,max)",
types=c("min=integer()","max=integer()"),
discrete=TRUE)))
N ~ dUnif(min=0,max=10)
The error from compiling the model is the following, which comes from rUnif():
Error: Number of dimensions 1 of the return() argument does not match number 0 given in the returnType() statement. This occurred for: return(s)
It seems that the NIMBLE version of runif() must return a vector, even when n=1?
Any help greatly appreciated, as this is likely obvious to those of you more versed in user-defined distributions.
Thanks a lot.
Cheers,
Brook
> On Jun 26, 2025, at 09:22, 'Chris Paciorek' via nimble-users <
nimble...@googlegroups.com> wrote:
>
> hi Brook,
>
> You could use `dcat` for this, provided the valid integer values are positive. You'd need to set the probability parameter vector equal to 1/K for the K valid values. And if the valid integers are not sequential, you'd need to set appropriate elements of the probability vector to 0. (And if the valid integers were not all positive, you could take the random variable that has dcat as its distribution and use subtraction to map onto the actual integers of interest.)
>
> It would also be possible to create a user-defined distribution.
>
> If there are lots of gaps and you are using MCMC, it may make sampling less efficient.
>
> -chris
>
> On Thu, Jun 26, 2025 at 7:42 AM 'Brook Milligan' via nimble-users <
nimble...@googlegroups.com> wrote:
> Hi,
>
> I need an integer-valued uniform distribution for a prior, but it is unclear that one is provided by NIMBLE. Am I missing it or is it missing?
>
> Thanks for your help.
>
> Cheers,
> Brook
>
> --
> You received this message because you are subscribed to the Google Groups "nimble-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
tonimble-user...@googlegroups.com.
> --
> You received this message because you are subscribed to the Google Groups "nimble-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
tonimble-user...@googlegroups.com.
> To view this discussion visit
https://groups.google.com/d/msgid/nimble-users/CA%2B7Ts_orXr5nfNh3p1GiEk-wV-uoR8NgxgGQLY-SZh2FGeQ3xw%40mail.gmail.com.