Hi All,
I am trying to implement a multivariate dynamic linear model where many observations Y
-values are NA
. To get around the NA
’s, I coded all NA
s at 10 (since the range of data is much small than 10) and created an indicator data array D
which has value 1 when Y!=10
and 0 otherwise. It appears rstan
does not have boolean indexing. This is the error message I get:
SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 1).
Chain 1: Unrecoverable error evaluating the log probability at the initial value.
Chain 1: Exception: matrix[multi,multi] row indexing: accessing element out of range. index 0 out of range; expecting index to be between 1 and 11 (in 'string', line 51, column 4 to column 75)
[1] "Error : Exception: matrix[multi,multi] row indexing: accessing element out of range. index 0 out of range; expecting index to be between 1 and 11 (in 'string', line 51, column 4 to column 75)"
[1] "error occurred during calling the sampler; sampling not done"
Any suggestions on how to alter this code so that it handles the NA
’s? There may be other issues with this code, but the error I am trying to solve right now is the indexing one.
Code below:
data {
int<lower=0> N ; // number of observations
int<lower=0> S ; // number of streams
matrix[N,S] Y ; // matrix of stream observations, including NAs (coded as 10)
array[N,S] int<lower=0,upper=1> D ; // matrix of indicators where D = 0 when Y = NA (i.e. 10)
real<lower=0.0> g ; // smoothness parameter (bigger = smoother)
}
parameters {
corr_matrix[S] R ;
vector[S] latent_log_sigma ;
matrix[S,N] z ;
vector[S] z_0 ;
}
transformed parameters{
vector<lower=0.0>[S] sigma = exp(-1+sqrt2()*latent_log_sigma) ;
matrix[S,S] L = cholesky_decompose(R) ;
matrix[S,N] mu ;
{
mu[,1] = g * sigma .* ( L * z_0 ) ;
for(j in 2:N){
mu[,j] = mu[,j-1] + g * sigma .* (L * z[,j]) ;
}
}
matrix[S,S] Sigma = quad_form_diag(R,g*sigma) ;
}
model {
//priors and latent processes
for(s in 1:S){
target+= std_normal_lpdf(latent_log_sigma[s]) ;
target+= std_normal_lpdf(z_0[s]) ;
for(j in 1:N){
target+= std_normal_lpdf(z[s,j]) ;
}
}
// likelihood
for(j in 1:N){
target+= multi_normal_lpdf(Y[j,D[j,]]|mu[D[j,],j],Sigma[D[j,],D[j,]]) ;
}
}