I am trying to follow the most simple example for imputing missing data using slicing and indexing as seen 3.3 Sliced missing data | Stan User’s Guide except that I have a predictor (rather than the response variable) that is missing.
I keep getting the error:
Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: normal_lpdf: Location parameter is nan, but must be finite!
Here is my reproducible example:
library(rstan)
x <- seq(from=0.9, to=0.0, by=-0.1)
mu <- 2 + 1.5*x
y <- rnorm(length(x), mu, 1)
x[7:8] <- NA
x[3:4] <- NA
d <- list(y = y, x_obs = c(x[1:2],x[5:6],x[9:10]), N = length(y), N_obs = 6, N_mis = 4, ii_obs = c(1,2,5,7,9,10), ii_mis = c(3,4,7,8))
stan_code <- "
data {
int<lower = 0> N;
int<lower = 0> N_obs;
int<lower = 0> N_mis;
int<lower = 1, upper = N> ii_obs[N_obs];
int<lower = 1, upper = N> ii_mis[N_mis];
real y[N];
real x_obs[N_obs];
}
parameters {
real x_mis[N_mis];
real<lower=0> sigma;
real alpha;
real beta;
real alpha_x;
real<lower=0> sigma_x;
}
transformed parameters {
real x[N];
x[ii_obs] = x_obs;
x[ii_mis] = x_mis;
}
model {
for (n in 1:N) {
target += normal_lpdf(y[n] | alpha + beta*x[n], sigma);
}
target += normal_lpdf(x | 0, 1);
target += normal_lpdf(sigma | 0, 2.5) - 1 * normal_lccdf(0 | 0, 2.5);
target += normal_lpdf(alpha | 2, 2.5);
target += normal_lpdf(beta | 0, 5);
}
"
#fit model
simple_mis_mod <- stan(model_code = stan_code, data = d,
chains = 1, iter = 2000, warmup = 1000,
thin = 1, cores = 4)
I must be missing something quite obvious:/
Thanks!