Hi,
I was wondering if you'd be able to explain what is causing the following error I get when making my model. Below is the error and my code. I've also attached data.
Defining model
Error: bad parameters for distribution dsmooth(k = k, L = L). (No available re-parameterization found.)
f_vec <- nimbleFunction(
run = function(x = double(0)){
k <- 6.06 # shape
r <- 1/.34 # scale = 1/rate
tmp_vec <- rep(NA, x)
for (i in 1:x) {
tmp_vec[i] <- pgamma(x-(i-1), k, rate = r) - pgamma(x-i, k, rate = r)
}
return(tmp_vec)
returnType(double(1))
})
# code my own choose function
my_choose <- nimbleFunction(
run = function(n = double(0), k = double(0)){
out <- factorial(n)/(factorial(k)*factorial(n-k))
return(out)
returnType(double(0))
})
# code smoother as prior
dsmooth <- nimbleFunction(
run = function(x = double(1),
k = double(0),
L = double(0),
log = integer(0, default = 0)) {
returnType(double(1))
tmp_mat <- matrix(NA, nrow = k+1, ncol = L)
for (j in 1:L) {
for(m in 0:k){
if ((j+m-k/2) > L){
tmp_mat[(m+1), j] <- my_choose(k, m)/(2^k)*x[L] }
else if ((j+m-k/2) < 1){
tmp_mat[(m+1), j] <- 0 }
else {
tmp_mat[(m+1), j] <- my_choose(k, m)/(2^k)*x[j+m-k/2]}
}}
tmp_vec <- rep(NA, L)
for (c in 1:L) {
tmp_vec[c] <- sum(tmp_mat[, c])
}
logProb <- log(tmp_vec)
if(log) return(logProb)
else return(exp(logProb))
})
#====================================================================
# set up MCMC
#====================================================================
modelCode <- nimbleCode({
for (i in 1:L){
#get mu
mu[i] <- sum(f_vec(i)*lambda[1:i])
#likelihood
x[i] ~ dpois(mu[i])
}
#prior
lambda[1:L] ~ dsmooth(k = k, L = L)
})
modelConsts <- list(L = nrow(dat), k = 4)
modelData <- list(x = dat$Notifications)
initsList <- list(lambda = abs(rnorm(nrow(dat), mean(dat$Notifications), sd(dat$Notifications))))
model <- nimbleModel(modelCode, data = modelData, inits = initsList, constants = modelConsts)