在 rstan 中为分级响应模型声明数据时出错

Error in declaring data in rstan for graded response model

我正在尝试使用 Stan,特别是通过 rstan, to fit a graded response model. Luo and Jiao (2018), available here,为此提供 Stan 代码。这是他们的代码,经过编辑仅包含更多白色 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]);
    }}
}

但是,当我尝试使用这段代码时,解析器抛出错误。这是重现错误的 R 代码:

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)

这是我收到的错误:

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.

我已经尝试通过代码和 Stan 手册来查看数据声明的问题,但我找不到问题。该声明似乎与 Stan Language Reference:

中的声明示例非常相似
int<lower = 1> N;

谁能告诉我我错过了什么?

您的代码在某些白色 space 中包含非标准字符,包括紧跟在 K 之后;