# Help for IRT model

**URL:** <https://discourse.mc-stan.org/t/help-for-irt-model/7618>\
**Category:** Modeling\
**Created:** [February 7, 2019, 3:59pm UTC](https://discourse.mc-stan.org/t/help-for-irt-model/7618 "2019-02-07T15:59:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Silvio\_Silva](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/silvio_silva/32/3951_2.png) [@Silvio\_Silva](https://discourse.mc-stan.org/u/Silvio_Silva)\
**Post date:** [February 7, 2019, 3:59pm UTC](https://discourse.mc-stan.org/t/help-for-irt-model/7618/1 "2019-02-07T15:59:28Z")

</div>

I am new in Stan and I’m starting my studies in Item Response Theory (IRT). My interest is to write IRT code, at the moment I am trying to adapt the “Generalized partial credit model”

P(Y\_{j,i}=k|\theta\_j)=\frac{exp\{\sum\_{l=1}^{k}\alpha\_i(\theta\_j-\beta\_{i,l})\}}{\sum\_{m=1}^{K}exp\{\sum\_{l=1}^{m}\alpha\_i(\theta\_j-\beta\_{i,l})\}}

The code is inspired by a code of BUGS that is in the following journal (pages 10 and 11)

> **[v36c01.pdf](https://www.jstatsoft.org/article/view/v036c01/v36c01.pdf)**
>
> 1459.90 KB

After several attempts I got this model below.

data{  
int \<lower=1\> n; // number of people  
int \<lower=1\> p; // number of items  
int \<lower=2,upper=5\> K; //number of categories  
int \<lower=1,upper=K\> Y[n,p]; // data base  
}

parameters {  
vector[n] teta;  
vector \<lower=0\>[p] alfa;  
ordered[K-1] beta[p]; //difficulty  
real m\_beta; //prior to the beta average  
real \<lower=0\> s\_beta; //priori of the standard deviation of beta  
}  
model{  
alfa~cauchy(0,5);  
teta~normal(0,1);  
m\_beta~normal(0,5);  
s\_beta~cauchy(0,5);

for (i in 1: p){  
for (k in 1:(K-1)){  
beta[i,k] ~ normal(m\_beta,s\_beta);  
}}

for(j in 1:n){  
for(i in 1:p){  
for(k in 1:(K-1)){  
real soma; //created variable  
real exp\_soma; //created variable  
real exp\_soma2;//created variable  
soma=soma+(alfa[i]\*(teta[j]-beta[i,k]));  
exp\_soma=exp(soma);  
exp\_soma2=exp\_soma2+exp\_soma;  
Y[j,i]~ordered\_logistic(exp\_soma/exp\_soma2,beta[i]);  
}  
}  
}

}

I do not know if it makes sense or if it is very wrong, in the end it tells me that it has the following error.

Chain 1: Rejecting initial value:  
Chain 1: Error evaluating the log probability at the initial value.  
Chain 1: Exception: ordered\_logistic: Location parameter is nan, but must be finite! (in ‘model1a787417cb86\_GPCM2’ at line 36)

I know the error is in the estimation function but I do not know how to solve it.

Tks

---

<div class="post-metadata">

**Author:** ![rjc10](https://avatars.discourse-cdn.com/v4/letter/r/e9bcb4/32.png) [@rjc10](https://discourse.mc-stan.org/u/rjc10)\
**Post date:** [February 7, 2019, 5:46pm UTC](https://discourse.mc-stan.org/t/help-for-irt-model/7618/2 "2019-02-07T17:46:58Z")

</div>

> [@Silvio\_Silva](#):
>
> ````R
> for(j in 1:n){
> for(i in 1:p){
> for(k in 1:(K-1)){
> real soma; //created variable
> real exp_soma; //created variable
> real exp_soma2;//created variable
> soma=soma+(alfa[i]*(teta[j]-beta[i,k]));
> exp_soma=exp(soma);
> exp_soma2=exp_soma2+exp_soma;
> Y[j,i]~ordered_logistic(exp_soma/exp_soma2,beta[i]);
> }
> }
> }
> ```R
> 
> ````

Hi Silvio,

I haven’t run the code again, but it looks like the problem is that in the `model` section, you’re declaring and initializing your variables within the `k` loop when you should be initializing them outside the `k` loop. Like this:

```R
   for(j in 1:n){
      for(i in 1:p){
         real soma = 0; //created variable
         real exp_soma2 = 0;//created variable
         for(k in 1:(K-1)){
            real exp_soma; //created variable
            soma=soma+(alfa[i]*(teta[j]-beta[i,k]));
            exp_soma=exp(soma);
            exp_soma2=exp_soma2+exp_soma;
            Y[j,i]~ordered_logistic(exp_soma/exp_soma2,beta[i]);
      }
   }
}

```

Right now, you’re not initializing `soma` to anything and then adding itself to another quantity, so you’ll end up adding an `NA` value to something else and getting `NA` as a result. Then `exp_soma` becomes `NA`, as do `exp_soma2` and finally you get an `NA` location parameter exception. Initializing `soma` and `exp_soma2` to zero outside your `k` loop should fix the problem without affecting your computations. (Might need to set them to 0.0, I’m not sure.)

Eventually, you may be able to speed up your code by vectorizing it, but I agree with starting with something slow and correct.

Cheers,

Richard

---

<div class="post-metadata">

**Author:** ![Silvio\_Silva](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/silvio_silva/32/3951_2.png) [@Silvio\_Silva](https://discourse.mc-stan.org/u/Silvio_Silva)\
**Post date:** [February 7, 2019, 8:37pm UTC](https://discourse.mc-stan.org/t/help-for-irt-model/7618/3 "2019-02-07T20:37:13Z")

</div>

Thank you very much Richard, I am very happy because it worked, it was very simple I will continue learning Stan, soon I will have more doubts, thank you.

---

<div class="post-metadata">

**Author:** ![saudiwin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/saudiwin/32/1137_2.png) [@saudiwin](https://discourse.mc-stan.org/u/saudiwin)\
**Post date:** [February 7, 2019, 8:49pm UTC](https://discourse.mc-stan.org/t/help-for-irt-model/7618/4 "2019-02-07T20:49:33Z")

</div>

Please check the Stan website for case studies of IRT models to use as example code.
