duckmayr
duckmayr

Reputation: 16930

Error in declaring data in rstan for graded response model

I am trying to use Stan, specifically through rstan, to fit a graded response model. Luo and Jiao (2018), available here, provide Stan code for doing so. Here is their code, edited only to include more white space:

data{
    int<lower=2, upper=4> K; //number of categories
    int <lower=0> n_student;
    int <lower=0> n_item;
    int<lower=1,upper=K> Y[n_student,n_item];
}

parameters {
    vector[n_student] theta;
    real<lower=0> alpha [n_item];
    ordered[K-1] kappa[n_item]; //category difficulty
    real mu_kappa; //mean of the prior distribution of category difficulty
    real<lower=0> sigma_kappa; //sd of the prior distribution of category difficulty
}

model{
    alpha ~ cauchy(0,5);
    theta ~ normal(0,1);
    for (i in 1: n_item){
        for (k in 1:(K-1)){
            kappa[i,k] ~ normal(mu_kappa,sigma_kappa);
    }}
    mu_kappa ~ normal(0,5);
    sigma_kappa ~ cauchy(0,5);
    for (i in 1:n_student){
        for (j in 1:n_item){
            Y[i,j] ~ ordered_logistic(theta[i]*alpha[j],kappa[j]);
    }}
}

generated quantities {
    vector[n_item] log_lik[n_student];
    for (i in 1: n_student){
        for (j in 1: n_item){
            log_lik[i, j] = ordered_logistic_log (Y[i, j],theta[i]*alpha[j],kappa[j]);
    }}
}

However, when I try to use this code, the parser throws an error. Here is the R code to reproduce the error:

library("rstan")

n <- 100
m <- 10
K <- 4
example_responses <- sample(x = 1:4, size = n * m, replace = TRUE)
example_responses <- matrix(example_responses, nrow = n, ncol = m)

example_dat <- list(K = K,
                    n_student = n,
                    n_item = m,
                    Y = example_responses)

fit <- stan(file = "~/grm.stan", data = example_dat)

Here is the error I receive:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
 error in 'modelf6471b3f018_grm' at line 2, column 21
  -------------------------------------------------
     2: 
     3: data {
     4:     int<lower=2, upper=4> K; // number of categories
                            ^
     5:     int<lower=0> n_student;                       
  -------------------------------------------------

PARSER EXPECTED: <one of the following:
  a variable declaration, beginning with type,
      (int, real, vector, row_vector, matrix, unit_vector,
       simplex, ordered, positive_ordered,
       corr_matrix, cov_matrix,
       cholesky_corr, cholesky_cov
  or '}' to close variable declarations>
Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model 'grm' due to the above error.

I've tried going through the code and the Stan manual to see what the issue is with the data declaration, but I can't find a problem with it. The declaration appears to be very similar to a declaration example in the Stan Language Reference:

int<lower = 1> N;

Can anyone tell me what I'm missing?

Upvotes: 1

Views: 831

Answers (1)

ssp3nc3r
ssp3nc3r

Reputation: 3822

Your code has non-standard characters in some of the white space, including right after K;

Upvotes: 1

Related Questions