Closed paciorek closed 1 week ago
I believe this is simply not supported and should be error-trapped and noted in documentation.
The issue is not the nestedness of dcats but rather the non-scalar stochastic indexing of p[z, 1:2]
. Note the following (adapted from the last example in test-ADmodels) also does not work:
set.seed(1)
code <- nimbleCode({
for(i in 1:5) {
y[i, 1:2] ~ dmnorm(mu[z[i], 1:2], cov = cov[1:2, 1:2])
z[i] ~ dcat(p[1:3])
}
for(i in 1:3)
for(j in 1:2)
mu[i,j] ~ dnorm(0,1)
p[1:3] ~ ddirch(alpha[1:3])
cov[1,1:2] <- c(1, 0)
cov[2, 1:2] <- c(0,1)
})
model <- nimbleModel(code,
data = list(y = matrix(rnorm(10), ncol=2)),
inits = list(z = c(1,2,2,1,3),
mu = matrix(rnorm(6), ncol=2),
alpha = 1:3, p = c(.2,.3,.5)),
buildDerivs=TRUE)
The problem is non-scalar stochastic indexing: mu[z[i], 1:2]
.
Making AD work through stochastic indexing is non-trivial, so we currently only have it working when the result is a scalar.
So that suggests we could suggest a work-around along these lines:
ptmp[1:2] <- c(p[z,1],p[z,2])
@perrydv is that correct?
Hmm, I guess so. Typically there will be another index (through time) and more categories than two, so it could get a bit cumbersome and potentially inefficient but that does look like a work-around for now. Since this looks like a discrete Markov model, another potential way forward is the HMM distributions in nimbleEcology
.
Simplifying from a user post on 2024-10-22, this causes model building to fail because of AD. Note that in testing we have a latent
dcat
with a dependentdnorm
and that is fine. So something about the twodcat
s may be the issue.