Hello,
I make up the 4-parameter beta distribution in NIMBLE. However, I always got a message:
checkDistributionFunctions: random generation function `rbeta4` is missing `returnType` or `returnType` does not match the type of the `x` argument to the corresponding density function.
The problem seems stemming from the random generation function. The code is provided below. I believe there is some problem in "rbeta4" below but I cannot spot it.
library("nimble")
dbeta4 <- nimbleFunction(
run = function(x=double(0,default=0.5), a=double(0,default=1),b=double(0,default=1),lower=double(0,default=0),upper=double(0,default=1), log = logical(0, default = FALSE)) {
returnType(double(0))
de = (upper - lower)
if (de==0){
p = 0.0
}else{
p = dbeta((x - lower)/(upper - lower), a, b)/de
}
if (log) return(log(p))
else return(p)
})
assign('dbeta4', dbeta4, envir = .GlobalEnv)
rbeta4 <- nimbleFunction(
run = function(n = integer(0), a=double(0,default=1),b=double(0,default=1),lower=double(0,default=0),upper=double(0,default=1)) {
returnType(double(0))
return((upper - lower) * rbeta(n, a, b) + lower)
})
assign('rbeta4', rbeta4, envir = .GlobalEnv)
x = seq(from=0, to=3, by=0.01)
a = 2
b = 2
lower = 1
upper = 2
r = rbeta4(n=10000,a,b,lower,upper)
code <- nimbleCode({
for (n in 1:N){
r[n] ~ dbeta4(a,b,lower,upper)
}
a ~ dunif(0.01,5)
b ~ dunif(0.01,5)
})
constants <- list(N=length(r),lower=lower,upper=upper)
data <- list(r=r)
inits <- list(a=0.01,b=0.01)
monitors = c("a","b")
MODEL <- nimbleModel(code = code, name = "MODEL", constants = constants,data = data, inits = inits)
cmodel <- compileNimble(MODEL)
conf <- configureMCMC(MODEL, monitors = monitors
,useConjugacy=TRUE ,print = FALSE
,enableWAIC=TRUE
,onlySlice=FALSE
)
modelMCMC <- buildMCMC(conf)
cmodelMCMC <- compileNimble(modelMCMC, project = MODEL ,resetFunctions = TRUE)
burnin = 1000
keep = 10000
nchains = 1
rand.seed = 1
niter = burnin + keep
out <- system.time(samples <- runMCMC(cmodelMCMC, niter = niter, nburnin = burnin, nchains=nchains
,samplesAsCodaMCMC=TRUE
,samples= TRUE
,summary=TRUE
,WAIC = TRUE
,setSeed = rand.seed))