# How to implement a poisson count model into stan?

``````data {
//int<lower = 0> N;
int<lower=0> n_examinee; // Number of examinees
int<lower=0> n_item; // Number of items
//int<lower = 0> y[N];
int<lower=0> Y[n_examinee, n_item]; //The data matrix (count)
}
parameters {
vector [n_examinee] theta;  //
vector <lower = 0> [n_item] easiness;  //
//real <lower = 0> sdt;
//real <lower = 0> alpha;
//real <lower = 0> beta;
}

transformed parameters {

matrix [n_examinee, n_item] lambdas;
for(i in 1:n_examinee){
for (j in 1:n_item){
lambdas[i,j] = exp(theta[i] + easiness[j]) ;
}
}
}

model {

theta ~ normal(0, .3);
///sdt ~ gamma(1,30);
easiness ~ uniform(0,3);
for(i in 1:n_examinee){
for (j in 1:n_item){
Y[i,j] ~ poisson(lambdas[i,j]);
}
}
}
``````

For a few weeks I have been trying to get this model to work in stan, specifically the RPCM(Rasch Poisson counts model), but for some reason haven’t been able to get it to accurately run. This is how I am generating the data in R:

``````n <- 200
n_item <- 20
easiness <- log(seq(5, 40, length.out = n_item))
theta <- rnorm(n, 0, .3)

gen_test_data <- function(true_deltas, true_abilities, n_persons) {
out <- data.frame(
item1 = numeric(n_persons)
)
for (j in 1:length(true_deltas)) {
lambdas <- exp(true_abilities + true_deltas[j])
out[[paste0("item", j)]] <- rpois(n_persons, lambdas)
}
return(out)
}

df <- gen_test_data(easiness, theta, length(theta))
rpcsm <- stan(file = "rpcm.stan", data = list(n_examinee = n,
n_item = n_item,
Y = df), iter = 4000)
``````

Could anyone point to any clear problems? I have tried different variations but the rhats are never good.

There might be other problems, but the first thing that I would try is making sure that the constraints on `easiness` imposed by its prior are matched by constraint declarations in the `parameters` block. In particular, the prior falls off a cliff at 3, and this will cause the posterior geometry to be impossibly nasty in that vicinity unless you declare `easiness` with an upper bound of 3. Alternatively, you could switch to a prior with softer edges than a uniform distribution.