Yup, you’re right, I overlooked that the Stan program uses N
to declare the dimension of y
. To avoid this issue, try the following Stan program and R script:
data {
int<lower=0> N;
int<lower=0> n_max;
int<lower=0> y[n_max];
real my;
}
parameters {
real<lower=0> omega;
real<lower=0, upper=1> alpha;
real<lower=0, upper=1> beta;
}
model {
real theta[N];
theta[1] = omega+alpha*my+beta*my;
y[1] ~ poisson(theta[1]);
for (n in 2:N){
theta[n] = omega+alpha*y[n-1]+beta*theta[n-1];
y[n] ~ poisson(theta[n]);
}
}
library(cmdstanr)
my_model <- cmdstan_model(...)
my_data_list <-list(N=NA,
n_max = 354,
y = c(0,2,3,5,2,1,1,5,3,8,18,10,14,12,12,9,28,31,47,55,59,77,94,144,157,151,178,153,205,228,285,319,316,241,274,202,216,320,338,318,304,299,260,268,325,314,474,373,351,365,418,400,349,563,450,591,414,422,510,489,714,575,620,385,384,351,441,414,458,352,245,297,425,375,405,355,336,259,244,261,388,295,382,267,235,205,254,231,254,274,200,165,177,191,197,206,179,155,121,151,187,173,226,197,247,193,159,189,216,230,257,202,163,116,156,172,156,143,152,149,98,133,112,131,104,107,77,90,120,103,126,97,92,85,98,135,133,119,86,100,97,101,115,100,103,106,111,88,91,143,150,163,140,99,81,85,144,146,157,177,120,104,97,221,185,171,179,161,108,79,126,122,140,165,129,89,93,150,135,119,192,161,170,143,192,263,373,335,302,312,305,524,380,393,461,384,313,297,386,419,544,569,557,559,665,615,714,728,922,801,822,1022,1449,1381,1786,2131,2202,1656,1677,2266,2414,2658,2425,1868,1917,1622,2743,2531,2773,2484,2432,2366,2219,3829,3565,3493,3301,2975,2248,2149,3850,3580,3578,3459,3467,2628,2820,4667,3948,3505,3586,3634,2832,2890,4491,3773,3704,3200,3076,2503,2093,3276,3059,2584,2090,1938,1366,1350,2058,1761,1605,1396,1315,1094,1038,1705,1485,1338,1151,1052,877,864,1403,1344,1338,1219,1233,950,1027,1718,1491,1228,1173,1450,1017,1413,1980,1867,1629,1252,590,1659,1958,2121,3221,2946,2165,1477,2736,2429,3467,2814,2576,2441,2094,1852,1746,2864,2512,2231,1201,1961,1694,1643,2526,2268,2100,1823,1657,1338,1326,1973,1747,1817,1595,1488,1231,1265,1709,1028,1747,1564,1369,1122,1134,1383,1308,1239,1216,1185,900,954,1375,1208),
my = 973.4915254)
outputs <- list()
for(i in 0:100){
my_data_list$N <- my_data_list$n_max - i
outputs[[i+1]] <- my_model$sample(data = my_data_list)
}