Hi all,
I'm trying to implement some simple vectorised distributions, specifically a vectorised normal distribution and a vectorised dLogExp (from {nimbleNoBounds}). dnorm_vec() and dLogExp_vec() are specified as follows (note that mean/sd/rate are not vectors to make them easy to use as prior distributions):
# nimbleFunction for vectorised dnorm
dnorm_vec <- nimbleFunction(
run = function(x = double(1),
mean = double(0, default = 0),
sd = double(0, default = 1),
log = logical(0, default = 0)) {
returnType(double(0))
log_dens <- sum(dnorm(x, mean, sd, log = TRUE))
if (log)
return(log_dens)
else
return(exp(log_dens))
}
)
# nimbleFunction for vectorised dLogExp
dLogExp_vec <- nimbleFunction (
run = function(x = double(1),
rate = double(0, default = 1),
log = integer(0, default = 0)) {
returnType(double(0))
y <- exp(x)
log_dens <- sum(log(rate) - (rate * y) + x)
if (log)
return(log_dens)
else
return(exp(log_dens))
}
)
When I put this distributions to use in some parts of the model, it works fine. For instance, for two log hazard rates, there's no problem at all:
# mortality hazard rates (monthly, juvenile and adult)
log_h_alpha[1:2] ~ dLogExp_vec(3)
h_alpha[1:2] <- exp(log_h_alpha[1:2])
For a different part of the model, nimble spits out the error: Error in mapCopy. Sizes don't match: 2 != 1
This is the part of the model where it shows up (note that the vectorised distributions are commented out—the model works fine without them, but when I apply the vectorised distributions nimble throws hundreds of those errors):
# random individual effects on mortality and capture (bivariate, noncentered)
# log_epsilon_sigma[1:2] ~ dLogExp_vec(2)
for (s in 1:2) {
log_epsilon_sigma[s] ~ dLogExp(2)
} # s
epsilon_sigma[1:2] <- exp(log_epsilon_sigma[1:2])
epsilon_chol[1:2, 1:2] ~ dlkj_corr_cholesky(2, 2)
epsilon_cor[1:2, 1:2] <- t(epsilon_chol[1:2, 1:2]) %*% epsilon_chol[1:2, 1:2]
# open individual loop
for (i in 1:n_ind) {
# individual effects cont.
# epsilon_z[1:2, i] ~ dnorm_vec(0, 1)
for (s in 1:2) {
epsilon_z[s, i] ~ dnorm(0, 1)
}
epsilon[1:2, i] <- (diag(epsilon_sigma[1:2])
%*% t(epsilon_chol[1:2, 1:2])
%*% epsilon_z[1:2, i])
What does this error mean and how can I avoid it? Am I making a mistake somewhere?
Cheers,
Matt