Hi,

I am trying to fit the number of crimes in an area with a Sparse Conditional Auto-Regressive model (CAR) (https://mc-stan.org/users/documentation/case-studies/mbjoseph-CARStan.html). The problem is fitting the crime y as:

Y_i \sim Poisson(exp( X_i^T \beta + \epsilon_i ))

\epsilon_i \sim N(0, [\tau(D -\alpha W)]^1)

with:

- \alpha parameter that controls spatial dependence
- W is a binary spatial contiguity matrix (e.g. if a and b are near, W_{a,b} = 1 and W_{b,a} = 1)
- \tau is a spatially varying precision parameter
- X are the covariates

This model accounts for the spatial auto-correlation (correcting the bias of \beta), and for overdispersion (through the variability).

Although the fit goes very for in-sample data (ofc for the random effects!), I don’t really understand how to use stan for new data. How can I modify the stan model to generate new random effects? E.g. random effect for a new area that was not used in the training phase.

```
data {
int<lower = 1> n;
int<lower = 1> p;
matrix[n, p] X;
int<lower = 0> y[n];
matrix<lower = 0, upper = 1>[n, n] W;
}
transformed data{
vector[n] zeros;
matrix<lower = 0>[n, n] D;
{
vector[n] W_rowsums;
for (i in 1:n) {
W_rowsums[i] = sum(W[i, ]);
}
D = diag_matrix(W_rowsums);
}
zeros = rep_vector(0, n);
}
parameters {
vector[p] beta;
vector[n] phi;
real<lower = 0> tau;
real<lower = 0, upper = 1> alpha;
}
model {
phi ~ multi_normal_prec(zeros, tau * (D - alpha * W));
beta ~ normal(0, 1);
tau ~ gamma(2, 2);
y ~ poisson_log(X * beta + phi);
}
generated quantities {
vector[n] log_lik;
vector[n] y_pred;
real mu;
for (i in 1:n) {
y_pred[i] = poisson_log_rng(X[i] * beta + phi[i]);
}
}
```

In this stan model (the simplest one) y_pred is for the in-sample data.

Thanks