I’m trying to write the model from section 10.1 of the manual (page 162) and having some troubles recovering its parameters.
This is my stan code:
data {
int<lower = 0, upper = 1> run_estimation; // a switch to evaluate the likelihood
int<lower=0> N ;
vector[N] y ;
}
parameters {
real alpha ;
real beta ;
real<lower=0> sigma ;
}
model {
alpha ~ normal(0,1) ;
beta ~ normal(0,1) ;
sigma ~ normal(0,1) ;
if(run_estimation==1) y[2:N] ~ normal(alpha + beta * y[1:(N - 1)], sigma) ;
}
generated quantities{
vector[N-1] y_sim ;
int j=0 ;
for (n in 2:N){
j = j + 1 ;
y_sim[j] = normal_rng(alpha + beta * y[n-1], sigma);
}
}
First, I generate some fake data to see if I can recover the parameters:
compiled_model_arone <- stan_model("arone.stan")
set.seed(9782)
y <- rnorm(1000)
sim_out <- sampling(compiled_model_arone, data = list(run_estimation = 0,
N = length(y),
y = y
),
seed = 42118)
fake_data_matrix <- sim_out %>%
as.data.frame %>%
select(contains("y_sim"))
Then, I try to recover the parameters:
draw <- 82
y_sim <-as.numeric(fake_data_matrix[draw,])
recapture_fit <- sampling(compiled_model_arone, data = list(run_estimation = 1,
N = length(y_sim),
y = y_sim
),
seed = 42118)
alpha <- as.data.frame(recapture_fit, pars = "alpha")
true_alpha<- as.matrix(sim_out, pars = "alpha")[draw]
ggplot(data = alpha, aes(x = alpha)) +
geom_density() + geom_vline(aes(xintercept = true_alpha),color="red")
## beta
beta <- as.data.frame(recapture_fit, pars = "beta")
true_beta <- as.matrix(sim_out, pars = "beta")[draw]
ggplot(data = beta, aes(x = beta)) +
geom_density() + geom_vline(aes(xintercept = true_beta),color="red")
## sigma
sigma <- as.data.frame(recapture_fit, pars = "sigma")
true_sigma <- as.matrix(sim_out, pars = "sigma")[draw]
ggplot(data = sigma, aes(x = sigma)) +
geom_density() + geom_vline(aes(xintercept = true_sigma),color="red")
For example. this is what I get for beta:
So, if I understand @James_Savage’s tutorial correctly, something is wrong with my model.
I also tried plotting y and the posterior mean of y:
y = y_sim[2:length(y_sim)]
y_sim = as.data.frame(recapture_fit, pars = "y_sim") %>% summarise_all(mean) %>% as.numeric()
df.plot <- data.frame(y =y, y_sim = y_sim)
ggplot(data = df.plot, aes(x=y, y=y_sim)) + geom_point() +
geom_abline(intercept = 0, slope = 1, linetype="dotdash") +
xlim(min(df.plot$y, df.plot$y_sim), max(df.plot$y, df.plot$y_sim)) +
ylim(min(df.plot$y, df.plot$y_sim), max(df.plot$y, df.plot$y_sim))
What am I missing?