stan 中的有序概率
Ordered probit in stan
我刚开始学习 stan,有几个问题。我正在尝试在 stan 中做一个有序的 probit 模型。我有一些问题。首先,下面的模型抛出一条错误消息 Stan model does not contain samples.
这是什么意思,我该如何修复它?
其次,我如何告诉 stan 我想要识别模型的约束条件?截至目前,它的位置不明。我想告诉 stan 将 tau
之一设置为特定值(例如 0),但不确定如何设置。
data{
int<lower=1> N; // number of obs
int<lower=3> J; // number of categories
int<lower=2> K; // num of predictors
int y[N]; // outcome var
matrix[N, K] x; // predictor vars
}
parameters{
ordered[J-1] tau; // thresholds
vector[K] beta; // beta coefficients
}
model{
vector[J] theta;
vector[N] xB;
beta ~ normal(0, 100);
xB <- x*beta;
for(n in 1:N){
theta[1] <- 1 - Phi(xB[n] - tau[1]);
for(j in 2:J-1)
theta[j] <- Phi(xB[n]-tau[j-1]) - Phi(xB[n]-tau[j]);
theta[J] <- Phi(xB[n] - tau[J-1]);
y[n] ~ categorical(theta);
}
}
编辑
这是我调用的 R 代码:
stan_data <- list(N = dim(insurance)[1], # 1000
K = dim(insurance)[2], #5
J = length(table(insurance$spend)), #3
y = insurance$spend, # vector of length N where each element is 0, 1, or 2
x = my_xmatrix) # matrix of dim 1000, 5
mcmc_oprobit <- stan(file="stan/oprobit.stan",
data = stan_data)
如果我打电话,
N <- 1000
J <- 3L
K <- 2L
y <- sample(0:2, N, replace = TRUE)
x <- matrix(rnorm(2 * N), N , 2)
mcmc_oprobit <- stan(file="oprobit.stan")
然后我最终得到
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception thrown at line 22:
stan::math::categorical_log: Number of categories is 0, but must be between (1, 3)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
在这种情况下,可以通过将结果变量重新编码为 1、2 或 3 而不是 0、1 或 2 来解决问题。但这提出了一个问题,即为什么您没有看到该信息性消息?您使用的是什么平台、GUI 和 RStan 版本号?
我刚开始学习 stan,有几个问题。我正在尝试在 stan 中做一个有序的 probit 模型。我有一些问题。首先,下面的模型抛出一条错误消息 Stan model does not contain samples.
这是什么意思,我该如何修复它?
其次,我如何告诉 stan 我想要识别模型的约束条件?截至目前,它的位置不明。我想告诉 stan 将 tau
之一设置为特定值(例如 0),但不确定如何设置。
data{
int<lower=1> N; // number of obs
int<lower=3> J; // number of categories
int<lower=2> K; // num of predictors
int y[N]; // outcome var
matrix[N, K] x; // predictor vars
}
parameters{
ordered[J-1] tau; // thresholds
vector[K] beta; // beta coefficients
}
model{
vector[J] theta;
vector[N] xB;
beta ~ normal(0, 100);
xB <- x*beta;
for(n in 1:N){
theta[1] <- 1 - Phi(xB[n] - tau[1]);
for(j in 2:J-1)
theta[j] <- Phi(xB[n]-tau[j-1]) - Phi(xB[n]-tau[j]);
theta[J] <- Phi(xB[n] - tau[J-1]);
y[n] ~ categorical(theta);
}
}
编辑
这是我调用的 R 代码:
stan_data <- list(N = dim(insurance)[1], # 1000
K = dim(insurance)[2], #5
J = length(table(insurance$spend)), #3
y = insurance$spend, # vector of length N where each element is 0, 1, or 2
x = my_xmatrix) # matrix of dim 1000, 5
mcmc_oprobit <- stan(file="stan/oprobit.stan",
data = stan_data)
如果我打电话,
N <- 1000
J <- 3L
K <- 2L
y <- sample(0:2, N, replace = TRUE)
x <- matrix(rnorm(2 * N), N , 2)
mcmc_oprobit <- stan(file="oprobit.stan")
然后我最终得到
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Exception thrown at line 22:
stan::math::categorical_log: Number of categories is 0, but must be between (1, 3)
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
在这种情况下,可以通过将结果变量重新编码为 1、2 或 3 而不是 0、1 或 2 来解决问题。但这提出了一个问题,即为什么您没有看到该信息性消息?您使用的是什么平台、GUI 和 RStan 版本号?