# Fitting Rasch mixture IRT model

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 ~ normal(0, 1) ;
mu ~ 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)
``````

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.
 "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

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 ~ normal(0, 1) ;
mu ~ 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]

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.

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

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

Hello @Rehab,

I know this is an old topic, but I was wondering if you managed to fit a mixture Rasch Model. And if so, if you could share the final working model you achieved?

I am trying to do a similar model, so that would be really helpful!

Thanks a lot.

Best,
Jill

For what it’s worth I’ve never been able to fit the mixture Rasch model in Stan even on simulated data.
I just can’t seem to find the right restrictions on the parameter space such that I don’t get label switching.

Maybe anyone else here has found a viable solution?