r - 在 rstan 中为分级响应模型声明数据时出错
问题描述
我正在尝试使用 Stan,特别是通过rstan
来拟合分级响应模型。可在此处获得的Luo 和 Jiao (2018)提供了执行此操作的 Stan 代码。这是他们的代码,仅经过编辑以包含更多空白:
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;
谁能告诉我我错过了什么?
解决方案
您的代码在某些空格中包含非标准字符,包括 K 之后;
推荐阅读
- css - 为注销的用户或访问者从前端隐藏 WP 插件
- angular - 无法从角度形式获取值?
- reactjs - 无法读取未定义的属性“类型”| 编译失败 - ReactApp
- python - 将二维值数组分配给 Pandas 多索引数据框
- html - Bootstrap 导航栏弹出窗体
- azure - Microsoft LUIS 创作 API 中的索引错误
- ios - 无法更改 UISearchBar 清除按钮图像的颜色
- oracle - 当“PARTITION LIST SUBQUERY”在执行计划中时(一个错误?)取消实例化包
- mysql - MYSQL - 使用 IN 运算符从查询中返回所有行
- image - 是否建议在 RGB 图像中使用三个单独的通道来使用 CNN 提取特征?