Fitting Rasch mixture IRT model


#1

Hello Stan users

I am a very new user of Stan. Any comments would be absolutely helpful.

I tried to fit Rasch mixture item response theory model, for only one item for simplicity, with the help of “Finite Mixture Model” section on Stan reference 2.17.0. However, the following error is displayed
(Error in stanc(file = file, model_code = model_code, model_name = model_name, :
c++ exception (unknown reason))
I know there are errors somewhere in my Stan code but could not figure them out.

Here is my code:

data {
int<lower=1> K; // number of mixture components
int<lower=1> N; // number of examinees
int<lower=0,upper=1> y[N]; // observations for one item only
}
parameters {
simplex[K] pi; // mixing proportions
vector[N] theta ; // ability of each examinee i
}

model {
theta ~ normal(mu[k],1) ;
for (k in 1:K){
mu[1] ~ normal(0, 1) ;
mu[2] ~ normal(1, 1) ; // to avoid label switching problem
pi ~ Dirichlet(0.5,0.5) ;
}
real log_pi[k]=log(pi) ;
for (i in 1:N){
real p; //create a local variable within the loop
p= inv_logit(theta[i])); // with b=0 for the item
y ~ bernoulli( p ) ;
real lps[K]=log_pi;
for (k in 1:K) {
lps[k] += (y[i]*log( p )+(1-y[i])*log(1-p)) ;
target += log_sum_exp(lps);
}}
}

Fitting the model in R :
library(rstan)
K=2
N=10
y = c(1,1,1,0,0,1,1,0,0,0)
data=list(K,N,y)
fit = stan(file = ‘rasch.stan’ ,data=data, iter = 2, chains = 1)
print(fit)


#2

Upgrade you rstan


#3

Thanks a lot bgoodri for your hint. I can see now the positions of too many errors and fix them

Now, I have a sampler error that says:

SAMPLING FOR MODEL ‘Rsc2’ NOW (CHAIN 1).
Rejecting initial value:
Log probability evaluates to log(0), i.e. negative infinity.
Stan can’t start sampling from this initial value.

Initialization between (-2, 2) failed after 100 attempts.
Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] "Error in sampler$call_sampler(args_list[[i]]) : Initialization failed."
error occurred during calling the sampler; sampling not done)

May you explain this error message please and why I am getting log(0).

Thanks


#4

Here is my updated Stan code

data {
  int<lower=1> K;                  // number of mixture components
  int<lower=1> N;                 // nymber of examinees
  int<lower=0,upper=1> y[N];   // observations
  vector<lower=0>[K] alpha ;
}
parameters {
  simplex[K] pi;               // mixing proportions
  vector[N] theta ;         // ability of each examinee i
  vector[K] mu ;
}
transformed parameters {
  real log_pi[K];
  log_pi[K]=log(pi[K]); 
}
model {
  mu[1] ~ normal(0, 1) ;
  mu[2] ~ normal(1, 1) ;
  for (k in 1:K){
    theta ~ normal(mu[k],1) ;
    pi ~ dirichlet(alpha) ; 
  }
  for (i in 1:N){  
    real p[N];           //create a local variable within the loop 
    p[i]= inv_logit(theta[i]); 
y[i] ~ bernoulli(p[i]) ;
for (k in 1:K) {
   real lps[K]; 
  lps[k] = log_pi[k]
           + (y[i]*log(p[i])+(1-y[i])*log(1-p[i])) ;
  target += log_sum_exp(lps)  ;
}}
}

[edit: escaped code]


#5

If you are on the most recent version of RStan, you should be getting a more informative message.

I don’t see what that mixture is that you’re doing and ho all this relates to an IRT model.

There are a lot of problems with this model, so I’ll only highlight a few.

for (k in 1:K){
    theta ~ normal(mu[k],1) ;

Because theta is a vector, this gives each theta[k] a total of K different distributions (the distributio will be the product of the densities). You probably meant theta[k].

For this

for (i in 1:N){  
    real p[N];           //create a local variable within the loop 
    p[i]= inv_logit(theta[i]);

You define the array in the loop, so if you go outside the loop, it won’t be defined. This is just very wasteful in creating arrays.

The indentation makes it hard to read. Why are there three closes at the end? Makes me think you may be missing a close brace somewhere.


#6

Please find the attachment because I could not write equations directly on email, to explain my model.


#7

I think the attachment may have gotten lost. I don’t see it.


#8

Reply to Bob.pdf (426.3 KB)