Great thanks, that makes sense. I think I may have achieved that with the code below (using urn terminology for white balls and black balls) but it is producing some odd results. There is no variance in the posterior distribution for the neghyp parameters but the random sample generating seems to be working well.
rm(list=ls())
library(nimble)
library(extraDistr)
dnhype2<-nimbleRcall(function(x= double(0,default=1), n = double(0, default = 1), m = double(0, default = 1), r = double(0, default = 1), log=integer(0,default=0)){}, Rfun="dnhyper",returnType=double(0))
rnhype2<-nimbleRcall(function(nn= double(0,default=1), n = double(0, default = 1), m = double(0, default = 1), r = double(0, default = 1)){}, Rfun="rnhyper",returnType=double(0))
dnhype <- nimbleFunction(
run = function(x= double(0), white = double(0, default = 1), black = double(0, default = 1), failures = double(0, default = 1),log = integer(0, default = 0)) {
returnType(double(0))
Prob<-dnhype2(x,white,black,failures)
return(Prob)
})
rnhype <- nimbleFunction(
run = function(n= double(0,default=1), white = double(0, default = 1), black = double(0, default = 1), failures = double(0, default = 1)) {
returnType(double(0))
return(rnhype2(1,white,black,failures))
})
nsites<-1000
testG<-rnhyper(nsites,50,50,3) # 'data'
testG[seq(2,nsites,by=2)]<-NA
data<-list(testG=testG)
constants<-list(nsites=nsites)
nhyper_code <- nimbleCode({
# failurs cannot be smaller than white
black~dunif(11,100)
white~dunif(11,100)
failures~dunif(1,10)
testG[i]~dnhype(black,white,failures)
}
})
# Inital values
testGinit<-rep(NA,nsites)
testGinit[
is.na(testG)]<-11
# best to initialize failures at 1, so that no inconsistencies arise with initial values
inits <- function() list(black=50, white=50,failures=1,testG=testGinit)
initsValues <- inits
# Parameters to monitor
params <- c("black","white","failures","testG")
# MCMC
ni <- 25000
nb <- 5000
## run nimble
nhype <- nimbleMCMC(
code = nhyper_code,
data=data,
constants=constants,
inits = initsValues,
monitors = params,
nburnin = nb,
niter = ni,
thin=100,
samplesAsCodaMCMC = TRUE
)
library(coda)
library(mcmcplots)
mcmcplot(nhype)
hist(nhype[,seq(2,nsites,by=2)])
hist(testG)
hist(nhype[,3])