Fail to complete an IRT model with missing data

Hello,

the following model is related to a previous post: Fail to run due to relatively high number of missing observations.

The model is the following:

functions {
  real rsm(int y, real theta, real beta, vector kappa) {
    vector[rows(kappa) + 1] unsummed;
    vector[rows(kappa) + 1] probs;
    unsummed = append_row(rep_vector(0, 1), theta - beta - kappa);
    probs = softmax(cumulative_sum(unsummed));
    return categorical_lpmf(y + 1 | probs);
  }
  real rsm_rng(real theta, real beta, vector kappa) {
    vector[rows(kappa) + 1] unsummed;
    vector[rows(kappa) + 1] probs;
    unsummed = append_row(rep_vector(0, 1), theta - beta - kappa);
    probs = softmax(cumulative_sum(unsummed));
    return categorical_rng(probs);
  }
}

data {
  int<lower=1> I;               // # items
  int<lower=1> J;               // # persons
  int<lower=1> N;               // # observations
  int<lower=0> N_mis;           // # missing observations
  int<lower=0,upper=1> R[N];    // whether observation j is missing
  int<lower=1, upper=I> ii[N];  // item for n
  int<lower=1, upper=J> jj[N];  // person for n
  int<lower=0> y[N];            // response for n
}

transformed data {
  int m;                        // # steps
  m = max(y);
}

parameters {
  vector[I] beta;
  vector[m-1] kappa_free;
  vector[J] theta;
  real<lower=0> sigma;
}

transformed parameters {
  vector[m] kappa;
  kappa[1:(m-1)] = kappa_free;
  kappa[m] = -1*sum(kappa_free);
}
  
model {
  beta ~ normal(0, 3);
  target += normal_lpdf(kappa | 0, 3);
  theta ~ normal(0, sigma);
  sigma ~ exponential(.1);
  for (n in 1:N) {
    if (R[n] == 1) {
      target += rsm(y[n], theta[jj[n]], beta[ii[n]], kappa);
    }
  } 
}

generated quantities {  
vector[N] y_mis;
for (n in 1:N) {
if (R[n] == 0){
  y_mis[n] = rsm_rng(theta[jj[n]], beta[ii[n]], kappa);
    }
  }  
}

The model runs but it fails to complete generating the following message:

Error in validObject(.Object) :
invalid class “stanfit” object: The following variables have undefined values: y_mis[5],The following variables have undefined values: y_mis[8],…

The latter part of the message is repeated numerous times for different observations. We are not sure how deal with this issue. Any directions?

Glancing at the generated quantities block, it looks like you’re only filling in some of the values in y_mis so the rest don’t exist. You can either give the other values some placeholder value so they get initialized and then ignore them or you can define y_mis to only have size equal to the number of elements you want to fill in.

Thank you!