Hello nimble community,
I'm trying to fit a model to estimate two parameters which govern the speed and temporal autocorrelation of movement tracking data. I'm using a bivariate normal distribution for each 2D velocity (v_x,v_y), where the mean depends on the previous velocity. The covariance matrix is proportional to the identity matrix but I'm having trouble with this - I can't declare the variance associated with each velocity component. It might be an indexing issue or something else that I've forgotten or haven't considered. I've attached the R script. Here is the model part with the error:
code <- nimbleCode({
# Priors for nu and tau (only positive values)
nu ~ dinvgamma(0.1,0.1) # Prior for nu
tau ~ dinvgamma(0.1,0.1) # Prior for tau
for (i in 1:n) {
dt[i] <- y[i,3] # time step of this iteration. This is given as data since future examples might not have regular gaps
# mean is dependent on previous velocity and tends to zero as dt increases
mu_decay_factor[i] <- exp(-dt[i]/tau) # factor between 0 and 1
mu[i, 1:2] <- y[i, 4:5] * mu_decay_factor[i] # data columns 4 and 5 are the previous x and y components respectively
# Diagonal covariance matrix
var[i] <- (2 / pi) * (nu^2) * (1-exp(-2*dt[i]/tau)) # variance of the velocity in x and y directions respectively ### THIS PRODUCES ALL NAs
cov_matrix[i,1,1] <- var[i] # variance in x direction
cov_matrix[i,2,2] <- var[i] # variance in y-direction
cov_matrix[i,1,2] <- 0 # Off-diagonal set to 0
cov_matrix[i,2,1] <- 0 # Off-diagonal set to 0
# Likelihood for y[i, 1:2] with data dependent mean
y[i, 1:2] ~ dmnorm(mu[i, 1:2], cov = cov_matrix[i,1:2, 1:2])
}
})
Error in chol.default(model$cov_matrix[getNodeFunctionIndexedInfo(INDEXEDNODEINFO_, :
the leading minor of order 1 is not positive definite
The issue is in the line declaring the var[i] nodes.
I'm returning to using nimble after a few years away so apologies if the error is a simple one and I should've spotted it. Any help would be greatly appreciated!
Best,
Mark