gpt4 book ai didi

r - R STAN 中的 Bernoulli Prior

转载 作者:行者123 更新时间:2023-12-04 02:45:08 25 4
gpt4 key购买 nike

我正在 STAN(rstan 库)中拟合逻辑模型。我的响应变量没有任何缺失值,但是我的协变量之一“HB”是二进制的,并且缺少条目。

因此,目标是在每次迭代时使用伯努利先验(参数为 0.5)来估算二元向量中缺失的条目。

但是,我遇到了问题:

  • 需要在参数或转换参数 block 中将缺失数据声明为实数或向量;
  • 模型 block 中伯努利分布的实现需要是整数;
  • 据我所知,STAN 中没有函数可以将真实的或向量到整数。

我使用了 section 3.3 of the STAN user guide 中提供的指南.对于下面的模型,解析器在伯努利赋值行(模型 block 中的倒数第二行)给我一个错误,说它需要整数。注意:我还尝试在参数 block 中将 HB_miss 定义为实数并得到相同的错误。

m2 <- '
data {
int<lower=0> N; // total number of observations
int<lower=0,upper=1> y[N]; // setting the dependent variable y as binary
vector[N] X; // independent variable 1

int<lower=0> N_obs;
int<lower=0> N_miss;
int<lower=1, upper=N> ii_obs[N_obs];
int<lower=1, upper=N> ii_miss[N_miss];

vector[N_obs] HB_obs; // independent variable 2 (observed)

}
parameters {
real b_0; // intercept
real b_X; // beta 1,2, ...
real b_HB;
vector[N_miss] HB_miss;
}
transformed parameters {
vector[N] HB;
HB[ii_obs] = HB_obs;
HB[ii_miss] = HB_miss;
}
model {
b_0 ~ normal(0,100);
b_X ~ normal(0,100);
b_HB ~ normal(0,100);
HB_miss ~ bernoulli(0.5); // This is where the parser gives me an error
y ~ bernoulli_logit(b_0 + b_X * X + b_HB * HB); // model
}

关于如何在 STAN 中有效地在 HB_miss 之前分配伯努利有什么想法吗?

最佳答案

由于您提到的原因,不可能在 Stan 程序中将缺失的离散值视为未知数。 Stan 中的所有算法都使用梯度,并且没有为离散的未知数定义导数。

相反,您需要边缘化未知值,当一切都是二进制时,这并不太乏味。本质上,您可以使用 log_mix function其参数是:

  1. 缺失值为 1 的概率,在你的情况下你说的是 0.5
  2. 如果缺失值为 1,则相关观察值的对数似然贡献
  3. 如果缺失值为 0,则相关观察值的对数似然贡献

所以,它会是这样的

for (n in 1:N)
target += log_mix(0.5, bernoulli_logit_lpmf(y[n] | b_0 + b_X * X[i] + b_HB),
bernoulli_logit_lpmf(y[n] | b_0 + b_X * X[i]));

更多详细信息,您可以阅读此博客post .

关于r - R STAN 中的 Bernoulli Prior,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57498783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com