Help for IRT model


#1

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)

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


#2

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:

   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


#3

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.


#4

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