1

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

首先,这是数据(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)

我的斯坦代码是:

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

代码有什么问题?

4

1 回答 1

0

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

于 2017-02-01T18:13:23.100 回答