# Deviance information criterion (DIC) with stan

**URL:** <https://discourse.mc-stan.org/t/deviance-information-criterion-dic-with-stan/8499>\
**Category:** Modeling\
**Tags:** loo\
**Created:** [April 16, 2019, 9:41pm UTC](https://discourse.mc-stan.org/t/deviance-information-criterion-dic-with-stan/8499 "2019-04-16T21:41:02Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lazhar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/lazhar/32/3227_2.png) [@Lazhar](https://discourse.mc-stan.org/u/Lazhar)\
**Post date:** [April 16, 2019, 9:41pm UTC](https://discourse.mc-stan.org/t/deviance-information-criterion-dic-with-stan/8499/1 "2019-04-16T21:41:02Z")

</div>

Hi all,  
How calculate the deviance information criterion (DIC) with stan?

---

<div class="post-metadata">

**Author:** ![increasechief](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/increasechief/32/2039_2.png) [@increasechief](https://discourse.mc-stan.org/u/increasechief)\
**Post date:** [April 16, 2019, 10:10pm UTC](https://discourse.mc-stan.org/t/deviance-information-criterion-dic-with-stan/8499/2 "2019-04-16T22:10:20Z")

</div>

As far as I know it’s not implemented. On the other hand there has been interesting work which did the calculation using posterior draws produced by Stan: [https://arxiv.org/abs/1806.09996](https://arxiv.org/abs/1806.09996)

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [April 16, 2019, 10:18pm UTC](https://discourse.mc-stan.org/t/deviance-information-criterion-dic-with-stan/8499/3 "2019-04-16T22:18:14Z")

</div>

It is not implemented because it is not nearly as good as estimator as that which is produced by `loo()`.

---

<div class="post-metadata">

**Author:** ![Lazhar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/lazhar/32/3227_2.png) [@Lazhar](https://discourse.mc-stan.org/u/Lazhar)\
**Post date:** [April 16, 2019, 11:53pm UTC](https://discourse.mc-stan.org/t/deviance-information-criterion-dic-with-stan/8499/4 "2019-04-16T23:53:07Z")

</div>

Thank you so much for your help. Did you have the R program to compute DIC from Stan?

---

<div class="post-metadata">

**Author:** ![Lazhar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/lazhar/32/3227_2.png) [@Lazhar](https://discourse.mc-stan.org/u/Lazhar)\
**Post date:** [April 16, 2019, 11:56pm UTC](https://discourse.mc-stan.org/t/deviance-information-criterion-dic-with-stan/8499/5 "2019-04-16T23:56:44Z")

</div>

Thank you so much fro your help and information. Yes, I used LOO for GEV model but it is difficult for a very big data.

---

<div class="post-metadata">

**Author:** ![increasechief](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/increasechief/32/2039_2.png) [@increasechief](https://discourse.mc-stan.org/u/increasechief)\
**Post date:** [April 16, 2019, 11:56pm UTC](https://discourse.mc-stan.org/t/deviance-information-criterion-dic-with-stan/8499/6 "2019-04-16T23:56:59Z")

</div>

My interest in DIC is limited to the results of that paper. Does the author of the paper have the ability to share their code?

Also the work was based on features of WinBUGS so there may be code examples in the R interface for that, or in other R packages.

---

<div class="post-metadata">

**Author:** ![Lazhar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/lazhar/32/3227_2.png) [@Lazhar](https://discourse.mc-stan.org/u/Lazhar)\
**Post date:** [April 17, 2019, 12:12am UTC](https://discourse.mc-stan.org/t/deviance-information-criterion-dic-with-stan/8499/7 "2019-04-17T00:12:02Z")

</div>

Thank you so much for your explanation and help.

---

<div class="post-metadata">

**Author:** ![avehtari](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/avehtari/32/5935_2.png) [@avehtari](https://discourse.mc-stan.org/u/avehtari)\
**Post date:** [April 17, 2019, 1:52am UTC](https://discourse.mc-stan.org/t/deviance-information-criterion-dic-with-stan/8499/8 "2019-04-17T01:52:50Z")

</div>

> [@Lazhar](#):
>
> Yes, I used LOO for GEV model but it is difficult for a very big data.

Can you tell more about your model and data, and how long it is taking if you use loo package? There are ways to make loo faster and I’m interested in case examples.

---

<div class="post-metadata">

**Author:** ![Lazhar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/lazhar/32/3227_2.png) [@Lazhar](https://discourse.mc-stan.org/u/Lazhar)\
**Post date:** [April 17, 2019, 5:32pm UTC](https://discourse.mc-stan.org/t/deviance-information-criterion-dic-with-stan/8499/9 "2019-04-17T17:32:58Z")

</div>

Thank you so much Prof Aki Vehtari  
the length of y is 47662.  
model:

functions{  
real gev\_lpdf (real y, real loc, real scale, real shape){  
real z;  
real inv\_shape;  
inv\_shape = 1.0/shape;  
z = 1 + (y - loc) \* shape / scale;  
return -log(scale) - (1 + inv\_shape)\*log(z) - pow(z,-inv\_shape);  
}

}

data {  
int\<lower=0\> n;  
int\<lower=0\> ns;  
int\<lower=0\> nt;  
vector [n] y; //data  
vector [nt] x; //covariates  
int site[n];  
int year[n];  
}

parameters {  
vector\<lower=0, upper=85\>[ns] alpha\_mu;  
vector [ns] beta\_mu;

vector\<lower=0, upper=50\>[ns] alpha\_sigma;  
vector [ns] beta\_sigma;

vector\<lower=-0.2, upper=0.2\> [ns] xi;

}

transformed parameters {  
matrix[nt,ns] mu;  
matrix[nt,ns] sigma;

for (i in 1:ns){  
for(t in 1:nt){  
mu[t,i] = alpha\_mu[i] + beta\_mu[i] \* x[t];  
sigma[t,i] = alpha\_sigma[i] + beta\_sigma[i] \* x[t];  
}  
}  
}

model {  
target += normal\_lpdf(alpha\_mu | 0, pow(10,2));  
target += normal\_lpdf(beta\_mu | 0, 10);

target += normal\_lpdf(alpha\_sigma | 0, 10);  
target += normal\_lpdf(beta\_sigma | 0, 10);

target += normal\_lpdf(xi | 0, 0.3);

for (i in 1:n){  
target += gev\_lpdf(y[i] | mu[year[i], site[i]],sigma[year[i], site[i]],xi[site[i]]);

}  
}  
generated quantities{  
vector[n] log\_lik;

for (i in 1:n){  
log\_lik[i] = gev\_lpdf(y[i] | mu[year[i], site[i]],sigma[year[i], site[i]],xi[site[i]]);  
}  
}

---

<div class="post-metadata">

**Author:** ![avehtari](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/avehtari/32/5935_2.png) [@avehtari](https://discourse.mc-stan.org/u/avehtari)\
**Post date:** [April 17, 2019, 6:45pm UTC](https://discourse.mc-stan.org/t/deviance-information-criterion-dic-with-stan/8499/10 "2019-04-17T18:45:25Z")

</div>

> [@Lazhar](#):
>
> the length of y is 47662.

Can you tell also ns and nt? That would help to figure out whether you need loo at all.

We’ll have soon have a loo version which would be fast for n=47622, too. Not also that Windows has a problem making loo slow with multiple cores. While waiting for faster loo(), you may also try waic(), which is better than dic, but not better than loo.

> [@Lazhar](#):
>
> vector\<lower=0, upper=85\>[ns] alpha\_mu;  
> vector\<lower=0, upper=50\>[ns] alpha\_sigma;

It would be better not to use use upper limit unless there is a real physical restriction at that value. You have priors for these, so you would get also better sampling behavior if you remove the upper limits.

> [@Lazhar](#):
>
> vector\<lower=-0.2, upper=0.2\> [ns] xi;

The same thing for this. Is there a real physical restriction at -0.2 and 0.2? If not, it’s better to let the continuous prior to handle the prior information.

---

<div class="post-metadata">

**Author:** ![Lazhar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/lazhar/32/3227_2.png) [@Lazhar](https://discourse.mc-stan.org/u/Lazhar)\
**Post date:** [April 17, 2019, 10:19pm UTC](https://discourse.mc-stan.org/t/deviance-information-criterion-dic-with-stan/8499/11 "2019-04-17T22:19:57Z")

</div>

Thank you so much.  
What about the generated quantities and log\_lik, is it correct?

---

<div class="post-metadata">

**Author:** ![Lazhar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/lazhar/32/3227_2.png) [@Lazhar](https://discourse.mc-stan.org/u/Lazhar)\
**Post date:** [April 17, 2019, 10:20pm UTC](https://discourse.mc-stan.org/t/deviance-information-criterion-dic-with-stan/8499/12 "2019-04-17T22:20:49Z")

</div>

ns: number of sites  
nt: number of times

---

<div class="post-metadata">

**Author:** ![avehtari](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/avehtari/32/5935_2.png) [@avehtari](https://discourse.mc-stan.org/u/avehtari)\
**Post date:** [April 17, 2019, 11:46pm UTC](https://discourse.mc-stan.org/t/deviance-information-criterion-dic-with-stan/8499/13 "2019-04-17T23:46:05Z")

</div>

> [@Lazhar](#):
>
> What about the generated quantities and log\_lik, is it correct?

Looks correct.

> [@Lazhar](#):
>
> ns: number of sites  
> nt: number of times

I meant what are the values of these? Helps to figure out the total number of parameters

Aki

---

<div class="post-metadata">

**Author:** ![Lazhar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/lazhar/32/3227_2.png) [@Lazhar](https://discourse.mc-stan.org/u/Lazhar)\
**Post date:** [April 18, 2019, 12:38am UTC](https://discourse.mc-stan.org/t/deviance-information-criterion-dic-with-stan/8499/14 "2019-04-18T00:38:16Z")

</div>

Thank you so much Profesor
