Hi All,
I am a Nimble user and I have a question on the stochastic nodes or dynamic indexing. Can anyone help me with that?
The data and model code I used are shown as follows:
### data simulation ###
set.seed(1007)
y <- c()
y[1:100] <- rnorm(100, mean = 1, sd = sqrt(1))
y[101:200] <- rnorm(100, mean = 4, sd = sqrt(1))
### model code ###
# self-defined function
VFun <- function(M, l, alpha) {
if (M < 1) {M <- 1}
V <- matrix(rbeta(M * l, 1, alpha), nrow = M, ncol = l)
if (l == 1) {
Vtem <- V[1:M, 1]
} else {
library(purrr)
Vtem <- as.numeric(unlist(purrr::map(1:M, ~(V[.x,1]*prod(1-V[.x,2:l])))))
}
return(Vtem)
}
VFunNim <- nimbleRcall(function(M = double(0), l = double(0), alpha = double(0)){},
Rfun = "VFun", returnType = double(1))
# nimble model code
BPCode <- nimbleCode({
for (i in 1:N) {
y[i] ~ dnorm(mu_y[i], psi)
mu_y[i] <- mu0 + res[i]
res[i] <- sum(H[1:L,i])
}
for (l in 1:L) {
Ci[l] ~ dpois(gamma)
Vtem[1:Ci[l]] <- VFunNim(Ci[l], l, alpha)
for (j in 1:Ci[l]) {
Theta[l,j] ~ dunif(0, N)
}
for (i in 1:N) {
H[l,i] <- inprod((Theta[l,1:Ci[l]] <= i), Vtem[1:Ci[l]])
}
}
alpha ~ dgamma(1, 1)
gamma ~ dunif(0, 100)
mu0 ~ dnorm(0, 0.01)
psi ~ dgamma(0.01, 0.01)
})
BPdata <- list(y = y)
BPconsts <- list(L = 20, N = length(BPdata$y))
BPinits <- list(psi = 100, gamma = 2, mu0 = 0, res = rnorm(BPconsts$N),
alpha= 1, Ci = rpois(BPconsts$L, 2), Vtem = runif(30,0,1),
Theta = matrix(runif(20*30, 0, BPconsts$N), nrow = 20, ncol = 30),
H = matrix(runif(BPconsts$L * BPconsts$N), nrow = BPconsts$L, ncol = BPconsts$N))
BPmodel <- nimbleModel(code = BPCode, name = "BPmodel", constants = BPconsts,
data = BPdata, inits = BPinits)
## error message:
Error in getSymbolicParentNodesRecurse(x, constNames, indexNames, nimbleFunctionNames, :
R function ':' has arguments that cannot be evaluated; either the function must be a nimbleFunction or values for the following inputs must be specified as constants in the model: Ci[l].
Thanks a lot.
Best,
Zh