# CAR model in STAN

**URL:** <https://discourse.mc-stan.org/t/car-model-in-stan/17472>\
**Category:** Modeling\
**Created:** [August 15, 2020, 5:54pm UTC](https://discourse.mc-stan.org/t/car-model-in-stan/17472 "2020-08-15T17:54:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saikat94](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/saikat94/32/5720_2.png) [@Saikat94](https://discourse.mc-stan.org/u/Saikat94)\
**Post date:** [August 15, 2020, 5:54pm UTC](https://discourse.mc-stan.org/t/car-model-in-stan/17472/1 "2020-08-15T17:54:45Z")

</div>

Hello everyone,

I am working on a Multinomial spatial classification problem, where we have considered a CAR prior on the regression coefficients \beta s. I have attached the stan code below. While the code is syntactically alright and it runs fine, the chains are not mixing properly. The data has 3 classes. M=336 and N=30K+.

Now I know, there might be several reasons for the chains not mixing, but I feel like I am also not specifying the model correctly. I was going through the STAN manual, and I saw a point about the K vs K-1 specification of the \beta s. However, I feel like the K-1 specification might not be appropriate given the CAR prior. Is there a workaround on this? Any other suggestion to improve the MCMC sampling is also welcome. Thank you in advance.

The CAR prior has been set up as per suggestion in Banerjee,Carlin, and Gelfand.

```stan
data {
  int<lower=1> N; // Number of data
  int<lower=1> M; // Number of covariates
  matrix[N, M] X;
  int<lower=1,upper=3> y[N];
  matrix [M,M] DW; // Diagonal matrix, entries equal the number of neighbors
  matrix [M,M] W; // Neighborhood matrix
  real lmin; // minimum eigenvalue of DW^{-1/2}W DW^{-1/2}
  real lmax; // maximum eigenvalue of DW^{-1/2}W DW^{-1/2}
}
transformed data{
 vector[M] zeros=rep_vector(0, M);
}

parameters {
  vector[M] beta;
  real alpha;             
}
transformed parameters{
  matrix[M,M] prec= inverse(DW-alpha*W);
}
model {
  beta ~ multi_normal(zeros,prec);
  alpha ~ uniform(1/lmin,1/lmax);
  y ~ categorical_logit(X*beta);
}

```

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [August 16, 2020, 2:24pm UTC](https://discourse.mc-stan.org/t/car-model-in-stan/17472/2 "2020-08-16T14:24:39Z")

</div>

for the ICAR model, we found that the “soft-sum-to-zero” constraint was preferable - the hard sum-to-zero constraint imposes a geometry that’s extremely difficult for the sampler to fit.

have you looked at the Stan case studies on the CAR and ICAR models?

[https://mc-stan.org/users/documentation/case-studies/mbjoseph-CARStan.html](https://mc-stan.org/users/documentation/case-studies/mbjoseph-CARStan.html)

[https://mc-stan.org/users/documentation/case-studies/icar\_stan.html#about-conditional-autoregressive-models](https://mc-stan.org/users/documentation/case-studies/icar_stan.html#about-conditional-autoregressive-models)

---

<div class="post-metadata">

**Author:** ![Saikat94](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/saikat94/32/5720_2.png) [@Saikat94](https://discourse.mc-stan.org/u/Saikat94)\
**Post date:** [August 19, 2020, 6:18pm UTC](https://discourse.mc-stan.org/t/car-model-in-stan/17472/3 "2020-08-19T18:18:39Z")

</div>

Hi. Thank you for your reply. I have looked into the links you have preferred before we set out to do this. The current model is actually an extension of a previous model, which was a Binomial classification and for that we used a CAR prior exactly of the form and it worked perfectly. So we wanted to try and see if a simple extension into the multinomial case would work or not.
