I’m developing a regression model using Rstan that will eventually accommodate missing observations in the predictors and the response. I am starting with the example in section 10.3 of the Stan manual, which describes how to use index arrays to handle missing data. The code below is an exact reproduction of what is in the manual. Attempting to fit the model on simulated data produces an error that I don’t understand. The code is pasted below along with a few lines from R to make this a reproducible example. What can I do to resolve this error?
From section 10.3 of the Stan manual (sliced missing data)
model <- "
data{
int<lower = 0> N_obs; // number of non-missing observations
int<lower = 0> N_mis; // number of missing observations
int<lower = 1, upper = N_obs + N_mis> ii_obs[N_obs]; // indexes of non-missing
int<lower = 1, upper = N_obs + N_mis> ii_mis[N_mis]; // indexes of missing
real y_obs[N_obs]; // the non-missing observations
}
transformed data{
int<lower = 0> N = N_obs + N_mis; // sample size
}
parameters{
real y_mis[N_mis]; // missing data are a parameter to be estimated
real<lower=0> sigma; // sigma must be gte 0
}
transformed parameters{
real y[N]; // the data, which will include missing and non-missing components
y[ii_obs] = y_obs; // populate y with the observed part of the data
y[ii_mis] = y_mis; // and the missing part of the data
}
model{
sigma ~ gamma(1, 1);
y[1] ~ normal(0, sigma);
y[2:N] ~ normal(y[1:(N-1)], sigma);
}
"
# Simulate some data
set.seed(1)
y_obs <- rnorm(1000,0,2)
ii_obs <- seq(1,1000,1)
ii_mis <- sample(seq(2,1000,1),10,replace=F)
ii_obs <- ii_obs[-ii_mis]
N_obs <- 990
N_mis <- 10
data_list <- list(y_obs=y_obs, ii_obs=ii_obs, ii_mis=ii_mis, N_obs=N_obs, N_mis=N_mis)
fit <- stan(model_code = model, data = data_list, cores = 1, chains = 2, iter = 2000)
DIAGNOSTIC(S) FROM PARSER:
Warning (non-fatal):
Left-hand side of sampling statement (~) may contain a non-linear transform of a parameter or local variable.
If it does, you need to include a target += statement with the log absolute determinant of the Jacobian of the transform.
Left-hand-side of sampling statement:
y[1] ~ normal(…)
Warning (non-fatal):
Left-hand side of sampling statement (~) may contain a non-linear transform of a parameter or local variable.
If it does, you need to include a target += statement with the log absolute determinant of the Jacobian of the transform.
Left-hand-side of sampling statement:
y[2:N] ~ normal(…)
In file included from C:/Program Files/R/R-3.4.0/library/BH/include/boost/config.hpp:39:0,
from C:/Program Files/R/R-3.4.0/library/BH/include/boost/math/tools/config.hpp:13,
from C:/Program Files/R/R-3.4.0/library/StanHeaders/include/stan/math/rev/core/var.hpp:7,
from C:/Program Files/R/R-3.4.0/library/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5,
from C:/Program Files/R/R-3.4.0/library/StanHeaders/include/stan/math/rev/core.hpp:12,
from C:/Program Files/R/R-3.4.0/library/StanHeaders/include/stan/math/rev/mat.hpp:4,
from C:/Program Files/R/R-3.4.0/library/StanHeaders/include/stan/math.hpp:4,
from C:/Program Files/R/R-3.4.0/library/StanHeaders/include/src/stan/model/model_header.hpp:4,
from file1806e89432d.cpp:8:
C:/Program Files/R/R-3.4.0/library/BH/include/boost/config/compiler/gcc.hpp:186:0: warning: “BOOST_NO_CXX11_RVALUE_REFERENCES” redefined
define BOOST_NO_CXX11_RVALUE_REFERENCES
^
:0:0: note: this is the location of the previous definition
cc1plus.exe: warning: unrecognized command line option "-Wno-ignored-attributes"
Error in new_CppObject_xp(fields$.module, fields$.pointer, …) :
mismatch in dimension declared and found in context; processing stage=data initialization; variable name=y_obs; position=0; dims declared=(990); dims found=(1000)
failed to create the sampler; sampling not done