data { int<lower = 1> T; //number of days int<lower = 0> N[T]; int<lower = 0> y[T];}
parameters { real mu; real eps_0; vector[T - 1] eps_raw; real<lower = -1, upper = 1> phi; real<lower=0> sigma;}
transformed parameters{ vector[T] eps; real<lower = 0, upper = 1> p[T];
eps[1] = eps_0; for (t in 2:T) eps[t] = phi * eps[t - 1] + sigma * eps_raw[t - 1];
for (t in 1:T) p[t] = inv_logit(mu + eps[t]);}
model { eps_0 ~ normal(0, sigma * sqrt(1 - square(phi))); eps_raw ~ normal(0, 1);
for (t in 1:T) y[t] ~ binomial(N[t], p[t]);}
Times = 100
N_1 = rpois(Times * 0.3, lambda = exp(rnorm(Times * 0.3, 3, 2)))N_2 = rpois(Times * 0.7, lambda = exp(rnorm(Times * 0.7, 3, 2)))
N = c(N_1[order(N_1)], N_2[order(N_2, decreasing = T)])
mu = qlogis(2/3)sd = 1/10eps = sd * arima.sim(model = list(ar = 0.9), n = Times)p = plogis(mu + eps)
y = rbinom(Times, N, p)
ar1_check = stan(data = list(T = Times, N = N, y = y), model_code = ar1_logit, control = list(adapt_delta = 0.99) )
pairs(ar1_check, pars = c("mu", "sigma", "phi", "eps_0"))
The funnel is even more dramatic in my applied example data:
853, 903, 888, 734, 989, 1010, 1262, 1438, 1431, 952, 1914, 3470, 2014, 1197, 912, 744, 476, 412, 200, 219, 205, 277, 263, 97, 95, 110, 73, 25, 77, 87, 64, 35, 20, 6, 16, 19, 15, 15, 20, 26, 10, 9, 5, 3, 2, 3, 0, 3, 0, 1, 2, 2, 2, 5, 3, 13, 9, 5, 2, 1, 2, 1, 3, 3, 3, 1, 1, 1, 2, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
y = c(0, 0, 18, 49, 81, 114, 97, 83, 82, 96, 183, 352, 441, 646, 662, 686, 563, 765, 780, 986, 1122, 1129, 750, 1517, 2699, 1570, 945, 722, 571, 372, 319, 160, 175, 160, 231, 206, 78, 77, 91, 60, 19, 63, 69, 51, 27, 16, 5, 13, 17, 13, 11, 15, 20, 9, 7, 4, 2, 2, 3, 0, 1, 0, 1, 2, 2, 1, 4, 3, 11, 7, 4, 2, 0, 2, 0, 2, 3, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
ar1_check = stan(data = list(T = length(y), N = N, y = y), model_code = ar1_logit, control = list(adapt_delta = 0.9999))
pairs(ar1_check, pars = c("mu", "sigma", "phi", "eps_0"))
What I've noticed in both my simulated and observed data is a very distinctive funnel shape in the pairs plot of the regression intercept and the autoregressive term.
library(rstan)options(mc.cores = parallel::detectCores())
Times = 150
N_1 = rpois(Times * 0.3, lambda = exp(rnorm(Times * 0.3, 4, 2)))N_2 = rpois(Times * 0.7, lambda = exp(rnorm(Times * 0.7, 4, 2)))
N = c(N_1[order(N_1)], N_2[order(N_2, decreasing = T)])
mu = qlogis(2/3)
sigma = 1/4eps = arima.sim(model = list(ar = 0.9), n = Times, sd = sigma)
p = plogis(mu + eps)
y = rbinom(Times, N, p)
ar1_logit = "
data { int<lower = 1> T; //number of days int<lower = 0> N[T]; int<lower = 0> y[T];}
parameters { real mu; real eps_0; vector[T - 1] eps_raw; real<lower = -1, upper = 1> phi; real<lower=0> sigma;}
transformed parameters{
real alpha;
vector[T] eps; real<lower = 0, upper = 1> p[T];
alpha = mu * (1 - phi);
eps[1] = eps_0; for (t in 2:T) eps[t] = phi * eps[t - 1] + sigma * eps_raw[t - 1];
for (t in 1:T)
p[t] = inv_logit(alpha + eps[t]);
}
model { eps_0 ~ normal(0, sigma * sqrt(1 - square(phi))); eps_raw ~ normal(0, 1);
for (t in 1:T) y[t] ~ binomial(N[t], p[t]);}"
ar1_check = stan(data = list(T = Times, N = N, y = y), model_code = ar1_logit,