RStan:在ordered_logistic()中使用矩阵

时间:2017-02-01 15:49:23

标签: stan rstan

我想在RStan中使用ordered_logistic()函数。

首先,这是数据(sample):

   gender   age partyID treatment_rand treatment_bias y_randT y_biasT
    <dbl> <dbl>   <dbl>          <dbl>          <dbl>   <dbl>   <dbl>
1       0    21       1              0              0       1       4
2       1    21       7              1              1       3       2
3       0    67       7              0              0       4       4
4       0    78       1              0              0       2       4
5       0    35       8              0              1       4       2

我使用数据子集:

X <- cbind(1, sample[, c("age", "partyID", "gender")])
choice_num <- 5
data_randT <- list(N=nrow(sample), D=ncol(X), t=sample$treatment_rand, X=X, y=sample$y_randT, K=choice_num)

我的Stan代码是:

data{
    int<lower=2> K;
    int<lower=0> N;
    int<lower=1> D;
    int<lower=1, upper=K> y[N];
    real<lower=0, upper=1> t[N];
    matrix[N,D] X;
}

parameters{
    vector[D] betaX;
    real betaT;
    ordered[K-1] c;
}

model{
    for(n in 1:N)
        y[n] ~ ordered_logistic(betaT*t[n] + X[n]*betaX, c);
}

如果我运行此代码,则会出现以下错误:

> fit_rand <- stan(model_code=stan_OrderedLogit, data=data_randT, seed=seed)
Error in new_CppObject_xp(fields$.module, fields$.pointer, ...) : 
  variable does not exist; processing stage=data initialization; variable name=X; base type=matrix_d
In addition: Warning messages:
1: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
2: In FUN(X[[i]], ...) : data with name X is not numeric and not used
failed to create the sampler; sampling not done

代码有什么问题?

1 个答案:

答案 0 :(得分:0)

我相信如果你在R中使用X矩阵而不是data.frame,它会起作用,就像在 X <- as.matrix(cbind(1, sample[, c("age", "partyID", "gender")])) 但是,我认为您首先要将partyID扩展为一组虚拟变量(不包括参考类别)。