首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Stan中的有序概率估计

Stan中的有序概率估计
EN

Stack Overflow用户
提问于 2018-11-27 08:54:13
回答 1查看 174关注 0票数 1

我正在尝试复制John Kruschke在Stan的“做贝叶斯分析”(第676页)中的有序概率JAGS模型:

JAGS模型:

代码语言:javascript
复制
model {
    for ( i in 1:Ntotal ) {
      y[i] ~ dcat( pr[i,1:nYlevels] )
      pr[i,1] <- pnorm( thresh[1] , mu , 1/sigma^2 )
      for ( k in 2:(nYlevels-1) ) {
        pr[i,k] <- max( 0 ,  pnorm( thresh[ k ] , mu , 1/sigma^2 )
                           - pnorm( thresh[k-1] , mu , 1/sigma^2 ) )
      }
      pr[i,nYlevels] <- 1 - pnorm( thresh[nYlevels-1] , mu , 1/sigma^2 )
    }
    mu ~ dnorm( (1+nYlevels)/2 , 1/(nYlevels)^2 )
    sigma ~ dunif( nYlevels/1000 , nYlevels*10 )
    for ( k in 2:(nYlevels-2) ) {  # 1 and nYlevels-1 are fixed, not stochastic
      thresh[k] ~ dnorm( k+0.5 , 1/2^2 )
    }
  }

到目前为止,我已经运行了以下内容,但没有产生与书中相同的结果。Stan模型:

代码语言:javascript
复制
data{
  int<lower=1> n; // number of obs
  int<lower=3> n_levels; // number of categories

  int y[n]; // outcome var 
}

parameters{
  real mu; // latent mean
  real<lower=0> sigma; // latent sd
  ordered[n_levels] thresh; // thresholds

}

model{
  vector[n_levels] pr[n];

  mu ~ normal( (1+n_levels)/2 , 1/(n_levels)^2 );
  sigma ~ uniform( n_levels/1000 , n_levels*10 );


  for ( k in 2:(n_levels-2) ) // 1 and nYlevels-1 are fixed, not stochastic
    thresh[k] ~ normal( k+0.5 , 1/2^2 );

  for(i in 1:n) {

    pr[i, 1] = normal_cdf(thresh[1], mu, 1/sigma^2);

    for (k in 2:(n_levels-1)) {
      pr[i, k] = max([0.0, normal_cdf(thresh[k], mu, 1/sigma^2) - normal_cdf(thresh[k-1], mu, 1/sigma^2)]);
    }

    pr[i, n_levels] = 1 - normal_cdf(thresh[n_levels - 1], mu, 1/sigma^2);

    y[i] ~ categorical(pr[i, 1:n_levels]);
  }

}

数据在这里:

代码语言:javascript
复制
list(n = 100L, n_levels = 7, y = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 7L))

应该恢复mu为1.0,sigma为2.5。相反,我得到的mu是3.98,sigma是1.25。

我确信我在Stan模型中做错了什么,但我还只是个初学者,不确定下一步该做什么。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-12-01 07:04:26

更新:在互联网上搜索(特别感谢Conor Goold)之后,我想出了这个解决方案,它非常接近地复制了书中的结果。当然,任何反馈/更好的模型分解都会得到一个公认的答案!

代码语言:javascript
复制
data {
  real L;                     // Lower fixed thresholds
  real<lower=L> U;            // Upper fixed threshold

  int<lower=2> J;             // Number of outcome levels

  int<lower=0> N;             // Data length

  int<lower=1,upper=J> y[N];  // Ordinal responses
}

transformed data {
  real<lower=0> diff;         // difference between upper and lower fixed thresholds
  int<lower=1> K;             // Number of thresholds

  K = J - 1;
  diff = U - L;
}

parameters {
  simplex[K - 1] thresh_raw;      // raw thresholds
  real mu; // latent mean
  real<lower=0> sigma; // latent sd
}

transformed parameters {
  ordered[K] thresh;     // new thresholds with fixed first and last

  thresh[1] = L;
  thresh[2:K] = L + diff * cumulative_sum(thresh_raw);
  thresh[K] = U; // Overwrite last value to fix it
}

model{
  vector[J] theta;                  // local parameter for ordinal categories

  //priors
  mu ~ normal( (1+J)/2.0 , J );
  sigma ~ uniform( J/1000.0 , J*10 );

  for (i in 2:K-2)
    thresh[i] ~ normal(i + .05, 2);

  // likelihood statement
  for(n in 1:N) {

    // probability of ordinal category definitions
    theta[1] = normal_cdf( thresh[1] , mu, sigma );

    for (l in 2:K)
      theta[l] = fmax(0, normal_cdf(thresh[l], mu, sigma ) - normal_cdf(thresh[l-1], mu, sigma));

    theta[J] = 1 - normal_cdf(thresh[K] , mu, sigma);

    y[n] ~ categorical(theta);
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53491260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档